attribute_spec.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. require 'rails_helper'
  2. RSpec.describe ObjectManager::Attribute, type: :model do
  3. describe 'callbacks' do
  4. context 'for setting default values on local data options' do
  5. let(:subject) { described_class.new }
  6. context ':null' do
  7. it 'sets nil values to true' do
  8. expect { subject.validate }
  9. .to change { subject.data_option[:null] }.to(true)
  10. end
  11. it 'does not overwrite false values' do
  12. subject.data_option[:null] = false
  13. expect { subject.validate }
  14. .not_to change { subject.data_option[:null] }
  15. end
  16. end
  17. context ':maxlength' do
  18. context 'for data_type: select / tree_select / checkbox' do
  19. let(:subject) { described_class.new(data_type: 'select') }
  20. it 'sets nil values to 255' do
  21. expect { subject.validate }
  22. .to change { subject.data_option[:maxlength] }.to(255)
  23. end
  24. end
  25. end
  26. context ':nulloption' do
  27. context 'for data_type: select / tree_select / checkbox' do
  28. let(:subject) { described_class.new(data_type: 'select') }
  29. it 'sets nil values to true' do
  30. expect { subject.validate }
  31. .to change { subject.data_option[:nulloption] }.to(true)
  32. end
  33. it 'does not overwrite false values' do
  34. subject.data_option[:nulloption] = false
  35. expect { subject.validate }
  36. .not_to change { subject.data_option[:nulloption] }
  37. end
  38. end
  39. end
  40. end
  41. end
  42. describe 'check name' do
  43. it 'rejects ActiveRecord reserved word "attribute"' do
  44. expect do
  45. described_class.add attributes_for :object_manager_attribute_text, name: 'attribute'
  46. end.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Name attribute is a reserved word! (2)')
  47. end
  48. %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|
  49. it "rejects Zammad reserved word '#{reserved_word}'" do
  50. expect do
  51. described_class.add attributes_for :object_manager_attribute_text, name: reserved_word
  52. end.to raise_error(ActiveRecord::RecordInvalid, %r{is a reserved word! \(1\)})
  53. end
  54. end
  55. %w[someting_id something_ids].each do |reserved_word|
  56. it "rejects word '#{reserved_word}' which is used for database references" do
  57. expect do
  58. described_class.add attributes_for :object_manager_attribute_text, name: reserved_word
  59. end.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Name can't get used because *_id and *_ids are not allowed")
  60. end
  61. end
  62. it 'rejects duplicate attribute name of conflicting types' do
  63. attribute = attributes_for :object_manager_attribute_text
  64. described_class.add attribute
  65. attribute[:data_type] = 'boolean'
  66. expect do
  67. described_class.add attribute
  68. end.to raise_error ActiveRecord::RecordInvalid
  69. end
  70. it 'accepts duplicate attribute name on the same types (editing an existing attribute)' do
  71. attribute = attributes_for :object_manager_attribute_text
  72. described_class.add attribute
  73. expect do
  74. described_class.add attribute
  75. end.not_to raise_error
  76. end
  77. it 'accepts duplicate attribute name on compatible types (editing the type of an existing attribute)' do
  78. attribute = attributes_for :object_manager_attribute_text
  79. described_class.add attribute
  80. attribute[:data_type] = 'select'
  81. attribute[:data_option_new] = { default: '', options: { 'a' => 'a' } }
  82. expect do
  83. described_class.add attribute
  84. end.not_to raise_error
  85. end
  86. it 'accepts valid attribute names' do
  87. expect do
  88. described_class.add attributes_for :object_manager_attribute_text
  89. end.not_to raise_error
  90. end
  91. end
  92. describe 'validate that referenced attributes are not set as inactive' do
  93. subject(:attr) { create(:object_manager_attribute_text) }
  94. before do
  95. allow(described_class)
  96. .to receive(:attribute_used_by_references?)
  97. .with(attr.object_lookup.name, attr.name)
  98. .and_return(is_referenced)
  99. attr.active = active
  100. end
  101. context 'when is used and changing to inactive' do
  102. let(:active) { false }
  103. let(:is_referenced) { true }
  104. it { is_expected.not_to be_valid }
  105. it do
  106. attr.valid?
  107. expect(attr.errors).not_to be_blank
  108. end
  109. end
  110. context 'when is not used and changing to inactive' do
  111. let(:active) { false }
  112. let(:is_referenced) { false }
  113. it { is_expected.to be_valid }
  114. end
  115. context 'when is used and staying active and chan' do
  116. let(:active) { true }
  117. let(:is_referenced) { true }
  118. it { is_expected.to be_valid }
  119. end
  120. end
  121. end