required_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. before do
  13. allow(subject).to receive(:value).and_return(value)
  14. end
  15. context 'when validation should be performed' do
  16. let(:value) { nil }
  17. shared_examples 'a permission based validator' do |permission:|
  18. let(:performing_user) { create(:agent_user) }
  19. before { UserInfo.current_user_id = performing_user.id }
  20. context "for applying permission (#{permission})" do
  21. let(:permission) { permission }
  22. before do
  23. attribute.screens = {
  24. action => {
  25. permission => {
  26. required: true
  27. }
  28. }
  29. }
  30. end
  31. context 'when action is edit' do
  32. let(:action) { 'edit' }
  33. let(:record) { create(:user) }
  34. it_behaves_like 'a validation with errors'
  35. end
  36. context 'when action is create_...' do
  37. let(:action) { 'create_middle' }
  38. it_behaves_like 'a validation with errors'
  39. end
  40. end
  41. end
  42. it_behaves_like 'a permission based validator', permission: 'ticket.agent'
  43. it_behaves_like 'a permission based validator', permission: '-all-'
  44. end
  45. context 'when validation should not be performed' do
  46. context 'for present value' do
  47. let(:value) { 'some_value' }
  48. it_behaves_like 'a validation without errors'
  49. end
  50. context 'when value is actually blank' do
  51. let(:value) { nil }
  52. context "when action wasn't performed by a user" do
  53. context 'for blank UserInfo.current_user_id', current_user_id: nil do
  54. it_behaves_like 'a validation without errors'
  55. end
  56. context 'for system UserInfo.current_user_id', current_user_id: 1 do
  57. it_behaves_like 'a validation without errors'
  58. end
  59. end
  60. context 'for required => false' do
  61. let(:performing_user) { create(:agent_user) }
  62. before { UserInfo.current_user_id = performing_user.id }
  63. context 'for applying permission' do
  64. let(:permission) { 'ticket.agent' }
  65. before do
  66. attribute.screens = {
  67. action => {
  68. permission => {
  69. required: false
  70. }
  71. }
  72. }
  73. end
  74. context 'when action is edit' do
  75. let(:action) { 'edit' }
  76. let(:record) { create(:user) }
  77. it_behaves_like 'a validation without errors'
  78. end
  79. context 'when action is create_...' do
  80. let(:action) { 'create_middle' }
  81. it_behaves_like 'a validation without errors'
  82. end
  83. end
  84. end
  85. context 'for not applying permission' do
  86. let(:permission) { 'ticket.customer' }
  87. before do
  88. attribute.screens = {
  89. action => {
  90. permission => {
  91. required: true
  92. }
  93. }
  94. }
  95. end
  96. context 'when action is edit' do
  97. let(:action) { 'edit' }
  98. let(:record) { create(:user) }
  99. it_behaves_like 'a validation without errors'
  100. end
  101. context 'when action is create_...' do
  102. let(:action) { 'create_middle' }
  103. it_behaves_like 'a validation without errors'
  104. end
  105. end
  106. end
  107. end
  108. end