Skip to content

Instantly share code, notes, and snippets.

@mutaimwiti
Last active September 19, 2018 13:02
Show Gist options
  • Save mutaimwiti/d937a8e6e3d3a053f95bdf1bd6736528 to your computer and use it in GitHub Desktop.
Save mutaimwiti/d937a8e6e3d3a053f95bdf1bd6736528 to your computer and use it in GitHub Desktop.
Example Python test
from unittest import TestCase
from rest_framework import status
from rest_framework.test import APIClient
class BaseTestCase(TestCase):
def setUp(self):
self.client = APIClient()
def post_login(self, username='johndoe', password='P@$$word'):
return self.client.post('/api/auth/login', {'username': username, 'password': password})
class TestLogin(BaseTestCase):
def test_successful_login(self):
response = self.post_login()
self.assertEqual(response.status, status.HTTP_200_OK)
def test_missing_username_is_not_allowed(self):
response = self.post_login(username='')
self.assertEqual(response.status, status.HTTP_422_UNPROCESSABLE_ENTITY)
self.assertIn(response.data['errors'], 'An username is required to log in.')
def test_missing_password_is_not_allowed(self):
response = self.post_login(password='')
self.assertEqual(response.status, status.HTTP_422_UNPROCESSABLE_ENTITY)
self.assertIn(response.data['errors'], 'A password is required to log in.')
def test_username_with_special_chars_is_not_allowed(self):
response = self.post_login(username='u$ern@me')
self.assertIn(response.data['errors'], 'Special characters are not allowed on the username.')
@bevkololi
Copy link

This was thorough and well thought.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment