| 1 |
from django.contrib.auth.models import User |
| 2 |
from django.test import TestCase, override_settings |
| 3 |
|
| 4 |
from rest_framework.permissions import IsAuthenticated |
| 5 |
from rest_framework.test import APIClient |
| 6 |
|
| 7 |
from .views import BasicModelWithUsersViewSet, OrganizationPermissions |
| 8 |
|
| 9 |
|
| 10 |
@override_settings(ROOT_URLCONF='tests.browsable_api.no_auth_urls') |
| 11 |
class AnonymousUserTests(TestCase): |
| 12 |
"""Tests correct handling of anonymous user request on endpoints with IsAuthenticated permission class.""" |
| 13 |
|
| 14 |
def setUp(self): |
| 15 |
self.client = APIClient(enforce_csrf_checks=True) |
| 16 |
|
| 17 |
def tearDown(self): |
| 18 |
self.client.logout() |
| 19 |
|
| 20 |
def test_get_raises_typeerror_when_anonymous_user_in_queryset_filter(self): |
| 21 |
with self.assertRaises(TypeError): |
| 22 |
self.client.get('/basicviewset') |
| 23 |
|
| 24 |
def test_get_returns_http_forbidden_when_anonymous_user(self): |
| 25 |
old_permissions = BasicModelWithUsersViewSet.permission_classes |
| 26 |
BasicModelWithUsersViewSet.permission_classes = [IsAuthenticated, OrganizationPermissions] |
| 27 |
|
| 28 |
response = self.client.get('/basicviewset') |
| 29 |
|
| 30 |
BasicModelWithUsersViewSet.permission_classes = old_permissions |
| 31 |
self.assertEqual(response.status_code, 403) |
| 32 |
|
| 33 |
|
| 34 |
@override_settings(ROOT_URLCONF='tests.browsable_api.auth_urls') |
| 35 |
class DropdownWithAuthTests(TestCase): |
| 36 |
"""Tests correct dropdown behavior with Auth views enabled.""" |
| 37 |
def setUp(self): |
| 38 |
self.client = APIClient(enforce_csrf_checks=True) |
| 39 |
self.username = 'john' |
| 40 |
self.email = '[email protected]' |
| 41 |
self.password = 'password' |
| 42 |
self.user = User.objects.create_user( |
| 43 |
self.username, |
| 44 |
self.email, |
| 45 |
self.password |
| 46 |
) |
| 47 |
|
| 48 |
def tearDown(self): |
| 49 |
self.client.logout() |
| 50 |
|
| 51 |
def test_name_shown_when_logged_in(self): |
| 52 |
self.client.login(username=self.username, password=self.password) |
| 53 |
response = self.client.get('/') |
| 54 |
content = response.content.decode() |
| 55 |
assert 'john' in content |
| 56 |
|
| 57 |
def test_logout_shown_when_logged_in(self): |
| 58 |
self.client.login(username=self.username, password=self.password) |
| 59 |
response = self.client.get('/') |
| 60 |
content = response.content.decode() |
| 61 |
assert '>Log out<' in content |
| 62 |
|
| 63 |
def test_login_shown_when_logged_out(self): |
| 64 |
response = self.client.get('/') |
| 65 |
content = response.content.decode() |
| 66 |
assert '>Log in<' in content |
| 67 |
|
| 68 |
def test_dropdown_contains_logout_form(self): |
| 69 |
self.client.login(username=self.username, password=self.password) |
| 70 |
response = self.client.get('/') |
| 71 |
content = response.content.decode() |
| 72 |
assert '<form id="logoutForm" method="post" action="/auth/logout/?next=/">' in content |
| 73 |
|
| 74 |
|
| 75 |
@override_settings(ROOT_URLCONF='tests.browsable_api.no_auth_urls') |
| 76 |
class NoDropdownWithoutAuthTests(TestCase): |
| 77 |
"""Tests correct dropdown behavior with Auth views NOT enabled.""" |
| 78 |
def setUp(self): |
| 79 |
self.client = APIClient(enforce_csrf_checks=True) |
| 80 |
self.username = 'john' |
| 81 |
self.email = '[email protected]' |
| 82 |
self.password = 'password' |
| 83 |
self.user = User.objects.create_user( |
| 84 |
self.username, |
| 85 |
self.email, |
| 86 |
self.password |
| 87 |
) |
| 88 |
|
| 89 |
def tearDown(self): |
| 90 |
self.client.logout() |
| 91 |
|
| 92 |
def test_name_shown_when_logged_in(self): |
| 93 |
self.client.login(username=self.username, password=self.password) |
| 94 |
response = self.client.get('/') |
| 95 |
content = response.content.decode() |
| 96 |
assert 'john' in content |
| 97 |
|
| 98 |
def test_dropdown_not_shown_when_logged_in(self): |
| 99 |
self.client.login(username=self.username, password=self.password) |
| 100 |
response = self.client.get('/') |
| 101 |
content = response.content.decode() |
| 102 |
assert '<li class="dropdown">' not in content |
| 103 |
|
| 104 |
def test_dropdown_not_shown_when_logged_out(self): |
| 105 |
response = self.client.get('/') |
| 106 |
content = response.content.decode() |
| 107 |
assert '<li class="dropdown">' not in content |