diff options
author | Heinrich Lee Yu <hleeyu@gmail.com> | 2018-10-25 22:20:06 +0800 |
---|---|---|
committer | Heinrich Lee Yu <hleeyu@gmail.com> | 2018-10-26 10:32:14 +0800 |
commit | bf1ed85a9d6a932a99d0a5fdf70e72ea36c2600c (patch) | |
tree | 66fabf664fc6e3f29a19322dbf3b7c6c11f1dc06 /spec/lib/api | |
parent | d4e26636e72ef150a6f4446558c4d24d3bf21e69 (diff) | |
download | gitlab-ce-bf1ed85a9d6a932a99d0a5fdf70e72ea36c2600c.tar.gz |
Refactor api validator to separate class
Diffstat (limited to 'spec/lib/api')
-rw-r--r-- | spec/lib/api/helpers/custom_validators_spec.rb | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/spec/lib/api/helpers/custom_validators_spec.rb b/spec/lib/api/helpers/custom_validators_spec.rb new file mode 100644 index 00000000000..810d05c479c --- /dev/null +++ b/spec/lib/api/helpers/custom_validators_spec.rb @@ -0,0 +1,62 @@ +require 'spec_helper' + +describe API::Helpers::CustomValidators do + let(:scope) do + Struct.new(:opts) do + def full_name(attr_name) + attr_name + end + end + end + + describe API::Helpers::CustomValidators::Absence do + subject do + described_class.new(['test'], {}, false, scope.new) + end + + context 'empty param' do + it 'does not raise a validation error' do + expect_no_validation_error({}) + end + end + + context 'invalid parameters' do + it 'should raise a validation error' do + expect_validation_error({ 'test' => 'some_value' }) + end + end + end + + describe API::Helpers::CustomValidators::IntegerNoneAny do + subject do + described_class.new(['test'], {}, false, scope.new) + end + + context 'valid parameters' do + it 'does not raise a validation error' do + expect_no_validation_error({ 'test' => 2 }) + expect_no_validation_error({ 'test' => 100 }) + expect_no_validation_error({ 'test' => 'None' }) + expect_no_validation_error({ 'test' => 'Any' }) + end + end + + context 'invalid parameters' do + it 'should raise a validation error' do + expect_validation_error({ 'test' => 'some_other_string' }) + end + end + end + + def expect_no_validation_error(params) + expect { validate_test_param!(params) }.not_to raise_error + end + + def expect_validation_error(params) + expect { validate_test_param!(params) }.to raise_error(Grape::Exceptions::Validation) + end + + def validate_test_param!(params) + subject.validate_param!('test', params) + end +end |