Skip to content

Commit

Permalink
only accept ror_id expressed as url for providers
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jun 15, 2019
1 parent 62638a6 commit 0b797db
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
4 changes: 2 additions & 2 deletions app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions spec/models/provider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions spec/requests/providers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 0b797db

Please sign in to comment.