cleanup API for looking up a user by email or username, add specs, fix invalid auto association in open id provider

This commit is contained in:
Sam 2013-10-28 16:29:07 +11:00
parent 370e961a90
commit 95e936c299
3 changed files with 42 additions and 59 deletions

View File

@ -124,25 +124,19 @@ class User < ActiveRecord::Base
end end
def self.find_by_username_or_email(username_or_email) def self.find_by_username_or_email(username_or_email)
users = if username_or_email.include?('@') if username_or_email.include?('@')
find_by_email(username_or_email) find_by_email(username_or_email)
else else
find_by_username(username_or_email) find_by_username(username_or_email)
end end
if users.size > 1
raise Discourse::TooManyMatches
else
users.first
end
end end
def self.find_by_email(email) def self.find_by_email(email)
where(email: Email.downcase(email)) where(email: Email.downcase(email)).first
end end
def self.find_by_username(username) def self.find_by_username(username)
where(username_lower: username.downcase) where(username_lower: username.downcase).first
end end
def enqueue_welcome_message(message_type) def enqueue_welcome_message(message_type)

View File

@ -0,0 +1,18 @@
require 'spec_helper'
# In the ghetto ... getting the spec to run in autospec
# thing is we need to load up all auth really early pre-fork
# it means that the require is not going to get a new copy
Auth.send(:remove_const, :OpenIdAuthenticator)
load 'auth/open_id_authenticator.rb'
describe Auth::OpenIdAuthenticator do
it "can lookup pre-existing user if trusted" do
auth = Auth::OpenIdAuthenticator.new("test", "id", trusted: true)
user = Fabricate(:user)
result = auth.after_authenticate(info: {email: user.email}, extra: {identity_url: 'abc'})
result.user.should == user
end
end

View File

@ -782,59 +782,30 @@ describe User do
end end
describe '.find_by_username_or_email' do describe '.find_by_username_or_email' do
it 'finds user by username' do it 'finds users' do
bob = Fabricate(:user, username: 'bob') bob = Fabricate(:user, username: 'bob', email: 'bob@example.com')
found_user = User.find_by_username_or_email('bob')
expect(found_user).to eq bob
end
it 'finds user by email' do
bob = Fabricate(:user, email: 'bob@example.com')
found_user = User.find_by_username_or_email('bob@example.com')
expect(found_user).to eq bob
end
context 'when user does not exist' do
it 'returns nil' do
found_user = User.find_by_username_or_email('doesnotexist@example.com') ||
User.find_by_username_or_email('doesnotexist')
expect(found_user).to be_nil
end
end
context 'when username case does not match' do
it 'finds user' do
bob = Fabricate(:user, username: 'bob')
found_user = User.find_by_username_or_email('Bob') found_user = User.find_by_username_or_email('Bob')
expect(found_user).to eq bob expect(found_user).to eq bob
end
end
context 'when email domain case does not match' do
it 'finds user' do
bob = Fabricate(:user, email: 'bob@example.com')
found_user = User.find_by_username_or_email('bob@Example.com') found_user = User.find_by_username_or_email('bob@Example.com')
expect(found_user).to eq bob
found_user = User.find_by_username_or_email('Bob@Example.com')
expect(found_user).to be_nil
found_user = User.find_by_username_or_email('bob1')
expect(found_user).to be_nil
found_user = User.find_by_email('bob@Example.com')
expect(found_user).to eq bob
found_user = User.find_by_email('bob')
expect(found_user).to be_nil
found_user = User.find_by_username('bOb')
expect(found_user).to eq bob expect(found_user).to eq bob
end end
end
context 'when multiple users are found' do
it 'raises an exception' do
user_query = [stub, stub]
User.stubs(:find_by_username).with('bob').returns(user_query)
expect { User.find_by_username_or_email('bob') }.to raise_error(Discourse::TooManyMatches)
end
end
end end
describe "#added_a_day_ago?" do describe "#added_a_day_ago?" do