Skip to content

Commit

Permalink
Refactor: move allow to before block
Browse files Browse the repository at this point in the history
And keep the Arrange Act Assert order.
  • Loading branch information
cbliard authored and oliverguenther committed Aug 6, 2024
1 parent a54729b commit 17dac48
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions spec/controllers/concerns/omniauth_login_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,7 @@
end

it "registers user via post" do
allow(OpenProject::OmniAuth::Authorization).to receive(:after_login!) do |user, auth_hash|
new_user = User.find_by_login("[email protected]")
expect(user).to eq new_user
expect(auth_hash).to include(omniauth_hash)
end
allow(OpenProject::OmniAuth::Authorization).to receive(:after_login!)

auth_source_registration = omniauth_hash.merge(
omniauth: true,
Expand All @@ -192,6 +188,8 @@
expect(response).to redirect_to home_url(first_time_user: true)

user = User.find_by_login("[email protected]")
expect(OpenProject::OmniAuth::Authorization)
.to have_received(:after_login!).with(user, a_hash_including(omniauth_hash), any_args)
expect(user).to be_an_instance_of(User)
expect(user.ldap_auth_source_id).to be_nil
expect(user.current_password).to be_nil
Expand Down Expand Up @@ -339,6 +337,8 @@
end

before do
allow(OpenProject::OmniAuth::Authorization).to receive(:after_login!)

OpenProject::OmniAuth::Authorization.callbacks.clear

# Let's set up a couple of authorization callbacks to see if the mechanism
Expand Down Expand Up @@ -376,20 +376,16 @@
end

it "logs in the user" do
allow(OpenProject::OmniAuth::Authorization).to receive(:after_login!) do |u, auth|
expect(u).to eq user
expect(auth).to eq omniauth_hash
end

post :omniauth_login, params: { provider: :google }

expect(response).to redirect_to my_page_path
expect(OpenProject::OmniAuth::Authorization)
.to have_received(:after_login!).with(user, omniauth_hash, any_args)
end

context "with wrong email address" do
before do
config.global_email = "[email protected]"
allow(OpenProject::OmniAuth::Authorization).to receive(:after_login!)
end

it "is rejected against google" do
Expand Down Expand Up @@ -420,8 +416,6 @@
end

it "is rejected against google" do
allow(OpenProject::OmniAuth::Authorization).to receive(:after_login!)

post :omniauth_login, params: { provider: :google }

expect(response).to redirect_to signin_path
Expand All @@ -431,26 +425,22 @@
end

it "is approved against any other provider" do
allow(OpenProject::OmniAuth::Authorization).to receive(:after_login!) do |u|
new_user = User.find_by identity_url: "some other:123545"

expect(u).to eq new_user
end

omniauth_hash.provider = "some other"

post :omniauth_login, params: { provider: :google }

expect(response).to redirect_to home_url(first_time_user: true)

# The authorization is successful which results in the registration
# of a new user in this case because we changed the provider
# and there isn't a user with that identity URL yet.
new_user = User.find_by identity_url: "some other:123545"
expect(OpenProject::OmniAuth::Authorization)
.to have_received(:after_login!).with(new_user, any_args)
end

# ... and to confirm that, here's what happens when the authorization fails
it "is rejected against any other provider with the wrong email" do
allow(OpenProject::OmniAuth::Authorization).to receive(:after_login!)

omniauth_hash.provider = "yet another"
config.global_email = "[email protected]"

Expand Down

0 comments on commit 17dac48

Please sign in to comment.