diff options
author | Robert Speicher <rspeicher@gmail.com> | 2016-11-28 08:08:14 +0300 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2016-11-28 12:34:27 +0300 |
commit | 13ad9a745a392e0bf0cedd0e1f318c1acee9b969 (patch) | |
tree | f8d7a170c358df03a137026d4d0112292cf9dd4d /spec/support/matchers/access_matchers.rb | |
parent | eff2404785f58241f947bd7388564f49119d517d (diff) |
Speed up Project security access specs
Prior, every single test was creating four `ProjectMember` objects, each
of which created one `User` record, even though each test only used
_one_ of those Users, if any.
Now each test only creates the single user record it needs, if it needs
one. This shaves minutes off of each spec file changed here.
Diffstat (limited to 'spec/support/matchers/access_matchers.rb')
-rw-r--r-- | spec/support/matchers/access_matchers.rb | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/spec/support/matchers/access_matchers.rb b/spec/support/matchers/access_matchers.rb index 0497e391860..691d7e05f57 100644 --- a/spec/support/matchers/access_matchers.rb +++ b/spec/support/matchers/access_matchers.rb @@ -7,7 +7,7 @@ module AccessMatchers extend RSpec::Matchers::DSL include Warden::Test::Helpers - def emulate_user(user) + def emulate_user(user, project = nil) case user when :user login_as(create(:user)) @@ -19,6 +19,18 @@ module AccessMatchers login_as(create(:user, external: true)) when User login_as(user) + when :owner + raise ArgumentError, "cannot emulate owner without project" unless project + + login_as(project.owner) + when *Gitlab::Access.sym_options.keys + raise ArgumentError, "cannot emulate user #{user} without project" unless project + + role = user + user = create(:user) + project.public_send(:"add_#{role}", user) + + login_as(user) else raise ArgumentError, "cannot emulate user #{user}" end @@ -26,8 +38,7 @@ module AccessMatchers def description_for(user, type) if user.kind_of?(User) - # User#inspect displays too much information for RSpec's description - # messages + # User#inspect displays too much information for RSpec's descriptions "be #{type} for the specified user" else "be #{type} for #{user}" @@ -36,21 +47,31 @@ module AccessMatchers matcher :be_allowed_for do |user| match do |url| - emulate_user(user) - visit url + emulate_user(user, @project) + visit(url) + status_code != 404 && current_path != new_user_session_path end + chain :of do |project| + @project = project + end + description { description_for(user, 'allowed') } end matcher :be_denied_for do |user| match do |url| - emulate_user(user) - visit url + emulate_user(user, @project) + visit(url) + status_code == 404 || current_path == new_user_session_path end + chain :of do |project| + @project = project + end + description { description_for(user, 'denied') } end end |