Skip to content

Commit 7537e32

Browse files
authored
Add user profile tests (#1119)
## Fixes issue #436 ## Description of Changes Added tests to validate `/user/` route logic and correct profile logic to match pre-specified tests. <img width="497" alt="Screenshot 2024-07-31 at 5 27 37 PM" src="https://github.com/user-attachments/assets/78703665-1623-4703-8fa9-a1cca59ba319"> There is not a `/users/` route, so I marked it out. ## Tests and Linting - [x] This branch is up-to-date with the `develop` branch. - [x] `pytest` passes on my local development environment. - [x] `pre-commit` passes on my local development environment.
1 parent 6e749e4 commit 7537e32

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

OpenOversight/app/main/views.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,10 @@ def get_tutorial():
288288
@login_required
289289
def profile(username: str):
290290
if re.search("^[A-Za-z][A-Za-z0-9_.]*$", username):
291-
user = User.by_username(username).one()
291+
try:
292+
user = User.by_username(username).one()
293+
except NoResultFound:
294+
abort(HTTPStatus.NOT_FOUND)
292295
else:
293296
abort(HTTPStatus.NOT_FOUND)
294297

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)