def test_password_reset_post_request_will_fail_without_token(self): """Tests if the password for a user is updated without token""" tester = app.test_client(self) res = tester.post("/api/v1/auth/register", data=json.dumps(self.user), content_type="application/json") self.assertEqual(res.status_code, 201) self.assertIn( "User " + self.user['first_name'] + " " + self.user['last_name'] + " has been registered successfully", str(res.data)) new_user = {"username": '******', "password": "******"} response = tester.post("/api/v1/login", data=json.dumps(new_user), content_type="application/json") self.assertEqual(response.status_code, 201) self.assertIn( 'You have successfully logged in!.Token created successfully', str(response.data)) # Test using user Moses and change his password from banana to oranges new_password = {'new_password': '******'} response = tester.post("/api/v1/auth/reset-password", data=json.dumps(new_password), content_type="application/json") self.assertEqual(response.status_code, 200) self.assertIn('Token is missing', str(response.data))
def test_logout_post_request_will_work_with_token(self): """Tests if the password for a user is updated without token""" tester = app.test_client(self) res = tester.post("/api/v1/auth/register", data=json.dumps(self.user), content_type="application/json") self.assertEqual(res.status_code, 201) self.assertIn( "User " + self.user['first_name'] + " " + self.user['last_name'] + " has been registered successfully", str(res.data)) new_user = {"username": '******', "password": "******"} response = tester.post("/api/v1/login", data=json.dumps(new_user), content_type="application/json") result_in_json = json.loads( response.data.decode('utf-8').replace("'", "\"")) self.assertEqual(response.status_code, 201) self.assertIn( 'You have successfully logged in!.Token created successfully', str(response.data)) # Test using user Moses and change his password from banana to oranges new_password = {'new_password': '******'} response = tester.post( "/api/v1/auth/logout", data=json.dumps(new_password), content_type="application/json", headers={"access-token": result_in_json["token"]}) self.assertEqual(response.status_code, 201) self.assertIn('You have successfully logged out', str(response.data))
def test_API_can_not_create_a_business_without_a_valid_email(self): """Tests if a business is created""" tester = app.test_client(self) res = tester.post("/api/v1/auth/register", data=json.dumps(self.user[0]), content_type="application/json") self.assertEqual(res.status_code, 201) self.assertIn( "User " + self.user[0]['first_name'] + " " + self.user[0]['last_name'] + " has been registered successfully", str(res.data)) new_user = {"username": '******', "password": "******"} response = tester.post("/api/v1/login", data=json.dumps(new_user), content_type="application/json") result_in_json = json.loads( response.data.decode('utf-8').replace("'", "\"")) self.assertEqual(response.status_code, 201) self.assertIn( 'You have successfully logged in!.Token created successfully', str(response.data)) self.businesses[0]['email'] = "my_fi" response = tester.post( "/api/v1/businesses", data=json.dumps(self.businesses[0]), content_type="application/json", headers={"access-token": result_in_json["token"]}) self.assertEqual(response.status_code, 200) self.assertIn("Invalid email address", str(response.data))
def test_API_can_get_all_reviews(self): """Tests if api can get all reviews""" tester = app.test_client(self) res = tester.post("/api/v1/auth/register", data=json.dumps(self.user), content_type="application/json") self.assertEqual(res.status_code, 201) self.assertIn( "User " + self.user['first_name'] + " " + self.user['last_name'] + " has been registered successfully", str(res.data)) new_user = {"username": '******', "password": "******"} response = tester.post("/api/v1/login", data=json.dumps(new_user), content_type="application/json") result_in_json = json.loads( response.data.decode('utf-8').replace("'", "\"")) self.assertEqual(response.status_code, 201) self.assertIn( 'You have successfully logged in!.Token created successfully', str(response.data)) response = tester.post( "/api/v1/businesses", data=json.dumps(self.business), content_type="application/json", headers={"access-token": result_in_json["token"]}) self.assertEqual(response.status_code, 201) self.assertIn( "Business " + self.business["business_name"] + " has been registered successfully", str(response.data)) # Add the first review response = tester.post("/api/businesses/1/reviews", data=json.dumps(self.reviews[0]), content_type="application/json") # check for review 1 content self.assertEqual(response.status_code, 201) self.assertIn('Business review has been added successfully', str(response.data)) # Add second review response = tester.post("/api/businesses/1/reviews", data=json.dumps(self.reviews[1]), content_type="application/json") # check for review 2 content self.assertEqual(response.status_code, 201) self.assertIn('Business review has been added successfully', str(response.data)) # Add third review response = tester.post("/api/businesses/1/reviews", data=json.dumps(self.reviews[2]), content_type="application/json") # check for review 3 content self.assertEqual(response.status_code, 201) self.assertIn('Business review has been added successfully', str(response.data)) # get all reviews res = tester.get("api/businesses/1/reviews", content_type="application/json") self.assertEqual(res.status_code, 200) self.assertIn('This business is the best', str(res.data)) self.assertIn('This business rocks', str(res.data)) self.assertIn('This business is awesome', str(res.data))
def test_API_can_not_get_reviews_for_non_existent_businesses(self): """Tests if API can not get reviews for non existent businesses""" tester = app.test_client(self) response = tester.get("/api/businesses/1/reviews", content_type="application/json") # check for response self.assertEqual(response.status_code, 200) self.assertIn('Not found', str(response.data))
def test_API_will_fail_to_create_a_user_account_without_valid_email(self): """Tests if a user account will not be created without a valid email""" tester = app.test_client(self) self.user['email'] = "moses" res = tester.post("/api/v1/auth/register", data=json.dumps(self.user), content_type="application/json") self.assertEqual(res.status_code, 200) self.assertIn('Invalid email address', str(res.data))
def test_API_will_fail_to_create_a_user_account_without_valid_gender(self): """Tests if a user account will be created without valid gender""" tester = app.test_client(self) self.user['gender'] = "gty" res = tester.post("/api/v1/auth/register", data=json.dumps(self.user), content_type="application/json") self.assertEqual(res.status_code, 200) self.assertIn('Invalid gender, Try Female, Male, F, M', str(res.data))
def test_Api_will_not_update_business_without_token(self): """Tests if a business is deleted""" tester = app.test_client(self) self.businesses[0]['business_name'] = "media_com" response = tester.put("/api/v1/businesses/1", data=json.dumps(self.businesses[0]), content_type="application/json") self.assertEqual(response.status_code, 200) self.assertIn('Token is missing', str(response.data))
def test_API_can_not_post_reviews_to_a_non_existent_business(self): """Tests if a business review is added to a non existent business""" tester = app.test_client(self) response = tester.post("/api/businesses/1/reviews", data=json.dumps(self.reviews[0]), content_type="application/json") # check for response self.assertEqual(response.status_code, 200) self.assertIn('Business to add a review to not found', str(response.data))
def test_API_will_create_account(self): """Tests if a user account is created""" tester = app.test_client(self) res = tester.post("/api/v1/auth/register", data=json.dumps(self.user), content_type="application/json") self.assertEqual(res.status_code, 201) self.assertIn( "User " + self.user['first_name'] + " " + self.user['last_name'] + " has been registered successfully", str(res.data))
def test_API_will_fail_to_create_a_user_account_without_all_fields(self): """Tests if a user account will not be created without all fields""" tester = app.test_client(self) self.user['password'] = "" with tester: res = tester.post("/api/v1/auth/register", data=json.dumps(self.user), content_type="application/json") self.assertEqual(res.status_code, 200) self.assertIn('All fields are required', str(res.data))
def test_API_will_update_business_with_token(self): """Tests if a business is updated""" tester = app.test_client(self) res = tester.post("/api/v1/auth/register", data=json.dumps(self.user[0]), content_type="application/json") self.assertEqual(res.status_code, 201) self.assertIn( "User " + self.user[0]['first_name'] + " " + self.user[0]['last_name'] + " has been registered successfully", str(res.data)) new_user = {"username": '******', "password": "******"} response = tester.post("/api/v1/login", data=json.dumps(new_user), content_type="application/json") result_in_json = json.loads( response.data.decode('utf-8').replace("'", "\"")) self.assertEqual(response.status_code, 201) self.assertIn( 'You have successfully logged in!.Token created successfully', str(response.data)) # Add first business response = tester.post( "/api/v1/businesses", data=json.dumps(self.businesses[0]), content_type="application/json", headers={"access-token": result_in_json["token"]}) self.assertEqual(response.status_code, 201) self.assertIn( "Business " + self.businesses[0]["business_name"] + " has been registered successfully", str(response.data)) # Add second business response = tester.post( "/api/v1/businesses", data=json.dumps(self.businesses[1]), content_type="application/json", headers={"access-token": result_in_json["token"]}) self.assertEqual(response.status_code, 201) self.assertIn( "Business " + self.businesses[1]["business_name"] + " has been registered successfully", str(response.data)) # Edit business 1 self.businesses[0]['business_name'] = "media_com" response = tester.put( "/api/v1/businesses/1", data=json.dumps(self.businesses[0]), content_type="application/json", headers={"access-token": result_in_json["token"]}) self.assertEqual(response.status, '201 CREATED') self.assertIn("Business updated successfully", str(response.data))
def test_API_has_unique_username_constraint(self): """Tests if the API has a unique username constraint""" tester = app.test_client(self) res = tester.post("/api/v1/auth/register", data=json.dumps(self.user), content_type="application/json") self.assertEqual(res.status_code, 201) self.assertIn( "User " + self.user['first_name'] + " " + self.user['last_name'] + " has been registered successfully", str(res.data)) res = tester.post("/api/v1/auth/register", data=json.dumps(self.user), content_type="application/json") self.assertEqual(res.status_code, 200) self.assertIn("User already exists", str(res.data))
def test_login_post_request_will_fail_without_correct_information(self): """Tests if login post request will work without correct username or password""" tester = app.test_client(self) res = tester.post("/api/v1/auth/register", data=json.dumps(self.user), content_type="application/json") self.assertEqual(res.status_code, 201) self.assertIn( "User " + self.user['first_name'] + " " + self.user['last_name'] + " has been registered successfully", str(res.data)) new_user = {"username": '******', "password": "******"} response = tester.post("/api/v1/login", data=json.dumps(new_user), content_type="application/json") self.assertEqual(response.status_code, 200) self.assertIn('Invalid username or password', str(response.data))
def test_API_can_get_all_businesses(self): """Tests if one can retrieve all registered businesses""" tester = app.test_client(self) res = tester.post("/api/v1/auth/register", data=json.dumps(self.user[0]), content_type="application/json") self.assertEqual(res.status_code, 201) self.assertIn( "User " + self.user[0]['first_name'] + " " + self.user[0]['last_name'] + " has been registered successfully", str(res.data)) new_user = {"username": '******', "password": "******"} response = tester.post("/api/v1/login", data=json.dumps(new_user), content_type="application/json") result_in_json = json.loads( response.data.decode('utf-8').replace("'", "\"")) self.assertEqual(response.status_code, 201) self.assertIn( 'You have successfully logged in!.Token created successfully', str(response.data)) # Add first business response = tester.post( "/api/v1/businesses", data=json.dumps(self.businesses[0]), content_type="application/json", headers={"access-token": result_in_json["token"]}) self.assertEqual(response.status_code, 201) self.assertIn( "Business " + self.businesses[0]["business_name"] + " has been registered successfully", str(response.data)) # Add second business response = tester.post( "/api/v1/businesses", data=json.dumps(self.businesses[1]), content_type="application/json", headers={"access-token": result_in_json["token"]}) self.assertEqual(response.status_code, 201) self.assertIn( "Business " + self.businesses[1]["business_name"] + " has been registered successfully", str(response.data)) response = tester.get("/api/v1/businesses") self.assertEqual(response.status_code, 200) self.assertIn("MyFi", str(response.data)) self.assertIn("Supercom", str(response.data))
def test_log_in_post_request(self): """Tests if a user is logged in""" tester = app.test_client(self) res = tester.post("/api/v1/auth/register", data=json.dumps(self.user), content_type="application/json") self.assertEqual(res.status_code, 201) self.assertIn( "User " + self.user['first_name'] + " " + self.user['last_name'] + " has been registered successfully", str(res.data)) new_user = {"username": '******', "password": "******"} response = tester.post("/api/v1/login", data=json.dumps(new_user), content_type="application/json") self.assertEqual(response.status_code, 201) self.assertIn( 'You have successfully logged in!.Token created successfully', str(response.data))
def test_API_will_not_update_a_business_without_owner(self): """Tests if a business is deleted""" tester = app.test_client(self) # add first test user res = tester.post("/api/v1/auth/register", data=json.dumps(self.user[0]), content_type="application/json") self.assertEqual(res.status_code, 201) self.assertIn( "User " + self.user[0]['first_name'] + " " + self.user[0]['last_name'] + " has been registered successfully", str(res.data)) new_user = {"username": '******', "password": "******"} # log in user response = tester.post("/api/v1/login", data=json.dumps(new_user), content_type="application/json") result_in_json = json.loads( response.data.decode('utf-8').replace("'", "\"")) self.assertEqual(response.status_code, 201) self.assertIn( 'You have successfully logged in!.Token created successfully', str(response.data)) # Add first business response = tester.post( "/api/v1/businesses", data=json.dumps(self.businesses[0]), content_type="application/json", headers={"access-token": result_in_json["token"]}) self.assertEqual(response.status_code, 201) self.assertIn( "Business " + self.businesses[0]["business_name"] + " has been registered successfully", str(response.data)) # Add second test_user res = tester.post("/api/v1/auth/register", data=json.dumps(self.user[1]), content_type="application/json") self.assertEqual(res.status_code, 201) self.assertIn( "User " + self.user[1]['first_name'] + " " + self.user[1]['last_name'] + " has been registered successfully", str(res.data)) new_user = {"username": '******', "password": "******"} # log in second user response = tester.post("/api/v1/login", data=json.dumps(new_user), content_type="application/json") result_in_json = json.loads( response.data.decode('utf-8').replace("'", "\"")) self.assertEqual(response.status_code, 201) self.assertIn( 'You have successfully logged in!.Token created successfully', str(response.data)) # Add second business response = tester.post( "/api/v1/businesses", data=json.dumps(self.businesses[1]), content_type="application/json", headers={"access-token": result_in_json["token"]}) self.assertEqual(response.status_code, 201) self.assertIn( "Business " + self.businesses[1]["business_name"] + " has been registered successfully", str(response.data)) # Update business 1 using second users token who does not own the business self.businesses[0]['business_name'] = "media_com" response = tester.put( "/api/v1/businesses/1", data=json.dumps(self.businesses[0]), content_type="application/json", headers={"access-token": result_in_json["token"]}) self.assertEqual(response.status, '200 OK') self.assertIn("Not enough privileges to perform action", str(response.data))
def test_API_will_fail_to_delete_a_business_without_token(self): """Tests if a business is deleted""" tester = app.test_client(self) response = tester.delete("/api/v1/businesses/1") self.assertEqual(response.status_code, 200) self.assertIn('Token is missing', str(response.data))
def test_API_welcome_page(self): """Tests if the home route is functional""" tester = app.test_client(self) res = tester.get("/home") self.assertEqual(res.status_code, 200) self.assertIn('You are welcome', str(res.data))