Skip to content

Commit d615a75

Browse files
DEV: Make overrides_email an integration spec (discourse#56)
We're moving the location of the email-overriding logic in discourse/discourse#15378, which makes the old unit test in this plugin fail. This commit makes it an integration test, so that it's more robust against core changes, and will continue to pass before and after the core changes.
1 parent 4aa665f commit d615a75

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# frozen_string_literal: true
2+
3+
require "rails_helper"
4+
5+
describe "OAuth2 Overrides Email", type: :request do
6+
fab!(:initial_email) { "initial@example.com" }
7+
fab!(:new_email) { "new@example.com" }
8+
fab!(:user) { Fabricate(:user, email: initial_email) }
9+
fab!(:uac) { UserAssociatedAccount.create!(user: user, provider_name: "oauth2_basic", provider_uid: "12345") }
10+
11+
before do
12+
SiteSetting.oauth2_enabled = true
13+
SiteSetting.oauth2_callback_user_id_path = "uid"
14+
SiteSetting.oauth2_fetch_user_details = false
15+
SiteSetting.oauth2_email_verified = true
16+
17+
OmniAuth.config.test_mode = true
18+
OmniAuth.config.mock_auth[:oauth2_basic] = OmniAuth::AuthHash.new(
19+
provider: 'oauth2_basic',
20+
uid: '12345',
21+
info: OmniAuth::AuthHash::InfoHash.new(
22+
email: new_email
23+
),
24+
extra: {
25+
raw_info: OmniAuth::AuthHash.new(
26+
email_verified: true
27+
)
28+
},
29+
credentials: OmniAuth::AuthHash.new
30+
)
31+
end
32+
33+
it "doesn't update email by default" do
34+
expect(user.reload.email).to eq(initial_email)
35+
36+
get "/auth/oauth2_basic/callback"
37+
expect(response.status).to eq(302)
38+
expect(session[:current_user_id]).to eq(user.id)
39+
40+
expect(user.reload.email).to eq(initial_email)
41+
end
42+
43+
it 'updates user email if enabled' do
44+
SiteSetting.oauth2_overrides_email = true
45+
46+
get "/auth/oauth2_basic/callback"
47+
expect(response.status).to eq(302)
48+
expect(session[:current_user_id]).to eq(user.id)
49+
50+
expect(user.reload.email).to eq(new_email)
51+
end
52+
end

spec/plugin_spec.rb

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,6 @@
2525
expect(result.user).to eq(user)
2626
end
2727

28-
it 'updated user email if enabled' do
29-
authenticator.stubs(:fetch_user_details).returns(email: user.email, user_id: 'id')
30-
31-
# Create association
32-
result = authenticator.after_authenticate(auth)
33-
expect(result.user).to eq(user)
34-
35-
# Change user email on remote system
36-
old_email = user.email
37-
authenticator.stubs(:fetch_user_details).returns(email: "newemail@example.com", user_id: 'id')
38-
39-
# Login again - no change
40-
result = authenticator.after_authenticate(auth)
41-
expect(result.user).to eq(user)
42-
expect(result.user.email).to eq(old_email)
43-
44-
# Enable site setting
45-
SiteSetting.oauth2_overrides_email = true
46-
result = authenticator.after_authenticate(auth)
47-
expect(result.user).to eq(user)
48-
expect(result.user.email).to eq("newemail@example.com")
49-
end
50-
5128
it 'validates user email if provider has verified' do
5229
SiteSetting.oauth2_email_verified = false
5330
authenticator.stubs(:fetch_user_details).returns(email: user.email, email_verified: true)

0 commit comments

Comments
 (0)