diff --git a/app/models/provider.rb b/app/models/provider.rb index 783fc5e19..79d6a8fca 100644 --- a/app/models/provider.rb +++ b/app/models/provider.rb @@ -34,12 +34,12 @@ class Provider < ActiveRecord::Base validates_presence_of :symbol, :name, :contact_name, :contact_email validates_uniqueness_of :symbol, message: "This name has already been taken" validates_format_of :contact_email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, message: "contact_email should be an email" - validates_format_of :website, :with => /https?:\/\/[\S]+/ , if: :website?, message: "Website should be an url" + validates_format_of :website, :with => /https?:\/\/[\S]+/ , if: :website?, message: "Website should be a URL" validates_inclusion_of :role_name, :in => %w( ROLE_FOR_PROFIT_PROVIDER ROLE_CONTRACTUAL_PROVIDER ROLE_CONSORTIUM_LEAD ROLE_ALLOCATOR ROLE_MEMBER ROLE_REGISTRATION_AGENCY ROLE_ADMIN ROLE_DEV ), :message => "Role %s is not included in the list" validates_inclusion_of :organization_type, :in => %w(nationalInstitution nationalLibrary academicInstitution academicLibrary researchInstitution governmentAgency publisher professionalSociety serviceProvider vendor), :message => "organization type %s is not included in the list", if: :organization_type? validates_inclusion_of :focus_area, :in => %w(biomedicalAndHealthSciences earthSciences humanities mathematicsAndComputerScience physicalSciencesAndEngineering socialSciences general), :message => "focus area %s is not included in the list", if: :focus_area? validate :freeze_symbol, :on => :update - validates_format_of :ror_id, :with => /\A(?:(http|https):\/\/)?(?:ror\.org\/)?(0\w{6}\d{2})\z/, if: :ror_id? + validates_format_of :ror_id, :with => /\A(http|https):\/\/ror\.org\/0\w{6}\d{2}\z/, if: :ror_id?, message: "ROR ID should be a URL" validates_format_of :twitter_handle, :with => /\A@[a-zA-Z0-9_]{1,15}\z/, if: :twitter_handle? # validates :technical_contact, contact: true diff --git a/spec/models/provider_spec.rb b/spec/models/provider_spec.rb index 42ddb967d..1b85201dc 100644 --- a/spec/models/provider_spec.rb +++ b/spec/models/provider_spec.rb @@ -9,9 +9,7 @@ it { should validate_presence_of(:contact_email) } it { should validate_presence_of(:contact_name) } it { is_expected.to strip_attribute(:name) } - it { - expect(provider).to be_valid - } + it { expect(provider).to be_valid } end describe "admin" do diff --git a/spec/requests/providers_spec.rb b/spec/requests/providers_spec.rb index d5ab92913..95dea1ed8 100644 --- a/spec/requests/providers_spec.rb +++ b/spec/requests/providers_spec.rb @@ -419,15 +419,10 @@ "country" => "GB" } } } end - it 'returns status code 422' do - post '/providers', params, headers - - expect(last_response.status).to eq(422) - end - it 'returns a validation failure message' do post '/providers', params, headers + expect(last_response.status).to eq(422) expect(json["errors"].first).to eq("source"=>"contact_email", "title"=>"Can't be blank") end end @@ -469,13 +464,23 @@ it 'updates the record' do put "/providers/#{provider.symbol}", params, headers + expect(last_response.status).to eq(200) expect(json.dig('data', 'attributes', 'contactName')).to eq("timAus") end + end - it 'returns status code 200' do + context 'ror_id in wrong format' do + let(:params) do + { "data" => { "type" => "providers", + "attributes" => { + "rorId" => "ror.org/05njkjr15" } } } + end + + it 'raises error' do put "/providers/#{provider.symbol}", params, headers - expect(last_response.status).to eq(200) + expect(last_response.status).to eq(422) + expect(json["errors"].first).to eq("source" => "ror_id", "title" => "Ror id should be a url") end end @@ -496,13 +501,8 @@ it 'updates the record' do put "/providers/#{provider.symbol}", params, headers - expect(json.dig('data', 'attributes', 'contactName')).to eq("timAus") - end - - it 'returns status code 200' do - put "/providers/#{provider.symbol}", params, headers - expect(last_response.status).to eq(200) + expect(json.dig('data', 'attributes', 'contactName')).to eq("timAus") end end