From beb137a6b8a9a9b3eb522d44aee241dc5118ddbf Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 7 Jan 2015 21:42:12 +0100 Subject: [PATCH] Improved tests. --- app/models/user.rb | 18 ++++++++++-- test/unit/user_test.rb | 67 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index d8f566fc0..4cce8452b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -31,7 +31,7 @@ class User < ApplicationModel include User::SearchIndex before_create :check_name, :check_email, :check_login, :check_password - before_update :check_password, :check_email + before_update :check_password, :check_email, :check_login after_create :avatar_check, :notify_clients_after_create after_update :avatar_check, :notify_clients_after_update after_destroy :avatar_destroy, :notify_clients_after_destroy @@ -95,6 +95,9 @@ returns end fullname = fullname + self.lastname end + if fullname == '' && self.email + fullname = self.email + end fullname end @@ -439,15 +442,26 @@ returns end def check_login + + # use email as login if not given if !self.login && self.email self.login = self.email end + + # if email has changed, login is old email, change also login + if self.changes && self.changes['email'] + if self.changes['email'][0] == self.login + self.login = self.email + end + end + + # check if login already exists if self.login self.login = self.login.downcase check = true while check exists = User.where( :login => self.login ).first - if exists + if exists && exists.id != self.id self.login = self.login + rand(999).to_s else check = false diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 121978771..2d57c882a 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -18,6 +18,7 @@ class UserTest < ActiveSupport::TestCase :firstname => 'Firstname', :lastname => 'Lastname', :image => nil, + :fullname => 'Firstname Lastname', :email => 'some@example.com', :login => 'some@example.com', }, @@ -88,6 +89,7 @@ class UserTest < ActiveSupport::TestCase :create_verify => { :firstname => 'Firstname', :lastname => 'Lastname', + :fullname => 'Firstname Lastname', :email => 'firstname.lastname@example.com', :login => 'login-1', }, @@ -185,6 +187,59 @@ class UserTest < ActiveSupport::TestCase :login => 'login-5', } }, + { + :name => '#10 - update create with login/email check', + :create => { + :firstname => '', + :lastname => '', + :email => 'caoyaoewfzfw@21222cn.com', + :updated_by_id => 1, + :created_by_id => 1, + }, + :create_verify => { + :firstname => '', + :lastname => '', + :fullname => 'caoyaoewfzfw@21222cn.com', + :email => 'caoyaoewfzfw@21222cn.com', + :login => 'caoyaoewfzfw@21222cn.com', + }, + :update => { + :email => 'caoyaoewfzfw@212224cn.com', + }, + :update_verify => { + :firstname => '', + :lastname => '', + :email => 'caoyaoewfzfw@212224cn.com', + :fullname => 'caoyaoewfzfw@212224cn.com', + :login => 'caoyaoewfzfw@212224cn.com', + } + }, + { + :name => '#11 - update create with login/email check', + :create => { + :firstname => 'Firstname', + :lastname => 'Lastname', + :email => 'some_tEst11@example.com', + :updated_by_id => 1, + :created_by_id => 1, + }, + :create_verify => { + :firstname => 'Firstname', + :lastname => 'Lastname', + :fullname => 'Firstname Lastname', + :email => 'some_test11@example.com', + }, + :update => { + :email => 'some_Test11-1@example.com', + }, + :update_verify => { + :firstname => 'Firstname', + :lastname => 'Lastname', + :email => 'some_test11-1@example.com', + :fullname => 'Firstname Lastname', + :login => 'some_test11-1@example.com', + } + }, ] tests.each { |test| @@ -199,7 +254,11 @@ class UserTest < ActiveSupport::TestCase test[:create_verify].each { |key, value| next if key == :image_md5 - assert_equal( value, user[key], "create check #{ key } in (#{ test[:name] })" ) + if user.respond_to?( key ) + assert_equal( value, user.send(key), "create check #{ key } in (#{ test[:name] })" ) + else + assert_equal( value, user[key], "create check #{ key } in (#{ test[:name] })" ) + end } if test[:create_verify][:image_md5] file = Avatar.get_by_hash( user.image ) @@ -211,7 +270,11 @@ class UserTest < ActiveSupport::TestCase test[:update_verify].each { |key, value| next if key == :image_md5 - assert_equal( value, user[key], "update check #{ key } in (#{ test[:name] })" ) + if user.respond_to?( key ) + assert_equal( value, user.send(key), "update check #{ key } in (#{ test[:name] })" ) + else + assert_equal( value, user[key], "update check #{ key } in (#{ test[:name] })" ) + end } if test[:update_verify][:image_md5]