2011-10-08 17:36:38 -04:00
|
|
|
RSpec::Matchers.define :be_valid_commit do
|
|
|
|
match do |actual|
|
|
|
|
actual != nil
|
|
|
|
actual.id == ValidCommit::ID
|
|
|
|
actual.message == ValidCommit::MESSAGE
|
|
|
|
actual.author.name == ValidCommit::AUTHOR_FULL_NAME
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec::Matchers.define :be_allowed_for do |user|
|
|
|
|
match do |url|
|
|
|
|
include UrlAccess
|
|
|
|
url_allowed?(user, url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec::Matchers.define :be_denied_for do |user|
|
|
|
|
match do |url|
|
|
|
|
include UrlAccess
|
|
|
|
url_denied?(user, url)
|
2011-10-26 09:46:25 -04:00
|
|
|
end
|
2011-10-08 17:36:38 -04:00
|
|
|
end
|
|
|
|
|
2011-10-17 06:39:03 -04:00
|
|
|
RSpec::Matchers.define :be_404_for do |user|
|
|
|
|
match do |url|
|
|
|
|
include UrlAccess
|
|
|
|
url_404?(user, url)
|
2011-10-26 09:46:25 -04:00
|
|
|
end
|
2011-10-17 06:39:03 -04:00
|
|
|
end
|
|
|
|
|
2012-08-29 01:49:26 -04:00
|
|
|
RSpec::Matchers.define :include_module do |expected|
|
|
|
|
match do
|
|
|
|
described_class.included_modules.include?(expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
failure_message_for_should do
|
|
|
|
"expected #{described_class} to include the #{expected} module"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-26 09:46:25 -04:00
|
|
|
module UrlAccess
|
2011-10-08 17:36:38 -04:00
|
|
|
def url_allowed?(user, url)
|
|
|
|
emulate_user(user)
|
|
|
|
visit url
|
2011-10-17 06:39:03 -04:00
|
|
|
(page.status_code != 404 && current_path != new_user_session_path)
|
2011-10-08 17:36:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def url_denied?(user, url)
|
|
|
|
emulate_user(user)
|
|
|
|
visit url
|
2011-10-17 06:39:03 -04:00
|
|
|
(page.status_code == 404 || current_path == new_user_session_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_404?(user, url)
|
|
|
|
emulate_user(user)
|
|
|
|
visit url
|
|
|
|
page.status_code == 404
|
2011-10-08 17:36:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def emulate_user(user)
|
|
|
|
user = case user
|
2012-11-05 22:31:55 -05:00
|
|
|
when :user then create(:user)
|
2011-10-08 17:36:38 -04:00
|
|
|
when :visitor then nil
|
2012-11-05 22:31:55 -05:00
|
|
|
when :admin then create(:admin)
|
2011-10-08 17:36:38 -04:00
|
|
|
else user
|
|
|
|
end
|
|
|
|
login_with(user) if user
|
|
|
|
end
|
|
|
|
end
|
2012-08-29 02:21:50 -04:00
|
|
|
|
|
|
|
# Extend shoulda-matchers
|
|
|
|
module Shoulda::Matchers::ActiveModel
|
|
|
|
class EnsureLengthOfMatcher
|
|
|
|
# Shortcut for is_at_least and is_at_most
|
|
|
|
def is_within(range)
|
2012-09-19 12:14:42 -04:00
|
|
|
is_at_least(range.min) && is_at_most(range.max)
|
2012-08-29 02:21:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|