attribute_spec.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. require 'rails_helper'
  2. RSpec.describe ObjectManager::Attribute, type: :model do
  3. let(:user_attribute_permissions) do
  4. create(:user, roles: [role_attribute_permissions])
  5. end
  6. let(:role_attribute_permissions) do
  7. create(:role).tap do |role|
  8. role.permission_grant('admin.organization')
  9. role.permission_grant('ticket.agent')
  10. end
  11. end
  12. describe 'callbacks' do
  13. context 'for setting default values on local data options' do
  14. let(:subject) { described_class.new }
  15. context ':null' do
  16. it 'sets nil values to true' do
  17. expect { subject.validate }
  18. .to change { subject.data_option[:null] }.to(true)
  19. end
  20. it 'does not overwrite false values' do
  21. subject.data_option[:null] = false
  22. expect { subject.validate }
  23. .not_to change { subject.data_option[:null] }
  24. end
  25. end
  26. context ':maxlength' 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 255' do
  30. expect { subject.validate }
  31. .to change { subject.data_option[:maxlength] }.to(255)
  32. end
  33. end
  34. end
  35. context ':nulloption' do
  36. context 'for data_type: select / tree_select / checkbox' do
  37. let(:subject) { described_class.new(data_type: 'select') }
  38. it 'sets nil values to true' do
  39. expect { subject.validate }
  40. .to change { subject.data_option[:nulloption] }.to(true)
  41. end
  42. it 'does not overwrite false values' do
  43. subject.data_option[:nulloption] = false
  44. expect { subject.validate }
  45. .not_to change { subject.data_option[:nulloption] }
  46. end
  47. end
  48. end
  49. end
  50. end
  51. describe 'check name' do
  52. it 'rejects ActiveRecord reserved word "attribute"' do
  53. expect do
  54. described_class.add attributes_for :object_manager_attribute_text, name: 'attribute'
  55. end.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Name attribute is a reserved word! (2)')
  56. end
  57. %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|
  58. it "rejects Zammad reserved word '#{reserved_word}'" do
  59. expect do
  60. described_class.add attributes_for :object_manager_attribute_text, name: reserved_word
  61. end.to raise_error(ActiveRecord::RecordInvalid, /is a reserved word! \(1\)/)
  62. end
  63. end
  64. %w[someting_id something_ids].each do |reserved_word|
  65. it "rejects word '#{reserved_word}' which is used for database references" do
  66. expect do
  67. described_class.add attributes_for :object_manager_attribute_text, name: reserved_word
  68. end.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Name can't get used because *_id and *_ids are not allowed")
  69. end
  70. end
  71. it 'rejects duplicate attribute name of conflicting types' do
  72. attribute = attributes_for :object_manager_attribute_text
  73. described_class.add attribute
  74. attribute[:data_type] = 'boolean'
  75. expect do
  76. described_class.add attribute
  77. end.to raise_error ActiveRecord::RecordInvalid
  78. end
  79. it 'accepts duplicate attribute name on the same types (editing an existing attribute)' do
  80. attribute = attributes_for :object_manager_attribute_text
  81. described_class.add attribute
  82. expect do
  83. described_class.add attribute
  84. end.not_to raise_error
  85. end
  86. it 'accepts duplicate attribute name on compatible types (editing the type of an existing attribute)' do
  87. attribute = attributes_for :object_manager_attribute_text
  88. described_class.add attribute
  89. attribute[:data_type] = 'select'
  90. attribute[:data_option_new] = { default: '', options: { 'a' => 'a' } }
  91. expect do
  92. described_class.add attribute
  93. end.not_to raise_error
  94. end
  95. it 'accepts valid attribute names' do
  96. expect do
  97. described_class.add attributes_for :object_manager_attribute_text
  98. end.not_to raise_error
  99. end
  100. end
  101. describe 'attribute permissions', db_strategy: :reset do
  102. it 'merges attribute permissions' do
  103. create(:object_manager_attribute_text, screens: { create: { 'admin.organization': { shown: true }, 'ticket.agent': { shown: false } } }, name: 'test_permissions')
  104. migration = described_class.migration_execute
  105. expect(migration).to be true
  106. attribute = described_class.by_object('Ticket', user_attribute_permissions).detect { |attr| attr[:name] == 'test_permissions' }
  107. expect(attribute[:screen]['create']['shown']).to be true
  108. end
  109. it 'overwrites permissions if all get set' do
  110. create(:object_manager_attribute_text, screens: { create: { '-all-': { shown: true }, 'admin.organization': { shown: false }, 'ticket.agent': { shown: false } } }, name: 'test_permissions_all')
  111. migration = described_class.migration_execute
  112. expect(migration).to be true
  113. attribute = described_class.by_object('Ticket', user_attribute_permissions).detect { |attr| attr[:name] == 'test_permissions_all' }
  114. expect(attribute[:screen]['create']['shown']).to be true
  115. end
  116. it 'is able to handle other values than true or false' do
  117. create(:object_manager_attribute_text, screens: { create: { '-all-': { shown: true, item_class: 'column' }, 'admin.organization': { shown: false }, 'ticket.agent': { shown: false } } }, name: 'test_permissions_item')
  118. migration = described_class.migration_execute
  119. expect(migration).to be true
  120. attribute = described_class.by_object('Ticket', user_attribute_permissions).detect { |attr| attr[:name] == 'test_permissions_item' }
  121. expect(attribute[:screen]['create']['item_class']).to eq('column')
  122. end
  123. end
  124. end