permit all symbols in image name

This commit is contained in:
Anton Chuchkalov 2016-01-12 12:01:07 +03:00
parent 3e9c24bbf2
commit 756d7b8d1c
5 changed files with 57 additions and 13 deletions

View File

@ -23,13 +23,13 @@ module Devops
set_field_validators :id, [::Validators::FieldValidator::NotNil, set_field_validators :id, [::Validators::FieldValidator::NotNil,
::Validators::FieldValidator::FieldType::String, ::Validators::FieldValidator::FieldType::String,
::Validators::FieldValidator::NotEmpty, ::Validators::FieldValidator::NotEmpty,
::Validators::FieldValidator::ImageName, ::Validators::FieldValidator::ImageId,
::Validators::Image::ImageInFilter] ::Validators::Image::ImageInFilter]
set_field_validators :remote_user, [::Validators::FieldValidator::NotNil, set_field_validators :remote_user, [::Validators::FieldValidator::NotNil,
::Validators::FieldValidator::FieldType::String, ::Validators::FieldValidator::FieldType::String,
::Validators::FieldValidator::NotEmpty, ::Validators::FieldValidator::NotEmpty,
::Validators::FieldValidator::ImageName] ::Validators::FieldValidator::ImageUsername]
set_field_validators :name, [::Validators::FieldValidator::NotNil, set_field_validators :name, [::Validators::FieldValidator::NotNil,
::Validators::FieldValidator::FieldType::String, ::Validators::FieldValidator::FieldType::String,

View File

@ -0,0 +1,18 @@
require_relative "base"
module Validators
module FieldValidator
class ImageId < Base
MAX_LEN = 100
NAME_REGEX = /\A[\w\-\.]{1,#{MAX_LEN}}\z/
def valid?
!NAME_REGEX.match(@value).nil?
end
def message
"Invalid value '#{@value}': it should contains symbols 'a-zA-Z0-9_-.' and length should be more then 1 and less or equals then #{MAX_LEN}"
end
end
end
end

View File

@ -3,15 +3,14 @@ module Validators
module FieldValidator module FieldValidator
class ImageName < Base class ImageName < Base
MAX_NAME_LEN = 100 MAX_LEN = 100
NAME_REGEX = /\A[\w\-\.]{1,#{MAX_NAME_LEN}}\z/
def valid? def valid?
!NAME_REGEX.match(@value).nil? @value.length <= MAX_LEN
end end
def message def message
"Invalid value '#{@value}': it should contains symbols 'a-zA-Z0-9_-.' and length should be more then 1 and less or equals then #{MAX_NAME_LEN}" "Invalid value '#{@value}': it should contains symbols 'a-zA-Z0-9_-.' and length should be more then 1 and less or equals then #{MAX_LEN}"
end end
end end
end end

View File

@ -0,0 +1,18 @@
require_relative "base"
module Validators
module FieldValidator
class ImageUsername < Base
MAX_NAME_LEN = 100
NAME_REGEX = /\A[\w\-\.]{1,#{MAX_NAME_LEN}}\z/
def valid?
!NAME_REGEX.match(@value).nil?
end
def message
"Invalid value '#{@value}': it should contains symbols 'a-zA-Z0-9_-.' and length should be more then 1 and less or equals then #{MAX_NAME_LEN}"
end
end
end
end

View File

@ -2,12 +2,13 @@ require 'db/mongo/models/image'
RSpec.describe Devops::Model::Image, type: :model do RSpec.describe Devops::Model::Image, type: :model do
let(:image) { build(:image) } let(:image) { build(:image) }
let(:name_with_dash) { 'asd-asd' } let(:string_with_dash) { 'asd-asd' }
let(:name_with_slash) { 'asd/asd' } let(:string_with_slash) { 'asd/asd' }
let(:string_with_parenthesis) { 'centos 6.5 x86_64 (development instance)' }
before do before do
allow(Provider::ProviderFactory).to receive(:providers).and_return(%w(openstack ec2 static')) allow(Provider::ProviderFactory).to receive(:providers).and_return(%w(openstack ec2 static'))
allow_any_instance_of(Validators::Image::ImageInFilter).to receive(:available_images).and_return([{'id' => 'test_image'}, {'id' => name_with_dash}, {'id' => name_with_slash}]) allow_any_instance_of(Validators::Image::ImageInFilter).to receive(:available_images).and_return([{'id' => 'test_image'}, {'id' => string_with_dash}, {'id' => string_with_slash}])
end end
it 'is valid with correct attrs' do it 'is valid with correct attrs' do
@ -21,16 +22,24 @@ RSpec.describe Devops::Model::Image, type: :model do
include_examples 'field type validation', :bootstrap_template, :maybe_nil, :non_empty_string, :only_word_symbols, :field_validator include_examples 'field type validation', :bootstrap_template, :maybe_nil, :non_empty_string, :only_word_symbols, :field_validator
it 'id should contain only letters, digits and dashes' do it 'id should contain only letters, digits and dashes' do
expect(build(:image, id: name_with_dash)).to be_valid expect(build(:image, id: string_with_dash)).to be_valid
expect(build(:image, id: name_with_slash)).not_to be_valid expect(build(:image, id: string_with_slash)).not_to be_valid
expect(build(:image, id: string_with_parenthesis)).not_to be_valid
end end
it "id should be included in image filters" do it "id should be included in image filters" do
expect(build(:image, id: 'wrong')).not_to be_valid expect(build(:image, id: 'wrong')).not_to be_valid
end end
it 'name should contain only letters, digits and dashes' do it 'name may contain everything' do
expect(build(:image, name: name_with_slash)).not_to be_valid expect(build(:image, name: string_with_dash)).to be_valid
expect(build(:image, name: string_with_slash)).to be_valid
expect(build(:image, name: string_with_parenthesis)).to be_valid
end
it 'name length should be less or equal than 100' do
expect(build(:image, name: 'a'*100)).to be_valid
expect(build(:image, name: 'a'*101)).not_to be_valid
end end
it 'bootstrap_template should be included in available bootstrap templates' do it 'bootstrap_template should be included in available bootstrap templates' do