Skip to content

Commit

Permalink
add url validation to commit model
Browse files Browse the repository at this point in the history
  • Loading branch information
lalithr95 committed Feb 21, 2016
1 parent e8ff595 commit c000995
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/models/commit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ class Commit < ActiveRecord::Base
has_many :benchmark_runs, as: :initiator, dependent: :destroy

validates :sha1, presence: true, length: { minimum: 5 }, uniqueness: { scope: :repo_id }
# TODO: Add validation of URL

validates :url, presence: true
validates_format_of :url, with: URI::regexp(%w(http https))
validates :repo_id, presence: true
validates :message, presence: true

Expand Down
17 changes: 17 additions & 0 deletions test/models/commit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,21 @@ class CommitTest < ActiveSupport::TestCase
commit = create(:commit, sha1: '1234567')
assert_includes commit.version, '12345'
end

test "validates valid URL" do
valid_url = ['https://github.com/ruby-bench/ruby-bench-web/blob/master/app/models/commit.rb',
'https://github.com/ruby-bench/ruby-bench-web/blob/master/app/models/commit.rb']
valid_url.each do |url|
commit = build(:commit, url: url)
assert commit.valid?
end
end

test "validates invalid URL" do
invalid_url = ['httpgithub.com/ruby-bench/ruby-bench-web/', 'INVLAID URL', 'ftp://github.com']
invalid_url.each do |url|
commit = build(:commit, url: url)
assert commit.invalid?
end
end
end

0 comments on commit c000995

Please sign in to comment.