from __future__ import unicode_literals from django.contrib.auth.models import User from django.test import TestCase from rest_framework.test import APIClient from .models import Foo, Bar 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) foo = Foo.objects.create(name='Foo') Bar.objects.create(foo=foo) 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_bug_2455_clone_request(self): self.client.login(username=self.username, password=self.password) json_response = self.client.get('/foo/1/?format=json') self.assertEqual(json_response.status_code, 200) browsable_api_response = self.client.get('/foo/1/') self.assertEqual(browsable_api_response.status_code, 200) 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, '