required_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. require 'rails_helper'
  2. require 'models/object_manager/attribute/validation/backend_examples'
  3. RSpec.describe ::ObjectManager::Attribute::Validation::Required do
  4. subject do
  5. described_class.new(
  6. record: record,
  7. attribute: attribute
  8. )
  9. end
  10. let(:record) { build(:user) }
  11. let(:attribute) { build(:object_manager_attribute_date) }
  12. it_behaves_like 'validate backend'
  13. context 'when validation should be performed' do
  14. let(:value) { nil }
  15. shared_examples 'a permission based validator' do |permission:|
  16. let(:performing_user) { create(:agent) }
  17. before { UserInfo.current_user_id = performing_user.id }
  18. context "for applying permission (#{permission})" do
  19. let(:permission) { permission }
  20. before do
  21. attribute.screens = {
  22. action => {
  23. permission => {
  24. required: true
  25. }
  26. }
  27. }
  28. end
  29. context 'when boolean field with false values' do
  30. let(:value) { false }
  31. let(:attribute) { build(:object_manager_attribute_boolean) }
  32. let(:action) { 'create_middle' }
  33. it_behaves_like 'a validation without errors'
  34. end
  35. context 'when action is edit' do
  36. let(:action) { 'edit' }
  37. let(:record) { create(:user) }
  38. it_behaves_like 'a validation with errors'
  39. end
  40. context 'when action is create_...' do
  41. let(:action) { 'create_middle' }
  42. it_behaves_like 'a validation with errors'
  43. end
  44. end
  45. end
  46. it_behaves_like 'a permission based validator', permission: 'ticket.agent'
  47. it_behaves_like 'a permission based validator', permission: '-all-'
  48. end
  49. context 'when validation should not be performed' do
  50. context 'for present value' do
  51. let(:value) { 'some_value' }
  52. it_behaves_like 'a validation without errors'
  53. end
  54. context 'when value is actually blank' do
  55. let(:value) { nil }
  56. context "when action wasn't performed by a user" do
  57. context 'for blank UserInfo.current_user_id', current_user_id: nil do
  58. it_behaves_like 'a validation without errors'
  59. end
  60. context 'for system UserInfo.current_user_id', current_user_id: 1 do
  61. it_behaves_like 'a validation without errors'
  62. end
  63. end
  64. context 'for required => false' do
  65. let(:performing_user) { create(:agent) }
  66. before { UserInfo.current_user_id = performing_user.id }
  67. context 'for applying permission' do
  68. let(:permission) { 'ticket.agent' }
  69. before do
  70. attribute.screens = {
  71. action => {
  72. permission => {
  73. required: false
  74. }
  75. }
  76. }
  77. end
  78. context 'when action is edit' do
  79. let(:action) { 'edit' }
  80. let(:record) { create(:user) }
  81. it_behaves_like 'a validation without errors'
  82. end
  83. context 'when action is create_...' do
  84. let(:action) { 'create_middle' }
  85. it_behaves_like 'a validation without errors'
  86. end
  87. end
  88. end
  89. context 'for not applying permission' do
  90. let(:permission) { 'ticket.customer' }
  91. before do
  92. attribute.screens = {
  93. action => {
  94. permission => {
  95. required: true
  96. }
  97. }
  98. }
  99. end
  100. context 'when action is edit' do
  101. let(:action) { 'edit' }
  102. let(:record) { create(:user) }
  103. it_behaves_like 'a validation without errors'
  104. end
  105. context 'when action is create_...' do
  106. let(:action) { 'create_middle' }
  107. it_behaves_like 'a validation without errors'
  108. end
  109. end
  110. end
  111. end
  112. end