def test_can_login_two_users_same_email(self): """ Assert that it's possible to login if two users have the same email on different channels. """ Channel.objects.create(name="Test", slug="test-channel") user1 = User.objects.create(email="*****@*****.**", password="******", object_channel="default") user2 = User.objects.create(email="*****@*****.**", password="******", object_channel="test-channel") response = authenticate() token = response.data['access_token'] response = APIClient().get( reverse('user-current-user'), format="json", HTTP_AUTHORIZATION="Bearer {}".format(token)) self.assertEqual(response.data["uuid"], str(user1.uuid)) response = authenticate(headers={"HTTP_X_OVP_CHANNEL": "test-channel"}) token = response.data['access_token'] response = APIClient().get( reverse('user-current-user'), format="json", HTTP_AUTHORIZATION="Bearer {}".format(token), HTTP_X_OVP_CHANNEL="test-channel") self.assertEqual(response.data["uuid"], str(user2.uuid))
def test_can_recover_password(self): """Assert the user can recover his password with a valid token""" # request token user = create_user('*****@*****.**') mail.outbox = [] # clear outbox response = create_token('*****@*****.**') # get token from mailbox email_content = mail.outbox[0].alternatives[0][0] token = re.search( '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}', email_content).group(0) # recover password data = { 'email': '*****@*****.**', 'token': token, 'new_password': '******' } client = APIClient() response = client.post(reverse('recover-password-list'), data, format="json") self.assertTrue(response.data['message'] == 'Password updated.') # Test authentication new password auth = authenticate('*****@*****.**', 'newpwvalidpw*') self.assertTrue(auth.data['access_token'] != None)
def test_cant_login_wrong_password(self): """ Assert that it's not possible to login with wrong password """ user = create_user('*****@*****.**', 'invalidpassword') response = authenticate() self.assertTrue( response.data == { "error": "invalid_grant", "error_description": "Invalid credentials given." })
def test_can_patch_password(self): """Assert that it's possible to update user password""" response = create_user('*****@*****.**', 'abcabcabc') u = models.User.objects.get(uuid=response.data['uuid']) data = {'password': '******', 'current_password': '******'} client = APIClient() client.force_authenticate(user=u) response = client.patch(reverse('user-current-user'), data, format="json") self.assertTrue(response.status_code == 200) self.assertTrue("password" not in response.data) response = authenticate('*****@*****.**', data['password']) self.assertTrue(response.data['access_token'] != None)
def test_can_login(self): """ Assert that it's possible to login """ user = create_user('*****@*****.**', 'validpassword') response = authenticate() self.assertTrue(response.data['access_token'] != None)