Skip to content

Commit

Permalink
Added tests for when a user was created with an upper case username. …
Browse files Browse the repository at this point in the history
…Also fixed the before and after sections for Mongo test cases.
  • Loading branch information
kristiankristensen committed Nov 1, 2011
1 parent 880fc38 commit 1db298a
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 7 deletions.
1 change: 0 additions & 1 deletion lib/sorcery/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def require_login
# Takes credentials and returns a user on successful authentication.
# Runs hooks after login or failed login.
def login(*credentials)
logger.info(credentials)
user = user_class.authenticate(*credentials)
if user
return_to_url = session[:return_to_url]
Expand Down
3 changes: 1 addition & 2 deletions lib/sorcery/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ def sorcery_config
# returns the user if success, nil otherwise.
def authenticate(*credentials)
raise ArgumentError, "at least 2 arguments required" if credentials.size < 2

credentials[0].downcase! if @sorcery_config.downcase_username_before_authenticating
user = find_by_credentials(credentials)
_salt = user.send(@sorcery_config.salt_attribute_name) if user && !@sorcery_config.salt_attribute_name.nil? && !@sorcery_config.encryption_provider.nil?
Expand Down Expand Up @@ -196,7 +195,7 @@ class Config

:email_attribute_name, # change default email attribute.

:downcase_username_before_authenticating, # downcase the username before trying to authenticate, default is false
:downcase_username_before_authenticating, # downcase the username before trying to authenticate, default is false

:crypted_password_attribute_name, # change default crypted_password attribute.
:salt_join_token, # what pattern to use to join the password with the salt
Expand Down
4 changes: 2 additions & 2 deletions lib/sorcery/model/adapters/mongo_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ def save!(options = {})

module ClassMethods
def credential_regex(credential)
return /^#{credential}$/i if (@sorcery_config.downcase_username_before_authenticating)
return { :$regex => /^#{credential}$/i } if (@sorcery_config.downcase_username_before_authenticating)
return credential
end

def find_by_credentials(credentials)
@sorcery_config.username_attribute_names.each do |attribute|
@user = where(attribute => { :$regex => credential_regex(credentials[0]) } ).first
@user = where(attribute => credential_regex(credentials[0])).first
break if @user
end
@user
Expand Down
4 changes: 2 additions & 2 deletions lib/sorcery/model/adapters/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ def increment(attr)

module ClassMethods
def credential_regex(credential)
return /^#{credential}$/i if (@sorcery_config.downcase_username_before_authenticating)
return { :$regex => /^#{credential}$/i } if (@sorcery_config.downcase_username_before_authenticating)
return credential
end

def find_by_credentials(credentials)
@sorcery_config.username_attribute_names.each do |attribute|
@user = where(attribute => { :$regex => credential_regex(credentials[0]) } ).first
@user = where(attribute => credential_regex(credentials[0])).first
break if @user
end
@user
Expand Down
15 changes: 15 additions & 0 deletions spec/rails3/spec/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@
session[:user_id].should == @user.id
end

it "login(username,password) should return nil and not set the session when user was created with upper case username, config is default, and log in username is lower case" do
create_new_user({:username => 'GIZMO1', :email => "bla1@bla.com", :password => 'secret1'})
get :test_login, :username => 'gizmo1', :password => 'secret1'
assigns[:user].should be_nil
session[:user_id].should be_nil
end

it "login(username,password) should return the user and set the session with user.id when user was created with upper case username and config is downcase before authenticating" do
sorcery_model_property_set(:downcase_username_before_authenticating, true)
create_new_user({:username => 'GIZMO1', :email => "bla1@bla.com", :password => 'secret1'})
get :test_login, :username => 'gizmo1', :password => 'secret1'
assigns[:user].should == @user
session[:user_id].should == @user.id
end

it "logout should clear the session" do
cookies[:remember_me_token] = nil
session[:user_id] = @user.id
Expand Down
20 changes: 20 additions & 0 deletions spec/rails3_mongo_mapper/spec/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@
before(:all) do
sorcery_reload!
User.delete_all
end

before(:each) do
create_new_user
end

after(:each) do
Sorcery::Controller::Config.reset!
sorcery_reload!
User.delete_all
sorcery_controller_property_set(:user_class, User)
sorcery_model_property_set(:username_attribute_names, [:username, :email])
end
Expand Down Expand Up @@ -77,6 +82,21 @@
assigns[:user].should == @user
session[:user_id].should == @user.id
end

it "login(username,password) should return nil and not set the session when user was created with upper case username, config is default, and log in username is lower case" do
create_new_user({:username => 'GIZMO1', :email => "bla1@bla.com", :password => 'secret1'})
get :test_login, :username => 'gizmo1', :password => 'secret1'
assigns[:user].should be_nil
session[:user_id].should be_nil
end

it "login(username,password) should return the user and set the session with user.id when user was created with upper case username and config is downcase before authenticating" do
sorcery_model_property_set(:downcase_username_before_authenticating, true)
create_new_user({:username => 'GIZMO1', :email => "bla1@bla.com", :password => 'secret1'})
get :test_login, :username => 'gizmo1', :password => 'secret1'
assigns[:user].should == @user
session[:user_id].should == @user.id
end

it "logout should clear the session" do
cookies[:remember_me_token] = nil
Expand Down
20 changes: 20 additions & 0 deletions spec/rails3_mongoid/spec/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@
before(:all) do
sorcery_reload!
User.delete_all
end

before(:each) do
create_new_user
end

after(:each) do
Sorcery::Controller::Config.reset!
sorcery_reload!
User.delete_all
sorcery_controller_property_set(:user_class, User)
sorcery_model_property_set(:username_attribute_names, [:username, :email])
end
Expand Down Expand Up @@ -78,6 +83,21 @@
session[:user_id].should == @user.id
end

it "login(username,password) should return nil and not set the session when user was created with upper case username, config is default, and log in username is lower case" do
create_new_user({:username => 'GIZMO1', :email => "bla1@bla.com", :password => 'secret1'})
get :test_login, :username => 'gizmo1', :password => 'secret1'
assigns[:user].should be_nil
session[:user_id].should be_nil
end

it "login(username,password) should return the user and set the session with user.id when user was created with upper case username and config is downcase before authenticating" do
sorcery_model_property_set(:downcase_username_before_authenticating, true)
create_new_user({:username => 'GIZMO1', :email => "bla1@bla.com", :password => 'secret1'})
get :test_login, :username => 'gizmo1', :password => 'secret1'
assigns[:user].should == @user
session[:user_id].should == @user.id
end

it "logout should clear the session" do
cookies[:remember_me_token] = nil
session[:user_id] = @user.id
Expand Down

0 comments on commit 1db298a

Please sign in to comment.