mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-11-04 01:47:59 +03:00 
			
		
		
		
	A Python 3 cleanup that allows for less noise in the code. https://docs.python.org/3/library/stdtypes.html#bytes.decode https://docs.python.org/3/library/stdtypes.html#str.encode
		
			
				
	
	
		
			75 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from django.contrib.auth.models import User
 | 
						|
from django.test import TestCase, override_settings
 | 
						|
 | 
						|
from rest_framework.test import APIClient
 | 
						|
 | 
						|
 | 
						|
@override_settings(ROOT_URLCONF='tests.browsable_api.auth_urls')
 | 
						|
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'
 | 
						|
        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('/')
 | 
						|
        content = response.content.decode()
 | 
						|
        assert 'john' in content
 | 
						|
 | 
						|
    def test_logout_shown_when_logged_in(self):
 | 
						|
        self.client.login(username=self.username, password=self.password)
 | 
						|
        response = self.client.get('/')
 | 
						|
        content = response.content.decode()
 | 
						|
        assert '>Log out<' in content
 | 
						|
 | 
						|
    def test_login_shown_when_logged_out(self):
 | 
						|
        response = self.client.get('/')
 | 
						|
        content = response.content.decode()
 | 
						|
        assert '>Log in<' in content
 | 
						|
 | 
						|
 | 
						|
@override_settings(ROOT_URLCONF='tests.browsable_api.no_auth_urls')
 | 
						|
class NoDropdownWithoutAuthTests(TestCase):
 | 
						|
    """Tests correct dropdown behaviour with Auth views NOT enabled."""
 | 
						|
    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('/')
 | 
						|
        content = response.content.decode()
 | 
						|
        assert 'john' in content
 | 
						|
 | 
						|
    def test_dropdown_not_shown_when_logged_in(self):
 | 
						|
        self.client.login(username=self.username, password=self.password)
 | 
						|
        response = self.client.get('/')
 | 
						|
        content = response.content.decode()
 | 
						|
        assert '<li class="dropdown">' not in content
 | 
						|
 | 
						|
    def test_dropdown_not_shown_when_logged_out(self):
 | 
						|
        response = self.client.get('/')
 | 
						|
        content = response.content.decode()
 | 
						|
        assert '<li class="dropdown">' not in content
 |