forked from PerceptumNL/KhanLatest
/
user_models_test.py
595 lines (471 loc) · 23.4 KB
/
user_models_test.py
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
from __future__ import with_statement
import datetime
import mock
from google.appengine.ext import db
import custom_exceptions
import coaches
import exercise_models
import phantom_users.phantom_util
from testutil import gae_model
from testutil import mock_datetime
from coach_resources.coach_request_model import CoachRequest
from user_models import Capabilities, UserData, UniqueUsername, ParentChildPair
from user_models import _USER_KEY_PREFIX
from testutil import testsize
import setting_model
class UserDataCoachTest(gae_model.GAEModelTestCase):
def setUp(self):
super(UserDataCoachTest, self).setUp(db_consistency_probability=1)
def make_user(self, email):
return UserData.insert_for(email, email)
def make_user_json(self, user, is_coaching):
return {
'email': user.key_email,
'isCoachingLoggedInUser': is_coaching,
}
def test_add_a_coach(self):
student = self.make_user('student@gmail.com')
coach = self.make_user('coach@gmail.com')
coaches_json = [self.make_user_json(coach, True)]
coaches.update_coaches(student, coaches_json)
self.assertEqual(1, len(student.coaches))
self.assertTrue(student.is_visible_to(coach))
self.assertTrue(coach.has_students())
def test_add_multiple_coaches(self):
bella = self.make_user('bella@gmail.com')
jacob = self.make_user('jacob@gmail.com')
edward = self.make_user('edward@gmail.com')
coaches_json = [self.make_user_json(coach, True) for coach in
[jacob, edward]]
coaches.update_coaches(bella, coaches_json)
self.assertEqual(2, len(bella.coaches))
self.assertTrue(bella.is_visible_to(jacob))
self.assertTrue(jacob.has_students())
self.assertTrue(bella.is_visible_to(edward))
self.assertTrue(edward.has_students())
def test_remove_coach(self):
bella = self.make_user('bella@gmail.com')
jacob = self.make_user('jacob@gmail.com')
jacob_json = [self.make_user_json(jacob, True)]
coaches.update_coaches(bella, jacob_json)
coaches.update_coaches(bella, [])
self.assertEqual(0, len(bella.coaches))
self.assertFalse(bella.is_visible_to(jacob))
self.assertFalse(jacob.has_students())
def test_return_no_requester_emails_on_update_coaches_when_coaching_logged_in_user(self):
bella = self.make_user('bella@gmail.com')
jacob = self.make_user('jacob@gmail.com')
jacob_json = [self.make_user_json(jacob, True)]
requester_emails = coaches.update_coaches(bella, jacob_json)
self.assertEqual([], requester_emails)
def test_return_requester_email_on_update_coaches_when_not_coaching_logged_in_user(self):
bella = self.make_user('bella@gmail.com')
jacob = self.make_user('jacob@gmail.com')
jacob_json = [self.make_user_json(jacob, False)]
requester_emails = coaches.update_coaches(bella, jacob_json)
self.assertEqual([jacob.key_email], requester_emails)
def test_raises_exception_on_add_nonexistent_coach(self):
bella = self.make_user('bella@gmail.com')
coaches_json = [{
'email': 'legolas@gmail.com',
'isCoachingLoggedInUser': True,
}]
self.assertRaises(custom_exceptions.InvalidEmailException,
coaches.update_coaches,
bella,
coaches_json)
def test_noop_on_update_requests_with_email(self):
bella = self.make_user('bella@gmail.com')
jacob = self.make_user('jacob@gmail.com')
CoachRequest.get_or_insert_for(jacob, bella, 'bella@gmail.com')
coaches.update_requests(bella, [jacob.key_email])
requests_for_bella = CoachRequest.get_for_student(bella).fetch(1000)
self.assertEqual(1, len(requests_for_bella))
requests_by_jacob = CoachRequest.get_for_coach(jacob).fetch(1000)
self.assertEqual(1, len(requests_by_jacob))
def test_requesting_by_username_doesnt_leak_email(self):
bella = self.make_user('bella@gmail.com')
self.assertTrue(bella.claim_username('bella'))
jacob = self.make_user('jacob@gmail.com')
# Request is made using username.
CoachRequest.get_or_insert_for(jacob, bella, 'bella')
requests_for_bella = CoachRequest.get_for_student(bella).fetch(1000)
self.assertEqual(1, len(requests_for_bella))
self.assertEqual(bella.username,
requests_for_bella[0].student_requested_identifier)
requests_by_jacob = CoachRequest.get_for_coach(jacob).fetch(1000)
self.assertEqual(1, len(requests_by_jacob))
self.assertEqual(bella.username,
requests_by_jacob[0].student_requested_identifier)
def test_clear_request_on_update_requests_with_no_email(self):
bella = self.make_user('bella@gmail.com')
edward = self.make_user('edward@gmail.com')
CoachRequest.get_or_insert_for(edward, bella, 'bella@gmail.com')
coaches.update_requests(bella, [])
requests_for_bella = CoachRequest.get_for_student(bella).fetch(1000)
self.assertEqual(0, len(requests_for_bella))
requests_by_edward = CoachRequest.get_for_coach(edward).fetch(1000)
self.assertEqual(0, len(requests_by_edward))
def test_noop_on_update_when_not_coaching_logged_in_user(self):
# Bella + Edward's daughter,
# (Spoiler Alert!) who Jacob falls in love with in Book 4
renesmee = self.make_user('renesmee@gmail.com')
jacob = self.make_user('jacob@gmail.com')
CoachRequest.get_or_insert_for(jacob, renesmee, 'renesmee@gmail.com')
requests_for_renesmee = CoachRequest.get_for_student(renesmee).fetch(1000)
self.assertEqual(1, len(requests_for_renesmee))
coaches_json = [self.make_user_json(jacob, False)]
coaches.update_coaches_and_requests(renesmee, coaches_json)
self.assertFalse(renesmee.is_visible_to(jacob))
requests_for_renesmee = CoachRequest.get_for_student(renesmee).fetch(1000)
self.assertEqual(1, len(requests_for_renesmee))
def test_accept_request_on_update_when_coaching_logged_in_user(self):
renesmee = self.make_user('renesmee@gmail.com')
jacob = self.make_user('jacob@gmail.com')
CoachRequest.get_or_insert_for(jacob, renesmee, 'renesmee@gmail.com')
coaches_json = [self.make_user_json(jacob, True)]
coaches.update_coaches_and_requests(renesmee, coaches_json)
self.assertTrue(renesmee.is_visible_to(jacob))
requests_for_renesmee = CoachRequest.get_for_student(renesmee).fetch(1000)
self.assertEqual(0, len(requests_for_renesmee))
def test_ignore_nonexistent_requester_email_on_update_requests(self):
renesmee = self.make_user('renesmee@gmail.com')
jacob = self.make_user('jacob@gmail.com')
CoachRequest.get_or_insert_for(jacob, renesmee, 'renesmee@gmail.com')
coaches_json = [{
'email': 'legolas@gmail.com',
'isCoachingLoggedInUser': False,
}]
coaches.update_requests(renesmee, coaches_json)
requests_for_renesmee = CoachRequest.get_for_student(renesmee).fetch(1000)
self.assertEqual(0, len(requests_for_renesmee))
class UsernameTest(gae_model.GAEModelTestCase):
def tearDown(self):
# Clear all usernames just to be safe
for u in UniqueUsername.all():
u.delete()
super(UsernameTest, self).tearDown()
def validate(self, username):
return UniqueUsername.is_valid_username(username)
def test_user_name_validates_length_requirement(self):
# This test verifies that the multiple places that verify a username's
# length are in sync.
for i in range(10):
candidate = 'a' * i
self.assertEquals(
not UniqueUsername.is_username_too_short(candidate),
self.validate(candidate))
def test_user_name_fuzzy_match(self):
""" Tests user name search can ignore periods properly. """
def k(n):
return UniqueUsername.build_key_name(n)
self.assertEqual(k('mr.pants'), k('mrpants'))
self.assertEqual(k('mr.pants...'), k('mrpants'))
self.assertEqual(k('mrpants'), k('mrpants'))
self.assertEqual(k('MrPants'), k('mrpants'))
def test_bad_user_name_fails_validation(self):
self.assertFalse(self.validate(''))
self.assertFalse(self.validate('a')) # Too short
self.assertFalse(self.validate('4score7yrs')) # Must start with letter
self.assertFalse(self.validate('.dotsarebadtoo'))
self.assertFalse(self.validate('!nvalid'))
self.assertFalse(self.validate('B@dCharacters'))
self.assertFalse(self.validate('I cannot read instructions'))
self.assertFalse(self.validate(u'h\u0400llojello')) # Cyrillic chars
self.assertFalse(self.validate('mrpants@khanacademy.org'))
def test_good_user_name_validates(self):
self.assertTrue(self.validate('poopybutt'))
self.assertTrue(self.validate('mrpants'))
self.assertTrue(self.validate('instructionsareeasy'))
self.assertTrue(self.validate('coolkid1983'))
def make_user(self, email):
u = UserData.insert_for(email, email)
u.put()
return u
def test_claiming_username_works(self):
u1 = self.make_user("bob")
u2 = self.make_user("robert")
# Free
self.assertTrue(u1.claim_username("superbob"))
self.assertEqual("superbob", u1.username)
# Now it's taken
self.assertFalse(u2.claim_username("superbob"))
# But something completely different should still be good
self.assertTrue(u2.claim_username("sadbob"))
self.assertEqual("sadbob", u2.username)
def test_releasing_usernames(self):
clock = mock_datetime.MockDatetime()
u1 = self.make_user("bob")
u2 = self.make_user("robert")
# u1 gets "superbob", but changes his mind.
self.assertTrue(u1.claim_username("superbob", clock))
self.assertEqual("superbob", u1.username)
self.assertTrue(u1.claim_username("ultrabob", clock))
self.assertEqual("ultrabob", u1.username)
# TOTAL HACK - for some reason without this read (which shouldn't
# actually have any side effect), the following assert fails because
# there's no strong consistency ensured on the HRD.
db.get([u1.key()])
self.assertEqual(
u1.user_id,
UserData.get_from_username("ultrabob").user_id)
self.assertEqual(
None,
UserData.get_from_username("superbob"))
# Usernames go into a holding pool, even after they're released
self.assertFalse(u2.claim_username("superbob", clock))
# Note that the original owner can't even have it back
self.assertFalse(u1.claim_username("superbob", clock))
# Still no good at the border of the holding period
clock.advance(UniqueUsername.HOLDING_PERIOD_DELTA)
self.assertFalse(u2.claim_username("superbob", clock))
# OK - now u2 can have it.
clock.advance_days(1)
self.assertTrue(u2.claim_username("superbob", clock))
self.assertEqual("superbob", u2.username)
db.get([u2.key()])
self.assertEqual(
u2.user_id,
UserData.get_from_username("superbob").user_id)
def test_usernames_dont_match_if_invalid(self):
self.assertFalse(UniqueUsername.matches(None, None))
self.assertFalse(UniqueUsername.matches("superbob", None))
self.assertFalse(UniqueUsername.matches("superbob", "i n v a l id"))
def test_username_matching(self):
self.assertTrue(UniqueUsername.matches("superbob", "super.bob"))
self.assertTrue(UniqueUsername.matches("superbob", "SuperBob"))
self.assertFalse(UniqueUsername.matches("superbob", "fakebob"))
def test_deleting_user_releases_username(self):
clock = mock_datetime.MockDatetime()
u1 = self.make_user("bob")
self.assertTrue(u1.claim_username("bob"))
self.assertFalse(UniqueUsername.is_available_username("bob",
clock=clock))
u1.delete(clock=clock)
# We don't do anything special to delete it immediately, so it's still
# in the holding period.
clock.advance(UniqueUsername.HOLDING_PERIOD_DELTA)
clock.advance(datetime.timedelta(1))
self.assertTrue(UniqueUsername.is_available_username("bob",
clock=clock))
class ProfileSegmentTest(gae_model.GAEModelTestCase):
def to_url(self, user):
return user.profile_root
def from_url(self, url):
# Profile URLs are of the form "/profile/segment"
return UserData.get_from_url_segment(url[9:-1])
def create_phantom(self):
user_id = phantom_users.phantom_util._create_phantom_user_id()
return UserData.insert_for(user_id, user_id)
def test_url_segment_generation(self):
# Pre-phantom users can't have profile URLs
prephantom = UserData.pre_phantom()
self.assertTrue(self.from_url(self.to_url(prephantom)) is None)
# Phantom users can't have profile URLs
phantom = self.create_phantom()
self.assertTrue(self.from_url(self.to_url(phantom)) is None)
# Normal users are cool, though.
bob = UserData.insert_for(
"http://googleid.khanacademy.org/1234",
"bob@gmail.com")
bob.put()
self.assertEqual(
self.from_url(self.to_url(bob)).user_id,
bob.user_id)
sally = UserData.insert_for(
"http://facebookid.khanacademy.org/1234",
"http://facebookid.khanacademy.org/1234")
sally.put()
self.assertEqual(
self.from_url(self.to_url(sally)).user_id,
sally.user_id)
def test_return_key_in_profile_root_for_users_without_username(self):
bob = UserData.insert_for(
"http://googleid.khanacademy.org/1234",
"bob@gmail.com")
desired_profile_root = ("/profile/" + _USER_KEY_PREFIX + str(bob.key())
+ "/")
self.assertEquals(desired_profile_root, bob.profile_root)
def test_return_username_in_profile_root_if_exists(self):
bob = UserData.insert_for(
"http://googleid.khanacademy.org/1234",
"bob@gmail.com")
username = "bobby"
bob.claim_username(username)
desired_profile_root = "/profile/" + username + "/"
self.assertEquals(desired_profile_root, bob.profile_root)
class UserDataCreationTest(gae_model.GAEModelTestCase):
def flush(self, items):
""" Ensures items are flushed in the HRD. """
db.get([item.key() for item in items if item])
def insert_user(self, user_id, email, username=None, password=None):
return UserData.insert_for(user_id, email, username, password)
def test_creation_without_username(self):
added = [
self.insert_user("larry", "email1@gmail.com"),
self.insert_user("curly", "email2@gmail.com"),
self.insert_user("moe", "email3@gmail.com"),
]
# We don't care about consistency policy issues - we just want proper
# counts and such.
self.flush(added)
self.assertEqual(3, UserData.all().count())
self.assertEqual(set(["larry", "curly", "moe"]),
set(user.user_id for user in UserData.all()))
# "Re-adding" moe doesn't duplicate.
self.flush([self.insert_user("moe", "email3@gmail.com")])
self.assertEqual(3, UserData.all().count())
def test_creation_with_bad_username(self):
self.assertTrue(self.insert_user("larry", "email1@gmail.com", "!!!!!")
is None)
def test_creation_with_existing_username(self):
self.flush([self.insert_user("larry", "email1@gmail.com", "larry")])
self.assertEqual(1, UserData.all().count())
self.assertEqual("larry", UserData.all()[0].user_id)
self.assertEqual("larry", UserData.all()[0].username)
self.assertTrue(self.insert_user("larry2", "tooslow@gmail.com",
"larry") is None)
@testsize.medium()
def test_creation_with_password(self):
self.flush([self.insert_user("larry",
"email1@gmail.com",
"larry",
"Password1")])
self.assertEqual(1, UserData.all().count())
retrieved = UserData.all()[0]
self.assertEqual("larry", retrieved.user_id)
self.assertTrue(retrieved.validate_password("Password1"))
self.assertFalse(retrieved.validate_password("Password2"))
class UserConsumptionTest(gae_model.GAEModelTestCase):
def make_exercise(self, name):
exercise = exercise_models.Exercise(name=name)
exercise.put()
return exercise
@testsize.medium()
def test_user_identity_consumption(self):
superman = UserData.insert_for(
"superman@gmail.krypt",
email="superman@gmail.krypt",
username="superman",
password="Password1",
gender="male",
)
clark = UserData.insert_for(
"clark@kent.com",
email="clark@kent.com",
username=None,
password=None,
)
clark.consume_identity(superman)
self.assertEqual("superman@gmail.krypt", clark.user_id)
self.assertEqual("superman@gmail.krypt", clark.email)
self.assertEqual(clark.key(),
UserData.get_from_username("superman").key())
self.assertEqual(clark.key(),
UserData.get_from_user_id("superman@gmail.krypt").key())
self.assertTrue(clark.validate_password("Password1"))
def test_user_exercise_preserved_after_consuming(self):
# A user goes on as a phantom...
phantom = UserData.insert_for("phantom", "phantom")
exercises = [
self.make_exercise("Adding 1"),
self.make_exercise("Multiplication yo"),
self.make_exercise("All about chickens"),
]
# Does some exercises....
for e in exercises:
ue = phantom.get_or_insert_exercise(e)
ue.total_done = 7
ue.put()
# Signs up!
jimmy = UserData.insert_for("justjoinedjimmy@gmail.com",
email="justjoinedjimmy@gmail.com")
phantom.consume_identity(jimmy)
# Make sure we can still see the old user exercises
shouldbejimmy = UserData.get_from_user_id("justjoinedjimmy@gmail.com")
user_exercises = (exercise_models.UserExercise.
get_for_user_data(shouldbejimmy).fetch(100))
self.assertEqual(len(exercises), len(user_exercises))
for ue in user_exercises:
self.assertEqual(7, ue.total_done)
class ParentChildPairTest(gae_model.GAEModelTestCase):
def make_user(self, email):
return UserData.insert_for(email, email)
def make_child_account(self, parent, username):
# TODO(benkomalo): figure out a way to stub out datetime.date.today
# nicely so this doesn't break in 2015
child = parent.spawn_child(username,
datetime.date(2012, 1, 1),
password="childpassword")
return child
def test_basic_child_creation(self):
parent = self.make_user("parent@gmail.com")
# TODO(benkomalo): figure out a way to stub out datetime.date.today
# nicely so this doesn't break in 2015
child = parent.spawn_child(username="childuser",
birthdate=datetime.date(2012, 1, 1),
password="childpassword")
self.assertTrue(child is not None)
self.assertTrue(child.is_child_account())
# Note that child accounts can't collect real names, so it falls back
# to username.
self.assertEquals("childuser", child.nickname)
self.assertEquals("childuser", child.username)
def test_basic_bond(self):
parent = self.make_user("parent@gmail.com")
child = self.make_child_account(parent, "child")
retrieved_bonds = ParentChildPair.get_for_parent(parent)
self.assertEquals(1, len(retrieved_bonds.fetch(1000)))
self.assertEquals(child.key(),
retrieved_bonds[0].resolve_child().key())
retrieved_parent_bond = ParentChildPair.get_for_child(child)
self.assertEquals(parent.key(),
retrieved_parent_bond.resolve_parent().key())
def test_invalid_bonds(self):
too_old = self.make_user("oops@gmail.com")
parent = self.make_user("parent@gmail.com")
with mock.patch("logging.error") as errorlog:
self.assertFalse(ParentChildPair.make_bond(parent, too_old))
self.assertEquals(1, errorlog.call_count)
def test_cant_have_multiple_parent_accounts(self):
parent = self.make_user("parent@gmail.com")
parent2 = self.make_user("parent2@gmail.com")
child = self.make_child_account(parent, "child")
with mock.patch("logging.error") as errorlog:
# Going to fail because the child already has a parent.
self.assertFalse(ParentChildPair.make_bond(parent2, child))
self.assertEquals(1, errorlog.call_count)
self.assertTrue(ParentChildPair.is_pair(parent, child))
self.assertFalse(ParentChildPair.is_pair(parent2, child))
def assertFullyCoached(self, child, parent):
self.assertTrue(child.is_coached_by(parent))
self.assertTrue(child.is_visible_to(parent))
self.assertTrue(parent.has_students())
self.assertTrue(any(student.key() == child.key()
for student in parent.get_students_data()))
self.assertTrue(any(coach.key() == parent.key()
for coach in child.get_coaches_data()))
def test_parent_is_automatic_coach(self):
parent = self.make_user("parent@gmail.com")
child = self.make_child_account(parent, "child")
self.assertFullyCoached(child, parent)
def test_ensure_parents_cant_be_removed_via_coach_interfaces(self):
parent = self.make_user("parent@gmail.com")
child = self.make_child_account(parent, "child")
child.set_can_modify_coaches()
coaches.update_coaches(child, []) # Remove all normal coaches
self.assertFullyCoached(child, parent)
class CapabilitiesTest(gae_model.GAEModelTestCase):
def test_basic_addition_removal(self):
capabilities = []
self.assertFalse(Capabilities._list_includes(capabilities, 'foo'))
capabilities = Capabilities._set_capability_in_list(
capabilities, 'foo', allow=True)
self.assertTrue(Capabilities._list_includes(capabilities, 'foo'))
# Adding is idempotent - subsequent calls shouldn't do much.
capabilities = Capabilities._set_capability_in_list(
capabilities, 'foo', allow=True)
self.assertTrue(Capabilities._list_includes(capabilities, 'foo'))
capabilities = Capabilities._set_capability_in_list(
capabilities, 'foo', allow=False)
self.assertFalse(Capabilities._list_includes(capabilities, 'foo'))