|
| 1 | +require 'rails_helper' |
| 2 | +require_relative '../../lib/managed_authenticator' |
| 3 | +describe Auth::ManagedAuthenticator do |
| 4 | + |
| 5 | + let(:authenticator) { |
| 6 | + Class.new(described_class).new do |
| 7 | + def name |
| 8 | + "myauth" |
| 9 | + end |
| 10 | + end |
| 11 | + } |
| 12 | + |
| 13 | + let(:hash) { |
| 14 | + { |
| 15 | + provider: "myauth", |
| 16 | + uid: "1234", |
| 17 | + info: { |
| 18 | + name: "Best Display Name", |
| 19 | + email: "awesome@example.com", |
| 20 | + nickname: "IAmGroot" |
| 21 | + }, |
| 22 | + credentials: { |
| 23 | + token: "supersecrettoken" |
| 24 | + }, |
| 25 | + extra: { |
| 26 | + raw_info: { |
| 27 | + randominfo: "some info" |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + context 'after_authenticate' do |
| 34 | + it 'can match account from an existing association' do |
| 35 | + user = Fabricate(:user) |
| 36 | + associated = UserAssociatedAccount.create!(user: user, provider_name: 'myauth', provider_uid: "1234") |
| 37 | + result = authenticator.after_authenticate(hash) |
| 38 | + |
| 39 | + expect(result.user.id).to eq(user.id) |
| 40 | + associated.reload |
| 41 | + expect(associated.info["name"]).to eq("Best Display Name") |
| 42 | + expect(associated.info["email"]).to eq("awesome@example.com") |
| 43 | + expect(associated.credentials["token"]).to eq("supersecrettoken") |
| 44 | + expect(associated.extra["raw_info"]["randominfo"]).to eq("some info") |
| 45 | + end |
| 46 | + |
| 47 | + describe 'match by email' do |
| 48 | + it 'works normally' do |
| 49 | + user = Fabricate(:user) |
| 50 | + result = authenticator.after_authenticate(hash.deep_merge(info: { email: user.email })) |
| 51 | + expect(result.user.id).to eq(user.id) |
| 52 | + end |
| 53 | + |
| 54 | + it 'works if there is already an association with the target account' do |
| 55 | + user = Fabricate(:user, email: "awesome@example.com") |
| 56 | + result = authenticator.after_authenticate(hash) |
| 57 | + expect(result.user.id).to eq(user.id) |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | +end |
0 commit comments