123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704 |
- # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- require 'rails_helper'
- RSpec.describe Gql::Mutations::Ticket::Create, :aggregate_failures, type: :graphql do
- let(:query) do
- <<~QUERY
- mutation ticketCreate($input: TicketCreateInput!) {
- ticketCreate(input: $input) {
- ticket {
- id
- title
- group {
- name
- }
- priority {
- name
- }
- customer {
- fullname
- }
- owner {
- fullname
- }
- objectAttributeValues {
- attribute {
- name
- }
- value
- }
- tags
- }
- errors {
- message
- field
- }
- }
- }
- QUERY
- end
- let(:agent) { create(:agent, groups: [ Group.find_by(name: 'Users')]) }
- let(:customer) { create(:customer) }
- let(:user) { agent }
- let(:group) { agent.groups.first }
- let(:priority) { Ticket::Priority.last }
- let(:article_payload) { nil }
- let(:input_base_payload) do
- {
- title: 'Ticket Create Mutation Test',
- groupId: gql.id(group),
- priorityId: gql.id(priority),
- customer: { id: gql.id(customer) },
- ownerId: gql.id(agent),
- tags: %w[foo bar],
- article: article_payload
- # pending_time: 10.minutes.from_now,
- # type: ...
- }
- end
- let(:input_payload) { input_base_payload }
- let(:variables) { { input: input_payload } }
- let(:expected_base_response) do
- {
- 'id' => gql.id(Ticket.last),
- 'title' => 'Ticket Create Mutation Test',
- 'owner' => { 'fullname' => agent.fullname },
- 'group' => { 'name' => agent.groups.first.name },
- 'customer' => { 'fullname' => customer.fullname },
- 'priority' => { 'name' => Ticket::Priority.last.name },
- 'tags' => %w[foo bar],
- 'objectAttributeValues' => [],
- }
- end
- let(:expected_response) do
- expected_base_response
- end
- def it_creates_ticket(articles: 0, stores: 0)
- expect { gql.execute(query, variables: variables) }
- .to change(Ticket, :count).by(1)
- .and change(Ticket::Article, :count).by(articles)
- .and change(Store, :count).by(stores)
- end
- def it_fails_to_create_ticket
- expect { gql.execute(query, variables: variables) }
- .not_to change(Ticket, :count)
- end
- context 'when creating a new ticket' do
- context 'with an agent', authenticated_as: :agent do
- it 'creates Ticket record' do
- it_creates_ticket
- expect(gql.result.data[:ticket]).to eq(expected_response)
- end
- context 'without title' do
- let(:input_payload) { input_base_payload.tap { |h| h[:title] = ' ' } }
- it 'fails validation' do
- it_fails_to_create_ticket
- expect(gql.result.error_message).to include('Variable $input of type TicketCreateInput! was provided invalid value for title')
- end
- end
- context 'with custom object_attribute', db_strategy: :reset do
- let(:object_attribute) do
- screens = { create: { 'admin.organization': { shown: true, required: false } } }
- create(:object_manager_attribute_text, object_name: 'Ticket', screens: screens).tap do |_oa|
- ObjectManager::Attribute.migration_execute
- end
- end
- let(:input_payload) do
- input_base_payload.merge(
- {
- objectAttributeValues: [ { name: object_attribute.name, value: 'object_attribute_value' } ]
- }
- )
- end
- let(:expected_response) do
- expected_base_response.merge(
- {
- 'objectAttributeValues' => [{ 'attribute' => { 'name'=>object_attribute.name }, 'value' => 'object_attribute_value' }]
- }
- )
- end
- it 'creates the ticket' do
- it_creates_ticket
- expect(gql.result.data[:ticket]).to eq(expected_response)
- end
- end
- context 'with links' do
- let!(:other_ticket) { create(:ticket, group: agent.groups.first) }
- let(:links) do
- [
- { linkObjectId: gql.id(other_ticket), linkType: 'child' },
- { linkObjectId: gql.id(other_ticket), linkType: 'normal' },
- ]
- end
- let(:input_payload) { input_base_payload.merge(links:) }
- it 'creates the ticket and adds links' do
- it_creates_ticket
- expect(Link.list(link_object: 'Ticket', link_object_value: Ticket.last.id)).to contain_exactly(
- { 'link_object' => 'Ticket', 'link_object_value' => other_ticket.id, 'link_type' => 'parent' },
- { 'link_object' => 'Ticket', 'link_object_value' => other_ticket.id, 'link_type' => 'normal' },
- )
- end
- end
- context 'with issue tracker links' do
- let(:github_link) { 'https://github.com/issue/123' }
- let(:input_payload) { input_base_payload.merge(externalReferences: { github: [github_link] }) }
- before { Setting.set('github_integration', true) }
- it 'creates the ticket and adds issue trackeer links' do
- it_creates_ticket
- expect(Ticket.last.preferences)
- .to include('github' => include('issue_links' => contain_exactly(github_link)))
- end
- end
- context 'when customer is provided as an email address' do
- let(:email_address) { Faker::Internet.email }
- let(:input_payload) { input_base_payload.merge(customer: { email: email_address }) }
- context 'with valid email address' do
- it 'creates the ticket and a new customer' do
- it_creates_ticket
- expect(User.find_by(email: email_address)).to be_present
- expect(gql.result.data[:ticket][:customer][:fullname]).to eq(User.find_by(email: email_address).fullname)
- end
- end
- context 'with invalid email address' do
- let(:email_address) { 'invalid-email' }
- it 'fails to create the ticket' do
- it_fails_to_create_ticket
- expect(gql.result.error_message).to include('The email address is invalid.')
- end
- end
- context 'with valid email address of an existing customer' do
- let(:email_address) { customer.email }
- it 'creates the ticket' do
- it_creates_ticket
- expect(gql.result.data[:ticket][:customer][:fullname]).to eq(customer.fullname)
- end
- end
- end
- context 'when creating the ticket in a group with only :create permission' do
- let(:group) { create(:group) }
- let(:owner) { create(:agent, groups: [group]) }
- let(:input_payload) { input_base_payload.merge(ownerId: gql.id(owner)) }
- before do
- user.groups << group
- user.group_names_access_map = { user.groups.first.name => ['full'], group.name => ['create'] }
- end
- it 'creates the ticket in the correct group, but returns an error trying to access the new ticket' do
- expect { gql.execute(query, variables: variables) }.to change(Ticket, :count).by(1)
- expect(Ticket.last.group.id).to eq(group.id)
- expect(gql.result.payload['data']['ticketCreate']).to eq({ 'ticket' => nil, 'errors' => nil }) # Mutation did run, but data retrieval was not authorized.
- expect(gql.result.payload['errors'].first['message']).to eq('Access forbidden by Gql::Types::TicketType')
- expect(gql.result.payload['errors'].first['extensions']['type']).to eq('Exceptions::Forbidden')
- end
- end
- context 'when creating the ticket in a group without email address' do
- let(:group) { create(:group, email_address: nil) }
- let(:agent) { create(:agent, groups: [group]) }
- let(:article_payload) { { body: 'dummy', type: 'email' } }
- let(:input_payload) { input_base_payload.merge(groupId: gql.id(group)) }
- it 'fails to create the ticket' do
- it_fails_to_create_ticket
- expect(gql.result.payload['data']['ticketCreate']).to eq(
- {
- 'ticket' => nil,
- 'errors' => [
- {
- 'message' => 'This group has no email address configured for outgoing communication.',
- 'field' => 'group_id'
- }
- ]
- }
- )
- end
- end
- context 'with no permission to the group' do
- let(:group) { create(:group) }
- it 'raises an error', :aggregate_failures do
- it_fails_to_create_ticket
- expect(gql.result.error_type).to eq(Exceptions::Forbidden)
- expect(gql.result.error_message).to eq('Access forbidden by Gql::Types::GroupType')
- end
- end
- context 'with article' do
- before do
- Group.find(agent.groups.first.id).update(email_address: create(:email_address))
- end
- context 'with inline attachments' do
- let(:body) do
- <<~BODY
- This is a test article with inline attachments.
- <img tabindex="0" style="width: 421px; max-width: 100%;" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAATCAYAAACQjC21AAAKn2lDQ1BJQ0MgUHJvZmlsZQAASImVlgdQU1kXgO976SGhhV5Db9JbACmhh96bqIQkQCgxBEITG7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4bXBtZXRhPgr9cvHjAAABXklEQVQ4EYVU0bbDIAya/f9vXi8EiLFn2/VBYwKEWtt1Y7w+DCbXzCNxI5EcWcsbYTVf4dwvJvZYzz5DrFBr46Wr+YrO6t5wQobbzwfYEnRqe/aQ2nXmld7QG7rZITa5l6iwv5P1yNsF00QFiX2FnNZRIZ9nurGKcVQ4rMJvUh9P2hL7z6i+wFwVFFHs8sGwtqwaei4tr7SOg0bg8M1lmmxwApLULpnvK87Qzn6w+goBQ9ivoUcm4mmh7qHpedPABPZN+NLDuudElchRBcgALCU88ZaAQ/dkkeEDFEfCa9fX5SwWBA49EvSdoXLUsY5Q0M/u/ekFTfFWPmK5GqkKw+MqXn16/dhIt5MdMNttatOTRMLne6xPL56I6/MZEvJhN8+/EDjh8z3270tN0wsV8xmIoPnOFWLz9CDZsb5lqdXMvKjCJB4Qh0JOPAvt0A1ajEV7cvMgUKiQ1bgnWuMPeuaEBXgk9rkAAAAASUVORK5CYII=" />
- BODY
- end
- let(:article_payload) do
- {
- body: body,
- contentType: 'text/html',
- }
- end
- it 'creates a new ticket + a new article with inline attachments' do
- it_creates_ticket(articles: 1, stores: 1)
- expect(Store.last.filename).to eq('image1.png')
- end
- end
- context 'with attachments' do
- let(:article_payload) do
- form_id = SecureRandom.uuid
- file_name = 'file1.txt'
- file_type = 'text/plain'
- file_content = Base64.strict_encode64('file1')
- UploadCache.new(form_id).tap do |cache|
- cache.add(
- data: file_content,
- filename: file_name,
- preferences: { 'Content-Type' => file_type },
- created_by_id: agent.id
- )
- end
- {
- body: 'dummy',
- contentType: 'text/html',
- attachments: {
- formId: form_id,
- files: [
- {
- name: file_name,
- type: file_type,
- content: file_content,
- },
- ],
- },
- }
- end
- it 'creates a new ticket + a new article with attachments' do
- it_creates_ticket(articles: 1, stores: 1)
- expect(Store.last.filename).to eq('file1.txt')
- end
- end
- context 'with inline attachments + attachments' do
- let(:body) do
- <<~BODY
- This is a test article with inline attachments.
- <img tabindex="0" style="width: 421px; max-width: 100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CiAgICAgICAgIDxleGlmOlVzZXJDb21tZW50PlNjcmVlbnNob3Q8L2V4aWY6VXNlckNvbW1lbnQ+CiAgICAgIDwvcmRmOkRlc2NyaXB0aW9uPgogICA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgr9cvHjAAABXklEQVQ4EYVU0bbDIAya/f9vXi8EiLFn2/VBYwKEWtt1Y7w+DCbXzCNxI5EcWcsbYTVf4dwvJvZYzz5DrFBr46Wr+YrO6t5wQobbzwfYEnRqe/aQ2nXmld7QG7rZITa5l6iwv5P1yNsF00QFiX2FnNZRIZ9nurGKcVQ4rMJvUh9P2hL7z6i+wFwVFFHs8sGwtqwaei4tr7SOg0bg8M1lmmxwApLULpnvK87Qzn6w+goBQ9ivoUcm4mmh7qHpedPABPZN+NLDuudElchRBcgALCU88ZaAQ/dkkeEDFEfCa9fX5SwWBA49EvSdoXLUsY5Q0M/u/ekFTfFWPmK5GqkKw+MqXn16/dhIt5MdMNttatOTRMLne6xPL56I6/MZEvJhN8+/EDjh8z3270tN0wsV8xmIoPnOFWLz9CDZsb5lqdXMvKjCJB4Qh0JOPAvt0A1ajEV7cvMgUKiQ1bgnWuMPeuaEBXgk9rkAAAAASUVORK5CYII=" />
- BODY
- end
- let(:article_payload) do
- form_id = SecureRandom.uuid
- file_name = 'file1.txt'
- file_type = 'text/plain'
- file_content = Base64.strict_encode64('file1')
- UploadCache.new(form_id).tap do |cache|
- cache.add(
- data: file_content,
- filename: file_name,
- preferences: { 'Content-Type' => file_type },
- created_by_id: agent.id
- )
- end
- {
- body: body,
- contentType: 'text/html',
- attachments: {
- formId: form_id,
- files: [
- {
- name: file_name,
- type: file_type,
- content: file_content,
- },
- ],
- },
- }
- end
- it 'creates a new ticket + a new article with inline attachments + attachments' do
- it_creates_ticket(articles: 1, stores: 2)
- expect(Store.last.filename).to eq('image1.png')
- end
- end
- context 'with a specific sender' do
- let(:article_payload) do
- {
- body: 'dummy',
- sender: 'Agent',
- }
- end
- it 'creates a new ticket + a new article with a specific sender' do
- it_creates_ticket(articles: 1)
- expect(Ticket.last.articles.last.sender.name).to eq('Agent')
- end
- it 'sets correct "to" and "from" values', :aggregate_failures do
- it_creates_ticket(articles: 1)
- expect(Ticket.last.articles.last)
- .to have_attributes(
- from: agent.fullname,
- to: "#{customer.fullname} <#{customer.email}>"
- )
- end
- end
- context 'with no type' do
- let(:article_payload) do
- {
- body: 'dummy',
- }
- end
- it 'creates a new ticket + a new article, but falls back to type "note"' do
- it_creates_ticket(articles: 1)
- expect(Ticket.last.articles.last.type.name).to eq('note')
- end
- end
- context 'with a specific type' do
- let(:article_payload) do
- {
- body: 'dummy',
- type: Ticket::Article::Type.first.name,
- to: 'dummy@example.org',
- }
- end
- it 'creates a new ticket + a new article with a specific type' do
- it_creates_ticket(articles: 1)
- expect(Ticket.last.articles.last.type.name).to eq(Ticket::Article::Type.first.name)
- end
- context 'with all integrations disabled' do
- let(:article_payload) do
- {
- body: 'dummy',
- to: ['to@example.com'],
- type: 'email',
- security: {
- method: 'SMIME',
- options: %w[encryption sign]
- }
- }
- end
- before do
- Setting.set('smime_integration', false)
- Setting.set('pgp_integration', false)
- end
- it 'doesn\'t set security if security integrations are not enabled', :aggregate_failures do
- it_creates_ticket(articles: 1)
- expect(Ticket.last.articles.last.preferences[:security]).to be_nil
- end
- end
- context 'with smime enabled' do
- let(:article_payload) do
- {
- body: 'dummy',
- to: ['to@example.com'],
- type: 'email',
- security: {
- method: 'SMIME',
- options: %w[encryption sign]
- }
- }
- end
- before do
- Setting.set('smime_integration', true)
- Setting.set('pgp_integration', false)
- end
- it 'creates a new ticket with correct security preferences', :aggregate_failures do
- it_creates_ticket(articles: 1)
- expect(Ticket.last.articles.last.preferences[:security]).to eq(
- 'type' => 'S/MIME',
- 'encryption' => { 'success' => true },
- 'sign' => { 'success' => true },
- )
- end
- end
- context 'with pgp enabled' do
- let(:article_payload) do
- {
- body: 'dummy',
- to: ['to@example.com'],
- type: 'email',
- security: {
- method: 'PGP',
- options: %w[encryption sign]
- }
- }
- end
- before do
- Setting.set('smime_integration', false)
- Setting.set('pgp_integration', true)
- end
- it 'creates a new ticket with correct security preferences', :aggregate_failures do
- it_creates_ticket(articles: 1)
- expect(Ticket.last.articles.last.preferences[:security]).to eq(
- 'type' => 'PGP',
- 'encryption' => { 'success' => true },
- 'sign' => { 'success' => true },
- )
- end
- end
- end
- end
- context 'with to: and cc: being string values' do
- let(:article_payload) do
- {
- body: 'dummy',
- to: 'to@example.com',
- cc: 'cc@example.com',
- }
- end
- it 'creates a new ticket + a new article and sets correct "to" and "cc" values', :aggregate_failures do
- it_creates_ticket(articles: 1)
- expect(Ticket.last.articles.last).to have_attributes(to: 'to@example.com', cc: 'cc@example.com')
- end
- end
- context 'with to: and cc: containing array values' do
- let(:article_payload) do
- {
- body: 'dummy',
- to: ['to@example.com', 'to2@example.com'],
- cc: ['cc@example.com', 'cc2@example.com'],
- }
- end
- it 'creates a new ticket + a new article and sets correct "to" and "cc" values', :aggregate_failures do
- it_creates_ticket(articles: 1)
- expect(Ticket.last.articles.last).to have_attributes(to: 'to@example.com, to2@example.com', cc: 'cc@example.com, cc2@example.com')
- end
- end
- context 'with a shared draft' do
- let(:shared_draft) { create(:ticket_shared_draft_start, group:) }
- let(:input_payload) do
- input_base_payload
- .merge(sharedDraftId: Gql::ZammadSchema.id_from_object(shared_draft))
- end
- it 'passed to ticket create service' do
- expect_any_instance_of(Service::Ticket::Create)
- .to receive(:execute)
- .with(ticket_data: include(shared_draft:))
- .and_call_original
- gql.execute(query, variables: variables)
- end
- end
- end
- context 'with a customer', authenticated_as: :customer do
- let(:input_payload) { input_base_payload.tap { |h| h.delete(:customer) } }
- let(:expected_response) do
- expected_base_response.merge(
- {
- 'owner' => { 'fullname' => nil },
- 'priority' => { 'name' => Ticket::Priority.where(default_create: true).first.name },
- 'tags' => nil
- }
- )
- end
- it 'creates the ticket with filtered values' do
- it_creates_ticket
- expect(gql.result.data[:ticket]).to eq(expected_response)
- end
- context 'when sending a different customerId' do
- let(:input_payload) { input_base_payload.tap { |h| h[:customer][:id] = gql.id(create(:customer)) } }
- it 'fails creating a ticket with permission exception' do
- it_fails_to_create_ticket
- expect(gql.result.error_type).to eq(Exceptions::Forbidden)
- expect(gql.result.error_message).to eq('Access forbidden by Gql::Types::UserType')
- end
- end
- context 'with links' do
- let!(:other_ticket) { create(:ticket, customer: customer) }
- let(:links) do
- [
- { linkObjectId: gql.id(other_ticket), linkType: 'child' },
- { linkObjectId: gql.id(other_ticket), linkType: 'normal' },
- ]
- end
- let(:input_payload) { input_base_payload.merge(links:) }
- it 'creates the ticket without links' do
- it_creates_ticket
- expect(Link.list(link_object: 'Ticket', link_object_value: Ticket.last.id)).to eq([])
- end
- end
- context 'with issue tracker links' do
- let(:github_link) { 'https://github.com/issue/123' }
- let(:input_payload) { input_base_payload.merge(externalReferences: { github: [github_link] }) }
- before { Setting.set('github_integration', true) }
- it 'creates the ticket and adds issue tracker links' do
- it_creates_ticket
- expect(Ticket.last.preferences).not_to include('github')
- end
- end
- context 'with article' do
- context 'with a forbidden sender' do
- let(:article_payload) do
- {
- body: 'dummy',
- sender: 'Agent',
- }
- end
- it 'creates a new ticket + a new article, but falls back to "Customer" as sender' do
- it_creates_ticket(articles: 1)
- expect(Ticket.last.articles.last.sender.name).to eq('Customer')
- end
- end
- context 'with type "phone"' do
- let(:article_payload) do
- {
- body: 'dummy',
- type: 'phone',
- }
- end
- it 'creates a new ticket + a new article, but falls back to "note" as type' do
- it_creates_ticket(articles: 1)
- expect(Ticket.last.articles.last.type.name).to eq('note')
- end
- it 'sets correct "to" and "from" values', :aggregate_failures do
- it_creates_ticket(articles: 1)
- expect(Ticket.last.articles.last)
- .to have_attributes(
- to: Ticket.last.group.name,
- from: customer.fullname
- )
- end
- end
- context 'with an article flagged as internal' do
- let(:article_payload) do
- {
- body: 'dummy',
- internal: true,
- }
- end
- it 'creates a new ticket + a new article, but flags it as not internal' do
- it_creates_ticket(articles: 1)
- expect(Ticket.last.articles.last.internal).to be(false)
- end
- end
- end
- end
- context 'with an agent that has a specific role limited to create/update permission', authenticated_as: :user do
- let(:user) { create(:user, roles: [api_role]) }
- let(:api_role) do
- role = create(:role, name: 'API', permission_names: ['ticket.agent'])
- role.group_names_access_map = {
- Group.first.name => %w[create],
- }
- role
- end
- let(:input_payload) do
- {
- title: 'Test title for issue #4647',
- groupId: gql.id(Group.first),
- customer: { id: gql.id(customer) },
- article: article_payload,
- }
- end
- let(:article_payload) do
- {
- type: 'web',
- internal: false,
- sender: 'Customer',
- subject: 'Test subject',
- body: SecureRandom.uuid,
- }
- end
- before { Trigger.destroy_all } # triggers may cause additional articles to be created
- it 'contains correct "origin_by" + "from" information' do
- gql.execute(query, variables: variables)
- expect(Ticket.last.articles.last).to have_attributes(
- origin_by_id: customer.id,
- from: "#{customer.fullname} <#{customer.email}>",
- )
- end
- end
- end
- end
|