attribute_spec.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://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. it 'rejects duplicate attribute name of conflicting types' do
  64. attribute = attributes_for :object_manager_attribute_text
  65. described_class.add attribute
  66. attribute[:data_type] = 'boolean'
  67. expect do
  68. described_class.add attribute
  69. end.to raise_error ActiveRecord::RecordInvalid
  70. end
  71. it 'accepts duplicate attribute name on the same types (editing an existing attribute)' do
  72. attribute = attributes_for :object_manager_attribute_text
  73. described_class.add attribute
  74. expect do
  75. described_class.add attribute
  76. end.not_to raise_error
  77. end
  78. it 'accepts duplicate attribute name on compatible types (editing the type of an existing attribute)' do
  79. attribute = attributes_for :object_manager_attribute_text
  80. described_class.add attribute
  81. attribute[:data_type] = 'select'
  82. attribute[:data_option_new] = { default: '', options: { 'a' => 'a' } }
  83. expect do
  84. described_class.add attribute
  85. end.not_to raise_error
  86. end
  87. it 'accepts valid attribute names' do
  88. expect do
  89. described_class.add attributes_for :object_manager_attribute_text
  90. end.not_to raise_error
  91. end
  92. end
  93. describe 'validate that referenced attributes are not set as inactive' do
  94. subject(:attr) { create(:object_manager_attribute_text) }
  95. before do
  96. allow(described_class)
  97. .to receive(:attribute_used_by_references?)
  98. .with(attr.object_lookup.name, attr.name)
  99. .and_return(is_referenced)
  100. attr.active = active
  101. end
  102. context 'when is used and changing to inactive' do
  103. let(:active) { false }
  104. let(:is_referenced) { true }
  105. it { is_expected.not_to be_valid }
  106. it do
  107. attr.valid?
  108. expect(attr.errors).not_to be_blank
  109. end
  110. end
  111. context 'when is not used and changing to inactive' do
  112. let(:active) { false }
  113. let(:is_referenced) { false }
  114. it { is_expected.to be_valid }
  115. end
  116. context 'when is used and staying active and chan' do
  117. let(:active) { true }
  118. let(:is_referenced) { true }
  119. it { is_expected.to be_valid }
  120. end
  121. end
  122. end