attribute_spec.rb 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe ObjectManager::Attribute, type: :model do
  4. describe 'callbacks' do
  5. context 'for setting default values on local data options' do
  6. subject(:attr) { described_class.new }
  7. context ':null' do
  8. it 'sets nil values to true' do
  9. expect { attr.validate }
  10. .to change { attr.data_option[:null] }.to(true)
  11. end
  12. it 'does not overwrite false values' do
  13. attr.data_option[:null] = false
  14. expect { attr.validate }
  15. .not_to change { attr.data_option[:null] }
  16. end
  17. end
  18. context ':maxlength' do
  19. context 'for data_type: select / tree_select / checkbox' do
  20. subject(:attr) { described_class.new(data_type: 'select') }
  21. it 'sets nil values to 255' do
  22. expect { attr.validate }
  23. .to change { attr.data_option[:maxlength] }.to(255)
  24. end
  25. end
  26. end
  27. context ':nulloption' do
  28. context 'for data_type: select / tree_select / checkbox' do
  29. subject(:attr) { described_class.new(data_type: 'select') }
  30. it 'sets nil values to true' do
  31. expect { attr.validate }
  32. .to change { attr.data_option[:nulloption] }.to(true)
  33. end
  34. it 'does not overwrite false values' do
  35. attr.data_option[:nulloption] = false
  36. expect { attr.validate }
  37. .not_to change { attr.data_option[:nulloption] }
  38. end
  39. end
  40. end
  41. end
  42. end
  43. describe 'check name' do
  44. it 'rejects ActiveRecord reserved word "attribute"' do
  45. expect do
  46. described_class.add attributes_for :object_manager_attribute_text, name: 'attribute'
  47. end.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Name attribute is a reserved word')
  48. end
  49. %w[destroy true false integer select drop create alter index table varchar blob date datetime timestamp url icon initials avatar permission validate subscribe unsubscribe translate search _type _doc _id id action].each do |reserved_word|
  50. it "rejects Zammad reserved word '#{reserved_word}'" do
  51. expect do
  52. described_class.add attributes_for :object_manager_attribute_text, name: reserved_word
  53. end.to raise_error(ActiveRecord::RecordInvalid, %r{is a reserved word})
  54. end
  55. end
  56. %w[someting_id something_ids].each do |reserved_word|
  57. it "rejects word '#{reserved_word}' which is used for database references" do
  58. expect do
  59. described_class.add attributes_for :object_manager_attribute_text, name: reserved_word
  60. end.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Name can't be used because *_id and *_ids are not allowed")
  61. end
  62. end
  63. %w[title tags number].each do |not_editable_attribute|
  64. it "rejects '#{not_editable_attribute}' which is used" do
  65. expect do
  66. described_class.add attributes_for :object_manager_attribute_text, name: not_editable_attribute
  67. end.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Name attribute is not editable')
  68. end
  69. end
  70. %w[priority state note].each do |existing_attribute|
  71. it "rejects '#{existing_attribute}' which is used" do
  72. expect do
  73. described_class.add attributes_for :object_manager_attribute_text, name: existing_attribute
  74. end.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Name #{existing_attribute} already exists")
  75. end
  76. end
  77. it 'rejects duplicate attribute name of conflicting types' do
  78. attribute = attributes_for(:object_manager_attribute_text)
  79. described_class.add attribute
  80. attribute[:data_type] = 'boolean'
  81. expect do
  82. described_class.add attribute
  83. end.to raise_error ActiveRecord::RecordInvalid
  84. end
  85. it 'accepts duplicate attribute name on the same types (editing an existing attribute)' do
  86. attribute = attributes_for(:object_manager_attribute_text)
  87. described_class.add attribute
  88. expect do
  89. described_class.add attribute
  90. end.not_to raise_error
  91. end
  92. it 'accepts duplicate attribute name on compatible types (editing the type of an existing attribute)' do
  93. attribute = attributes_for(:object_manager_attribute_text)
  94. described_class.add attribute
  95. attribute[:data_type] = 'select'
  96. attribute[:data_option_new] = { default: '', options: { 'a' => 'a' } }
  97. expect do
  98. described_class.add attribute
  99. end.not_to raise_error
  100. end
  101. it 'accepts valid attribute names' do
  102. expect do
  103. described_class.add attributes_for :object_manager_attribute_text
  104. end.not_to raise_error
  105. end
  106. end
  107. describe 'validate that referenced attributes are not set as inactive' do
  108. subject(:attr) { create(:object_manager_attribute_text) }
  109. before do
  110. allow(described_class)
  111. .to receive(:attribute_used_by_references?)
  112. .with(attr.object_lookup.name, attr.name)
  113. .and_return(is_referenced)
  114. attr.active = active
  115. end
  116. context 'when is used and changing to inactive' do
  117. let(:active) { false }
  118. let(:is_referenced) { true }
  119. it { is_expected.not_to be_valid }
  120. it do
  121. attr.valid?
  122. expect(attr.errors).not_to be_blank
  123. end
  124. end
  125. context 'when is not used and changing to inactive' do
  126. let(:active) { false }
  127. let(:is_referenced) { false }
  128. it { is_expected.to be_valid }
  129. end
  130. context 'when is used and staying active and chan' do
  131. let(:active) { true }
  132. let(:is_referenced) { true }
  133. it { is_expected.to be_valid }
  134. end
  135. end
  136. describe 'Class methods:' do
  137. describe '.pending_migration?', db_strategy: :reset do
  138. it 'returns false if there are no pending migrations' do
  139. expect(described_class.pending_migration?).to be false
  140. end
  141. it 'returns true if there are pending migrations' do
  142. create(:object_manager_attribute_text)
  143. expect(described_class.pending_migration?).to be true
  144. end
  145. it 'returns false if migration was executed' do
  146. create(:object_manager_attribute_text)
  147. described_class.migration_execute
  148. expect(described_class.pending_migration?).to be false
  149. end
  150. end
  151. describe '.attribute_to_references_hash_objects' do
  152. it 'returns classes with conditions' do
  153. expect(described_class.attribute_to_references_hash_objects).to contain_exactly(Trigger, Overview, Job, Sla, Report::Profile)
  154. end
  155. end
  156. describe '.data_options_hash' do
  157. context 'when hash' do
  158. let(:check) do
  159. {
  160. 'a' => 'A',
  161. 'b' => 'B',
  162. 'c' => 'c',
  163. }
  164. end
  165. it 'does return the options as hash' do
  166. expect(described_class.data_options_hash(check)).to eq({
  167. 'a' => 'A',
  168. 'b' => 'B',
  169. 'c' => 'c',
  170. })
  171. end
  172. end
  173. context 'when array' do
  174. let(:check) do
  175. [
  176. {
  177. value: 'a',
  178. name: 'A',
  179. },
  180. {
  181. value: 'b',
  182. name: 'B',
  183. },
  184. {
  185. value: 'c',
  186. name: 'c',
  187. },
  188. ]
  189. end
  190. it 'does return the options as hash' do
  191. expect(described_class.data_options_hash(check)).to eq({
  192. 'a' => 'A',
  193. 'b' => 'B',
  194. 'c' => 'c',
  195. })
  196. end
  197. end
  198. context 'when tree array' do
  199. let(:check) do
  200. [
  201. {
  202. value: 'a',
  203. name: 'A',
  204. },
  205. {
  206. value: 'b',
  207. name: 'B',
  208. },
  209. {
  210. value: 'c',
  211. name: 'c',
  212. children: [
  213. {
  214. value: 'c::a',
  215. name: 'c sub a',
  216. },
  217. {
  218. value: 'c::b',
  219. name: 'c sub b',
  220. },
  221. {
  222. value: 'c::c',
  223. name: 'c sub c',
  224. },
  225. ],
  226. },
  227. ]
  228. end
  229. it 'does return the options as hash' do
  230. expect(described_class.data_options_hash(check)).to eq({
  231. 'a' => 'A',
  232. 'b' => 'B',
  233. 'c' => 'c',
  234. 'c::a' => 'c sub a',
  235. 'c::b' => 'c sub b',
  236. 'c::c' => 'c sub c',
  237. })
  238. end
  239. end
  240. end
  241. end
  242. describe '#data_option_validations' do
  243. context 'when maxlength is checked for non-integers' do
  244. shared_examples 'tests the exception on invalid maxlength values' do |type|
  245. context "when type '#{type}'" do
  246. subject(:attr) { described_class.new(data_type: type, data_option: { maxlength: 'brbrbr' }) }
  247. it 'does throw an exception' do
  248. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{Data option must have integer for :maxlength})
  249. end
  250. end
  251. end
  252. include_examples 'tests the exception on invalid maxlength values', 'input'
  253. include_examples 'tests the exception on invalid maxlength values', 'textarea'
  254. include_examples 'tests the exception on invalid maxlength values', 'richtext'
  255. end
  256. context 'when type is checked' do
  257. shared_examples 'tests the exception on invalid types' do |type|
  258. context "when type '#{type}'" do
  259. subject(:attr) { described_class.new(data_type: type, data_option: { type: 'brbrbr' }) }
  260. it 'does throw an exception' do
  261. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{must have one of text/password/tel/fax/email/url for :type})
  262. end
  263. end
  264. end
  265. include_examples 'tests the exception on invalid types', 'input'
  266. end
  267. context 'when min max values are checked' do
  268. shared_examples 'tests the exception on invalid min max values' do |type|
  269. context "when type '#{type}'" do
  270. context 'when no integer for min' do
  271. subject(:attr) { described_class.new(data_type: type, data_option: { min: 'brbrbr' }) }
  272. it 'does throw an exception' do
  273. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{must have integer for :min})
  274. end
  275. end
  276. context 'when no integer for max' do
  277. subject(:attr) { described_class.new(data_type: type, data_option: { max: 'brbrbr' }) }
  278. it 'does throw an exception' do
  279. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{must have integer for :max})
  280. end
  281. end
  282. context 'when high integer for min' do
  283. subject(:attr) { described_class.new(data_type: type, data_option: { min: 999_999_999_999 }) }
  284. it 'does throw an exception' do
  285. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{min must be lower than 2147483648})
  286. end
  287. end
  288. context 'when high integer for max' do
  289. subject(:attr) { described_class.new(data_type: type, data_option: { max: 999_999_999_999 }) }
  290. it 'does throw an exception' do
  291. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{max must be lower than 2147483648})
  292. end
  293. end
  294. context 'when negative high integer for min' do
  295. subject(:attr) { described_class.new(data_type: type, data_option: { min: -999_999_999_999 }) }
  296. it 'does throw an exception' do
  297. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{min must be higher than -2147483648})
  298. end
  299. end
  300. context 'when negative high integer for max' do
  301. subject(:attr) { described_class.new(data_type: type, data_option: { max: -999_999_999_999 }) }
  302. it 'does throw an exception' do
  303. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{max must be higher than -2147483648})
  304. end
  305. end
  306. context 'when min is greater than max' do
  307. subject(:attr) { described_class.new(data_type: type, data_option: { min: 5, max: 2 }) }
  308. it 'does throw an exception' do
  309. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{min must be lower than max})
  310. end
  311. end
  312. end
  313. end
  314. include_examples 'tests the exception on invalid min max values', 'integer'
  315. end
  316. context 'when default is checked' do
  317. shared_examples 'tests the exception on missing default' do |type|
  318. context "when type '#{type}'" do
  319. subject(:attr) { described_class.new(data_type: type, data_option: {}) }
  320. it 'does throw an exception' do
  321. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{must have value for :default})
  322. end
  323. end
  324. end
  325. include_examples 'tests the exception on missing default', 'select'
  326. include_examples 'tests the exception on missing default', 'tree_select'
  327. include_examples 'tests the exception on missing default', 'checkbox'
  328. include_examples 'tests the exception on missing default', 'boolean'
  329. end
  330. context 'when relation is checked' do
  331. shared_examples 'tests the exception on missing relation' do |type|
  332. context "when type '#{type}'" do
  333. subject(:attr) { described_class.new(data_type: type, data_option: {}) }
  334. it 'does throw an exception' do
  335. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{must have non-nil value for either :options or :relation})
  336. end
  337. end
  338. end
  339. include_examples 'tests the exception on missing relation', 'select'
  340. include_examples 'tests the exception on missing relation', 'tree_select'
  341. include_examples 'tests the exception on missing relation', 'checkbox'
  342. end
  343. context 'when nil options are checked' do
  344. shared_examples 'tests the exception on missing nil options' do |type|
  345. context "when type '#{type}'" do
  346. subject(:attr) { described_class.new(data_type: type, data_option: {}) }
  347. it 'does throw an exception' do
  348. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{must have non-nil value for :options})
  349. end
  350. end
  351. end
  352. include_examples 'tests the exception on missing nil options', 'boolean'
  353. end
  354. context 'when future is checked' do
  355. shared_examples 'tests the exception on missing future' do |type|
  356. context "when type '#{type}'" do
  357. subject(:attr) { described_class.new(data_type: type, data_option: {}) }
  358. it 'does throw an exception' do
  359. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{must have boolean value for :future})
  360. end
  361. end
  362. end
  363. include_examples 'tests the exception on missing future', 'datetime'
  364. end
  365. context 'when past is checked' do
  366. shared_examples 'tests the exception on missing past' do |type|
  367. context "when type '#{type}'" do
  368. subject(:attr) { described_class.new(data_type: type, data_option: {}) }
  369. it 'does throw an exception' do
  370. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{must have boolean value for :past})
  371. end
  372. end
  373. end
  374. include_examples 'tests the exception on missing past', 'datetime'
  375. end
  376. end
  377. describe 'undefined method `to_hash` on editing select fields in the admin interface after migration to 5.1 #4027', db_strategy: :reset do
  378. let(:select_field) { create(:object_manager_attribute_select) }
  379. before do
  380. described_class.migration_execute
  381. end
  382. it 'does save the attribute with sorted options' do
  383. add = select_field.attributes.deep_symbolize_keys
  384. add[:data_option_new] = add[:data_option]
  385. add[:data_option_new][:options] = [
  386. {
  387. name: 'a',
  388. value: 'a',
  389. },
  390. {
  391. name: 'b',
  392. value: 'b',
  393. },
  394. {
  395. name: 'c',
  396. value: 'c',
  397. },
  398. ]
  399. described_class.add(add)
  400. described_class.migration_execute
  401. expect_result = {
  402. 'key_1' => 'value_1',
  403. 'key_2' => 'value_2',
  404. 'key_3' => 'value_3',
  405. 'a' => 'a',
  406. 'b' => 'b',
  407. 'c' => 'c'
  408. }
  409. expect(select_field.reload.data_option[:historical_options]).to eq(expect_result)
  410. end
  411. end
  412. describe '#add' do
  413. context 'when data is valid' do
  414. let(:attribute) do
  415. {
  416. object: 'Ticket',
  417. name: 'test1',
  418. display: 'Test 1',
  419. data_type: 'input',
  420. data_option: {
  421. maxlength: 200,
  422. type: 'text',
  423. null: false,
  424. },
  425. active: true,
  426. screens: {},
  427. position: 20,
  428. created_by_id: 1,
  429. updated_by_id: 1,
  430. editable: false,
  431. to_migrate: false,
  432. }
  433. end
  434. it 'is successful' do
  435. expect { described_class.add(attribute) }.to change(described_class, :count)
  436. expect(described_class.get(object: 'Ticket', name: 'test1')).to have_attributes(attribute)
  437. end
  438. end
  439. context 'when data is invalid' do
  440. let(:attribute) do
  441. {
  442. object: 'Ticket',
  443. name: 'test2_id',
  444. display: 'Test 2 with id',
  445. data_type: 'input',
  446. data_option: {
  447. maxlength: 200,
  448. type: 'text',
  449. null: false,
  450. },
  451. active: true,
  452. screens: {},
  453. position: 20,
  454. created_by_id: 1,
  455. updated_by_id: 1,
  456. }
  457. end
  458. it 'raises an error' do
  459. expect { described_class.add(attribute) }.to raise_error(ActiveRecord::RecordInvalid)
  460. end
  461. end
  462. context 'when adding a json field' do
  463. let(:expected_attributes) do
  464. {
  465. data_type: 'autocompletion_ajax_external_data_source',
  466. active: true,
  467. }
  468. end
  469. let(:attribute) { create(:object_manager_attribute_autocompletion_ajax_external_data_source) }
  470. it 'works on postgresql', db_adapter: :postgresql do
  471. expect { attribute }.to change(described_class, :count)
  472. expect(attribute).to have_attributes(expected_attributes)
  473. end
  474. it 'fails on mysql', db_adapter: :mysql do
  475. expect { attribute }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Data type can only be created on postgresql databases')
  476. end
  477. end
  478. end
  479. describe '#get' do
  480. context 'when attribute exists' do
  481. before do
  482. create(:object_manager_attribute_text, name: 'test3')
  483. end
  484. it 'returns the attribute' do
  485. expect(described_class.get(object: 'Ticket', name: 'test3')).to have_attributes(name: 'test3', editable: true)
  486. end
  487. end
  488. context 'when attribute does not exist' do
  489. it 'returns nil' do
  490. expect(described_class.get(object: 'Ticket', name: 'test4')).to be_nil
  491. end
  492. end
  493. end
  494. describe '#remove' do
  495. context 'when attribute exists' do
  496. before do
  497. create(:object_manager_attribute_text, name: 'test3')
  498. end
  499. it 'is successful' do
  500. expect { described_class.remove(object: 'Ticket', name: 'test3') }.to change(described_class, :count)
  501. expect(described_class.get(object: 'Ticket', name: 'test3')).to be_nil
  502. end
  503. end
  504. context 'when attribute does not exist' do
  505. it 'raises an error' do
  506. expect { described_class.remove(object: 'Ticket', name: 'test4') }.to raise_error(RuntimeError)
  507. end
  508. end
  509. end
  510. end