-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathtest_views.py
More file actions
40 lines (33 loc) · 1.55 KB
/
test_views.py
File metadata and controls
40 lines (33 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from django.core.cache import cache
from django.test import TestCase
from django.urls import reverse
from openwisp_users.tests.utils import TestOrganizationMixin
class TestRestFrameworkViews(TestOrganizationMixin, TestCase):
def setUp(self):
cache.clear()
def test_obtain_auth_token(self):
self._create_user(username="tester", password="tester")
params = {"username": "tester", "password": "tester"}
url = reverse("users:user_auth_token")
r = self.client.post(url, params)
self.assertIn("token", r.data)
def test_protected_api_mixin_view(self):
auth_error = "Authentication credentials were not provided."
user = self._create_user(username="tester", password="tester")
path = reverse("users:user_detail", args=(user.pk,))
response = self.client.get(path)
self.assertEqual(response.headers["WWW-Authenticate"], "Bearer")
self.assertEqual(response.data["detail"], auth_error)
self.assertEqual(response.status_code, 401)
def test_invalid_uuid_routes_return_404(self):
invalid_uuid_paths = (
"/api/v1/users/user/not-a-uuid/",
"/api/v1/users/organization/not-a-uuid/",
"/api/v1/users/user/not-a-uuid/password/",
"/api/v1/users/user/not-a-uuid/email/",
"/api/v1/users/user/not-a-uuid/email/1/",
)
for path in invalid_uuid_paths:
with self.subTest(path=path):
response = self.client.get(path)
self.assertEqual(response.status_code, 404)