mirror of
https://github.com/discourse/discourse.git
synced 2025-05-28 13:51:18 +08:00
DEV: Allow ManagedAuthenticator classes to match by username (#18517)
This commit is contained in:
@ -254,6 +254,78 @@ RSpec.describe Auth::ManagedAuthenticator do
|
||||
expect(user.user_profile.location).to eq("DiscourseVille")
|
||||
end
|
||||
end
|
||||
|
||||
describe 'match by username' do
|
||||
let(:user_match_authenticator) {
|
||||
Class.new(described_class) do
|
||||
def name
|
||||
"myauth"
|
||||
end
|
||||
def match_by_email
|
||||
false
|
||||
end
|
||||
def match_by_username
|
||||
true
|
||||
end
|
||||
end.new
|
||||
}
|
||||
|
||||
it 'works normally' do
|
||||
SiteSetting.username_change_period = 0
|
||||
user = Fabricate(:user)
|
||||
result = user_match_authenticator.after_authenticate(hash.deep_merge(info: { nickname: user.username }))
|
||||
expect(result.user.id).to eq(user.id)
|
||||
expect(UserAssociatedAccount.find_by(provider_name: 'myauth', provider_uid: "1234").user_id).to eq(user.id)
|
||||
end
|
||||
|
||||
it 'works if there is already an association with the target account' do
|
||||
SiteSetting.username_change_period = 0
|
||||
user = Fabricate(:user, username: "IAmGroot")
|
||||
result = user_match_authenticator.after_authenticate(hash)
|
||||
expect(result.user.id).to eq(user.id)
|
||||
end
|
||||
|
||||
it 'works if the username is different case' do
|
||||
SiteSetting.username_change_period = 0
|
||||
user = Fabricate(:user, username: "IAMGROOT")
|
||||
result = user_match_authenticator.after_authenticate(hash)
|
||||
expect(result.user.id).to eq(user.id)
|
||||
end
|
||||
|
||||
it 'does not match if username_change_period isn\'t 0' do
|
||||
SiteSetting.username_change_period = 3
|
||||
user = Fabricate(:user, username: "IAmGroot")
|
||||
result = user_match_authenticator.after_authenticate(hash)
|
||||
expect(result.user).to eq(nil)
|
||||
end
|
||||
|
||||
it 'does not match if default match_by_username not overriden' do
|
||||
SiteSetting.username_change_period = 0
|
||||
authenticator = Class.new(described_class) do
|
||||
def name
|
||||
"myauth"
|
||||
end
|
||||
end.new
|
||||
user = Fabricate(:user, username: "IAmGroot")
|
||||
result = authenticator.after_authenticate(hash)
|
||||
expect(result.user).to eq(nil)
|
||||
end
|
||||
|
||||
it 'does not match if match_by_username is false' do
|
||||
SiteSetting.username_change_period = 0
|
||||
authenticator = Class.new(described_class) do
|
||||
def name
|
||||
"myauth"
|
||||
end
|
||||
def match_by_username
|
||||
false
|
||||
end
|
||||
end.new
|
||||
user = Fabricate(:user, username: "IAmGroot")
|
||||
result = authenticator.after_authenticate(hash)
|
||||
expect(result.user).to eq(nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'description_for_user' do
|
||||
|
Reference in New Issue
Block a user