def test_authenticate_creates_user_object(self): """ If `authenticate` is called with valid credentials then a User object should be created """ User = get_user_model() self.assertEqual(User.objects.count(), 0) google_user = users.User('*****@*****.**', _user_id='111111111100000000001') backend = AppEngineUserAPI() user = backend.authenticate(google_user=google_user,) self.assertEqual(user.email, '*****@*****.**') self.assertEqual(User.objects.count(), 1) # Calling authenticate again with the same credentials should not create another user user2 = backend.authenticate(google_user=google_user) self.assertEqual(user.pk, user2.pk)
def test_force_user_pre_creation(self): User = get_user_model() self.assertEqual(User.objects.count(), 0) google_user = users.User('*****@*****.**', _user_id='111111111100000000001') backend = AppEngineUserAPI() self.assertIsNone(backend.authenticate(google_user=google_user,)) self.assertEqual(User.objects.count(), 0) # superusers don't need pre-creation of User object. self.assertEqual(User.objects.count(), 0) with sleuth.switch('google.appengine.api.users.is_current_user_admin', lambda: True): user = backend.authenticate(google_user=google_user,) self.assertEqual(User.objects.count(), 1) self.assertEquals(User.objects.get(), user)
def test_user_id_switch(self): """ Users sometimes login with the same email, but a different google user id. We handle those cases by blanking out the email on the old user object and creating a new one with the new user id. """ email = '*****@*****.**' old_user = users.User(email=email, _user_id='111111111100000000001') new_user = users.User(email=email, _user_id='111111111100000000002') User = get_user_model() backend = AppEngineUserAPI() # Authenticate 1st time, creating the user user1 = backend.authenticate(google_user=old_user) self.assertEqual(user1.email, email) self.assertTrue(user1.username.endswith('1')) self.assertEqual(1, User.objects.count()) # Now another user logs in using the same email user2 = backend.authenticate(google_user=new_user) self.assertEqual(user2.email, email) self.assertTrue(user2.username.endswith('2')) self.assertEqual(2, User.objects.count()) # The old account is kept around, but the email is blanked user1 = User.objects.get(pk=user1.pk) self.assertEqual(user1.email, None)
def test_user_pre_creation(self): """ User objects for Google-Accounts-based users should be able to be pre-created in DB and then matched by email address when they log in. """ User = get_user_model() backend = AppEngineUserAPI() email = '*****@*****.**' # Pre-create our user User.objects.pre_create_google_user(email) # Now authenticate this user via the Google Accounts API google_user = users.User(email=email, _user_id='111111111100000000001') user = backend.authenticate(google_user=google_user) # Check things self.assertEqual(user.email, email) self.assertIsNotNone(user.last_login) self.assertFalse(user.has_usable_password())