Checking date input for Rate#for. #1920
This commit is contained in:
@@ -31,7 +31,14 @@ class Rate < ActiveRecord::Base
|
||||
# Check input since it's a "public" API
|
||||
raise Rate::InvalidParameterException.new("user must be a User instance") unless user.is_a?(User)
|
||||
raise Rate::InvalidParameterException.new("project must be a Project instance") unless project.nil? || project.is_a?(Project)
|
||||
|
||||
raise Rate::InvalidParameterException.new("date must be a valid Date string (e.g. YYYY-MM-DD)") unless date.is_a?(String)
|
||||
|
||||
begin
|
||||
Date.parse(date)
|
||||
rescue ArgumentError
|
||||
raise Rate::InvalidParameterException.new("date must be a valid Date string (e.g. YYYY-MM-DD)")
|
||||
end
|
||||
|
||||
rate = self.for_user_project_and_date(user, project, date)
|
||||
|
||||
return nil if rate.nil?
|
||||
|
||||
@@ -199,7 +199,7 @@ describe Rate, 'for' do
|
||||
}.should raise_error(Rate::InvalidParameterException, "user must be a User instance")
|
||||
end
|
||||
|
||||
it 'with an invalid project should be nil' do
|
||||
it 'with an invalid project should raise an InvalidParameterException' do
|
||||
object = mock('random_object_with_id_attribute')
|
||||
Rate.should_not_receive(:for_user_project_and_date)
|
||||
lambda {
|
||||
@@ -207,6 +207,23 @@ describe Rate, 'for' do
|
||||
}.should raise_error(Rate::InvalidParameterException, "project must be a Project instance")
|
||||
end
|
||||
|
||||
it 'with an invalid object for date should raise an InvalidParameterException' do
|
||||
object = mock('random_object_thats_not_a_string')
|
||||
Rate.should_not_receive(:for_user_project_and_date)
|
||||
lambda {
|
||||
Rate.for(@user, @project, object)
|
||||
}.should raise_error(Rate::InvalidParameterException, "date must be a valid Date string (e.g. YYYY-MM-DD)")
|
||||
end
|
||||
|
||||
it 'with an invalid date string should raise an InvalidParameterException' do
|
||||
Rate.should_not_receive(:for_user_project_and_date)
|
||||
lambda {
|
||||
Rate.for(@user, @project, '2000-13-40')
|
||||
}.should raise_error(Rate::InvalidParameterException, "date must be a valid Date string (e.g. YYYY-MM-DD)")
|
||||
end
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
||||
describe Rate, 'for_user_project_and_date (private)' do
|
||||
|
||||
Reference in New Issue
Block a user