required_spec.rb 3.9 KB

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