|
| 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 |
0 commit comments