-
Notifications
You must be signed in to change notification settings - Fork 482
/
user_test.rb
1861 lines (1507 loc) · 61.4 KB
/
user_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
require 'test_helper'
class UserTest < ActiveSupport::TestCase
setup do
@good_data = {email: 'foo@bar.com', password: 'foosbars', name: 'tester', user_type: User::TYPE_STUDENT, age: 28}
@good_data_young = {email: 'foo@bar.com', password: 'foosbars', name: 'tester', user_type: User::TYPE_STUDENT, age: 8}
end
test 'make_teachers_21' do
teacher = create :teacher, birthday: Time.now - 18.years
assert_equal '21+', teacher.age
end
# Disable this test if and when we do require teachers to complete school data
test 'school info should not be validated' do
school_attributes = {
country: 'US',
school_type: SchoolInfo::SCHOOL_TYPE_PUBLIC,
state: nil
}
assert_creates(User) do
create :teacher, school_info_attributes: school_attributes
end
end
test 'ensure school info values are saved correctly when state and zip are passed in different ways' do
# state and zip fields are usually passed as school_state and school_zip, but should
# be accepted both ways when preprocessed
school_attributes = {
country: 'US',
school_type: SchoolInfo::SCHOOL_TYPE_PUBLIC,
school_state: 'CA',
school_zip: '94107'
}
teacher = create :teacher, school_info_attributes: school_attributes
assert teacher.school_info.state == 'CA', teacher.school_info.state
assert teacher.school_info.zip == 94107, teacher.school_info.zip
school_attributes = {
country: 'US',
school_type: SchoolInfo::SCHOOL_TYPE_PUBLIC,
state: 'CA',
zip: '94107'
}
teacher = create :teacher, school_info_attributes: school_attributes
assert teacher.school_info.state == 'CA', teacher.school_info.state
assert teacher.school_info.zip == 94107, teacher.school_info.zip
end
test 'identical school info should not be duplicated in the database' do
school_attributes = {
country: 'US',
school_type: SchoolInfo::SCHOOL_TYPE_PUBLIC,
state: 'CA'
}
teachers = create_list(:teacher, 2, school_info_attributes: school_attributes)
attr = teachers[0].process_school_info_attributes(school_attributes)
school_info = SchoolInfo.where(attr).first
assert teachers[0].school_info == school_info, "Teacher info: #{teachers[0].school_info.inspect} not equal to #{school_info.inspect}"
assert teachers[1].school_info == school_info, "Teacher info: #{teachers[1].school_info.inspect} not equal to #{school_info.inspect}"
assert SchoolInfo.where(attr).count == 1
end
test 'normalize_email' do
teacher = create :teacher, email: 'CAPS@EXAMPLE.COM'
assert_equal 'caps@example.com', teacher.email
end
test 'hash_email' do
teacher = create :teacher
teacher.update!(email: 'hash_email@example.com')
assert_equal User.hash_email('hash_email@example.com'),
teacher.hashed_email
end
test "log in with password with pepper" do
assert Devise.pepper
user = User.create! @good_data
# if password is already peppered we don't need to change the hashed pw
assert_no_change('user.reload.encrypted_password') do
assert user.valid_password?("foosbars")
refute user.valid_password?("foosbarsasdasds")
end
end
test "logging in with password created without pepper saves new password" do
a_pepper = "x" * 30
Devise.stubs(:pepper).returns(nil)
# create the user without the pepper
user = User.create! @good_data
Devise.stubs(:pepper).returns(a_pepper)
# update pw with new hashed pw
assert_change('user.reload.encrypted_password') do
assert user.valid_password?("foosbars")
refute user.valid_password?("foosbarsasdasds")
end
# doesn't change second time
assert_no_change('user.reload.encrypted_password') do
assert user.valid_password?("foosbars")
refute user.valid_password?("foosbarsasdasds")
end
end
test "cannot create user with panda in name" do
user = User.create(@good_data.merge({name: panda_panda}))
refute user.valid?
assert user.errors[:name].length == 1
end
test "cannot create user with panda in email" do
user = User.create(@good_data.merge({email: "#{panda_panda}@panda.com"}))
refute user.valid?
assert user.errors[:email].length == 1
end
test "cannot create user with invalid email" do
user = User.create(@good_data.merge({email: 'foo@bar@com'}))
refute user.valid?
assert user.errors[:email].length == 1
end
test "cannot create user with no type" do
user = User.create(@good_data.merge(user_type: nil))
refute user.valid?
assert user.errors[:user_type].length == 1
end
test "cannot create user with no name" do
user = User.create(@good_data.merge(name: nil))
refute user.valid?
assert user.errors[:name].length == 1
end
test "cannot create user with invalid type" do
user = User.create(@good_data.merge(user_type: 'xxxxx'))
refute user.valid?
assert user.errors[:user_type].length == 1
end
test "cannot create user with duplicate email" do
# actually create a user
User.create!(@good_data)
# Now create second user
user = User.create(@good_data)
assert_equal ['Email has already been taken'], user.errors.full_messages
# Now create second user with duplicate email with different case
user = User.create(@good_data.merge(email: @good_data[:email].upcase))
assert_equal ['Email has already been taken'], user.errors.full_messages
end
test "cannot create young user with duplicate email" do
# actually create a user
User.create!(@good_data_young)
# Now create second user
user = User.create(@good_data_young.merge(hashed_email: User.hash_email(@good_data_young[:email])))
assert_equal ['Email has already been taken'], user.errors.full_messages
# Now create second user with duplicate username with different case
user = User.create(@good_data_young.merge(hashed_email: User.hash_email(@good_data_young[:email].upcase)))
assert_equal ['Email has already been taken'], user.errors.full_messages
end
test "can create a user with age" do
Timecop.travel Time.local(2013, 9, 1, 12, 0, 0) do
assert_creates(User) do
user = User.create(@good_data.merge({age: '7', email: 'new@email.com'}))
assert_equal Date.new(Date.today.year - 7, Date.today.month, Date.today.day), user.birthday
assert_equal 7, user.age
end
end
end
test "can create a user with age 21+" do
Timecop.travel Time.local(2013, 9, 1, 12, 0, 0) do
assert_creates(User) do
user = User.create(@good_data.merge({age: '21+', email: 'new@email.com'}))
assert_equal Date.new(Date.today.year - 21, Date.today.month, Date.today.day), user.birthday
assert_equal "21+", user.age
end
end
end
test "cannot create a user with age that's not a number" do
assert_does_not_create(User) do
user = User.create(@good_data.merge({age: 'old', email: 'new@email.com'}))
assert_equal ["Age is not included in the list"], user.errors.full_messages
# we don't care about this error message that much because users
# should not be able to select -1 (they have a dropdown from
# 4..100)
end
end
test "cannot create a user with negative age" do
assert_does_not_create(User) do
user = User.create(@good_data.merge({age: -15, email: 'new@email.com'}))
assert_equal ["Age is not included in the list"], user.errors.full_messages
# we don't care about this error message that much because users
# should not be able to select -1 (they have a dropdown from
# 4..100)
end
end
test "cannot create a user with too large age" do
assert_does_not_create(User) do
user = User.create(@good_data.merge({age: 15_000_000, email: 'new@email.com'}))
assert_equal ["Age is not included in the list"], user.errors.full_messages
# we don't care about this error message that much because users
# should not be able to select -1 (they have a dropdown from
# 4..100)
end
end
test "can update a user with age" do
Timecop.travel Time.local(2013, 9, 1, 12, 0, 0) do
user = User.create(@good_data.merge({age: '7', email: 'new@email.com'}))
assert_equal 7, user.age
user.update_attributes(age: '9')
assert_equal Date.new(Date.today.year - 9, Date.today.month, Date.today.day), user.birthday
assert_equal 9, user.age
end
end
test "can save a user with age" do
user = create :user, age: 10
user.update_attribute(:birthday, nil) # hacky
user = User.find(user.id)
user.age = 24
assert_equal "21+", user.age
user.save!
user.reload
assert_equal "21+", user.age
end
test "corrects age when saving a user with invalid age" do
user = create :user, age: 10
user.update_attribute(:birthday, Time.now - 1.month) # hacky
user = User.find(user.id)
user.age = 24
assert_equal "21+", user.age
user.save!
user.reload
assert_equal "21+", user.age
end
test "does not update birthday if age is the same" do
user = User.create(@good_data.merge({age: '7', email: 'new@email.com'}))
assert_equal 7, user.age
Timecop.freeze(Date.today + 40) do
assert_no_difference('user.reload.birthday') do
user.update_attributes(age: '7')
end
assert_equal 7, user.age
end
end
test "can create user without email" do
assert_creates(User) do
User.create!(user_type: User::TYPE_STUDENT, name: 'Student without email', password: 'xxxxxxxx', provider: 'manual', age: 12)
end
end
test "cannot create self-managed user without email or hashed email" do
assert_does_not_create(User) do
User.create(user_type: User::TYPE_STUDENT, name: 'Student without email', password: 'xxxxxxxx', hashed_email: '', email: '', age: 12)
end
end
test "cannot create teacher without email" do
assert_does_not_create(User) do
User.create(user_type: User::TYPE_TEACHER, name: 'Bad Teacher', password: 'xxxxxxxx', provider: 'manual')
end
end
test "cannot make an account without email a teacher" do
user = User.create(user_type: User::TYPE_STUDENT, name: 'Student without email', password: 'xxxxxxxx', provider: 'manual')
user.user_type = User::TYPE_TEACHER
refute user.save
end
test "cannot make an account without email an admin" do
user = User.create(user_type: User::TYPE_STUDENT, name: 'Student without email', password: 'xxxxxxxx', provider: 'manual')
user.admin = true
refute user.save
end
test "cannot create admin without email" do
assert_does_not_create(User) do
User.create(user_type: User::TYPE_STUDENT, admin: true, name: 'Wannabe admin', password: 'xxxxxxxx', provider: 'manual')
end
end
test "gallery" do
user = create(:user)
assert_equal [], user.gallery_activities
assert_does_not_create(GalleryActivity) do
create(:user_level, user: user)
end
ga2 = nil
assert_creates(GalleryActivity) do
user_level2 = create(:user_level, user: user)
ga2 = GalleryActivity.create!(
user: user,
user_level: user_level2
)
end
assert_does_not_create(GalleryActivity) do
create(:user_level, user: user)
end
ga4 = nil
assert_creates(GalleryActivity) do
user_level4 = create(:user_level, user: user)
ga4 = GalleryActivity.create!(
user: user,
user_level: user_level4
)
end
assert_equal [ga4, ga2], user.reload.gallery_activities
end
test "short name" do
assert_equal 'Laurel', create(:user, name: 'Laurel Fan').short_name # first name last name
assert_equal 'Winnie', create(:user, name: 'Winnie the Pooh').short_name # middle name
assert_equal "D'Andre", create(:user, name: "D'Andre Means").short_name # punctuation ok
assert_equal '樊瑞', create(:user, name: '樊瑞').short_name # ok, this isn't actually right but ok for now
assert_equal 'Laurel', create(:user, name: 'Laurel').short_name # just one name
assert_equal 'some', create(:user, name: ' some whitespace in front ').short_name # whitespace in front
end
test "initial" do
assert_equal 'L', create(:user, name: 'Laurel Fan').initial # first name last name
assert_equal 'W', create(:user, name: 'Winnie the Pooh').initial # middle name
assert_equal "D", create(:user, name: "D'Andre Means").initial # punctuation ok
assert_equal '樊', create(:user, name: '樊瑞').initial # ok, this isn't actually right but ok for now
assert_equal 'L', create(:user, name: 'Laurel').initial # just one name
assert_equal 'S', create(:user, name: ' some whitespace in front ').initial # whitespace in front
end
test "find_for_authentication with nonsense" do
# login by username still works
user = create :user
assert_equal user, User.find_for_authentication(login: user.username)
# login by email still works
email_user = create :user, email: 'not@an.email'
assert_equal email_user, User.find_for_authentication(login: 'not@an.email')
# login by hashed email
hashed_email_user = create :user, age: 4
assert_equal hashed_email_user,
User.find_for_authentication(login: '', hashed_email: hashed_email_user.hashed_email)
# wat you can't do that hax0rs
assert_nil User.find_for_authentication(email: {'$acunetix' => 1})
# this used to raise a mysql error, now we sanitize it into a nonsense string
end
test "creating manual provider user without username generates username" do
user = User.create(@good_data.merge({provider: User::PROVIDER_MANUAL}))
assert_equal 'tester', user.username
end
test 'can get next_unpassed_progression_level if not completed any unplugged levels' do
user = create :user
twenty_hour = Script.twenty_hour_script
twenty_hour.script_levels.each do |script_level|
next if script_level.level.game.unplugged? # skip all unplugged
next if script_level.chapter > 33
UserLevel.create(
user: user,
level: script_level.level,
script: twenty_hour,
attempts: 1,
best_result: Activity::MINIMUM_PASS_RESULT
)
end
assert_equal(35, user.next_unpassed_progression_level(twenty_hour).chapter)
end
test 'can get next_unpassed_progression_level, not tainted by other user progress' do
user = create :user
other_user = create :user
twenty_hour = Script.twenty_hour_script
twenty_hour.script_levels.each do |script_level|
next if script_level.chapter > 33
UserLevel.create(
user: other_user,
level: script_level.level,
script: twenty_hour,
attempts: 1,
best_result: Activity::MINIMUM_PASS_RESULT
)
end
assert_equal(2, user.next_unpassed_progression_level(twenty_hour).chapter)
end
test 'can get next_unpassed_progression_level when most recent level is not passed' do
user = create :user
twenty_hour = Script.twenty_hour_script
twenty_hour.script_levels.each do |script_level|
next if script_level.chapter != 3
UserLevel.create(
user: user,
level: script_level.level,
script: twenty_hour,
attempts: 1,
best_result: Activity::MINIMUM_FINISHED_RESULT
)
end
# The level we most recently had progress on we did not pass, so that's
# where we should go
assert_equal(3, user.next_unpassed_progression_level(twenty_hour).chapter)
end
test 'can get next_unpassed_progression_level when most recent level is last level' do
user = create :user
twenty_hour = Script.twenty_hour_script
script_level = twenty_hour.script_levels.last
UserLevel.create(
user: user,
level: script_level.level,
script: twenty_hour,
attempts: 1,
best_result: Activity::MINIMUM_PASS_RESULT
)
# User's most recent progress is on last level in script. There's nothing
# following it, so just return to the last level
assert_equal(script_level.chapter, user.next_unpassed_progression_level(twenty_hour).chapter)
end
test 'can get next_unpassed_progression_level when most recent level is only followed by unplugged levels' do
user = create :user
script = create :script
script_levels = [
create(:script_level, script: script, levels: [create(:maze)]),
create(:script_level, script: script, levels: [create(:maze)]),
create(:script_level, script: script, levels: [create(:unplugged)]),
]
create :user_script, user: user, script: script
UserLevel.create(
user: user,
level: script_levels[1].level,
script: script,
attempts: 1,
best_result: Activity::MINIMUM_PASS_RESULT
)
# User's most recent progress is on second last level of script, but none of
# the levels after it are "progression" levels. Just return to the last level
# we made progress on.
assert_equal(2, user.next_unpassed_progression_level(script).chapter)
end
test 'can get next_unpassed_progression_level when most recent level not a progression level' do
user = create :user
script = create :script
script_levels = [
create(:script_level, script: script, levels: [create(:maze)]),
create(:script_level, script: script, levels: [create(:unplugged)]),
create(:script_level, script: script, levels: [create(:unplugged)]),
create(:script_level, script: script, levels: [create(:maze)]),
]
create :user_script, user: user, script: script
UserLevel.create(
user: user,
level: script_levels[1].level,
script: script,
attempts: 1,
best_result: Activity::MINIMUM_PASS_RESULT
)
# User's most recent progress is on unplugged level, that is followed by another
# unplugged level. We should end up at the first non unplugged level
assert_equal(4, user.next_unpassed_progression_level(script).chapter)
end
test 'can get next_unpassed_progression_level when we have no progress' do
user = create :user
script = create :script
create(:script_level, script: script, levels: [create(:maze)])
create(:script_level, script: script, levels: [create(:maze)])
create :user_script, user: user, script: script
# User's most recent progress is on unplugged level, that is followed by another
# unplugged level. We should end up at the first non unplugged level
assert_equal(1, user.next_unpassed_progression_level(script).chapter)
end
test 'script with inactive level completed is completed' do
user = create :user
level = create :maze, name: 'maze 1'
level2 = create :maze, name: 'maze 2'
script = create :script
script_level = create(
:script_level,
script: script,
levels: [level, level2],
properties: '{"maze 2": {"active": false}}'
)
create :user_script, user: user, script: script
UserLevel.create(
user: user, level: script_level.levels[1],
script: script,
attempts: 1,
best_result: Activity::MINIMUM_PASS_RESULT
)
assert user.completed?(script)
end
test 'script with active level completed is completed' do
user = create :user
level = create :maze, name: 'maze 1'
level2 = create :maze, name: 'maze 2'
script = create :script
script_level = create(
:script_level,
script: script,
levels: [level, level2],
properties: '{"maze 2": {"active": false}}'
)
create :user_script, user: user, script: script
UserLevel.create(
user: user,
level: script_level.levels[0],
script: script,
attempts: 1,
best_result: Activity::MINIMUM_PASS_RESULT
)
assert user.completed?(script)
end
test 'user is created with secret picture and word' do
user = create :user
assert user.secret_picture
assert user.secret_words
assert user.secret_words !~ /SecretWord/ # using the actual word not the object to_s
end
test 'students have hashed email not plaintext email' do
student = create :student, email: 'will_be_hashed@email.xx'
assert student.email.blank?
assert student.hashed_email.present?
end
test 'teachers have hashed email and plaintext email' do
teacher = create :teacher, email: 'email@email.xx'
assert teacher.email.present?
assert teacher.hashed_email.present?
end
test 'cannot create duplicate hashed and plaintext email' do
birthday_4 = Date.new(2010, 10, 4)
birthday_20 = Date.new(1994, 10, 4)
# create the younger user first
email1 = 'email1@email.xx'
create :user, birthday: birthday_4, email: email1
assert_does_not_create(User) do
# cannot create an older user with duplicate email
user = User.create @good_data.merge(birthday: birthday_20, email: email1)
assert_equal ['Email has already been taken'], user.errors.full_messages
end
assert_does_not_create(User) do
# cannot create a younger user with duplicate email
user = User.create @good_data.merge(birthday: birthday_4, email: email1)
assert_equal ['Email has already been taken'], user.errors.full_messages
end
# create the older user first
email2 = 'email2@email.xx'
create :user, birthday: birthday_20, email: email2
assert_does_not_create(User) do
# cannot create an older user with duplicate email
user = User.create @good_data.merge(birthday: birthday_20, email: email2)
assert_equal ['Email has already been taken'], user.errors.full_messages
end
assert_does_not_create(User) do
# cannot create a younger user with duplicate email
user = User.create @good_data.merge(birthday: birthday_4, email: email2)
assert_equal ['Email has already been taken'], user.errors.full_messages
end
assert_does_not_create(User) do
# cannot create a younger user with duplicate email
user = User.create @good_data.merge(birthday: birthday_4, email: '', hashed_email: Digest::MD5.hexdigest(email2))
assert_equal ['Email has already been taken'], user.errors.full_messages
end
end
test 'changing user from teacher to student removes email' do
user = create :teacher
assert user.email.present?
assert user.hashed_email.present?
user.user_type = User::TYPE_STUDENT
user.save!
assert user.email.blank?
assert user.hashed_email.present?
end
test 'changing user from teacher to student removes full_address' do
user = create :teacher
user.update(full_address: 'fake address')
user.user_type = User::TYPE_STUDENT
user.save!
assert user.full_address.nil?
end
test 'changing user from teacher to student removed unconfirmed_email' do
user = create :teacher
user.update(email: 'unconfirmed_email@example.com')
assert user.unconfirmed_email.present?
user.update(user_type: User::TYPE_STUDENT)
assert_nil user.unconfirmed_email
end
test 'changing user from student to teacher saves email' do
user = create :student, email: 'email@old.xx'
assert user.email.blank?
assert user.hashed_email
user.update_attributes(user_type: User::TYPE_TEACHER, email: 'email@old.xx')
user.save!
assert_equal 'email@old.xx', user.email
assert_equal '21+', user.age
end
test 'sanitize_race_data sanitizes closed_dialog' do
user = create :student
user.update!(races: %w(white closed_dialog))
assert_equal %w(closed_dialog), user.reload.races
end
test 'sanitize_race_data sanitizes too many races' do
user = create :student
user.update!(races: %w(white black hispanic asian american_indian hawaiian))
assert_equal %w(nonsense), user.reload.races
end
test 'sanitize_race_data sanitizes non-races' do
user = create :student
user.update!(races: %w(not_a_race white))
assert_equal %w(nonsense), user.reload.races
end
test 'sanitize_race_data noops valid responses' do
user = create :student
user.update!(races: %w(black hispanic))
assert_equal %w(black hispanic), user.reload.races
end
test 'under 13' do
user = create :user
refute user.under_13?
user.age = 13
refute user.under_13?
user.save!
refute user.under_13?
user.age = 10
assert user.under_13?
user.save!
assert user.under_13?
user = create :user
user.update_attribute(:birthday, nil) # cheating...
user = user.reload
assert user.age.nil?
assert user.under_13?
end
test "no send reset password for blank email" do
error_user = User.send_reset_password_instructions(email: '')
assert error_user.errors[:email]
assert ActionMailer::Base.deliveries.empty?
end
test "no send reset password for empty email" do
error_user = User.send_reset_password_instructions(email: nil)
assert error_user.errors[:email]
assert ActionMailer::Base.deliveries.empty?
end
test 'send reset password for student' do
email = 'email@email.xx'
student = create :student, password: 'oldone', email: email
assert User.send_reset_password_instructions(email: email)
mail = ActionMailer::Base.deliveries.first
assert_equal [email], mail.to
assert_equal 'Code.org reset password instructions', mail.subject
student = User.find(student.id)
old_password = student.encrypted_password
assert mail.body.to_s =~ /reset_password_token=(.+)"/
# HACK: Fix my syntax highlighting "
token = $1
User.reset_password_by_token(
reset_password_token: token,
password: 'newone',
password_confirmation: 'newone'
)
student = User.find(student.id)
# password was changed
assert old_password != student.encrypted_password
end
test 'send reset password for student without age' do
email = 'email@email.xx'
student = create :student, age: 10, email: email
student.update_attribute(:birthday, nil) # hacky
student = User.find(student.id)
refute student.age
User.send_reset_password_instructions(email: email)
mail = ActionMailer::Base.deliveries.first
assert_equal [email], mail.to
assert_equal 'Code.org reset password instructions', mail.subject
student = student.reload
refute student.age
assert student.reset_password_token
end
test 'actually reset password for student without age' do
email = 'email@email.xx'
student = create :student, age: 10, email: email
student.update_attribute(:birthday, nil) # hacky
student = User.find(student.id)
refute student.age
old_password = student.encrypted_password
student.reset_password('goodpassword', 'goodpassword')
# changed password
assert student.reload.encrypted_password != old_password
end
test 'user in_progress_and_completed_scripts does not include deleted scripts' do
user = create :user
real_script = Script.starwars_script
fake_script = create :script
user_script_1 = create :user_script, user: user, script: real_script
user_script_2 = create :user_script, user: user, script: fake_script
fake_script.destroy!
# Preconditions for test: The script is gone, but the associated UserScript still exists.
# If we start failing this setup assertion (that is, we do automated cleanup
# when deleting a script) then we can probably delete this test.
refute Script.exists?(fake_script.id), "Precondition for test: Expected Script #{fake_script.id} to be deleted."
assert UserScript.exists?(user_script_2.id), "Precondition for test: Expected UserScript #{user_script_2.id} to still exist."
# Test: We only get back the userscript for the script that still exists
scripts = user.in_progress_and_completed_scripts
assert_equal scripts.size, 1
assert scripts.include?(user_script_1)
end
test 'user is working on script' do
user = create :user
s1 = create :user_script, user: user, started_at: (Time.now - 10.days), last_progress_at: (Time.now - 4.days)
assert user.working_on?(s1.script)
end
test 'user is working on scripts' do
user = create :user
s1 = create :user_script, user: user, started_at: (Time.now - 10.days), last_progress_at: (Time.now - 4.days)
s2 = create :user_script, user: user, started_at: (Time.now - 50.days), last_progress_at: (Time.now - 3.days)
c = create :user_script, user: user, started_at: (Time.now - 10.days), completed_at: (Time.now - 8.days)
# all scripts
assert_equal [s2, s1, c], user.user_scripts
assert_equal [s2.script, s1.script, c.script], user.scripts
# working on scripts
assert_equal [s2.script, s1.script], user.working_on_scripts
# primary script -- most recently progressed in
assert_equal s2.script, user.primary_script
# add an assigned script that's more recent
a = create :user_script, user: user, started_at: (Time.now - 1.day)
assert_equal [a.script, s2.script, s1.script], user.working_on_scripts
assert_equal a.script, user.primary_script
# make progress on an older script
s1.update_attribute(:last_progress_at, Time.now - 3.hours)
assert_equal [s1.script, a.script, s2.script], user.working_on_scripts
assert_equal s1.script, user.primary_script
end
test 'user has completed script' do
user = create :user
s1 = create :user_script, user: user, started_at: (Time.now - 10.days), completed_at: (Time.now - 4.days)
assert user.completed?(s1.script)
end
test 'user has completed script but no completed_at' do
# We have some users in our system who have completed all levels but don't have completed_at set.
# This test exercises this case by not setting completed_at, but because the script has no levels there
# is no next level for the user to go to, and so completed? succeeds using a fallback code path.
user = create :user
s1 = create :user_script, user: user, started_at: (Time.now - 10.days), last_progress_at: (Time.now - 4.days)
assert s1.completed_at.nil?
assert user.completed?(s1.script)
end
test 'user should prefer working on 20hour instead of hoc' do
user = create :user
start_date = Time.now - 3.months
twenty_hour = Script.twenty_hour_script
hoc = Script.find_by(name: 'hourofcode')
# do a level that is both in script 1 and hoc
[twenty_hour, hoc].each do |script|
UserLevel.create!(
user_id: user.id,
level_id: Script.twenty_hour_script.script_levels[1].level.id,
script: script,
created_at: start_date,
updated_at: start_date
)
end
user.backfill_user_scripts([twenty_hour, hoc])
assert_equal [twenty_hour, hoc], user.working_on_scripts
end
test "user scripts backfills started_at and completed_at" do
begin
start_date = Time.now - 15.days
progress_date = Time.now - 4.days
user = create(:student)
script = Script.find_by_name("course2")
sl1 = script.script_levels[1]
sl2 = script.script_levels[5]
UserLevel.record_timestamps = false # ooh
UserLevel.create!(
user_id: user.id,
level_id: sl1.level.id,
script: script,
created_at: start_date,
updated_at: start_date
)
UserLevel.create!(
user_id: user.id,
level_id: sl2.level.id,
script: script,
created_at: progress_date,
updated_at: progress_date
)
assert_creates(UserScript) do
user.backfill_user_scripts([script])
end
user_script = UserScript.last
assert_equal start_date.to_i, user_script.started_at.to_i
assert_equal progress_date.to_i, user_script.last_progress_at.to_i
assert_nil user_script.assigned_at
assert_nil user_script.completed_at
ensure
UserLevel.record_timestamps = true
end
end
def complete_script_for_user(user, script, completed_date = Time.now)
# complete all except last level a day earlier
script.script_levels[0..-2].each do |sl|
UserLevel.create!(
user_id: user.id,
level_id: sl.level_id,
script: script,
best_result: 100,
created_at: completed_date - 1.day,
updated_at: completed_date - 1.day
)
end
# completed last level
sl = script.script_levels.last
UserLevel.create!(
user_id: user.id,
level_id: sl.level_id,
script: script,
best_result: 100,
created_at: completed_date,
updated_at: completed_date
)
end
test "backfill user_scripts backfills completed_at" do
begin
UserLevel.record_timestamps = false
completed_date = Time.now - 20.days
student = create :student
script = Script.find_by_name('playlab')
complete_script_for_user(student, script, completed_date)
assert_creates(UserScript) do
student.backfill_user_scripts([script])
end
user_script = UserScript.last
assert_equal completed_date.to_i - 1.day, user_script.started_at.to_i
assert_equal completed_date.to_i, user_script.last_progress_at.to_i
assert_nil user_script.assigned_at
assert_equal completed_date.to_i, user_script.completed_at.to_i
ensure
UserLevel.record_timestamps = true
end
end
test "backfill user_scripts does not backfill completed_at if last level not passed" do
begin
UserLevel.record_timestamps = false
completed_date = Time.now - 20.days
student = create :student
script = Script.find_by_name('playlab')
# complete the script
complete_script_for_user(student, script, completed_date)