Skip to content

Commit

Permalink
Merge pull request #1298 from openjournals/validate_repository_url
Browse files Browse the repository at this point in the history
Add validation for repository URL
  • Loading branch information
xuanxu committed Dec 1, 2023
2 parents ab7d881 + c6f6530 commit eba4a7d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/models/paper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class Paper < ApplicationRecord
validates_presence_of :track_id, on: :create, message: "You must select a valid subject for the paper", if: Proc.new { JournalFeatures.tracks? }
validates :kind, inclusion: { in: Rails.application.settings["paper_types"] }, allow_nil: true
validates :submission_kind, inclusion: { in: SUBMISSION_KINDS, message: "You must select a submission type" }, allow_nil: false
validates_format_of :repository_url, with: /\Ahttps?:\/\//i, on: :create, message: "Repository URL is missing the protocol segment (http/https)"
validate :check_repository_address, on: :create, unless: Proc.new {|paper| paper.is_a_retraction_notice?}

def notify_editors
Expand Down
27 changes: 22 additions & 5 deletions spec/models/paper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,31 @@
expect(paper.submitting_author).to eq(user)
end

it "should have a complete value for repository url" do
params = { title: 'Test paper',
body: 'A test paper description',
repository_url: 'github.com/arfon/fidgit',
software_version: 'v1.0.0',
submitting_author: create(:user),
submission_kind: 'new',
track: create(:track) }

paper = Paper.create(params)
expect(paper).to_not be_valid
expect(paper.errors.messages[:repository_url].first).to eq("Repository URL is missing the protocol segment (http/https)")

paper = Paper.create(params.merge(repository_url: 'http://github.com/arfon/fidgit'))
expect(paper).to be_valid
end

it "must have a track assigned on creation if tracks are enabled" do
enable_feature(:tracks) do
no_track_params = { title: 'Test paper',
body: 'A test paper description',
repository_url: 'http://github.com/arfon/fidgit',
software_version: 'v1.0.0',
submitting_author: create(:user),
submission_kind: 'new' }
body: 'A test paper description',
repository_url: 'http://github.com/arfon/fidgit',
software_version: 'v1.0.0',
submitting_author: create(:user),
submission_kind: 'new' }

valid_params = no_track_params.merge track: create(:track)

Expand Down

0 comments on commit eba4a7d

Please sign in to comment.