summaryrefslogtreecommitdiff
path: root/spec/support
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2015-07-21 22:09:02 -0400
committerRobert Speicher <rspeicher@gmail.com>2015-07-22 00:01:17 -0400
commita2ecfdc5859c1703dbbad6dde888f7a0eb5817c8 (patch)
tree6141d33a0c24cbf3bf6fdb5321bf29dfd6b27e8f /spec/support
parent1fee24a36143e8057d495e8a7e0f2d4b93d83f2a (diff)
downloadgitlab-ce-a2ecfdc5859c1703dbbad6dde888f7a0eb5817c8.tar.gz
Move access-related matchers to their own module
Diffstat (limited to 'spec/support')
-rw-r--r--spec/support/matchers.rb34
-rw-r--r--spec/support/matchers/access_matchers.rb54
2 files changed, 54 insertions, 34 deletions
diff --git a/spec/support/matchers.rb b/spec/support/matchers.rb
index 87a034cca3b..d9b27ab08d8 100644
--- a/spec/support/matchers.rb
+++ b/spec/support/matchers.rb
@@ -1,37 +1,3 @@
-def emulate_user(user)
- user = case user
- when :user then create(:user)
- when :visitor then nil
- when :admin then create(:admin)
- else user
- end
- login_with(user) if user
-end
-
-RSpec::Matchers.define :be_allowed_for do |user|
- match do |url|
- emulate_user(user)
- visit url
- status_code != 404 && current_path != new_user_session_path
- end
-end
-
-RSpec::Matchers.define :be_denied_for do |user|
- match do |url|
- emulate_user(user)
- visit url
- status_code == 404 || current_path == new_user_session_path
- end
-end
-
-RSpec::Matchers.define :be_not_found_for do |user|
- match do |url|
- emulate_user(user)
- visit url
- status_code == 404
- end
-end
-
RSpec::Matchers.define :include_module do |expected|
match do
described_class.included_modules.include?(expected)
diff --git a/spec/support/matchers/access_matchers.rb b/spec/support/matchers/access_matchers.rb
new file mode 100644
index 00000000000..558e8b1612f
--- /dev/null
+++ b/spec/support/matchers/access_matchers.rb
@@ -0,0 +1,54 @@
+# AccessMatchers
+#
+# The custom matchers contained in this module are used to test a user's access
+# to a URL by emulating a specific user or type of user account, visiting the
+# URL, and then checking the response status code and resulting path.
+module AccessMatchers
+ extend RSpec::Matchers::DSL
+ include Warden::Test::Helpers
+
+ def emulate_user(user)
+ case user
+ when :user
+ login_as(create(:user))
+ when :visitor
+ logout
+ when :admin
+ login_as(create(:admin))
+ when User
+ login_as(user)
+ else
+ raise ArgumentError, "cannot emulate user #{user}"
+ end
+ end
+
+ def description_for(user, type)
+ if user.kind_of?(User)
+ # User#inspect displays too much information for RSpec's description
+ # messages
+ "be #{type} for supplied User"
+ else
+ "be #{type} for #{user}"
+ end
+ end
+
+ matcher :be_allowed_for do |user|
+ match do |url|
+ emulate_user(user)
+ visit url
+ status_code != 404 && current_path != new_user_session_path
+ end
+
+ description { description_for(user, 'allowed') }
+ end
+
+ matcher :be_denied_for do |user|
+ match do |url|
+ emulate_user(user)
+ visit url
+ status_code == 404 || current_path == new_user_session_path
+ end
+
+ description { description_for(user, 'denied') }
+ end
+end