|
| 1 | +from http import HTTPStatus |
| 2 | + |
| 3 | +from flask import current_app |
| 4 | + |
| 5 | +from OpenOversight.app.models.database import User |
| 6 | +from OpenOversight.app.utils.constants import ENCODING_UTF_8 |
| 7 | +from OpenOversight.tests.constants import AC_USER_EMAIL, GENERAL_USER_EMAIL |
| 8 | +from OpenOversight.tests.routes.route_helpers import login_ac, login_admin, login_user |
| 9 | + |
| 10 | + |
| 11 | +def test_user_cannot_see_profile_if_not_logged_in(mockdata, client, session): |
| 12 | + with current_app.test_request_context(): |
| 13 | + user = User.query.filter_by(email=GENERAL_USER_EMAIL).first() |
| 14 | + rv = client.get(f"/user/{user.username}") |
| 15 | + |
| 16 | + # Assert that there is a redirect |
| 17 | + assert rv.status_code == HTTPStatus.FOUND |
| 18 | + |
| 19 | + |
| 20 | +def test_user_profile_for_invalid_regex_username(mockdata, client, session): |
| 21 | + with current_app.test_request_context(): |
| 22 | + login_user(client) |
| 23 | + rv = client.get("/user/this_name_is_mad]]bogus") |
| 24 | + |
| 25 | + # Assert page returns error |
| 26 | + assert rv.status_code == HTTPStatus.NOT_FOUND |
| 27 | + |
| 28 | + |
| 29 | +def test_user_profile_for_invalid_username(mockdata, client, session): |
| 30 | + with current_app.test_request_context(): |
| 31 | + login_user(client) |
| 32 | + rv = client.get("/user/this_name_is_mad_bogus") |
| 33 | + |
| 34 | + # Assert page returns error |
| 35 | + assert rv.status_code == HTTPStatus.NOT_FOUND |
| 36 | + |
| 37 | + |
| 38 | +def test_user_profile_does_not_use_id(mockdata, client, session): |
| 39 | + with current_app.test_request_context(): |
| 40 | + _, user = login_user(client) |
| 41 | + rv = client.get(f"/user/{user.id}") |
| 42 | + |
| 43 | + # Assert page returns error |
| 44 | + assert rv.status_code == HTTPStatus.NOT_FOUND |
| 45 | + |
| 46 | + |
| 47 | +def test_user_can_see_own_profile(mockdata, client, session): |
| 48 | + with current_app.test_request_context(): |
| 49 | + _, user = login_user(client) |
| 50 | + rv = client.get(f"/user/{user.username}") |
| 51 | + |
| 52 | + assert rv.status_code == HTTPStatus.OK |
| 53 | + assert bytes(f"Profile: {user.username}", ENCODING_UTF_8) in rv.data |
| 54 | + |
| 55 | + |
| 56 | +def test_user_can_see_other_users_profile(mockdata, client, session): |
| 57 | + with current_app.test_request_context(): |
| 58 | + login_user(client) |
| 59 | + other_user = User.query.filter_by(email=AC_USER_EMAIL).first() |
| 60 | + rv = client.get(f"/user/{other_user.username}") |
| 61 | + |
| 62 | + assert rv.status_code == HTTPStatus.OK |
| 63 | + assert bytes(f"Profile: {other_user.username}", ENCODING_UTF_8) in rv.data |
| 64 | + |
| 65 | + |
| 66 | +def test_ac_user_can_see_other_users_profile(mockdata, client, session): |
| 67 | + with current_app.test_request_context(): |
| 68 | + login_ac(client) |
| 69 | + other_user = User.query.filter_by(email=GENERAL_USER_EMAIL).first() |
| 70 | + rv = client.get(f"/user/{other_user.username}") |
| 71 | + |
| 72 | + assert rv.status_code == HTTPStatus.OK |
| 73 | + assert bytes(f"Profile: {other_user.username}", ENCODING_UTF_8) in rv.data |
| 74 | + |
| 75 | + |
| 76 | +def test_admin_user_can_see_other_users_profile(mockdata, client, session): |
| 77 | + with current_app.test_request_context(): |
| 78 | + login_admin(client) |
| 79 | + other_user = User.query.filter_by(email=GENERAL_USER_EMAIL).first() |
| 80 | + rv = client.get(f"/user/{other_user.username}") |
| 81 | + |
| 82 | + assert rv.status_code == HTTPStatus.OK |
| 83 | + assert bytes(f"Profile: {other_user.username}", ENCODING_UTF_8) in rv.data |
0 commit comments