Skip to content

Commit abb8fb5

Browse files
committed
Add check for missing Profile. closes #127
1 parent 8fd4622 commit abb8fb5

3 files changed

Lines changed: 107 additions & 4 deletions

File tree

src/core/views.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from drf_yasg.utils import swagger_auto_schema
1111
from rest_auth.registration.views import RegisterView as BaseRegisterView
1212
from rest_auth.registration.views import SocialConnectView, SocialLoginView
13+
from rest_framework.exceptions import NotFound, ValidationError, bad_request
1314
from rest_framework.generics import RetrieveUpdateAPIView
1415
from rest_framework.permissions import AllowAny, IsAuthenticated
1516

@@ -69,13 +70,17 @@ class AdminUpdateProfile(RetrieveUpdateAPIView):
6970
}
7071

7172
def get_object(self):
72-
email = self.request.query_params["email"]
73+
email = self.request.query_params.get("email")
7374
if email:
74-
profile = Profile.objects.get(user__email=email)
75+
try:
76+
profile = Profile.objects.get(user__email=email)
77+
except Profile.DoesNotExist:
78+
raise NotFound
79+
7580
self.check_permissions(self.request)
7681
return profile
7782

78-
return None
83+
raise ValidationError({"error": "Missing email query param"})
7984

8085
@swagger_auto_schema(manual_parameters=[email_param])
8186
def get(self, request, *args, **kwargs):

src/tests/fixtures.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import factory
44
import pytest
5-
from django.contrib.auth.models import User
5+
from django.contrib.auth.models import Group, User
66
from rest_framework.test import APIClient
77
from rest_framework_jwt.settings import api_settings
88

@@ -24,6 +24,20 @@ def user(db) -> User:
2424
return user
2525

2626

27+
@pytest.fixture
28+
def profile_admin_group(db) -> Group:
29+
group = Group(name="ProfileAdmin")
30+
group.save()
31+
return group
32+
33+
34+
@pytest.fixture
35+
def profile_admin(user: User, profile_admin_group: Group) -> User:
36+
user.groups.add(profile_admin_group)
37+
user.save()
38+
return user
39+
40+
2741
@pytest.fixture
2842
def authed_client(client, user: User):
2943
payload = jwt_payload_handler(user)
@@ -32,6 +46,22 @@ def authed_client(client, user: User):
3246
return client
3347

3448

49+
@pytest.fixture
50+
def authed_admin_client(client, admin_user: User):
51+
payload = jwt_payload_handler(admin_user)
52+
jwt = jwt_encode_handler(payload)
53+
client.credentials(HTTP_AUTHORIZATION=f"Bearer {jwt}")
54+
return client
55+
56+
57+
@pytest.fixture
58+
def profile_admin_client(client, profile_admin: User):
59+
payload = jwt_payload_handler(profile_admin)
60+
jwt = jwt_encode_handler(payload)
61+
client.credentials(HTTP_AUTHORIZATION=f"Bearer {jwt}")
62+
return client
63+
64+
3565
@pytest.fixture
3666
def register_form() -> Dict[str, str]:
3767
user = f.UserFactory.build()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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

Comments
 (0)