2014-09-02 16:53:37 +04:00
|
|
|
from django.contrib.auth.models import User
|
2016-06-01 17:31:00 +03:00
|
|
|
from django.test import TestCase, override_settings
|
2014-09-02 16:53:37 +04:00
|
|
|
|
2022-06-08 15:41:26 +03:00
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2014-09-02 16:53:37 +04:00
|
|
|
from rest_framework.test import APIClient
|
|
|
|
|
2022-06-08 15:41:26 +03:00
|
|
|
from .views import BasicModelWithUsersViewSet, OrganizationPermissions
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(ROOT_URLCONF='tests.browsable_api.no_auth_urls')
|
|
|
|
class AnonymousUserTests(TestCase):
|
|
|
|
"""Tests correct handling of anonymous user request on endpoints with IsAuthenticated permission class."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client = APIClient(enforce_csrf_checks=True)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
def test_get_raises_typeerror_when_anonymous_user_in_queryset_filter(self):
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
self.client.get('/basicviewset')
|
|
|
|
|
|
|
|
def test_get_returns_http_forbidden_when_anonymous_user(self):
|
|
|
|
old_permissions = BasicModelWithUsersViewSet.permission_classes
|
|
|
|
BasicModelWithUsersViewSet.permission_classes = [IsAuthenticated, OrganizationPermissions]
|
|
|
|
|
|
|
|
response = self.client.get('/basicviewset')
|
|
|
|
|
|
|
|
BasicModelWithUsersViewSet.permission_classes = old_permissions
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2014-09-02 16:53:37 +04:00
|
|
|
|
2016-06-01 17:31:00 +03:00
|
|
|
@override_settings(ROOT_URLCONF='tests.browsable_api.auth_urls')
|
2014-09-02 16:53:37 +04:00
|
|
|
class DropdownWithAuthTests(TestCase):
|
|
|
|
"""Tests correct dropdown behaviour with Auth views enabled."""
|
|
|
|
def setUp(self):
|
|
|
|
self.client = APIClient(enforce_csrf_checks=True)
|
|
|
|
self.username = 'john'
|
|
|
|
self.email = 'lennon@thebeatles.com'
|
|
|
|
self.password = 'password'
|
2016-06-01 12:40:54 +03:00
|
|
|
self.user = User.objects.create_user(
|
|
|
|
self.username,
|
|
|
|
self.email,
|
|
|
|
self.password
|
|
|
|
)
|
2014-09-02 16:53:37 +04:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
def test_name_shown_when_logged_in(self):
|
|
|
|
self.client.login(username=self.username, password=self.password)
|
|
|
|
response = self.client.get('/')
|
2019-05-01 08:49:17 +03:00
|
|
|
content = response.content.decode()
|
2016-12-01 19:17:36 +03:00
|
|
|
assert 'john' in content
|
2014-09-02 16:53:37 +04:00
|
|
|
|
|
|
|
def test_logout_shown_when_logged_in(self):
|
|
|
|
self.client.login(username=self.username, password=self.password)
|
|
|
|
response = self.client.get('/')
|
2019-05-01 08:49:17 +03:00
|
|
|
content = response.content.decode()
|
2016-12-01 19:17:36 +03:00
|
|
|
assert '>Log out<' in content
|
2014-09-02 16:53:37 +04:00
|
|
|
|
|
|
|
def test_login_shown_when_logged_out(self):
|
|
|
|
response = self.client.get('/')
|
2019-05-01 08:49:17 +03:00
|
|
|
content = response.content.decode()
|
2016-12-01 19:17:36 +03:00
|
|
|
assert '>Log in<' in content
|
2014-09-02 16:53:37 +04:00
|
|
|
|
2024-02-20 01:28:04 +03:00
|
|
|
def test_dropdown_contains_logout_form(self):
|
|
|
|
self.client.login(username=self.username, password=self.password)
|
|
|
|
response = self.client.get('/')
|
|
|
|
content = response.content.decode()
|
|
|
|
assert '<form id="logoutForm" method="post" action="/auth/logout/?next=/">' in content
|
|
|
|
|
2014-09-02 16:53:37 +04:00
|
|
|
|
2016-06-01 17:31:00 +03:00
|
|
|
@override_settings(ROOT_URLCONF='tests.browsable_api.no_auth_urls')
|
2014-09-02 16:53:37 +04:00
|
|
|
class NoDropdownWithoutAuthTests(TestCase):
|
2014-09-02 17:11:23 +04:00
|
|
|
"""Tests correct dropdown behaviour with Auth views NOT enabled."""
|
2014-09-02 16:53:37 +04:00
|
|
|
def setUp(self):
|
|
|
|
self.client = APIClient(enforce_csrf_checks=True)
|
|
|
|
self.username = 'john'
|
|
|
|
self.email = 'lennon@thebeatles.com'
|
|
|
|
self.password = 'password'
|
2016-06-01 12:40:54 +03:00
|
|
|
self.user = User.objects.create_user(
|
|
|
|
self.username,
|
|
|
|
self.email,
|
|
|
|
self.password
|
|
|
|
)
|
2014-09-02 16:53:37 +04:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
def test_name_shown_when_logged_in(self):
|
|
|
|
self.client.login(username=self.username, password=self.password)
|
|
|
|
response = self.client.get('/')
|
2019-05-01 08:49:17 +03:00
|
|
|
content = response.content.decode()
|
2016-12-01 19:17:36 +03:00
|
|
|
assert 'john' in content
|
2014-09-02 16:53:37 +04:00
|
|
|
|
|
|
|
def test_dropdown_not_shown_when_logged_in(self):
|
|
|
|
self.client.login(username=self.username, password=self.password)
|
|
|
|
response = self.client.get('/')
|
2019-05-01 08:49:17 +03:00
|
|
|
content = response.content.decode()
|
2016-12-01 19:17:36 +03:00
|
|
|
assert '<li class="dropdown">' not in content
|
2014-09-02 16:53:37 +04:00
|
|
|
|
|
|
|
def test_dropdown_not_shown_when_logged_out(self):
|
|
|
|
response = self.client.get('/')
|
2019-05-01 08:49:17 +03:00
|
|
|
content = response.content.decode()
|
2016-12-01 19:17:36 +03:00
|
|
|
assert '<li class="dropdown">' not in content
|