create_spec.rb 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Ticket::Create, :aggregate_failures, type: :graphql do
  4. let(:query) do
  5. <<~QUERY
  6. mutation ticketCreate($input: TicketCreateInput!) {
  7. ticketCreate(input: $input) {
  8. ticket {
  9. id
  10. title
  11. group {
  12. name
  13. }
  14. priority {
  15. name
  16. }
  17. customer {
  18. fullname
  19. }
  20. owner {
  21. fullname
  22. }
  23. objectAttributeValues {
  24. attribute {
  25. name
  26. }
  27. value
  28. }
  29. tags
  30. }
  31. errors {
  32. message
  33. field
  34. }
  35. }
  36. }
  37. QUERY
  38. end
  39. let(:agent) { create(:agent, groups: [ Group.find_by(name: 'Users')]) }
  40. let(:customer) { create(:customer) }
  41. let(:user) { agent }
  42. let(:group) { agent.groups.first }
  43. let(:priority) { Ticket::Priority.last }
  44. let(:article_payload) { nil }
  45. let(:input_base_payload) do
  46. {
  47. title: 'Ticket Create Mutation Test',
  48. groupId: gql.id(group),
  49. priorityId: gql.id(priority),
  50. customer: { id: gql.id(customer) },
  51. ownerId: gql.id(agent),
  52. tags: %w[foo bar],
  53. article: article_payload
  54. # pending_time: 10.minutes.from_now,
  55. # type: ...
  56. }
  57. end
  58. let(:input_payload) { input_base_payload }
  59. let(:variables) { { input: input_payload } }
  60. let(:expected_base_response) do
  61. {
  62. 'id' => gql.id(Ticket.last),
  63. 'title' => 'Ticket Create Mutation Test',
  64. 'owner' => { 'fullname' => agent.fullname },
  65. 'group' => { 'name' => agent.groups.first.name },
  66. 'customer' => { 'fullname' => customer.fullname },
  67. 'priority' => { 'name' => Ticket::Priority.last.name },
  68. 'tags' => %w[foo bar],
  69. 'objectAttributeValues' => [],
  70. }
  71. end
  72. let(:expected_response) do
  73. expected_base_response
  74. end
  75. def it_creates_ticket(articles: 0, stores: 0)
  76. expect { gql.execute(query, variables: variables) }
  77. .to change(Ticket, :count).by(1)
  78. .and change(Ticket::Article, :count).by(articles)
  79. .and change(Store, :count).by(stores)
  80. end
  81. def it_fails_to_create_ticket
  82. expect { gql.execute(query, variables: variables) }
  83. .not_to change(Ticket, :count)
  84. end
  85. context 'when creating a new ticket' do
  86. context 'with an agent', authenticated_as: :agent do
  87. it 'creates Ticket record' do
  88. it_creates_ticket
  89. expect(gql.result.data['ticket']).to eq(expected_response)
  90. end
  91. context 'without title' do
  92. let(:input_payload) { input_base_payload.tap { |h| h[:title] = ' ' } }
  93. it 'fails validation' do
  94. it_fails_to_create_ticket
  95. expect(gql.result.error_message).to include('Variable $input of type TicketCreateInput! was provided invalid value for title')
  96. end
  97. end
  98. context 'with custom object_attribute', db_strategy: :reset do
  99. let(:object_attribute) do
  100. screens = { create: { 'admin.organization': { shown: true, required: false } } }
  101. create(:object_manager_attribute_text, object_name: 'Ticket', screens: screens).tap do |_oa|
  102. ObjectManager::Attribute.migration_execute
  103. end
  104. end
  105. let(:input_payload) do
  106. input_base_payload.merge(
  107. {
  108. objectAttributeValues: [ { name: object_attribute.name, value: 'object_attribute_value' } ]
  109. }
  110. )
  111. end
  112. let(:expected_response) do
  113. expected_base_response.merge(
  114. {
  115. 'objectAttributeValues' => [{ 'attribute' => { 'name'=>object_attribute.name }, 'value' => 'object_attribute_value' }]
  116. }
  117. )
  118. end
  119. it 'creates the ticket' do
  120. it_creates_ticket
  121. expect(gql.result.data['ticket']).to eq(expected_response)
  122. end
  123. end
  124. context 'when customer is provided as an email address' do
  125. let(:email_address) { Faker::Internet.email }
  126. let(:input_payload) { input_base_payload.merge(customer: { email: email_address }) }
  127. context 'with valid email address' do
  128. it 'creates the ticket and a new customer' do
  129. it_creates_ticket
  130. expect(User.find_by(email: email_address)).to be_present
  131. expect(gql.result.data['ticket']['customer']['fullname']).to eq(User.find_by(email: email_address).fullname)
  132. end
  133. end
  134. context 'with invalid email address' do
  135. let(:email_address) { 'invalid-email' }
  136. it 'fails to create the ticket' do
  137. it_fails_to_create_ticket
  138. expect(gql.result.error_message).to include('The email address is invalid.')
  139. end
  140. end
  141. context 'with valid email address of an existing customer' do
  142. let(:email_address) { customer.email }
  143. it 'creates the ticket' do
  144. it_creates_ticket
  145. expect(gql.result.data['ticket']['customer']['fullname']).to eq(customer.fullname)
  146. end
  147. end
  148. end
  149. context 'when creating the ticket in a group with only :create permission' do
  150. let(:group) { create(:group) }
  151. let(:owner) { create(:agent, groups: [group]) }
  152. let(:input_payload) { input_base_payload.merge(ownerId: gql.id(owner)) }
  153. before do
  154. user.groups << group
  155. user.group_names_access_map = { user.groups.first.name => ['full'], group.name => ['create'] }
  156. end
  157. it 'creates the ticket in the correct group, but returns an error trying to access the new ticket' do
  158. expect { gql.execute(query, variables: variables) }.to change(Ticket, :count).by(1)
  159. expect(Ticket.last.group.id).to eq(group.id)
  160. expect(gql.result.payload['data']['ticketCreate']).to eq({ 'ticket' => nil, 'errors' => nil }) # Mutation did run, but data retrieval was not authorized.
  161. expect(gql.result.payload['errors'].first['message']).to eq('Access forbidden by Gql::Types::TicketType')
  162. expect(gql.result.payload['errors'].first['extensions']['type']).to eq('Exceptions::Forbidden')
  163. end
  164. end
  165. context 'when creating the ticket in a group without email address' do
  166. let(:group) { create(:group, email_address: nil) }
  167. let(:agent) { create(:agent, groups: [group]) }
  168. let(:article_payload) { { body: 'dummy', type: 'email' } }
  169. let(:input_payload) { input_base_payload.merge(groupId: gql.id(group)) }
  170. it 'fails to create the ticket' do
  171. it_fails_to_create_ticket
  172. expect(gql.result.payload['data']['ticketCreate']).to eq(
  173. {
  174. 'ticket' => nil,
  175. 'errors' => [
  176. {
  177. 'message' => 'This group has no email address configured for outgoing communication.',
  178. 'field' => 'group_id'
  179. }
  180. ]
  181. }
  182. )
  183. end
  184. end
  185. context 'with no permission to the group' do
  186. let(:group) { create(:group) }
  187. it 'raises an error', :aggregate_failures do
  188. it_fails_to_create_ticket
  189. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  190. expect(gql.result.error_message).to eq('Access forbidden by Gql::Types::GroupType')
  191. end
  192. end
  193. context 'with article' do
  194. before do
  195. Group.find(agent.groups.first.id).update(email_address: create(:email_address))
  196. end
  197. context 'with inline attachments' do
  198. let(:body) do
  199. <<~BODY
  200. This is a test article with inline attachments.
  201. <img tabindex="0" style="width: 421px; max-width: 100%;" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAATCAYAAACQjC21AAAKn2lDQ1BJQ0MgUHJvZmlsZQAASImVlgdQU1kXgO976SGhhV5Db9JbACmhh96bqIQkQCgxBEITG7K4gmtBRASUBV0RUHBViqwFsaDCIqDYdUEWBXVdLNhQ+R8whN2/zn9mzrvfnHfuKW/ufXMAIJOYfH4qLAlAGi9TEOLlSo2KjqHingI8QAMZ4AAMmKwMPj0oyA8gsrj+Xd7fBtDcetN4Lta/vv+vIsXmZLAA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5P1yNsF00QFiX2FnNZRIZ9nurGKcVQ4rMJvUh9P2hL7z6i+wFwVFFHs8sGwtqwaei4tr7SOg0bg8M1lmmxwApLULpnvK87Qzn6w+goBQ9ivoUcm4mmh7qHpedPABPZN+NLDuudElchRBcgALCU88ZaAQ/dkkeEDFEfCa9fX5SwWBA49EvSdoXLUsY5Q0M/u/ekFTfFWPmK5GqkKw+MqXn16/dhIt5MdMNttatOTRMLne6xPL56I6/MZEvJhN8+/EDjh8z3270tN0wsV8xmIoPnOFWLz9CDZsb5lqdXMvKjCJB4Qh0JOPAvt0A1ajEV7cvMgUKiQ1bgnWuMPeuaEBXgk9rkAAAAASUVORK5CYII=" />
  202. BODY
  203. end
  204. let(:article_payload) do
  205. {
  206. body: body,
  207. contentType: 'text/html',
  208. }
  209. end
  210. it 'creates a new ticket + a new article with inline attachments' do
  211. it_creates_ticket(articles: 1, stores: 1)
  212. expect(Store.last.filename).to eq('image1.png')
  213. end
  214. end
  215. context 'with attachments' do
  216. let(:article_payload) do
  217. form_id = SecureRandom.uuid
  218. file_name = 'file1.txt'
  219. file_type = 'text/plain'
  220. file_content = Base64.strict_encode64('file1')
  221. UploadCache.new(form_id).tap do |cache|
  222. cache.add(
  223. data: file_content,
  224. filename: file_name,
  225. preferences: { 'Content-Type' => file_type },
  226. created_by_id: agent.id
  227. )
  228. end
  229. {
  230. body: 'dummy',
  231. contentType: 'text/html',
  232. attachments: {
  233. formId: form_id,
  234. files: [
  235. {
  236. name: file_name,
  237. type: file_type,
  238. content: file_content,
  239. },
  240. ],
  241. },
  242. }
  243. end
  244. it 'creates a new ticket + a new article with attachments' do
  245. it_creates_ticket(articles: 1, stores: 1)
  246. expect(Store.last.filename).to eq('file1.txt')
  247. end
  248. end
  249. context 'with inline attachments + attachments' do
  250. let(:body) do
  251. <<~BODY
  252. This is a test article with inline attachments.
  253. <img tabindex="0" style="width: 421px; max-width: 100%;" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAATCAYAA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L2V4aWY6VXNlckNvbW1lbnQ+CiAgICAgIDwvcmRmOkRlc2NyaXB0aW9uPgogICA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgr9cvHjAAABXklEQVQ4EYVU0bbDIAya/f9vXi8EiLFn2/VBYwKEWtt1Y7w+DCbXzCNxI5EcWcsbYTVf4dwvJvZYzz5DrFBr46Wr+YrO6t5wQobbzwfYEnRqe/aQ2nXmld7QG7rZITa5l6iwv5P1yNsF00QFiX2FnNZRIZ9nurGKcVQ4rMJvUh9P2hL7z6i+wFwVFFHs8sGwtqwaei4tr7SOg0bg8M1lmmxwApLULpnvK87Qzn6w+goBQ9ivoUcm4mmh7qHpedPABPZN+NLDuudElchRBcgALCU88ZaAQ/dkkeEDFEfCa9fX5SwWBA49EvSdoXLUsY5Q0M/u/ekFTfFWPmK5GqkKw+MqXn16/dhIt5MdMNttatOTRMLne6xPL56I6/MZEvJhN8+/EDjh8z3270tN0wsV8xmIoPnOFWLz9CDZsb5lqdXMvKjCJB4Qh0JOPAvt0A1ajEV7cvMgUKiQ1bgnWuMPeuaEBXgk9rkAAAAASUVORK5CYII=" />
  254. BODY
  255. end
  256. let(:article_payload) do
  257. form_id = SecureRandom.uuid
  258. file_name = 'file1.txt'
  259. file_type = 'text/plain'
  260. file_content = Base64.strict_encode64('file1')
  261. UploadCache.new(form_id).tap do |cache|
  262. cache.add(
  263. data: file_content,
  264. filename: file_name,
  265. preferences: { 'Content-Type' => file_type },
  266. created_by_id: agent.id
  267. )
  268. end
  269. {
  270. body: body,
  271. contentType: 'text/html',
  272. attachments: {
  273. formId: form_id,
  274. files: [
  275. {
  276. name: file_name,
  277. type: file_type,
  278. content: file_content,
  279. },
  280. ],
  281. },
  282. }
  283. end
  284. it 'creates a new ticket + a new article with inline attachments + attachments' do
  285. it_creates_ticket(articles: 1, stores: 2)
  286. expect(Store.last.filename).to eq('image1.png')
  287. end
  288. end
  289. context 'with a specific sender' do
  290. let(:article_payload) do
  291. {
  292. body: 'dummy',
  293. sender: 'Agent',
  294. }
  295. end
  296. it 'creates a new ticket + a new article with a specific sender' do
  297. it_creates_ticket(articles: 1)
  298. expect(Ticket.last.articles.last.sender.name).to eq('Agent')
  299. end
  300. it 'sets correct "to" and "from" values', :aggregate_failures do
  301. it_creates_ticket(articles: 1)
  302. expect(Ticket.last.articles.last)
  303. .to have_attributes(
  304. from: agent.fullname,
  305. to: "#{customer.fullname} <#{customer.email}>"
  306. )
  307. end
  308. end
  309. context 'with no type' do
  310. let(:article_payload) do
  311. {
  312. body: 'dummy',
  313. }
  314. end
  315. it 'creates a new ticket + a new article, but falls back to type "note"' do
  316. it_creates_ticket(articles: 1)
  317. expect(Ticket.last.articles.last.type.name).to eq('note')
  318. end
  319. end
  320. context 'with a specific type' do
  321. let(:article_payload) do
  322. {
  323. body: 'dummy',
  324. type: Ticket::Article::Type.first.name,
  325. }
  326. end
  327. it 'creates a new ticket + a new article with a specific type' do
  328. it_creates_ticket(articles: 1)
  329. expect(Ticket.last.articles.last.type.name).to eq(Ticket::Article::Type.first.name)
  330. end
  331. context 'with all integrations disabled' do
  332. let(:article_payload) do
  333. {
  334. body: 'dummy',
  335. to: ['to@example.com'],
  336. type: 'email',
  337. security: {
  338. method: 'SMIME',
  339. options: %w[encryption sign]
  340. }
  341. }
  342. end
  343. before do
  344. Setting.set('smime_integration', false)
  345. Setting.set('pgp_integration', false)
  346. end
  347. it 'doesn\'t set security if security integrations are not enabled', :aggregate_failures do
  348. it_creates_ticket(articles: 1)
  349. expect(Ticket.last.articles.last.preferences[:security]).to be_nil
  350. end
  351. end
  352. context 'with smime enabled' do
  353. let(:article_payload) do
  354. {
  355. body: 'dummy',
  356. to: ['to@example.com'],
  357. type: 'email',
  358. security: {
  359. method: 'SMIME',
  360. options: %w[encryption sign]
  361. }
  362. }
  363. end
  364. before do
  365. Setting.set('smime_integration', true)
  366. Setting.set('pgp_integration', false)
  367. end
  368. it 'creates a new ticket with correct security preferences', :aggregate_failures do
  369. it_creates_ticket(articles: 1)
  370. expect(Ticket.last.articles.last.preferences[:security]).to eq(
  371. 'type' => 'S/MIME',
  372. 'encryption' => { 'success' => true },
  373. 'sign' => { 'success' => true },
  374. )
  375. end
  376. end
  377. context 'with pgp enabled' do
  378. let(:article_payload) do
  379. {
  380. body: 'dummy',
  381. to: ['to@example.com'],
  382. type: 'email',
  383. security: {
  384. method: 'PGP',
  385. options: %w[encryption sign]
  386. }
  387. }
  388. end
  389. before do
  390. Setting.set('smime_integration', false)
  391. Setting.set('pgp_integration', true)
  392. end
  393. it 'creates a new ticket with correct security preferences', :aggregate_failures do
  394. it_creates_ticket(articles: 1)
  395. expect(Ticket.last.articles.last.preferences[:security]).to eq(
  396. 'type' => 'PGP',
  397. 'encryption' => { 'success' => true },
  398. 'sign' => { 'success' => true },
  399. )
  400. end
  401. end
  402. end
  403. end
  404. context 'with to: and cc: being string values' do
  405. let(:article_payload) do
  406. {
  407. body: 'dummy',
  408. to: 'to@example.com',
  409. cc: 'cc@example.com',
  410. }
  411. end
  412. it 'creates a new ticket + a new article and sets correct "to" and "cc" values', :aggregate_failures do
  413. it_creates_ticket(articles: 1)
  414. expect(Ticket.last.articles.last).to have_attributes(to: 'to@example.com', cc: 'cc@example.com')
  415. end
  416. end
  417. context 'with to: and cc: containing array values' do
  418. let(:article_payload) do
  419. {
  420. body: 'dummy',
  421. to: ['to@example.com', 'to2@example.com'],
  422. cc: ['cc@example.com', 'cc2@example.com'],
  423. }
  424. end
  425. it 'creates a new ticket + a new article and sets correct "to" and "cc" values', :aggregate_failures do
  426. it_creates_ticket(articles: 1)
  427. expect(Ticket.last.articles.last).to have_attributes(to: 'to@example.com, to2@example.com', cc: 'cc@example.com, cc2@example.com')
  428. end
  429. end
  430. context 'with a shared draft' do
  431. let(:shared_draft) { create(:ticket_shared_draft_start, group:) }
  432. let(:input_payload) do
  433. input_base_payload
  434. .merge(sharedDraftId: Gql::ZammadSchema.id_from_object(shared_draft))
  435. end
  436. it 'passed to ticket create service' do
  437. expect_any_instance_of(Service::Ticket::Create)
  438. .to receive(:execute)
  439. .with(ticket_data: include(shared_draft:))
  440. .and_call_original
  441. gql.execute(query, variables: variables)
  442. end
  443. end
  444. end
  445. context 'with a customer', authenticated_as: :customer do
  446. let(:input_payload) { input_base_payload.tap { |h| h.delete(:customer) } }
  447. let(:expected_response) do
  448. expected_base_response.merge(
  449. {
  450. 'owner' => { 'fullname' => nil },
  451. 'priority' => { 'name' => Ticket::Priority.where(default_create: true).first.name },
  452. 'tags' => nil
  453. }
  454. )
  455. end
  456. it 'creates the ticket with filtered values' do
  457. it_creates_ticket
  458. expect(gql.result.data['ticket']).to eq(expected_response)
  459. end
  460. context 'when sending a different customerId' do
  461. let(:input_payload) { input_base_payload.tap { |h| h[:customer][:id] = gql.id(create(:customer)) } }
  462. it 'fails creating a ticket with permission exception' do
  463. it_fails_to_create_ticket
  464. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  465. expect(gql.result.error_message).to eq('Access forbidden by Gql::Types::UserType')
  466. end
  467. end
  468. context 'with article' do
  469. context 'with a forbidden sender' do
  470. let(:article_payload) do
  471. {
  472. body: 'dummy',
  473. sender: 'Agent',
  474. }
  475. end
  476. it 'creates a new ticket + a new article, but falls back to "Customer" as sender' do
  477. it_creates_ticket(articles: 1)
  478. expect(Ticket.last.articles.last.sender.name).to eq('Customer')
  479. end
  480. end
  481. context 'with type "phone"' do
  482. let(:article_payload) do
  483. {
  484. body: 'dummy',
  485. type: 'phone',
  486. }
  487. end
  488. it 'creates a new ticket + a new article, but falls back to "note" as type' do
  489. it_creates_ticket(articles: 1)
  490. expect(Ticket.last.articles.last.type.name).to eq('note')
  491. end
  492. it 'sets correct "to" and "from" values', :aggregate_failures do
  493. it_creates_ticket(articles: 1)
  494. expect(Ticket.last.articles.last)
  495. .to have_attributes(
  496. to: Ticket.last.group.name,
  497. from: customer.fullname
  498. )
  499. end
  500. end
  501. context 'with an article flagged as internal' do
  502. let(:article_payload) do
  503. {
  504. body: 'dummy',
  505. internal: true,
  506. }
  507. end
  508. it 'creates a new ticket + a new article, but flags it as not internal' do
  509. it_creates_ticket(articles: 1)
  510. expect(Ticket.last.articles.last.internal).to be(false)
  511. end
  512. end
  513. end
  514. end
  515. context 'with an agent that has a specific role limited to create/update permission', authenticated_as: :user do
  516. let(:user) { create(:user, roles: [api_role]) }
  517. let(:api_role) do
  518. role = create(:role, name: 'API', permission_names: ['ticket.agent'])
  519. role.group_names_access_map = {
  520. Group.first.name => %w[create],
  521. }
  522. role
  523. end
  524. let(:input_payload) do
  525. {
  526. title: 'Test title for issue #4647',
  527. groupId: gql.id(Group.first),
  528. customer: { id: gql.id(customer) },
  529. article: article_payload,
  530. }
  531. end
  532. let(:article_payload) do
  533. {
  534. type: 'web',
  535. internal: false,
  536. sender: 'Customer',
  537. subject: 'Test subject',
  538. body: SecureRandom.uuid,
  539. }
  540. end
  541. before { Trigger.destroy_all } # triggers may cause additional articles to be created
  542. it 'contains correct "origin_by" + "from" information' do
  543. gql.execute(query, variables: variables)
  544. expect(Ticket.last.articles.last).to have_attributes(
  545. origin_by_id: customer.id,
  546. from: "#{customer.fullname} <#{customer.email}>",
  547. )
  548. end
  549. end
  550. end
  551. end