|
| 1 | +import humps |
| 2 | +import pytest |
| 3 | +from django import test |
| 4 | +from django.contrib.auth.models import User |
| 5 | +from django.urls import reverse |
| 6 | + |
| 7 | + |
| 8 | +def test_profile_updates_correctly( |
| 9 | + profile_admin_client: test.Client, user: User, update_profile_params |
| 10 | +): |
| 11 | + url = f"{reverse('admin_update_profile')}?email={user.email}" |
| 12 | + res = profile_admin_client.patch(url, humps.camelize(update_profile_params)) |
| 13 | + |
| 14 | + assert res.status_code == 200 |
| 15 | + |
| 16 | + user.refresh_from_db() |
| 17 | + profile = user.profile |
| 18 | + |
| 19 | + for key, val in update_profile_params.items(): |
| 20 | + assert getattr(profile, key) == val |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize( |
| 24 | + argnames="method, status", |
| 25 | + argvalues=[("get", 400), ("put", 400), ("post", 405), ("patch", 400)], |
| 26 | +) |
| 27 | +def test_requires_query_param( |
| 28 | + profile_admin_client: test.Client, method: str, status: int |
| 29 | +): |
| 30 | + request_method = getattr(profile_admin_client, method) |
| 31 | + url = f"{reverse('admin_update_profile')}" |
| 32 | + res = request_method(url) |
| 33 | + |
| 34 | + assert res.status_code == status |
| 35 | + |
| 36 | + |
| 37 | +def test_missing_profile_returns_404(profile_admin_client: test.Client): |
| 38 | + url = f"{reverse('admin_update_profile')}?email=abc" |
| 39 | + res = profile_admin_client.get(url) |
| 40 | + |
| 41 | + assert res.status_code == 404 |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize( |
| 45 | + argnames="method, status", argvalues=[("get", 200), ("post", 405), ("patch", 200)] |
| 46 | +) |
| 47 | +def test_staff_user_has_access( |
| 48 | + authed_admin_client: test.Client, user: User, method: str, status: int |
| 49 | +): |
| 50 | + request_method = getattr(authed_admin_client, method) |
| 51 | + url = f"{reverse('admin_update_profile')}?email={user.email}" |
| 52 | + res = request_method(url) |
| 53 | + |
| 54 | + assert res.status_code == status |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.parametrize( |
| 58 | + argnames="method, status", |
| 59 | + argvalues=[("get", 403), ("put", 403), ("post", 405), ("patch", 403)], |
| 60 | +) |
| 61 | +def test_view_requires_profile_admin_group( |
| 62 | + authed_client: test.Client, user: User, method: str, status: int |
| 63 | +): |
| 64 | + request_method = getattr(authed_client, method) |
| 65 | + url = f"{reverse('admin_update_profile')}?email={user.email}" |
| 66 | + res = request_method(url) |
| 67 | + |
| 68 | + assert res.status_code == status |
0 commit comments