attribute_spec.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 options validation' do
  243. it 'calls ObjectManager::Attribute::DataOptionValidator' do
  244. record = described_class.new
  245. expect_any_instance_of(described_class::DataOptionValidator).to receive(:validate).with(record)
  246. record.valid?
  247. end
  248. end
  249. describe 'undefined method `to_hash` on editing select fields in the admin interface after migration to 5.1 #4027', db_strategy: :reset do
  250. let(:select_field) { create(:object_manager_attribute_select) }
  251. before do
  252. described_class.migration_execute
  253. end
  254. it 'does save the attribute with sorted options' do
  255. add = select_field.attributes.deep_symbolize_keys
  256. add[:data_option_new] = add[:data_option]
  257. add[:data_option_new][:options] = [
  258. {
  259. name: 'a',
  260. value: 'a',
  261. },
  262. {
  263. name: 'b',
  264. value: 'b',
  265. },
  266. {
  267. name: 'c',
  268. value: 'c',
  269. },
  270. ]
  271. described_class.add(add)
  272. described_class.migration_execute
  273. expect_result = {
  274. 'key_1' => 'value_1',
  275. 'key_2' => 'value_2',
  276. 'key_3' => 'value_3',
  277. 'a' => 'a',
  278. 'b' => 'b',
  279. 'c' => 'c'
  280. }
  281. expect(select_field.reload.data_option[:historical_options]).to eq(expect_result)
  282. end
  283. end
  284. describe '#add' do
  285. context 'when data is valid' do
  286. let(:attribute) do
  287. {
  288. object: 'Ticket',
  289. name: 'test1',
  290. display: 'Test 1',
  291. data_type: 'input',
  292. data_option: {
  293. maxlength: 200,
  294. type: 'text',
  295. null: false,
  296. },
  297. active: true,
  298. screens: {},
  299. position: 20,
  300. created_by_id: 1,
  301. updated_by_id: 1,
  302. editable: false,
  303. to_migrate: false,
  304. }
  305. end
  306. it 'is successful' do
  307. expect { described_class.add(attribute) }.to change(described_class, :count)
  308. expect(described_class.get(object: 'Ticket', name: 'test1')).to have_attributes(attribute)
  309. end
  310. end
  311. context 'when data is invalid' do
  312. let(:attribute) do
  313. {
  314. object: 'Ticket',
  315. name: 'test2_id',
  316. display: 'Test 2 with id',
  317. data_type: 'input',
  318. data_option: {
  319. maxlength: 200,
  320. type: 'text',
  321. null: false,
  322. },
  323. active: true,
  324. screens: {},
  325. position: 20,
  326. created_by_id: 1,
  327. updated_by_id: 1,
  328. }
  329. end
  330. it 'raises an error' do
  331. expect { described_class.add(attribute) }.to raise_error(ActiveRecord::RecordInvalid)
  332. end
  333. end
  334. context 'when adding a json field' do
  335. let(:expected_attributes) do
  336. {
  337. data_type: 'autocompletion_ajax_external_data_source',
  338. active: true,
  339. }
  340. end
  341. let(:attribute) { create(:object_manager_attribute_autocompletion_ajax_external_data_source) }
  342. it 'works on postgresql', db_adapter: :postgresql do
  343. expect { attribute }.to change(described_class, :count)
  344. expect(attribute).to have_attributes(expected_attributes)
  345. end
  346. it 'fails on mysql', db_adapter: :mysql do
  347. expect { attribute }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Data type can only be created on postgresql databases')
  348. end
  349. end
  350. end
  351. describe '#get' do
  352. context 'when attribute exists' do
  353. before do
  354. create(:object_manager_attribute_text, name: 'test3')
  355. end
  356. it 'returns the attribute' do
  357. expect(described_class.get(object: 'Ticket', name: 'test3')).to have_attributes(name: 'test3', editable: true)
  358. end
  359. end
  360. context 'when attribute does not exist' do
  361. it 'returns nil' do
  362. expect(described_class.get(object: 'Ticket', name: 'test4')).to be_nil
  363. end
  364. end
  365. end
  366. describe '#remove' do
  367. context 'when attribute exists' do
  368. before do
  369. create(:object_manager_attribute_text, name: 'test3')
  370. end
  371. it 'is successful' do
  372. expect { described_class.remove(object: 'Ticket', name: 'test3') }.to change(described_class, :count)
  373. expect(described_class.get(object: 'Ticket', name: 'test3')).to be_nil
  374. end
  375. end
  376. context 'when attribute does not exist' do
  377. it 'raises an error' do
  378. expect { described_class.remove(object: 'Ticket', name: 'test4') }.to raise_error(RuntimeError)
  379. end
  380. end
  381. end
  382. end