From a77c3b59b188284144f7094eb9b442b51e55fd33 Mon Sep 17 00:00:00 2001 From: wvengen Date: Fri, 20 Sep 2013 22:39:19 +0200 Subject: [PATCH] allow to login with email instead of nick as well + tests --- app/models/user.rb | 6 +++--- db/migrate/20130920201529_allow_missing_nick.rb | 9 +++++++++ db/schema.rb | 4 ++-- spec/integration/session_spec.rb | 9 ++++++++- spec/models/user_spec.rb | 10 ++++++++++ 5 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20130920201529_allow_missing_nick.rb diff --git a/app/models/user.rb b/app/models/user.rb index 2b4a399b..cf5829d5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/db/migrate/20130920201529_allow_missing_nick.rb b/db/migrate/20130920201529_allow_missing_nick.rb new file mode 100644 index 00000000..f5b96d3e --- /dev/null +++ b/db/migrate/20130920201529_allow_missing_nick.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 0d5b1326..4170d918 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/integration/session_spec.rb b/spec/integration/session_spec.rb index d6942e94..aa71972d 100644 --- a/spec/integration/session_spec.rb +++ b/spec/integration/session_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index bc90fa01..fec01465 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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