data_option_validator_spec.rb 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe ObjectManager::Attribute::DataOptionValidator, type: :model do
  4. let(:record) { ObjectManager::Attribute.new(data_type: type, data_option: data_option) }
  5. let(:instance) { described_class.new }
  6. let(:type) { 'ibnput' }
  7. let(:data_option) { {} }
  8. describe '#validate', aggregate_failures: true do
  9. context 'when type is input' do
  10. let(:type) { 'input' }
  11. it 'runs expected validations' do
  12. allow(instance).to receive_messages(%i[type_check maxlength_check])
  13. instance.validate(record)
  14. expect(instance).to have_received(:type_check).with(record)
  15. expect(instance).to have_received(:maxlength_check).with(record)
  16. end
  17. end
  18. context 'when type is integer' do
  19. let(:type) { 'integer' }
  20. it 'runs expected validations' do
  21. allow(instance).to receive_messages([:min_max_check])
  22. instance.validate(record)
  23. expect(instance).to have_received(:min_max_check).with(record)
  24. end
  25. end
  26. context 'when type is boolean' do
  27. let(:type) { 'boolean' }
  28. it 'runs expected validations' do
  29. allow(instance).to receive_messages(%i[default_check presence_check])
  30. instance.validate(record)
  31. expect(instance).to have_received(:default_check).with(record)
  32. expect(instance).to have_received(:presence_check).with(record)
  33. end
  34. end
  35. context 'when type is datetime' do
  36. let(:type) { 'datetime' }
  37. it 'runs expected validations' do
  38. allow(instance).to receive_messages(%i[future_check past_check])
  39. instance.validate(record)
  40. expect(instance).to have_received(:future_check).with(record)
  41. expect(instance).to have_received(:past_check).with(record)
  42. end
  43. end
  44. %w[textarea richtext].each do |type|
  45. context "when type is #{type}" do
  46. let(:type) { type }
  47. it 'runs expected validations' do
  48. allow(instance).to receive_messages([:maxlength_check])
  49. instance.validate(record)
  50. expect(instance).to have_received(:maxlength_check).with(record)
  51. end
  52. end
  53. end
  54. %w[tree_select multi_tree_select multiselect select checkbox].each do |type|
  55. context "when type is #{type}" do
  56. let(:type) { type }
  57. it 'runs expected validations' do
  58. allow(instance).to receive_messages(%i[default_check relation_check])
  59. instance.validate(record)
  60. expect(instance).to have_received(:default_check).with(record)
  61. expect(instance).to have_received(:relation_check).with(record)
  62. end
  63. end
  64. end
  65. end
  66. describe '#maxlength_check' do
  67. before { instance.send(:maxlength_check, record) }
  68. context 'when maxlength is integer' do
  69. let(:data_option) { { maxlength: 123 } }
  70. it { expect(record.errors).to be_blank }
  71. end
  72. context 'when maxlength is string' do
  73. let(:data_option) { { maxlength: 'brbrbr' } }
  74. it { expect(record.errors.full_messages).to match_array('Max length must be an integer.') }
  75. end
  76. context 'when maxlength is missing' do
  77. it { expect(record.errors.full_messages).to match_array('Max length must be an integer.') }
  78. end
  79. end
  80. describe '#type_check' do
  81. before { instance.send(:type_check, record) }
  82. context 'when type is in the list' do
  83. let(:data_option) { { type: 'text' } }
  84. it { expect(record.errors).to be_blank }
  85. end
  86. context 'when type is not in the list' do
  87. let(:data_option) { { type: 'brbrbr' } }
  88. it { expect(record.errors.full_messages).to match_array('Input field must be text, password, tel, fax, email or url type.') }
  89. end
  90. context 'when type is missing' do
  91. it { expect(record.errors.full_messages).to match_array('Input field must be text, password, tel, fax, email or url type.') }
  92. end
  93. end
  94. describe '#default_check' do
  95. before { instance.send(:default_check, record) }
  96. context 'when default is truthy' do
  97. let(:data_option) { { default: 'text' } }
  98. it { expect(record.errors).to be_blank }
  99. end
  100. context 'when default is falsy' do
  101. let(:data_option) { { default: nil } }
  102. it { expect(record.errors).to be_blank }
  103. end
  104. context 'when default is not present' do
  105. it { expect(record.errors.full_messages).to match_array('Default value is required.') }
  106. end
  107. end
  108. describe '#relation_check' do
  109. before { instance.send(:relation_check, record) }
  110. context 'when options is present' do
  111. let(:data_option) { { options: { value: true } } }
  112. it { expect(record.errors).to be_blank }
  113. end
  114. context 'when options is empty' do
  115. let(:data_option) { { options: [] } }
  116. it { expect(record.errors).to be_blank }
  117. end
  118. context 'when relation is present' do
  119. let(:data_option) { { relation: 'sample' } }
  120. it { expect(record.errors).to be_blank }
  121. end
  122. context 'when relation is empty' do
  123. let(:data_option) { { relation: [] } }
  124. it { expect(record.errors).to be_blank }
  125. end
  126. context 'when neither optios nor relation present' do
  127. it { expect(record.errors.full_messages).to match_array('Options or relation is required.') }
  128. end
  129. end
  130. describe '#presence_check' do
  131. before { instance.send(:presence_check, record) }
  132. context 'when options is present' do
  133. let(:data_option) { { options: { value: true } } }
  134. it { expect(record.errors).to be_blank }
  135. end
  136. context 'when options is empty' do
  137. let(:data_option) { { options: [] } }
  138. it { expect(record.errors).to be_blank }
  139. end
  140. context 'when neither optios nor relation present' do
  141. it { expect(record.errors.full_messages).to match_array('Options are required.') }
  142. end
  143. end
  144. describe '#future_check' do
  145. before { instance.send(:future_check, record) }
  146. context 'when future is truthy' do
  147. let(:data_option) { { future: true } }
  148. it { expect(record.errors).to be_blank }
  149. end
  150. context 'when future is falsy' do
  151. let(:data_option) { { future: false } }
  152. it { expect(record.errors).to be_blank }
  153. end
  154. context 'when future is not present' do
  155. it { expect(record.errors.full_messages).to match_array('Allow future dates toggle value is required.') }
  156. end
  157. end
  158. describe '#past_check' do
  159. before { instance.send(:past_check, record) }
  160. context 'when past is truthy' do
  161. let(:data_option) { { past: true } }
  162. it { expect(record.errors).to be_blank }
  163. end
  164. context 'when past is falsy' do
  165. let(:data_option) { { past: false } }
  166. it { expect(record.errors).to be_blank }
  167. end
  168. context 'when past is not present' do
  169. it { expect(record.errors.full_messages).to match_array('Allow past dates toggle value is required.') }
  170. end
  171. end
  172. describe '#min_max_check' do
  173. before { instance.send(:min_max_check, record) }
  174. context 'when min & max are valid' do
  175. let(:data_option) { { min: -9, max: 9 } }
  176. it { expect(record.errors).to be_blank }
  177. end
  178. context 'when min is missing' do
  179. let(:data_option) { { min: nil } }
  180. it { expect(record.errors.full_messages).to include('Minimal value must be an integer') }
  181. end
  182. context 'when min is a string' do
  183. let(:data_option) { { min: '1' } }
  184. it { expect(record.errors.full_messages).to include('Minimal value must be an integer') }
  185. end
  186. context 'when min is too low' do
  187. let(:data_option) { { min: -2_147_483_648 } }
  188. it { expect(record.errors.full_messages).to include('Minimal value must be higher than -2147483648') }
  189. end
  190. context 'when min is too high' do
  191. let(:data_option) { { min: 2_147_483_648 } }
  192. it { expect(record.errors.full_messages).to include('Minimal value must be lower than 2147483648') }
  193. end
  194. context 'when max is missing' do
  195. let(:data_option) { { max: nil } }
  196. it { expect(record.errors.full_messages).to include('Minimal value must be an integer') }
  197. end
  198. context 'when max is a string' do
  199. let(:data_option) { { max: '1' } }
  200. it { expect(record.errors.full_messages).to include('Maximal value must be an integer') }
  201. end
  202. context 'when max is too low' do
  203. let(:data_option) { { max: -2_147_483_648 } }
  204. it { expect(record.errors.full_messages).to include('Maximal value must be higher than -2147483648') }
  205. end
  206. context 'when max is too high' do
  207. let(:data_option) { { max: 2_147_483_648 } }
  208. it { expect(record.errors.full_messages).to include('Maximal value must be lower than 2147483648') }
  209. end
  210. context 'when max is equal to min' do
  211. let(:data_option) { { min: 9, max: 9 } }
  212. it { expect(record.errors).to be_blank }
  213. end
  214. context 'when max is lower than min' do
  215. let(:data_option) { { min: 9, max: -9 } }
  216. it { expect(record.errors.full_messages).to include('Maximal value must be higher than or equal to minimal value') }
  217. end
  218. end
  219. end