|
| 1 | +import json |
| 2 | + |
| 3 | +from flask.testing import FlaskClient |
| 4 | + |
| 5 | +from labconnect import db |
| 6 | +from labconnect.models import User, UserDepartments, UserMajors |
| 7 | + |
| 8 | + |
| 9 | +def login_as_student(test_client: FlaskClient): |
| 10 | + """Helper function to log in a user and handle the auth flow.""" |
| 11 | + response = test_client.get("/login") |
| 12 | + assert response.status_code == 302 |
| 13 | + |
| 14 | + redirect_url = response.headers["Location"] |
| 15 | + code = redirect_url.split("code=")[1] |
| 16 | + |
| 17 | + token_response = test_client.post("/token", json={"code": code}) |
| 18 | + assert token_response.status_code == 200 |
| 19 | + |
| 20 | + |
| 21 | +# === GET /profile Tests === |
| 22 | + |
| 23 | + |
| 24 | +def test_get_profile_success(test_client: FlaskClient): |
| 25 | + """ |
| 26 | + logged-in user: '/profile' endpoint is requested (GET) |
| 27 | + -> correct data and 200 status |
| 28 | + """ |
| 29 | + login_as_student(test_client) |
| 30 | + |
| 31 | + response = test_client.get("/profile") |
| 32 | + data = json.loads(response.data) |
| 33 | + |
| 34 | + assert response.status_code == 200 |
| 35 | + assert data[ "email"] == "[email protected]" |
| 36 | + assert data["first_name"] == "Test" |
| 37 | + assert data["last_name"] == "User" |
| 38 | + assert "departments" in data |
| 39 | + assert "majors" in data |
| 40 | + |
| 41 | + |
| 42 | +def test_get_profile_unauthorized(test_client: FlaskClient): |
| 43 | + """ |
| 44 | + no user is logged in: '/profile' endpoint is requested (GET) |
| 45 | + -> 401 Unauthorized status is returned. |
| 46 | + """ |
| 47 | + test_client.get("/logout") |
| 48 | + response = test_client.get("/profile") |
| 49 | + assert response.status_code == 401 |
| 50 | + |
| 51 | + |
| 52 | +# === PUT /profile Tests === |
| 53 | + |
| 54 | + |
| 55 | +def test_update_profile_success(test_client: FlaskClient): |
| 56 | + """ |
| 57 | + logged-in user: '/profile' endpoint is updated with new data (PUT) |
| 58 | + -> 200 status and database changed. |
| 59 | + """ |
| 60 | + login_as_student(test_client) |
| 61 | + |
| 62 | + update_data = { |
| 63 | + "first_name": "UpdatedFirst", |
| 64 | + "last_name": "UpdatedLast", |
| 65 | + "preferred_name": "Pref", |
| 66 | + "class_year": 2025, |
| 67 | + "website": "https://new.example.com", |
| 68 | + "description": "This is an updated description.", |
| 69 | + "departments": ["CS"], |
| 70 | + "majors": ["CSCI", "MATH"], |
| 71 | + } |
| 72 | + |
| 73 | + response = test_client.put("/profile", json=update_data) |
| 74 | + assert response.status_code == 200 |
| 75 | + assert "Profile updated successfully" in json.loads(response.data)["msg"] |
| 76 | + |
| 77 | + # Verify the changes in the database |
| 78 | + user = db.session.execute( |
| 79 | + db. select( User). where( User. email == "[email protected]") |
| 80 | + ).scalar_one() |
| 81 | + assert user.first_name == "UpdatedFirst" |
| 82 | + assert user.website == "https://new.example.com" |
| 83 | + assert user.class_year == 2025 |
| 84 | + |
| 85 | + user_depts = ( |
| 86 | + db.session.execute( |
| 87 | + db.select(UserDepartments.department_id).where( |
| 88 | + UserDepartments.user_id == user.id |
| 89 | + ) |
| 90 | + ) |
| 91 | + .scalars() |
| 92 | + .all() |
| 93 | + ) |
| 94 | + assert set(user_depts) == {"CS"} |
| 95 | + |
| 96 | + user_majors = ( |
| 97 | + db.session.execute( |
| 98 | + db.select(UserMajors.major_code).where(UserMajors.user_id == user.id) |
| 99 | + ) |
| 100 | + .scalars() |
| 101 | + .all() |
| 102 | + ) |
| 103 | + assert set(user_majors) == {"CSCI", "MATH"} |
| 104 | + |
| 105 | + |
| 106 | +def test_update_profile_partial(test_client: FlaskClient): |
| 107 | + """ |
| 108 | + logged-in user: '/profile' endpoint is updated with partial data (PUT) |
| 109 | + -> check only provided fields updated. |
| 110 | + """ |
| 111 | + login_as_student(test_client) |
| 112 | + |
| 113 | + update_data = { |
| 114 | + "website": "https://partial.update.com", |
| 115 | + "description": "Only this was updated.", |
| 116 | + } |
| 117 | + |
| 118 | + response = test_client.put("/profile", json=update_data) |
| 119 | + assert response.status_code == 200 |
| 120 | + |
| 121 | + user = db.session.execute( |
| 122 | + db. select( User). where( User. email == "[email protected]") |
| 123 | + ).scalar_one() |
| 124 | + assert user.website == "https://partial.update.com" |
| 125 | + assert user.description == "Only this was updated." |
| 126 | + assert user.last_name == "User" |
| 127 | + |
| 128 | + |
| 129 | +def test_update_profile_unauthorized(test_client: FlaskClient): |
| 130 | + """ |
| 131 | + no user is logged in: '/profile' endpoint is sent a PUT request |
| 132 | + -> 401 Unauthorized status. |
| 133 | + """ |
| 134 | + test_client.get("/logout") |
| 135 | + update_data = {"first_name": "ShouldFail"} |
| 136 | + response = test_client.put("/profile", json=update_data) |
| 137 | + assert response.status_code == 401 |
0 commit comments