frontend_attributes_spec.rb 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::ObjectManager::FrontendAttributes, type: :graphql do
  4. context 'when fetching frontend attributes' do
  5. let(:query) do
  6. <<~QUERY
  7. query objectManagerFrontendAttributes(
  8. $object: EnumObjectManagerObjects!
  9. ) {
  10. objectManagerFrontendAttributes(
  11. object: $object
  12. ) {
  13. attributes {
  14. name
  15. display
  16. dataType
  17. dataOption
  18. isInternal
  19. }
  20. screens {
  21. name
  22. attributes
  23. }
  24. }
  25. }
  26. QUERY
  27. end
  28. let(:variables) { { object: object } }
  29. let(:expected_result_agent) { nil }
  30. let(:expected_result_customer) { nil }
  31. shared_context 'when fetching frontend attributes as agent and customer' do
  32. context 'with an agent', authenticated_as: :user, db_strategy: :reset do
  33. let(:user) { create(:admin) }
  34. let(:object_attribute) do
  35. create(:object_manager_attribute_text, object_name: object, screens: { 'edit' => {
  36. 'ticket.agent' => {
  37. 'shown' => true,
  38. 'required' => true,
  39. },
  40. 'admin.organization' => {
  41. 'shown' => true,
  42. 'required' => true,
  43. }
  44. } }).tap do
  45. ObjectManager::Attribute.migration_execute
  46. end
  47. end
  48. it 'returns frontend object attributes' do
  49. gql.execute(query, variables: variables)
  50. expect(gql.result.data).to eq(expected_result_agent)
  51. end
  52. context 'with a shown attribute' do
  53. before do
  54. object_attribute
  55. gql.execute(query, variables: variables)
  56. end
  57. it 'does contain the shown attribute' do
  58. edit_screen_attributes = gql.result.data['screens'].find { |screen| screen['name'] == 'edit' }['attributes']
  59. expect(edit_screen_attributes).to include(object_attribute.name)
  60. end
  61. it 'does contain shown attribute which is not internal' do
  62. frontend_object_attribute = gql.result.data['attributes'].find { |attribute| attribute['name'] == object_attribute.name }
  63. expect(frontend_object_attribute['isInternal']).to be(false)
  64. end
  65. end
  66. context 'with a hidden attribute' do
  67. before do
  68. object_attribute
  69. object_attribute.update!(screens: { 'edit' => {
  70. 'ticket.agent' => {
  71. 'shown' => false,
  72. 'required' => false,
  73. },
  74. 'admin.organization' => {
  75. 'shown' => false,
  76. 'required' => false,
  77. }
  78. },
  79. 'view' => {
  80. 'ticket.agent' => {
  81. 'shown' => false,
  82. 'required' => false,
  83. },
  84. 'admin.organization' => {
  85. 'shown' => false,
  86. 'required' => false,
  87. }
  88. } })
  89. gql.execute(query, variables: variables)
  90. end
  91. it 'does contain also the hidden attribute because core workflow is active for the screen (edit)' do
  92. edit_screen_attributes = gql.result.data['screens'].find { |screen| screen['name'] == 'edit' }['attributes']
  93. expect(edit_screen_attributes).to include(object_attribute.name)
  94. end
  95. it 'does not contain the hidden attribute because for the screen (view)' do
  96. edit_screen_attributes = gql.result.data['screens'].find { |screen| screen['name'] == 'view' }['attributes']
  97. expect(edit_screen_attributes).not_to include(object_attribute.name)
  98. end
  99. end
  100. end
  101. context 'with a customer', authenticated_as: :user do
  102. let(:user) { create(:customer) }
  103. it 'returns all object manager attributes' do
  104. gql.execute(query, variables: variables)
  105. expect(gql.result.data).to eq(expected_result_customer)
  106. end
  107. end
  108. end
  109. context 'with object "Organization"' do
  110. let(:object) { 'Organization' }
  111. let(:expected_result_agent) do
  112. {
  113. 'attributes' => [
  114. {
  115. 'name' => 'name',
  116. 'display' => 'Name',
  117. 'dataType' => 'input',
  118. 'dataOption' => { 'type' => 'text',
  119. 'maxlength' => 150,
  120. 'null' => false,
  121. 'item_class' => 'formGroup--halfSize' },
  122. 'isInternal' => true,
  123. },
  124. {
  125. 'name' => 'shared',
  126. 'display' => 'Shared organization',
  127. 'dataType' => 'boolean',
  128. 'dataOption' => { 'null' => true,
  129. 'default' => true,
  130. 'note' => "Customers in the organization can view each other's items.",
  131. 'item_class' => 'formGroup--halfSize',
  132. 'options' => { 'true' => 'yes',
  133. 'false' => 'no' },
  134. 'translate' => true,
  135. 'permission' => ['admin.organization'] },
  136. 'isInternal' => true,
  137. },
  138. {
  139. 'name' => 'domain_assignment',
  140. 'display' => 'Domain based assignment',
  141. 'dataType' => 'boolean',
  142. 'dataOption' => { 'null' => true,
  143. 'default' => false,
  144. 'note' => 'Assign users based on user domain.',
  145. 'item_class' => 'formGroup--halfSize',
  146. 'options' => { 'true' => 'yes',
  147. 'false' => 'no' },
  148. 'translate' => true,
  149. 'permission' => ['admin.organization'] },
  150. 'isInternal' => true,
  151. },
  152. {
  153. 'name' => 'domain',
  154. 'display' => 'Domain',
  155. 'dataType' => 'input',
  156. 'dataOption' => { 'type' => 'text',
  157. 'maxlength' => 150,
  158. 'null' => true,
  159. 'item_class' => 'formGroup--halfSize' },
  160. 'isInternal' => true,
  161. },
  162. {
  163. 'name' => 'vip',
  164. 'display' => 'VIP',
  165. 'dataType' => 'boolean',
  166. 'dataOption' => { 'null' => true,
  167. 'default' => false,
  168. 'options' => { 'false' => 'no', 'true' => 'yes' },
  169. 'translate' => true,
  170. 'permission' => ['admin.organization'],
  171. 'item_class' => 'formGroup--halfSize' },
  172. 'isInternal' => true,
  173. },
  174. {
  175. 'name' => 'note',
  176. 'display' => 'Note',
  177. 'dataType' => 'richtext',
  178. 'dataOption' => { 'type' => 'text',
  179. 'maxlength' => 5000,
  180. 'no_images' => true,
  181. 'null' => true,
  182. 'note' => 'Notes are visible to agents only, never to customers.' },
  183. 'isInternal' => true,
  184. },
  185. {
  186. 'name' => 'active',
  187. 'display' => 'Active',
  188. 'dataType' => 'active',
  189. 'dataOption' => { 'null' => true,
  190. 'default' => true,
  191. 'permission' => ['admin.organization'] },
  192. 'isInternal' => true,
  193. }
  194. ],
  195. 'screens' => [
  196. {
  197. 'name' => 'edit',
  198. 'attributes' => %w[name shared domain_assignment domain vip note active],
  199. },
  200. {
  201. 'name' => 'create',
  202. 'attributes' => %w[name shared domain_assignment domain vip note active],
  203. },
  204. {
  205. 'name' => 'view',
  206. 'attributes' => %w[name shared domain_assignment domain note],
  207. }
  208. ],
  209. }
  210. end
  211. let(:expected_result_customer) do
  212. {
  213. 'attributes' => [
  214. {
  215. 'name' => 'name',
  216. 'display' => 'Name',
  217. 'dataType' => 'input',
  218. 'dataOption' => { 'type' => 'text',
  219. 'maxlength' => 150,
  220. 'null' => false,
  221. 'item_class' => 'formGroup--halfSize' },
  222. 'isInternal' => true,
  223. },
  224. {
  225. 'name' => 'domain',
  226. 'display' => 'Domain',
  227. 'dataType' => 'input',
  228. 'dataOption' => { 'type' => 'text',
  229. 'maxlength' => 150,
  230. 'null' => true,
  231. 'item_class' => 'formGroup--halfSize' },
  232. 'isInternal' => true,
  233. },
  234. {
  235. 'name' => 'note',
  236. 'display' => 'Note',
  237. 'dataType' => 'richtext',
  238. 'dataOption' => { 'type' => 'text',
  239. 'maxlength' => 5000,
  240. 'no_images' => true,
  241. 'null' => true,
  242. 'note' => 'Notes are visible to agents only, never to customers.' },
  243. 'isInternal' => true,
  244. },
  245. ],
  246. 'screens' => [
  247. {
  248. 'name' => 'edit',
  249. 'attributes' => %w[name domain note],
  250. },
  251. {
  252. 'name' => 'create',
  253. 'attributes' => %w[name domain note],
  254. },
  255. {
  256. 'name' => 'view',
  257. 'attributes' => %w[name],
  258. }
  259. ]
  260. }
  261. end
  262. include_context 'when fetching frontend attributes as agent and customer'
  263. end
  264. context 'with object "Ticket"' do
  265. let(:object) { 'Ticket' }
  266. let(:expected_result_agent) do
  267. {
  268. 'attributes' => [
  269. {
  270. 'name' => 'title',
  271. 'display' => 'Title',
  272. 'dataType' => 'input',
  273. 'dataOption' => {
  274. 'type' => 'text',
  275. 'maxlength' => 200,
  276. 'null' => false,
  277. 'translate' => false
  278. },
  279. 'isInternal' => true,
  280. },
  281. {
  282. 'name' => 'customer_id',
  283. 'display' => 'Customer',
  284. 'dataType' => 'user_autocompletion',
  285. 'dataOption' => {
  286. 'relation' => 'User',
  287. 'autocapitalize' => false,
  288. 'multiple' => false,
  289. 'guess' => true,
  290. 'null' => false,
  291. 'limit' => 200,
  292. 'placeholder' => 'Enter Person or Organization/Company',
  293. 'minLengt' => 2,
  294. 'translate' => false,
  295. 'permission' => ['ticket.agent'],
  296. 'belongs_to' => 'customer',
  297. },
  298. 'isInternal' => true,
  299. },
  300. {
  301. 'name' => 'organization_id',
  302. 'display' => 'Organization',
  303. 'dataType' => 'autocompletion_ajax_customer_organization',
  304. 'dataOption' => {
  305. 'relation' => 'Organization',
  306. 'autocapitalize' => false,
  307. 'multiple' => false,
  308. 'null' => true,
  309. 'translate' => false,
  310. 'permission' => ['ticket.agent', 'ticket.customer'],
  311. 'belongs_to' => 'organization',
  312. },
  313. 'isInternal' => true,
  314. },
  315. {
  316. 'name' => 'group_id',
  317. 'display' => 'Group',
  318. 'dataType' => 'select',
  319. 'dataOption' => {
  320. 'default' => '',
  321. 'relation' => 'Group',
  322. 'relation_condition' => { 'access'=>'full' },
  323. 'nulloption' => true,
  324. 'multiple' => false,
  325. 'null' => false,
  326. 'translate' => false,
  327. 'only_shown_if_selectable' => true,
  328. 'permission' => ['ticket.agent', 'ticket.customer'],
  329. 'maxlength' => 255,
  330. 'belongs_to' => 'group',
  331. },
  332. 'isInternal' => true,
  333. },
  334. {
  335. 'name' => 'owner_id',
  336. 'display' => 'Owner',
  337. 'dataType' => 'select',
  338. 'dataOption' => {
  339. 'default' => '',
  340. 'relation' => 'User',
  341. 'relation_condition' => { 'roles'=>'Agent' },
  342. 'nulloption' => true,
  343. 'multiple' => false,
  344. 'null' => true,
  345. 'translate' => false,
  346. 'permission' => ['ticket.agent'],
  347. 'maxlength' => 255,
  348. 'belongs_to' => 'owner',
  349. },
  350. 'isInternal' => true,
  351. },
  352. {
  353. 'name' => 'state_id',
  354. 'display' => 'State',
  355. 'dataType' => 'select',
  356. 'dataOption' => {
  357. 'relation' => 'TicketState',
  358. 'nulloption' => true,
  359. 'multiple' => false,
  360. 'null' => false,
  361. 'default' => 2,
  362. 'translate' => true,
  363. 'filter' => Ticket::State.by_category(:viewable).pluck(:id),
  364. 'maxlength' => 255,
  365. 'belongs_to' => 'state',
  366. },
  367. 'isInternal' => true,
  368. },
  369. {
  370. 'name' => 'pending_time',
  371. 'display' => 'Pending till',
  372. 'dataType' => 'datetime',
  373. 'dataOption' => {
  374. 'future' => true,
  375. 'past' => false,
  376. 'diff' => nil,
  377. 'null' => true,
  378. 'translate' => true,
  379. 'permission' => ['ticket.agent'],
  380. },
  381. 'isInternal' => true,
  382. },
  383. {
  384. 'name' => 'priority_id',
  385. 'display' => 'Priority',
  386. 'dataType' => 'select',
  387. 'dataOption' => {
  388. 'relation' => 'TicketPriority',
  389. 'nulloption' => false,
  390. 'multiple' => false,
  391. 'null' => false,
  392. 'default' => 2,
  393. 'translate' => true,
  394. 'maxlength' => 255,
  395. 'belongs_to' => 'priority',
  396. },
  397. 'isInternal' => true,
  398. },
  399. {
  400. 'name' => 'tags',
  401. 'display' => 'Tags',
  402. 'dataType' => 'tag',
  403. 'dataOption' => {
  404. 'type' => 'text',
  405. 'null' => true,
  406. 'translate' => false
  407. },
  408. 'isInternal' => true,
  409. }
  410. ],
  411. 'screens' => [
  412. {
  413. 'attributes' => %w[title customer_id organization_id],
  414. 'name' => 'create_top'
  415. },
  416. {
  417. 'attributes' => %w[group_id owner_id state_id pending_time priority_id],
  418. 'name' => 'edit'
  419. },
  420. {
  421. 'attributes' => %w[group_id owner_id state_id pending_time priority_id],
  422. 'name' => 'create_middle'
  423. },
  424. {
  425. 'attributes' => ['tags'],
  426. 'name' => 'create_bottom'
  427. }
  428. ]
  429. }
  430. end
  431. let(:expected_result_customer) do
  432. {
  433. 'attributes' => [
  434. {
  435. 'name' => 'title',
  436. 'display' => 'Title',
  437. 'dataType' => 'input',
  438. 'dataOption' => {
  439. 'type' => 'text',
  440. 'maxlength' => 200,
  441. 'null' => false,
  442. 'translate' => false
  443. },
  444. 'isInternal' => true,
  445. },
  446. {
  447. 'name' => 'organization_id',
  448. 'display' => 'Organization',
  449. 'dataType' => 'autocompletion_ajax_customer_organization',
  450. 'dataOption' => {
  451. 'relation' => 'Organization',
  452. 'autocapitalize' => false,
  453. 'multiple' => false,
  454. 'null' => true,
  455. 'translate' => false,
  456. 'permission' => ['ticket.agent', 'ticket.customer'],
  457. 'belongs_to' => 'organization',
  458. },
  459. 'isInternal' => true,
  460. },
  461. {
  462. 'name' => 'group_id',
  463. 'display' => 'Group',
  464. 'dataType' => 'select',
  465. 'dataOption' => {
  466. 'default' => '',
  467. 'relation' => 'Group',
  468. 'relation_condition' => { 'access'=>'full' },
  469. 'nulloption' => true,
  470. 'multiple' => false,
  471. 'null' => false,
  472. 'translate' => false,
  473. 'only_shown_if_selectable' => true,
  474. 'permission' => ['ticket.agent', 'ticket.customer'],
  475. 'maxlength' => 255,
  476. 'belongs_to' => 'group',
  477. },
  478. 'isInternal' => true,
  479. },
  480. {
  481. 'name' => 'state_id',
  482. 'display' => 'State',
  483. 'dataType' => 'select',
  484. 'dataOption' => {
  485. 'relation' => 'TicketState',
  486. 'nulloption' => true,
  487. 'multiple' => false,
  488. 'null' => false,
  489. 'default' => 2,
  490. 'translate' => true,
  491. 'filter' => Ticket::State.by_category(:viewable).pluck(:id),
  492. 'maxlength' => 255,
  493. 'belongs_to' => 'state',
  494. },
  495. 'isInternal' => true,
  496. },
  497. ],
  498. 'screens' => [
  499. {
  500. 'attributes' => %w[title organization_id],
  501. 'name' => 'create_top'
  502. },
  503. {
  504. 'attributes' => ['state_id'],
  505. 'name' => 'edit'
  506. },
  507. {
  508. 'attributes' => %w[group_id state_id],
  509. 'name' => 'create_middle'
  510. },
  511. {
  512. 'attributes' => [],
  513. 'name' => 'create_bottom'
  514. }
  515. ]
  516. }
  517. end
  518. include_context 'when fetching frontend attributes as agent and customer'
  519. end
  520. end
  521. end