Class | UserTest
In: | test/unit/notifier_test.rb test/unit/user_test.rb
Parent: | Test::Unit::TestCase

Copyright (c) 2006 Logica

### Methods

setup setup test01_deliver_summary test_admin test_change_password test_create test_new_cadmin test_new_signup test_set_new_pw test_updates tst_login_basicauthentication tst_login_bugzilla

### Public Instance methods

setup()

[Source]

        # File test/unit/notifier_test.rb, line 21
    21:   def setup
    22:     logger.debug "Test Case: #{name}"
    23:     @oup_20060721 = create_oup_20060721
    24:     @oup_wiki = create_oup_wiki(@oup_20060721)
    25:     @andy = users(:andy)
    26:     @george = users(:george)
    27:     @tony = users(:tony)
    28:     @cash = users(:cash)
    29:   end

setup()

[Source]

        # File test/unit/user_test.rb, line 23
    23:   def setup
    24:     logger.debug "Test Case: #{name}"
    25:     @emails = ActionMailer::Base::deliveries
    26:     @emails.clear
    27:     @andy, @cash, @tony, @george = users(:andy), users(:cash), users(:tony), users(:george)
    28:   end

test01_deliver_summary()

TODO include all record types:

[Source]

        # File test/unit/notifier_test.rb, line 32
    32:   def test01_deliver_summary
    33:     assert_not_nil @oup_wiki
    34:     page = WikiPage.find_by_filename('artifact,_fdRfkBUJEdqrUt4zetC1gg.html')
    35:     assert_not_nil page
    36:     #date_previous_week = date - 7.days
    37:     #puts "Previous week: #{date_previous_week}"
    38:     #start_of_previous_week = (date - 7.days).at_beginning_of_week
    39:     #start_of_week = date.at_beginning_of_week
    40:     #puts "Previous week: from #{start_of_previous_week} to #{start_of_week}"
    41:     #puts "Previous day from #{(Time.now - 1.day).at_beginning_of_day} to #{Time.now.at_beginning_of_day}"
    42:     #puts "Previous month #{(Time.now - 1.month).at_beginning_of_month} to #{Time.now.at_beginning_of_month}"
    43:     comment1 = Comment.new(:user => @andy, :text => 'some text', :page => page, :site => @oup_wiki, :version => page.versions[0])
    44:     comment1.created_on = Time.now - 3.days
    45:     assert comment1.save!
    46:     page = WikiPage.find_by_filename('artifact,_fdRfkBUJEdqrUt4zetC1gg.html')
    47:     checkout = Checkout.new(:user => @andy, :page => page, :site => @oup_wiki, :note => 'Checkout to test notifier')
    48:     assert checkout.save!
    49:     assert Checkout.count > 0
    50:     assert Comment.count > 0
    51:     starttime = Time.now.at_beginning_of_month
    52:     endtime = (Time.now + 1.month).at_beginning_of_month
    53:     @tony.created_on = Time.now - 3.days
    54:     @tony.save!
    55:     assert 0 < User.find(:all, :conditions => ['created_on > ? and created_on < ?', starttime, endtime ], :order => 'created_on DESC').size
    56:     Notifier::deliver_summary({:type => 'M', :dummy => ''}, Time.now + 1.month) # plus 1 month, because records are created in the current month
    57:   end

test_admin()

Shows:

1. Cannot update a user to Y or C without specifying the user
2. Cadmin can upgrade user to admin, downgrade to user, admin kan upgrade user to admin but not downgrade admin to user
3. Cadmin can make another user the cadmin

. C -> Y or C -> N not possible

[Source]

         # File test/unit/user_test.rb, line 189
    189:   def test_admin
    190:     cadmin = User.find_central_admin
    191:     user = User.find_all_by_admin('N')[0]
    192:     admin = User.find_all_by_admin('Y')[0]
    193:     assert_not_nil cadmin
    194:     assert_not_nil user
    195:     assert_not_nil admin
    196:     user.admin = 'Y'
    197:     assert !user.save
    198:     assert_equal 'Admin can only be set by an admin', user.errors.full_messages.join(", ")
    199:     user.admin = 'C'
    200:     assert !user.save
    201:     assert_equal 'Admin can only be set to C by the central admin', user.errors.full_messages.join(", ")
    202:     # 2
    203:     user.admin = 'Y'
    204:     user.user = cadmin
    205:     assert user.save
    206:     user.admin = 'N'
    207:     user.user = cadmin
    208:     assert user.save
    209:     user.admin = 'Y'
    210:     user.user = admin
    211:     assert user.save
    212:     user.admin = 'N'
    213:     user.user = admin
    214:     assert !user.save
    215:     assert_equal 'Admin can only be revoked by the central admin', user.errors.full_messages.join(", ")
    216:     user.user = cadmin
    217:     assert user.save
    218:     # 3
    219:     assert cadmin.cadmin?
    220:     User.cadmin(cadmin, user)
    221:     user.save
    222:     cadmin.save
    223:     assert_equal '', user.errors.full_messages.join(", ")
    224:     assert_equal '', cadmin.errors.full_messages.join(", ")
    225:     user.reload
    226:     cadmin.reload
    227:     assert user.cadmin?
    228:     assert !cadmin.cadmin?
    229:     assert_equal 'Y', cadmin.admin
    230:     assert_equal 'C', user.admin
    231:   end

test_change_password()

[Source]

         # File test/unit/user_test.rb, line 134
    134:   def test_change_password
    135:     user = User.find_by_name('User2')
    136:     assert_raise(RuntimeError) {user.change_password(User.new)}
    137:     assert_raise(RuntimeError) {user.change_password(User.new(:password =>'', :password_confirmation => ''))}
    138:     user.change_password(User.new(:password =>'xyz', :password_confirmation => '123'))
    139:     assert !user.save
    140:     assert_equal "Password doesn't match confirmation", user.errors.full_messages.join(", ")
    141:     user.change_password(User.new(:password =>'xyz', :password_confirmation => 'xyz'))
    142:     assert user.save
    143:     assert_equal '', user.errors.full_messages.join(', ')
    144:     user = User.find_by_name('User2')
    145:     user.password = 'xyz'
    146:     login_user = user.try_to_login
    147:     assert_equal hash_pw('xyz'), login_user.hashed_password
    148:     assert_not_nil login_user
    149:   end

test_create()

[Source]

        # File test/unit/user_test.rb, line 30
    30:   def test_create
    31:     assert_kind_of User, @andy
    32:     #assert_equal 1, @andy.id
    33:     assert_equal "andy.kaufman@epf.eclipse.org", @andy.email
    34:     assert_equal "Andy Kaufman", @andy.name
    35:     assert_equal "localhost", @andy.ip_address
    36:     assert_equal hash_pw(@andy.name), @andy.hashed_password
    37:     assert_equal "Y", @andy.admin
    38:   end

test_new_cadmin()

Shows:

1. When the central admin is created, the ‘Templates’ Wiki is created

[Source]

         # File test/unit/user_test.rb, line 260
    260:   def test_new_cadmin
    261:     params = {:name => 'onno', :email => 'Onno@epf.eclipse.org', :password => 'xyz', :password_confirmation => 'xyz'}
    262:     # cannot create cadmin if there are users
    263:     assert_raise(RuntimeError) {cadmin = User.new_cadmin(params)}
    264:     User.delete_all
    265:     # params are needed
    266:     cadmin = User.new_cadmin({})
    267:     assert !cadmin.save
    268:     assert_equal "Name can't be blank, Password confirmation can't be blank, Password can't be blank, Email can't be blank, Email is invalid", cadmin.errors.full_messages.join(", ")
    269:     # password needs to be confirmed
    270:     cadmin = User.new_cadmin(:name => 'onno', :email => 'Onno@epf.eclipse.org', :password => 'xyz', :password_confirmation => '123')
    271:     assert !cadmin.save
    272:     assert_equal "Password doesn't match confirmation", cadmin.errors.full_messages.join(", ")
    273:     # valid email is required
    274:     cadmin = User.new_cadmin(:name => 'onno', :email => 'Onno(at)epf.org', :password => 'xyz', :password_confirmation => 'xyz')
    275:     assert !cadmin.save
    276:     assert_equal "Email is invalid", cadmin.errors.full_messages.join(", ")
    277:     # cadmin is created, note domain restriction does not apply to cadmin account
    278:     cadmin = User.new_cadmin(:id => 5, :name => 'onno', :email => 'Onno@noneExistingDomain.Com', :password => 'xyz', :password_confirmation => 'xyz')
    279:     assert cadmin.save
    280:     assert_equal 26, Site.templates.size
    281:     # 1
    282:     assert_equal "", cadmin.errors.full_messages.join(", ")
    283:     assert_equal 'onno@noneexistingdomain.com', cadmin.email # email set to downcase
    284:     assert_not_nil cadmin.hashed_password
    285:     assert_equal hash_pw('xyz'), cadmin.hashed_password # password is hashed and stored
    286:     assert_not_nil cadmin.confirmed_on # account or email does not need to be confirmed
    287:     # TODO: we can update attributes
    288:   end

test_new_signup()

[Source]

        # File test/unit/user_test.rb, line 40
    40:   def test_new_signup
    41:     assert User.count > 0
    42:     assert_not_nil ENV['EPFWIKI_DOMAINS']
    43:     # we need params
    44:     user = User.new_signup({})
    45:     assert !user.save
    46:     assert_equal "Name can't be blank, Password confirmation can't be blank, Password can't be blank, Email can't be blank, Email is invalid, Email domain not valid", user.errors.full_messages.join(", ")
    47:     # password needs to be confirmed
    48:     user = User.new_signup({:name => "User10", :password => "User10", :email => "User10@epf.eclipse.org"}) #, :i_agree_to_the_terms_of_use => '1'
    49:     assert !user.save
    50:     assert_equal "Password confirmation can't be blank", user.errors.full_messages.join(", ")
    51:     # password needs to be confirmed 2
    52:     user = User.new_signup({:name => "User10", :password => "User10", :password_confirmation => "xyz", :email => "User10@epf.eclipse.org"})  # , :i_agree_to_the_terms_of_use => '1'
    53:     assert !user.save
    54:     assert_equal "Password doesn't match confirmation", user.errors.full_messages.join(", ")
    55:     # user created
    56:     user = User.new_signup({:name => "User10", :password => "User10", :password_confirmation => "User10", :email => "User10@epf.eclipse.org"})  # , :i_agree_to_the_terms_of_use => '1'
    57:     assert user.save
    58:     assert_equal "user10@epf.eclipse.org", user.email
    59:     assert_equal nil, user.confirmed_on # account needs to be confirmed
    60:     assert_equal hash_pw('User10'), user.hashed_password
    61:     assert_equal nil, user.hashed_password_new
    62:     # cannot login, not confirmed
    63:     login_user = user.try_to_login
    64:     assert_equal nil, login_user
    65:     # confirm account
    66:     # assert_equal hash_pw('User10'), user.hashed_password
    67:     user.confirm_account(hash_pw(user.hashed_password))
    68:     assert user.save
    69:     assert_not_nil user.confirmed_on
    70:     # can login
    71:     login_user = user.try_to_login
    72:     assert_not_nil login_user
    73:   end

test_set_new_pw()

[Source]

         # File test/unit/user_test.rb, line 75
     75:   def test_set_new_pw
     76:     # user created
     77:     user = User.new_signup({:name => "User11", :password => "User11", :password_confirmation => "User11", :email => "User11@epf.eclipse.org"})  # , :i_agree_to_the_terms_of_use => '1'
     78:     assert user.save
     79:     # confirm account
     80:     user.confirm_account(hash_pw(user.hashed_password))
     81:     assert user.save
     82:     assert_equal "", user.errors.full_messages.join(", ")
     83:     user = User.find_by_name('User11')
     84:     assert_not_nil user.confirmed_on
     85:     # can login
     86:     user.password = 'User11'
     87:     login_user = user.try_to_login
     88:     assert_not_nil login_user
     89:     # set new password
     90:     hashed_password = user.hashed_password
     91:     user.set_new_pw('new_password')
     92:     new_pw = user.password
     93:     assert user.save
     94:     assert_equal "", user.errors.full_messages.join(", ")
     95:     user.reload
     96:     assert_not_nil user.confirmed_on
     97:     assert_equal hash_pw(new_pw), user.hashed_password_new
     98:     # we can still sign in with the old password
     99:     user.password = "User11"
    100:     login_user = user.try_to_login
    101:     assert_not_nil login_user
    102:     # we cannot sign in with the new password
    103:     user.password = new_pw
    104:     login_user = user.try_to_login
    105:     assert_equal nil, login_user
    106:     # cannot confirm with the wrong token
    107:     user = User.find_by_name('User11')
    108:     assert_equal false, user.confirm_account("somewrongtoken")
    109:     # confirm the account
    110:     user = User.find_by_name('User11')
    111:     #assert_equal hash_pw(hash_pw(new_pw)), hash_pw(user.hashed_password_new)
    112:     user.confirm_account(hash_pw(hash_pw(new_pw)))
    113:     assert_equal hash_pw(new_pw), user.hashed_password
    114:     user.save
    115:     assert_equal "", user.errors.full_messages.join(", ")
    116:     user = User.find_by_name('User11')
    117:     assert_not_equal hashed_password, user.hashed_password
    118:     assert_equal hash_pw(new_pw), user.hashed_password
    119:     assert_equal nil, user.hashed_password_new
    120:     assert_not_nil user.confirmed_on
    121:     # we can sign in with the new password
    122:     user.password = new_pw
    123:     login_user = user.try_to_login
    124:     assert_not_nil login_user
    125:   end

test_updates()

[Source]

         # File test/unit/user_test.rb, line 127
    127:   def test_updates
    128:     user = User.find_by_name('Andy Kaufman')
    129:     user.name = "test04_updates"
    130:     assert user.save
    131:     assert_equal "", user.errors.full_messages.join(", ")
    132:   end

tst_login_basicauthentication()

Shows: 1 User can logon using basic authentication, first logon -> an account is created 2 Second logon succesfull, account is not created 3 Wrong password cannot logon 4 Failed to save, sends email

[Source]

         # File test/unit/user_test.rb, line 156
    156:   def tst_login_basicauthentication
    157:     # get password
    158:     pw = IO.readlines('S:/Keys/epfwiki_basic_authentication_key')[0]
    159:     # 1
    160:     ENV['EPFWIKI_DOMAINS'] = ENV['EPFWIKI_DOMAINS'] + ' @logicacmg.com'
    161:     user_count = User.count
    162:     user = User.new(:account => 'ostraaten', :password => pw)
    163:     logon_user = User.login(user.account, user.password)
    164:     assert_equal user_count + 1, User.count
    165:     # 2
    166:     logon_user = User.login(user.account, user.password)
    167:     assert_not_nil logon_user
    168:     assert_equal user_count + 1, User.count
    169:     # 3
    170:     logon_user = User.login(user.account, user.password + 'xyz')
    171:     assert_nil logon_user
    172:     assert_equal user_count + 1, User.count
    173:     # 4
    174:     User.find_by_account(user.account).destroy
    175:     assert_equal user_count, User.count
    176:     ActiveRecord::Migration::drop_column 'users', 'account'
    177:     assert_equal 0,@emails.size
    178:     logon_user = nil
    179:     logon_user = User.login(user.account, user.password)
    180:     assert_nil logon_user
    181:     assert_equal user_count, User.count
    182:   end

tst_login_bugzilla()

Shows: 1 User can logon using bugzilla authentication, on first logon an account is created 2 Second logon succesfull, account is not created 3 Wrong password cannot logon 4 User tries to switch validemail to Bugzilla authentication when using the same email -> TODO

[Source]

         # File test/unit/user_test.rb, line 238
    238:   def tst_login_bugzilla
    239:     # get password
    240:     pw = IO.readlines('S:/Keys/epfwiki_bugzilla_authentication_key')[0]
    241:     # 1
    242:     ENV['EPFWIKI_DOMAINS'] = ENV['EPFWIKI_DOMAINS'] + ' @logicacmg.com'
    243:     user_count = User.count
    244:     user = User.new(:email => 'onno.van.der.straaten@logicacmg.com', :password => pw)
    245:     logon_user = User.login(user.email, user.password)
    246:     assert_equal user_count + 1, User.count # TODO fails
    247:     # 2
    248:     logon_user = User.login(user.email, user.password)
    249:     assert_not_nil logon_user
    250:     assert_equal user_count + 1, User.count
    251:     # 3
    252:     logon_user = User.login(user.email, user.password + 'xyz')
    253:     assert_nil logon_user
    254:     assert_equal user_count + 1, User.count
    255:     # 4
    256:   end

[Validate]