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

Methods

Public Instance methods

[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

[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

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

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

[Source]

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

[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

Shows:

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

[Source]

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

[Source]

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

[Source]

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

[Source]

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

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

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

[Validate]