allow to login with email instead of nick as well + tests

This commit is contained in:
wvengen 2013-09-20 22:39:19 +02:00
parent fa99a0a852
commit a77c3b59b1
5 changed files with 32 additions and 6 deletions

View file

@ -25,9 +25,9 @@ class User < ActiveRecord::Base
# makes the current_user (logged-in-user) available in models
cattr_accessor :current_user
validates_presence_of :nick, :email
validates_presence_of :email
validates_presence_of :password, :on => :create
validates_length_of :nick, :in => 2..25
validates_length_of :nick, :in => 2..25, :allow_nil => true
validates_uniqueness_of :nick, :case_sensitive => false
validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
validates_uniqueness_of :email, :case_sensitive => false
@ -134,7 +134,7 @@ class User < ActiveRecord::Base
end
def self.authenticate(nick, password)
user = find_by_nick(nick)
user = (find_by_nick(nick) or find_by_email(nick))
if user && user.has_password(password)
user
else

View file

@ -0,0 +1,9 @@
class AllowMissingNick < ActiveRecord::Migration
def self.up
change_column :users, :nick, :string, :default => nil, :null => true
end
def self.down
change_column :users, :nick, :string, :default => "", :null => false
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130718183101) do
ActiveRecord::Schema.define(:version => 20130920201529) do
create_table "article_categories", :force => true do |t|
t.string "name", :default => "", :null => false
@ -324,7 +324,7 @@ ActiveRecord::Schema.define(:version => 20130718183101) do
add_index "tasks", ["workgroup_id"], :name => "index_tasks_on_workgroup_id"
create_table "users", :force => true do |t|
t.string "nick", :default => "", :null => false
t.string "nick"
t.string "password_hash", :default => "", :null => false
t.string "password_salt", :default => "", :null => false
t.string "first_name", :default => "", :null => false

View file

@ -4,7 +4,7 @@ describe 'the session', :type => :feature do
let(:user) { create :user }
describe 'login page', :type => :feature do
it 'is accesible' do
it 'is accessible' do
get login_path
expect(response).to be_success
end
@ -16,6 +16,13 @@ describe 'the session', :type => :feature do
login user.nick, 'XX'+user.password
expect(page).to have_selector('.alert-error')
end
it 'can log me in using an email address' do
visit login_path
fill_in 'nick', :with => user.email
fill_in 'password', :with => user.password
find('input[type=submit]').click
expect(page).to_not have_selector('.alert-error')
end
end
end

View file

@ -49,6 +49,16 @@ describe User do
it 'has a unique email' do
expect(build(:user, email: "#{user.email}")).to be_invalid
end
it 'can authenticate using email address' do
expect(User.authenticate(user.email, 'blahblah')).to be_true
end
it 'can authenticate when there is no nick' do
user.nick = nil
expect(user).to be_valid
expect(User.authenticate(user.email, 'blahblah')).to be_true
end
end
describe 'admin' do