def test_user_login_no_json(self): add_user() with self.client: response = log_in(self.client, {}) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) self.assertEqual(data["status"], "fail") self.assertEqual(data["message"], "Invalid payload")
def test_user_login_no_password(self): add_user() with self.client: response = log_in(self.client, {"email": "*****@*****.**"}) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) self.assertEqual(data["status"], "fail") self.assertEqual(data["message"], "Incorrect password")
def test_user_login_no_email(self): add_user() with self.client: response = log_in(self.client, {"password": "******"}) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 404) self.assertEqual(data["status"], "fail") self.assertEqual(data["message"], "User does not exist")
def test_registered_user_login(self): add_user() with self.client: response = log_in(self.client) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertEqual(response.content_type, "application/json") self.assertEqual(data["status"], "success") self.assertEqual(data["message"], "Successfully logged in") self.assertTrue(data["auth_token"])
def test_invalid_status_inactive(self): add_inactive_user() with self.client: resp_login = log_in(self.client) token = json.loads(resp_login.data.decode())["auth_token"] response = status(self.client, token) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 401) self.assertEqual(data["status"], "fail") self.assertEqual(data["message"], "Provide a valid auth token")
def test_valid_logout(self): add_user() with self.client: resp_login = log_in(self.client) token = json.loads(resp_login.data.decode())["auth_token"] response = logout(self.client, token) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertEqual(data["status"], "success") self.assertEqual(data["message"], "Successfully logged out")
def test_add_user_invalid_json(self): """Ensure error is thrown if the JSON object is empty""" add_admin() with self.client: resp_login = log_in(self.client) token = json.loads(resp_login.data.decode())["auth_token"] response = add_users(self.client, token, {}) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) self.assertEqual(data["status"], "fail") self.assertEqual(data["message"], "Invalid payload")
def test_add_user_not_admin(self): add_user() with self.client: resp_login = log_in(self.client) token = json.loads(resp_login.data.decode())["auth_token"] response = add_users(self.client, token) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 401) self.assertEqual(data["status"], "fail") self.assertEqual(data["message"], "You do not have permission to do that")
def test_add_user(self): """Ensure a new user can be added to the database""" add_admin() with self.client: resp_login = log_in(self.client) token = json.loads(resp_login.data.decode())["auth_token"] response = add_users(self.client, token) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 201) self.assertEqual(data["status"], "success") self.assertEqual(data["message"], "[email protected] was added!")
def test_invalid_logout_expired_token(self): add_user() current_app.config["TOKEN_EXPIRATION_SECONDS"] = -1 with self.client: resp_login = log_in(self.client) token = json.loads(resp_login.data.decode())["auth_token"] response = logout(self.client, token) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 401) self.assertEqual(data["status"], "fail") self.assertEqual(data["message"], "Signature expired. Please log in again")
def test_user_login_incorrect_email(self): add_user() with self.client: response = log_in(self.client, { "email": "*****@*****.**", "password": "******" }) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 404) self.assertEqual(response.content_type, "application/json") self.assertEqual(data["status"], "fail") self.assertEqual(data["message"], "User does not exist")
def test_user_status(self): add_user() with self.client: resp_login = log_in(self.client) token = json.loads(resp_login.data.decode())["auth_token"] response = status(self.client, token) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertEqual(data["status"], "success") self.assertTrue(data["data"] is not None) self.assertEqual(data["data"]["username"], "test") self.assertEqual(data["data"]["email"], "*****@*****.**") self.assertTrue(data["data"]["active"]) self.assertFalse(data["data"]["admin"])
def test_add_user_duplicate_email(self): """Ensure error is thrown if the email already exists""" add_admin() with self.client: resp_login = log_in(self.client) token = json.loads(resp_login.data.decode())["auth_token"] response = add_users(self.client, token, { "username": "******", "email": "*****@*****.**", "password": "******" }) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) self.assertEqual(data["status"], "fail") self.assertEqual(data["message"], "Sorry. That email already exists")
def test_add_user_empty_email(self): """ Ensure error is thrown if the JSON object does not have a email key """ add_admin() with self.client: resp_login = log_in(self.client) token = json.loads(resp_login.data.decode())["auth_token"] response = add_users(self.client, token, { "username": "******", "password": "******" }) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) self.assertEqual(data["status"], "fail") self.assertEqual(data["message"], "Invalid payload")