attribute_spec.rb 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. # Copyright (C) 2012-2022 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 { subject.validate }
  37. .not_to change { subject.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! (2)')
  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].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! \(1\)})
  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 get 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 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 '.attribute_to_references_hash_objects' do
  138. it 'returns classes with conditions' do
  139. expect(described_class.attribute_to_references_hash_objects).to match_array [Trigger, Overview, Job, Sla, Report::Profile ]
  140. end
  141. end
  142. describe '.data_options_hash' do
  143. context 'when hash' do
  144. let(:check) do
  145. {
  146. 'a' => 'A',
  147. 'b' => 'B',
  148. 'c' => 'c',
  149. }
  150. end
  151. it 'does return the options as hash' do
  152. expect(described_class.data_options_hash(check)).to eq({
  153. 'a' => 'A',
  154. 'b' => 'B',
  155. 'c' => 'c',
  156. })
  157. end
  158. end
  159. context 'when array' do
  160. let(:check) do
  161. [
  162. {
  163. value: 'a',
  164. name: 'A',
  165. },
  166. {
  167. value: 'b',
  168. name: 'B',
  169. },
  170. {
  171. value: 'c',
  172. name: 'c',
  173. },
  174. ]
  175. end
  176. it 'does return the options as hash' do
  177. expect(described_class.data_options_hash(check)).to eq({
  178. 'a' => 'A',
  179. 'b' => 'B',
  180. 'c' => 'c',
  181. })
  182. end
  183. end
  184. context 'when tree array' do
  185. let(:check) do
  186. [
  187. {
  188. value: 'a',
  189. name: 'A',
  190. },
  191. {
  192. value: 'b',
  193. name: 'B',
  194. },
  195. {
  196. value: 'c',
  197. name: 'c',
  198. children: [
  199. {
  200. value: 'c::a',
  201. name: 'c sub a',
  202. },
  203. {
  204. value: 'c::b',
  205. name: 'c sub b',
  206. },
  207. {
  208. value: 'c::c',
  209. name: 'c sub c',
  210. },
  211. ],
  212. },
  213. ]
  214. end
  215. it 'does return the options as hash' do
  216. expect(described_class.data_options_hash(check)).to eq({
  217. 'a' => 'A',
  218. 'b' => 'B',
  219. 'c' => 'c',
  220. 'c::a' => 'c sub a',
  221. 'c::b' => 'c sub b',
  222. 'c::c' => 'c sub c',
  223. })
  224. end
  225. end
  226. end
  227. end
  228. describe '#data_option_validations' do
  229. context 'when maxlength is checked for non-integers' do
  230. shared_examples 'tests the exception on invalid maxlength values' do |type|
  231. context "when type '#{type}'" do
  232. subject(:attr) { described_class.new(data_type: type, data_option: { maxlength: 'brbrbr' }) }
  233. it 'does throw an exception' do
  234. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{Data option must have integer for :maxlength})
  235. end
  236. end
  237. end
  238. include_examples 'tests the exception on invalid maxlength values', 'input'
  239. include_examples 'tests the exception on invalid maxlength values', 'textarea'
  240. include_examples 'tests the exception on invalid maxlength values', 'richtext'
  241. end
  242. context 'when type is checked' do
  243. shared_examples 'tests the exception on invalid types' do |type|
  244. context "when type '#{type}'" do
  245. subject(:attr) { described_class.new(data_type: type, data_option: { type: 'brbrbr' }) }
  246. it 'does throw an exception' do
  247. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{must have one of text/password/tel/fax/email/url for :type})
  248. end
  249. end
  250. end
  251. include_examples 'tests the exception on invalid types', 'input'
  252. end
  253. context 'when min max values are checked' do
  254. shared_examples 'tests the exception on invalid min max values' do |type|
  255. context "when type '#{type}'" do
  256. context 'when no integer for min' do
  257. subject(:attr) { described_class.new(data_type: type, data_option: { min: 'brbrbr' }) }
  258. it 'does throw an exception' do
  259. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{must have integer for :min})
  260. end
  261. end
  262. context 'when no integer for max' do
  263. subject(:attr) { described_class.new(data_type: type, data_option: { max: 'brbrbr' }) }
  264. it 'does throw an exception' do
  265. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{must have integer for :max})
  266. end
  267. end
  268. context 'when high integer for min' do
  269. subject(:attr) { described_class.new(data_type: type, data_option: { min: 999_999_999_999 }) }
  270. it 'does throw an exception' do
  271. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{min must be lower than 2147483648})
  272. end
  273. end
  274. context 'when high integer for max' do
  275. subject(:attr) { described_class.new(data_type: type, data_option: { max: 999_999_999_999 }) }
  276. it 'does throw an exception' do
  277. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{max must be lower than 2147483648})
  278. end
  279. end
  280. context 'when negative high integer for min' do
  281. subject(:attr) { described_class.new(data_type: type, data_option: { min: -999_999_999_999 }) }
  282. it 'does throw an exception' do
  283. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{min must be higher than -2147483648})
  284. end
  285. end
  286. context 'when negative high integer for max' do
  287. subject(:attr) { described_class.new(data_type: type, data_option: { max: -999_999_999_999 }) }
  288. it 'does throw an exception' do
  289. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{max must be higher than -2147483648})
  290. end
  291. end
  292. context 'when min is greater than max' do
  293. subject(:attr) { described_class.new(data_type: type, data_option: { min: 5, max: 2 }) }
  294. it 'does throw an exception' do
  295. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{min must be lower than max})
  296. end
  297. end
  298. end
  299. end
  300. include_examples 'tests the exception on invalid min max values', 'integer'
  301. end
  302. context 'when default is checked' do
  303. shared_examples 'tests the exception on missing default' do |type|
  304. context "when type '#{type}'" do
  305. subject(:attr) { described_class.new(data_type: type, data_option: {}) }
  306. it 'does throw an exception' do
  307. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{must have value for :default})
  308. end
  309. end
  310. end
  311. include_examples 'tests the exception on missing default', 'select'
  312. include_examples 'tests the exception on missing default', 'tree_select'
  313. include_examples 'tests the exception on missing default', 'checkbox'
  314. include_examples 'tests the exception on missing default', 'boolean'
  315. end
  316. context 'when relation is checked' do
  317. shared_examples 'tests the exception on missing relation' 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 non-nil value for either :options or :relation})
  322. end
  323. end
  324. end
  325. include_examples 'tests the exception on missing relation', 'select'
  326. include_examples 'tests the exception on missing relation', 'tree_select'
  327. include_examples 'tests the exception on missing relation', 'checkbox'
  328. end
  329. context 'when nil options are checked' do
  330. shared_examples 'tests the exception on missing nil options' do |type|
  331. context "when type '#{type}'" do
  332. subject(:attr) { described_class.new(data_type: type, data_option: {}) }
  333. it 'does throw an exception' do
  334. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{must have non-nil value for :options})
  335. end
  336. end
  337. end
  338. include_examples 'tests the exception on missing nil options', 'boolean'
  339. end
  340. context 'when future is checked' do
  341. shared_examples 'tests the exception on missing future' do |type|
  342. context "when type '#{type}'" do
  343. subject(:attr) { described_class.new(data_type: type, data_option: {}) }
  344. it 'does throw an exception' do
  345. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{must have boolean value for :future})
  346. end
  347. end
  348. end
  349. include_examples 'tests the exception on missing future', 'datetime'
  350. end
  351. context 'when past is checked' do
  352. shared_examples 'tests the exception on missing past' do |type|
  353. context "when type '#{type}'" do
  354. subject(:attr) { described_class.new(data_type: type, data_option: {}) }
  355. it 'does throw an exception' do
  356. expect { attr.save! }.to raise_error(ActiveRecord::RecordInvalid, %r{must have boolean value for :past})
  357. end
  358. end
  359. end
  360. include_examples 'tests the exception on missing past', 'datetime'
  361. end
  362. end
  363. describe 'undefined method `to_hash` on editing select fields in the admin interface after migration to 5.1 #4027', db_strategy: :reset do
  364. let(:select_field) { create(:object_manager_attribute_select) }
  365. before do
  366. described_class.migration_execute
  367. end
  368. it 'does save the attribute with sorted options' do
  369. add = select_field.attributes.deep_symbolize_keys
  370. add[:data_option_new] = add[:data_option]
  371. add[:data_option_new][:options] = [
  372. {
  373. name: 'a',
  374. value: 'a',
  375. },
  376. {
  377. name: 'b',
  378. value: 'b',
  379. },
  380. {
  381. name: 'c',
  382. value: 'c',
  383. },
  384. ]
  385. described_class.add(add)
  386. described_class.migration_execute
  387. expect_result = {
  388. 'key_1' => 'value_1',
  389. 'key_2' => 'value_2',
  390. 'key_3' => 'value_3',
  391. 'a' => 'a',
  392. 'b' => 'b',
  393. 'c' => 'c'
  394. }
  395. expect(select_field.reload.data_option[:historical_options]).to eq(expect_result)
  396. end
  397. end
  398. end