2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-10-28 13:29:07 +08:00
|
|
|
|
|
|
|
describe Auth::OpenIdAuthenticator do
|
|
|
|
|
|
|
|
it "can lookup pre-existing user if trusted" do
|
2018-07-23 23:51:57 +08:00
|
|
|
auth = Auth::OpenIdAuthenticator.new("test", "id", "enable_yahoo_logins", trusted: true)
|
2013-10-28 13:29:07 +08:00
|
|
|
|
|
|
|
user = Fabricate(:user)
|
2014-03-26 11:52:50 +08:00
|
|
|
response = OpenStruct.new(identity_url: 'abc')
|
2017-07-28 09:20:09 +08:00
|
|
|
result = auth.after_authenticate(info: { email: user.email }, extra: { response: response })
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(result.user).to eq(user)
|
2013-10-28 13:29:07 +08:00
|
|
|
end
|
2014-08-08 01:28:50 +08:00
|
|
|
|
|
|
|
it "raises an exception when email is missing" do
|
2018-07-23 23:51:57 +08:00
|
|
|
auth = Auth::OpenIdAuthenticator.new("test", "id", "enable_yahoo_logins", trusted: true)
|
2014-08-08 01:28:50 +08:00
|
|
|
response = OpenStruct.new(identity_url: 'abc')
|
2015-01-10 00:34:37 +08:00
|
|
|
expect { auth.after_authenticate(info: {}, extra: { response: response }) }.to raise_error(Discourse::InvalidParameters)
|
2014-08-08 01:28:50 +08:00
|
|
|
end
|
2018-07-27 23:20:47 +08:00
|
|
|
|
|
|
|
it 'can connect to a different existing user account' do
|
|
|
|
authenticator = Auth::OpenIdAuthenticator.new("test", "id", "enable_yahoo_logins", trusted: true)
|
|
|
|
user1 = Fabricate(:user)
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
|
|
|
|
UserOpenId.create!(url: "id/123" , user_id: user1.id, email: "bob@example.com", active: true)
|
|
|
|
|
|
|
|
hash = {
|
|
|
|
info: { email: user1.email }, extra: { response: OpenStruct.new(identity_url: 'id/123') }
|
|
|
|
}
|
|
|
|
|
|
|
|
result = authenticator.after_authenticate(hash, existing_account: user2)
|
|
|
|
|
|
|
|
expect(result.user.id).to eq(user2.id)
|
|
|
|
expect(UserOpenId.exists?(user_id: user1.id)).to eq(false)
|
|
|
|
expect(UserOpenId.exists?(user_id: user2.id)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'revoke' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2018-07-27 23:20:47 +08:00
|
|
|
let(:authenticator) { Auth::OpenIdAuthenticator.new("test", "id", "enable_yahoo_logins", trusted: true) }
|
|
|
|
|
|
|
|
it 'raises exception if no entry for user' do
|
|
|
|
expect { authenticator.revoke(user) }.to raise_error(Discourse::NotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'revokes correctly' do
|
|
|
|
UserOpenId.create!(url: "id/123" , user_id: user.id, email: "bob@example.com", active: true)
|
|
|
|
expect(authenticator.can_revoke?).to eq(true)
|
|
|
|
expect(authenticator.revoke(user)).to eq(true)
|
|
|
|
expect(authenticator.description_for_user(user)).to eq("")
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-10-28 13:29:07 +08:00
|
|
|
end
|