def test_delete_wakepark_not_resource_owner(test_app, test_db, add_wakepark): recreate_db() add_wakepark( "Stoke City Wakepark", "The only cablepark in Gauteng!", -25.952558, 28.185543, "stokecitywake", "google-oauth2|104755831296456998532", ) client = test_app.test_client() res_one = client.get("/wakeparks/") data = json.loads(res_one.data.decode()) assert res_one.status_code == 200 assert res_one.content_type == "application/json" assert len(data) == 1 res_two = client.delete( f"/wakeparks/1", headers={"Authorization": f"Bearer {access_token}"}) data = json.loads(res_two.data.decode()) assert res_two.status_code == 403 assert res_two.content_type == "application/json" assert "forbidden" in data["code"] assert "Not resource owner." in data["description"]
def test_valid_refresh(test_app, test_db, add_user): recreate_db() add_user("*****@*****.**", "password") client = test_app.test_client() res_login = client.post( f"/auth/login", data=json.dumps({ "email": "*****@*****.**", "password": "******" }), content_type="application/json", ) data = json.loads(res_login.data.decode()) refresh_token = json.loads(res_login.data.decode())["refresh_token"] res = client.post( f"/auth/refresh", data=json.dumps({"refresh_token": refresh_token}), content_type="application/json", ) data = json.loads(res.data.decode()) assert res.status_code == 200 assert res.content_type == "application/json" assert data["access_token"] assert data["refresh_token"]
def test_patch_wakepark_not_found(test_app, test_db): wakepark = { "name": "Stoke City Wakepark", "description": "The only 5 Tower and 2 Tower cablepark in Gauteng!", "location": { "lat": -25.952558, "lng": 28.185543 }, "social": { "instagram": "stokecitywake" }, "owner_id": "google-oauth2|104755831296456998532", } recreate_db() client = test_app.test_client() res = client.patch( f"/wakeparks/999", data=json.dumps(wakepark), headers={"Authorization": f"Bearer {access_token}"}, content_type="application/json", ) data = json.loads(res.data.decode()) assert res.status_code == 404 assert res.content_type == "application/json" assert not data["status"] assert "Resource not found" in data["message"]
def test_add_user_duplicate_email(test_app, test_db): recreate_db() client = test_app.test_client() client.post( f"/users/", data=json.dumps({ "email": "*****@*****.**", "password": "******" }), content_type="application/json", ) res = client.post( f"/users/", data=json.dumps({ "email": "*****@*****.**", "password": "******" }), content_type="application/json", ) data = json.loads(res.data.decode()) assert res.status_code == 400 assert not data["status"] assert "Sorry, that email already exists." in data["message"]
def test_user_status_valid(test_app, test_db, add_user): recreate_db() add_user("*****@*****.**", "password") client = test_app.test_client() res_login = client.post( f"/auth/login", data=json.dumps({ "email": "*****@*****.**", "password": "******" }), content_type="application/json", ) data = json.loads(res_login.data.decode()) access_token = json.loads(res_login.data.decode())["access_token"] res_status = client.get( f"/auth/status", headers={"Authorization": f"Bearer {access_token}"}, content_type="application/json", ) data = json.loads(res_status.data.decode()) assert res_status.status_code == 200 assert res_status.content_type == "application/json" assert "*****@*****.**" in data["email"] assert "password" not in data
def test_delete_wakepark(test_app, test_db, add_wakepark): recreate_db() add_wakepark( "Stoke City Wakepark", "The only cablepark in Gauteng!", -25.952558, 28.185543, "stokecitywake", "qjqhGiKHei9h71ll70DG3tWV2Kwg5KET@clients", ) client = test_app.test_client() res_one = client.get("/wakeparks/") data = json.loads(res_one.data.decode()) assert res_one.status_code == 200 assert res_one.content_type == "application/json" assert len(data) == 1 res_two = client.delete( f"/wakeparks/1", headers={"Authorization": f"Bearer {access_token}"}) data = json.loads(res_two.data.decode()) assert res_two.status_code == 200 assert res_two.content_type == "application/json" assert data["status"] assert "Wakepark was deleted" in data["message"] assert data["wakepark"] res_three = client.get("/wakeparks/") data = json.loads(res_three.data.decode()) assert res_three.status_code == 200 assert res_three.content_type == "application/json" assert len(data) == 0
def test_invalid_refresh_expired_token(test_app, test_db, add_user): recreate_db() add_user("*****@*****.**", "password") current_app.config["REFRESH_TOKEN_EXPIRATION"] = -1 client = test_app.test_client() res_login = client.post( f"/auth/login", data=json.dumps({ "email": "*****@*****.**", "password": "******" }), content_type="application/json", ) # time.sleep(4) data = json.loads(res_login.data.decode()) refresh_token = json.loads(res_login.data.decode())["refresh_token"] res_refresh = client.post( f"/auth/refresh", data=json.dumps({"refresh_token": refresh_token}), content_type="application/json", ) data = json.loads(res_refresh.data.decode()) assert res_refresh.status_code == 401 assert res_refresh.content_type == "application/json" assert "Signature expired. Please login again." in data["message"]
def test_user_status_expired_token(test_app, test_db, add_user): recreate_db() add_user("*****@*****.**", "password") current_app.config["ACCESS_TOKEN_EXPIRATION"] = -1 client = test_app.test_client() res_login = client.post( f"/auth/login", data=json.dumps({ "email": "*****@*****.**", "password": "******" }), content_type="application/json", ) data = json.loads(res_login.data.decode()) access_token = json.loads(res_login.data.decode())["access_token"] res = client.get( f"/auth/status", headers={"Authorization": f"Bearer {access_token}"}, content_type="application/json", ) data = json.loads(res.data.decode()) assert res.status_code == 401 assert res.content_type == "application/json" assert "Signature expired. Please login again." in data["message"]
def test_get_all_wakeparks(test_app, test_db, add_wakepark): recreate_db() client = test_app.test_client() add_wakepark( "Stoke City Wakepark", "The only cablepark in Gauteng!", -25.952558, 28.185543, "stokecitywake", "auth0|5ddb7e53ecc37b0ef0912ab8", ) add_wakepark( "Blue Rock", "Awesome 5-Tower cablepark with Unit Parktech features!", -25.952558, 28.185543, "blue_rock_waterski", "google-oauth2|104755831296456998532", ) res = client.get(f"/wakeparks/") data = json.loads(res.data.decode()) assert res.status_code == 200 assert res.content_type == "application/json" assert len(data) == 2
def test_delete_wakepark_unauthorised(test_app, test_db, add_wakepark, headers, code, description): recreate_db() add_wakepark( "Stoke City Wakepark", "The only cablepark in Gauteng!", -25.952558, 28.185543, "stokecitywake", "google-oauth2|104755831296456998532", ) client = test_app.test_client() res_one = client.get("/wakeparks/") data = json.loads(res_one.data.decode()) assert res_one.status_code == 200 assert res_one.content_type == "application/json" assert len(data) == 1 res_two = client.delete(f"/wakeparks/1", headers=headers) data = json.loads(res_two.data.decode()) assert res_two.status_code == 401 assert res_two.content_type == "application/json" assert description in data["description"] assert code in data["code"]
def test_user_status_no_token(test_app, test_db): recreate_db() client = test_app.test_client() res = client.get(f"/auth/status", content_type="application/json") data = json.loads(res.data.decode()) assert res.status_code == 403 assert res.content_type == "application/json" assert "Access token required." in data["message"] assert not data["status"]
def test_single_user(test_app, test_db, add_user): recreate_db() user = add_user("*****@*****.**", "password") client = test_app.test_client() res = client.get(f"/users/{user.id}") data = json.loads(res.data.decode()) assert res.status_code == 200 assert str(user.id) in data["id"] assert "*****@*****.**" in data["email"] assert "password" not in data
def test_admin_view_dev(): os.environ["FLASK_ENV"] = "development" assert os.getenv("FLASK_ENV") == "development" app = create_app() app.config.from_object("project.config.TestingConfig") with app.app_context(): recreate_db() client = app.test_client() resp = client.get("/admin/user/") assert resp.status_code == 200 assert os.getenv("FLASK_ENV") == "development"
def test_update_user_invalid_json(test_app, test_db, add_user): recreate_db() user = add_user("*****@*****.**", "password") client = test_app.test_client() res = client.put(f"/users/{user.id}", data=json.dumps({}), content_type="application/json") data = json.loads(res.data.decode()) assert res.status_code == 400 assert not data["status"] assert "Invalid payload" in data["message"]
def test_invalid_refresh_invalid_payload(test_app, test_db, add_user): recreate_db() client = test_app.test_client() res_refresh = client.post(f"/auth/refresh", data=json.dumps({}), content_type="application/json") data = json.loads(res_refresh.data.decode()) assert res_refresh.status_code == 400 assert res_refresh.content_type == "application/json" assert "Invalid payload." in data["message"]
def test_update_user_not_found(test_app, test_db): recreate_db() client = test_app.test_client() res = client.put( f"/users/999", data=json.dumps({"email": "*****@*****.**"}), content_type="application/json", ) data = json.loads(res.data.decode()) assert res.status_code == 404 assert not data["status"] assert "Resource not found" in data["message"]
def test_get_users(test_app, test_database): recreate_db() client = test_app.test_client() add_user("wanyaland", "*****@*****.**") add_user("nachwera", "*****@*****.**") resp = client.get("/users") data = json.loads(resp.data.decode()) assert resp.status_code == 200 assert len(data["data"]["users"]) == 2 assert "wanyaland" in data["data"]["users"][0]["username"] assert "*****@*****.**" in data["data"]["users"][0]["email"] assert "nachwera" in data["data"]["users"][1]["username"] assert "nachwera@gmail" in data["data"]["users"][1]["email"]
def test_user_registration_invalid_payload(test_app, test_db, payload, message): recreate_db() client = test_app.test_client() py = payload print("Payload: ", py) res = client.post( f"/auth/register", data=json.dumps(payload), content_type="application/json" ) data = json.loads(res.data.decode()) assert res.status_code == 400 assert res.content_type == "application/json" assert message in data["message"] assert not data["status"]
def test_all_users(test_app, test_database): recreate_db() add_user("jane", "*****@*****.**", "greaterthaneight") add_user("jack", "*****@*****.**", "greaterthaneight") client = test_app.test_client() resp = client.get("/users") data = json.loads(resp.data.decode()) assert resp.status_code == 200 assert len(data["data"]["users"]) == 2 assert "jane" in data["data"]["users"][0]["username"] assert "*****@*****.**" in data["data"]["users"][0]["email"] assert "jack" in data["data"]["users"][1]["username"] assert "*****@*****.**" in data["data"]["users"][1]["email"] assert "success" in data["status"]
def test_all_users(test_app, test_database): recreate_db() add_user('michael', '*****@*****.**') add_user('fletcher', '*****@*****.**') client = test_app.test_client() resp = client.get('/users') data = json.loads(resp.data.decode()) assert resp.status_code == 200 assert len(data['data']['users']) == 2 assert 'michael' in data['data']['users'][0]['username'] assert '*****@*****.**' in data['data']['users'][0]['email'] assert 'fletcher' in data['data']['users'][1]['username'] assert '*****@*****.**' in data['data']['users'][1]['email'] assert 'success' in data['status']
def test_get_all_users(test_app, test_database): recreate_db() add_user("bill", "*****@*****.**") add_user("bob", "*****@*****.**") client = test_app.test_client() resp = client.get("/users") data = json.loads(resp.data.decode()) assert resp.status_code == 200 assert len(data["data"]["users"]) == 2 assert "bill" in data["data"]["users"][0]["username"] assert "*****@*****.**" in data["data"]["users"][0]["email"] assert "bob" in data["data"]["users"][1]["username"] assert "*****@*****.**" in data["data"]["users"][1]["email"] assert "success" in data["status"]
def test_invalid_refresh_invalid_token(test_app, test_db, add_user): recreate_db() client = test_app.test_client() res_refresh = client.post( f"/auth/refresh", data=json.dumps({"refresh_token": "invalid"}), content_type="application/json", ) data = json.loads(res_refresh.data.decode()) assert res_refresh.status_code == 401 assert res_refresh.content_type == "application/json" assert "Invalid token. Please login again." in data["message"]
def test_get_all_users(test_app, test_db, add_user): recreate_db() client = test_app.test_client() add_user("*****@*****.**", "password") add_user("*****@*****.**", "password") res = client.get(f"/users/") data = json.loads(res.data.decode()) assert res.status_code == 200 assert len(data) == 2 assert "*****@*****.**" in data[0]["email"] assert "*****@*****.**" in data[1]["email"] assert "password" not in data[0] assert "password" not in data[1]
def test_all_users(test_app, test_database): recreate_db() add_user("test_all_users1", "*****@*****.**", "test_password") add_user("test_all_users2", "*****@*****.**", "test_password") client = test_app.test_client() resp = client.get("/users") data = json.loads(resp.data.decode()) assert resp.status_code == 200 assert len(data["data"]["users"]) == 2 assert "test_all_users1" in data["data"]["users"][0]["username"] assert "*****@*****.**" in data["data"]["users"][0]["email"] assert "test_all_users2" in data["data"]["users"][1]["username"] assert "*****@*****.**" in data["data"]["users"][1]["email"] assert "success" in data["status"]
def test_all_users(test_app, test_database): recreate_db() add_user("michael", "*****@*****.**", "greaterthaneight") add_user("fletcher", "*****@*****.**", "greaterthaneight") client = test_app.test_client() resp = client.get("/users") data = json.loads(resp.data.decode()) assert resp.status_code == 200 assert len(data["data"]["users"]) == 2 assert "michael" in data["data"]["users"][0]["username"] assert "*****@*****.**" in data["data"]["users"][0]["email"] assert "fletcher" in data["data"]["users"][1]["username"] assert "*****@*****.**" in data["data"]["users"][1]["email"] assert "success" in data["status"]
def test_create_wakepark_invalid_payload(test_app, test_db, payload, message): recreate_db() client = test_app.test_client() res = client.post( "/wakeparks/", data=json.dumps(payload), headers={"Authorization": f"Bearer {access_token}"}, content_type="application/json", ) data = json.loads(res.data.decode()) assert res.status_code == 400 assert res.content_type == "application/json" assert message in data["message"] assert data["errors"]
def test_user_status_invalid_no_bearer(test_app, test_db): recreate_db() client = test_app.test_client() res = client.get( f"/auth/status", headers={"Authorization": f"invalid"}, content_type="application/json", ) data = json.loads(res.data.decode()) assert res.status_code == 401 assert res.content_type == "application/json" assert "Invalid header." in data["message"] assert not data["status"]
def test_create_wakepark_unauthorised(test_app, test_db, payload, headers, code, description): recreate_db() client = test_app.test_client() res = client.post( "/wakeparks/", data=json.dumps(payload), headers=headers, content_type="application/json", ) data = json.loads(res.data.decode()) assert res.status_code == 401 assert res.content_type == "application/json" assert description in data["description"] assert code in data["code"]
def test_update_wakepark(test_app, test_db, add_wakepark): recreate_db() # initial wakepark new_wakepark = add_wakepark("Stoke City Wakepark", "The only cablepark in Gauteng!", -25.952558, 28.185543, "stokecitywake", "qjqhGiKHei9h71ll70DG3tWV2Kwg5KET@clients" # "google-oauth2|104755831296456998532" ) # updated wakepark wakepark = { "name": "Stoke City Wakepark", "description": "The only 5 Tower and 2 Tower cablepark in Gauteng!", "location": { "lat": -25.952558, "lng": 28.185543 }, "social": { "instagram": "stokecitywake" }, "owner_id": "qjqhGiKHei9h71ll70DG3tWV2Kwg5KET@clients" # "owner_id": "google-oauth2|104755831296456998532" } client = test_app.test_client() res_one = client.put( f"/wakeparks/{new_wakepark.id}", data=json.dumps(wakepark), headers={"Authorization": f"Bearer {access_token}"}, content_type="application/json", ) data = json.loads(res_one.data.decode()) assert res_one.status_code == 200 assert res_one.content_type == "application/json" assert data["status"] assert "Wakepark successfully updated" in data["message"] assert data["wakepark"] res_two = client.get(f"/wakeparks/{new_wakepark.id}") data = json.loads(res_two.data.decode()) assert res_two.status_code == 200 assert res_two.content_type == "application/json" assert ("The only 5 Tower and 2 Tower cablepark in Gauteng!" in data["description"])
def test_remove_user(test_app, test_database): recreate_db() user = add_user("user-to-be-removed", "*****@*****.**", "greaterthaneight") client = test_app.test_client() resp_one = client.get("/users") data = json.loads(resp_one.data.decode()) assert resp_one.status_code == 200 assert len(data["data"]["users"]) == 1 resp_two = client.delete(f"/users/{user.id}") data = json.loads(resp_two.data.decode()) assert resp_two.status_code == 200 assert "[email protected] was removed!" in data["message"] assert "success" in data["status"] resp_three = client.get("/users") data = json.loads(resp_three.data.decode()) assert resp_three.status_code == 200 assert len(data["data"]["users"]) == 0