-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1820 from carltongibson/login-dropdown
Hide login link in browsable API if the login view is not registered.
- Loading branch information
Showing
7 changed files
with
124 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from __future__ import unicode_literals | ||
from django.conf.urls import patterns, url, include | ||
|
||
from .views import MockView | ||
|
||
urlpatterns = patterns( | ||
'', | ||
(r'^$', MockView.as_view()), | ||
url(r'^auth/', include('rest_framework.urls', namespace='rest_framework')), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from __future__ import unicode_literals | ||
from django.conf.urls import patterns | ||
|
||
from .views import MockView | ||
|
||
urlpatterns = patterns( | ||
'', | ||
(r'^$', MockView.as_view()), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from __future__ import unicode_literals | ||
from django.contrib.auth.models import User | ||
from django.test import TestCase | ||
|
||
from rest_framework.test import APIClient | ||
|
||
|
||
class DropdownWithAuthTests(TestCase): | ||
"""Tests correct dropdown behaviour with Auth views enabled.""" | ||
|
||
urls = 'tests.browsable_api.auth_urls' | ||
|
||
def setUp(self): | ||
self.client = APIClient(enforce_csrf_checks=True) | ||
self.username = 'john' | ||
self.email = 'lennon@thebeatles.com' | ||
self.password = 'password' | ||
self.user = User.objects.create_user(self.username, self.email, self.password) | ||
|
||
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('/') | ||
self.assertContains(response, 'john') | ||
|
||
def test_logout_shown_when_logged_in(self): | ||
self.client.login(username=self.username, password=self.password) | ||
response = self.client.get('/') | ||
self.assertContains(response, '>Log out<') | ||
|
||
def test_login_shown_when_logged_out(self): | ||
response = self.client.get('/') | ||
self.assertContains(response, '>Log in<') | ||
|
||
|
||
class NoDropdownWithoutAuthTests(TestCase): | ||
"""Tests correct dropdown behaviour with Auth views NOT enabled.""" | ||
|
||
urls = 'tests.browsable_api.no_auth_urls' | ||
|
||
def setUp(self): | ||
self.client = APIClient(enforce_csrf_checks=True) | ||
self.username = 'john' | ||
self.email = 'lennon@thebeatles.com' | ||
self.password = 'password' | ||
self.user = User.objects.create_user(self.username, self.email, self.password) | ||
|
||
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('/') | ||
self.assertContains(response, 'john') | ||
|
||
def test_dropdown_not_shown_when_logged_in(self): | ||
self.client.login(username=self.username, password=self.password) | ||
response = self.client.get('/') | ||
self.assertNotContains(response, '<li class="dropdown">') | ||
|
||
def test_dropdown_not_shown_when_logged_out(self): | ||
response = self.client.get('/') | ||
self.assertNotContains(response, '<li class="dropdown">') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from __future__ import unicode_literals | ||
|
||
from rest_framework.views import APIView | ||
from rest_framework import authentication | ||
from rest_framework import renderers | ||
from rest_framework.response import Response | ||
|
||
|
||
class MockView(APIView): | ||
|
||
authentication_classes = (authentication.SessionAuthentication,) | ||
renderer_classes = (renderers.BrowsableAPIRenderer,) | ||
|
||
def get(self, request): | ||
return Response({'a': 1, 'b': 2, 'c': 3}) |