def test_signed_up_user_signin(self): with self.client: add_user(deepcopy(user_dict)) response = self.client.post('/auth/signin', data=json.dumps(user_dict), content_type='application/json') data = json.loads(response.data.decode()) self.assertTrue(data['message'] == 'Successfully signed in.') self.assertTrue(data['auth_token']) self.assertTrue(response.content_type == 'application/json') self.assertEqual(response.status_code, 200)
def test_valid_signout(self): add_user(deepcopy(user_dict)) with self.client: resp_login = self.client.post('/auth/signin', data=json.dumps(user_dict), content_type='application/json') token = json.loads(resp_login.data.decode())['auth_token'] response = self.client.get( '/auth/signout', headers={'Authorization': f'Bearer {token}'}) data = json.loads(response.data.decode()) self.assertTrue(data['message'] == 'Successfully signed out.') self.assertEqual(response.status_code, 200)
def test_get_all_users(self): """ Ensure get all users behaves correctly. """ add_user(deepcopy(user_dict)) add_user(deepcopy(user_dict2)) with self.client: response = self.client.get("/users") data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertEqual(len(data["users"]), 2) self.assertIn("*****@*****.**", data["users"][0]["email"]) self.assertIn("*****@*****.**", data["users"][1]["email"])
def test_invalid_signout_expired_token(self): add_user(deepcopy(user_dict)) with self.client: resp_login = self.client.post('/auth/signin', data=json.dumps(user_dict), content_type='application/json') time.sleep(4) token = json.loads(resp_login.data.decode())['auth_token'] response = self.client.get( '/auth/signout', headers={'Authorization': f'Bearer {token}'}) data = json.loads(response.data.decode()) self.assertTrue( data['message'] == 'Signature expired. Please sign in again.') self.assertEqual(response.status_code, 401)
def test_get_user(self): """ Ensure get single user behaves correctly. """ user = add_user(deepcopy(user_dict)) with self.client: response = self.client.get(f"/users/{user.id}") data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertIn(user.email, data["user"]["email"])
def seed_db(): """Seeds the database.""" add_simple_user(simple_user_dict) add_simple_user(simple_user_dict2) add_user(user_dict) add_user(user_dict2)
def test_decode_auth_token(self): user = add_user(deepcopy(user_dict)) auth_token = user.encode_auth_token(user.id) self.assertTrue(isinstance(auth_token, bytes)) self.assertEqual(User.decode_auth_token(auth_token), user.id)
def test_passwords_are_random(self): user1 = add_user(deepcopy(user_dict)) user2 = add_user(deepcopy(user_dict2)) self.assertNotEqual(user1.password_hash, user2.password_hash)
def test_to_dict(self): user = add_user(deepcopy(user_dict)) self.assertTrue(isinstance(user.to_dict(), dict))
def test_add_user_duplicate_email(self): user_copy = deepcopy(user_dict) add_user(user_copy) duplicate_user = User(**user_copy) db.session.add(duplicate_user) self.assertRaises(IntegrityError, db.session.commit)
def test_add_simple_user(self): user = add_user(deepcopy(user_dict)) self.assertTrue(user.id) self.assertEqual(user.email, '*****@*****.**') self.assertTrue(user.online)