update_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Ticket::Update, :aggregate_failures, type: :graphql do
  4. let(:query) do
  5. <<~QUERY
  6. mutation ticketUpdate($ticketId: ID!, $input: TicketUpdateInput!, $skipValidator: String) {
  7. ticketUpdate(ticketId: $ticketId, input: $input, skipValidator: $skipValidator) {
  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. }
  30. errors {
  31. message
  32. field
  33. exception
  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(:ticket) { create(:ticket, group: agent.groups.first, customer: customer) }
  45. let(:article_payload) { nil }
  46. let(:input_base_payload) do
  47. {
  48. title: 'Ticket Create Mutation Test',
  49. groupId: gql.id(group),
  50. priorityId: gql.id(priority),
  51. customer: { id: gql.id(customer) },
  52. ownerId: gql.id(agent),
  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) { { ticketId: gql.id(ticket), 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. 'objectAttributeValues' => [],
  69. }
  70. end
  71. let(:expected_response) do
  72. expected_base_response
  73. end
  74. context 'when updating a ticket' do
  75. context 'with an agent', authenticated_as: :agent do
  76. it 'updates the attributes' do
  77. gql.execute(query, variables: variables)
  78. expect(gql.result.data['ticket']).to eq(expected_response)
  79. end
  80. context 'without title' do
  81. let(:input_payload) { input_base_payload.tap { |h| h[:title] = ' ' } }
  82. it 'fails validation' do
  83. gql.execute(query, variables: variables)
  84. expect(gql.result.error_message).to include('Variable $input of type TicketUpdateInput! was provided invalid value for title')
  85. end
  86. end
  87. context 'with an article payload' do
  88. let(:article_payload) do
  89. {
  90. body: 'dummy',
  91. type: 'note',
  92. }
  93. end
  94. it 'adds a new article with a specific type' do
  95. expect { gql.execute(query, variables: variables) }
  96. .to change(Ticket::Article, :count).by(1)
  97. expect(Ticket.last.articles.last.type.name).to eq('note')
  98. end
  99. it 'adds a new article with a specific sender' do
  100. expect { gql.execute(query, variables: variables) }
  101. .to change(Ticket::Article, :count).by(1)
  102. expect(Ticket.last.articles.last.sender.name).to eq('Agent')
  103. end
  104. context 'with time unit' do
  105. let(:time_accounting_enabled) { true }
  106. let(:variables) { { ticketId: gql.id(ticket), input: input_payload, skipValidator: 'Service::Ticket::Update::Validator::TimeAccounting::ConditionMatchesError' } }
  107. let(:article_payload) do
  108. {
  109. body: 'dummy',
  110. type: 'web',
  111. timeUnit: 123,
  112. }
  113. end
  114. before do
  115. Setting.set('time_accounting', time_accounting_enabled)
  116. end
  117. it 'adds a new article with time unit' do
  118. expect { gql.execute(query, variables: variables) }
  119. .to change(Ticket::Article, :count).by(1)
  120. expect(Ticket.last.articles.last.ticket_time_accounting.time_unit).to eq(123)
  121. end
  122. context 'when time accounting disabled' do
  123. let(:time_accounting_enabled) { false }
  124. it 'does not create ticket article' do
  125. expect { gql.execute(query, variables: variables) }
  126. .not_to change(Ticket::Article, :count)
  127. expect(gql.result.error_message)
  128. .to match('Time Accounting is not enabled')
  129. end
  130. end
  131. end
  132. context 'with active secure mailing (S/MIME)' do
  133. before do
  134. Setting.set('smime_integration', true)
  135. end
  136. it 'adds a new article' do
  137. expect { gql.execute(query, variables: variables) }
  138. .to change(Ticket::Article, :count).by(1)
  139. expect(Ticket.last.articles.last.type.name).to eq('note')
  140. end
  141. end
  142. end
  143. context 'with custom object_attribute', db_strategy: :reset do
  144. let(:object_attribute) do
  145. screens = { create: { 'admin.organization': { shown: true, required: false } } }
  146. create(:object_manager_attribute_text, object_name: 'Ticket', screens: screens).tap do |_oa|
  147. ObjectManager::Attribute.migration_execute
  148. end
  149. end
  150. let(:input_payload) do
  151. input_base_payload.merge(
  152. {
  153. objectAttributeValues: [ { name: object_attribute.name, value: 'object_attribute_value' } ]
  154. }
  155. )
  156. end
  157. let(:expected_response) do
  158. expected_base_response.merge(
  159. {
  160. 'objectAttributeValues' => [{ 'attribute' => { 'name'=>object_attribute.name }, 'value' => 'object_attribute_value' }]
  161. }
  162. )
  163. end
  164. it 'updates the attributes' do
  165. gql.execute(query, variables: variables)
  166. expect(gql.result.data['ticket']).to eq(expected_response)
  167. end
  168. end
  169. context 'when moving the ticket into a group with only :change permission' do
  170. let(:group) { create(:group) }
  171. before do
  172. user.groups << group
  173. user.group_names_access_map = { user.groups.first.name => %w[read change], group.name => ['change'] }
  174. end
  175. it 'updates the ticket, but returns an error trying to access the new ticket' do
  176. gql.execute(query, variables: variables)
  177. expect(ticket.reload.group_id).to eq(group.id)
  178. expect(gql.result.payload['data']['ticketUpdate']).to eq({ 'ticket' => nil, 'errors' => nil }) # Mutation did run, but data retrieval was not authorized.
  179. expect(gql.result.payload['errors'].first['message']).to eq('Access forbidden by Gql::Types::TicketType')
  180. expect(gql.result.payload['errors'].first['extensions']['type']).to eq('Exceptions::Forbidden')
  181. end
  182. end
  183. context 'with no permission to the group' do
  184. let(:group) { create(:group) }
  185. it 'raises an error', :aggregate_failures do
  186. gql.execute(query, variables: variables)
  187. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  188. expect(gql.result.error_message).to eq('Access forbidden by Gql::Types::GroupType')
  189. end
  190. end
  191. context 'when ticket has a checklist and is being closed' do
  192. let(:checklist) { create(:checklist, ticket: ticket) }
  193. let(:input_base_payload) do
  194. {
  195. title: 'Ticket Create Mutation Test',
  196. groupId: gql.id(group),
  197. priorityId: gql.id(priority),
  198. customer: { id: gql.id(customer) },
  199. ownerId: gql.id(agent),
  200. article: article_payload,
  201. stateId: gql.id(Ticket::State.find_by(name: 'closed')),
  202. }
  203. end
  204. before do
  205. checklist
  206. gql.execute(query, variables: variables)
  207. end
  208. it 'returns a user error' do
  209. expect(gql.result.data).to eq({
  210. 'ticket' => nil,
  211. 'errors' => [
  212. 'message' => 'The ticket checklist is incomplete.',
  213. 'field' => nil,
  214. 'exception' => 'Service::Ticket::Update::Validator::ChecklistCompleted::IncompleteChecklistError',
  215. ],
  216. })
  217. end
  218. context 'when validator is being skipped' do
  219. let(:variables) { { ticketId: gql.id(ticket), input: input_payload, skipValidator: 'Service::Ticket::Update::Validator::ChecklistCompleted::IncompleteChecklistError' } }
  220. it 'updates the ticket' do
  221. expect(gql.result.data['ticket']).to eq(expected_response)
  222. end
  223. end
  224. end
  225. end
  226. context 'with a customer', authenticated_as: :customer do
  227. let(:input_payload) { input_base_payload.tap { |h| h.delete(:customer) } }
  228. let(:expected_response) do
  229. expected_base_response.merge(
  230. {
  231. 'owner' => { 'fullname' => nil },
  232. 'priority' => { 'name' => Ticket::Priority.where(default_create: true).first.name },
  233. }
  234. )
  235. end
  236. it 'updates the ticket with filtered values' do
  237. gql.execute(query, variables: variables)
  238. expect(gql.result.data['ticket']).to eq(expected_response)
  239. end
  240. context 'when sending a different customerId' do
  241. let(:input_payload) { input_base_payload.tap { |h| h[:customer][:id] = gql.id(create(:customer)) } }
  242. it 'fails creating a ticket with permission exception' do
  243. gql.execute(query, variables: variables)
  244. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  245. expect(gql.result.error_message).to eq('Access forbidden by Gql::Types::UserType')
  246. end
  247. end
  248. context 'with an article payload' do
  249. let(:article_payload) do
  250. {
  251. body: 'dummy',
  252. type: 'web',
  253. }
  254. end
  255. it 'adds a new article with a specific type' do
  256. expect { gql.execute(query, variables: variables) }
  257. .to change(Ticket::Article, :count).by(1)
  258. expect(Ticket.last.articles.last.type.name).to eq('web')
  259. end
  260. it 'adds a new article with a specific sender' do
  261. expect { gql.execute(query, variables: variables) }
  262. .to change(Ticket::Article, :count).by(1)
  263. expect(Ticket.last.articles.last.sender.name).to eq('Customer')
  264. end
  265. end
  266. end
  267. end
  268. end