forked from lucyparsons/OpenOversight
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user profile tests (lucyparsons#1119)
## Fixes issue lucyparsons#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.
- Loading branch information
1 parent
323aba2
commit 9fffc1b
Showing
2 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from http import HTTPStatus | ||
|
||
from flask import current_app | ||
|
||
from OpenOversight.app.models.database import User | ||
from OpenOversight.app.utils.constants import ENCODING_UTF_8 | ||
from OpenOversight.tests.constants import AC_USER_EMAIL, GENERAL_USER_EMAIL | ||
from OpenOversight.tests.routes.route_helpers import login_ac, login_admin, login_user | ||
|
||
|
||
def test_user_cannot_see_profile_if_not_logged_in(mockdata, client, session): | ||
with current_app.test_request_context(): | ||
user = User.query.filter_by(email=GENERAL_USER_EMAIL).first() | ||
rv = client.get(f"/user/{user.username}") | ||
|
||
# Assert that there is a redirect | ||
assert rv.status_code == HTTPStatus.FOUND | ||
|
||
|
||
def test_user_profile_for_invalid_regex_username(mockdata, client, session): | ||
with current_app.test_request_context(): | ||
login_user(client) | ||
rv = client.get("/user/this_name_is_mad]]bogus") | ||
|
||
# Assert page returns error | ||
assert rv.status_code == HTTPStatus.NOT_FOUND | ||
|
||
|
||
def test_user_profile_for_invalid_username(mockdata, client, session): | ||
with current_app.test_request_context(): | ||
login_user(client) | ||
rv = client.get("/user/this_name_is_mad_bogus") | ||
|
||
# Assert page returns error | ||
assert rv.status_code == HTTPStatus.NOT_FOUND | ||
|
||
|
||
def test_user_profile_does_not_use_id(mockdata, client, session): | ||
with current_app.test_request_context(): | ||
_, user = login_user(client) | ||
rv = client.get(f"/user/{user.id}") | ||
|
||
# Assert page returns error | ||
assert rv.status_code == HTTPStatus.NOT_FOUND | ||
|
||
|
||
def test_user_can_see_own_profile(mockdata, client, session): | ||
with current_app.test_request_context(): | ||
_, user = login_user(client) | ||
rv = client.get(f"/user/{user.username}") | ||
|
||
assert rv.status_code == HTTPStatus.OK | ||
assert bytes(f"Profile: {user.username}", ENCODING_UTF_8) in rv.data | ||
|
||
|
||
def test_user_can_see_other_users_profile(mockdata, client, session): | ||
with current_app.test_request_context(): | ||
login_user(client) | ||
other_user = User.query.filter_by(email=AC_USER_EMAIL).first() | ||
rv = client.get(f"/user/{other_user.username}") | ||
|
||
assert rv.status_code == HTTPStatus.OK | ||
assert bytes(f"Profile: {other_user.username}", ENCODING_UTF_8) in rv.data | ||
|
||
|
||
def test_ac_user_can_see_other_users_profile(mockdata, client, session): | ||
with current_app.test_request_context(): | ||
login_ac(client) | ||
other_user = User.query.filter_by(email=GENERAL_USER_EMAIL).first() | ||
rv = client.get(f"/user/{other_user.username}") | ||
|
||
assert rv.status_code == HTTPStatus.OK | ||
assert bytes(f"Profile: {other_user.username}", ENCODING_UTF_8) in rv.data | ||
|
||
|
||
def test_admin_user_can_see_other_users_profile(mockdata, client, session): | ||
with current_app.test_request_context(): | ||
login_admin(client) | ||
other_user = User.query.filter_by(email=GENERAL_USER_EMAIL).first() | ||
rv = client.get(f"/user/{other_user.username}") | ||
|
||
assert rv.status_code == HTTPStatus.OK | ||
assert bytes(f"Profile: {other_user.username}", ENCODING_UTF_8) in rv.data |