SHARE
TWEET

Untitled

a guest Mar 20th, 2019 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.contrib.auth.models import User
  2. from django.urls import reverse
  3. from rest_framework.test import APITestCase
  4.  
  5.  
  6. class TestLogin(APITestCase):
  7.     def test_login(self):
  8.         """
  9.         Ensure we can login a user.
  10.         """
  11.         self.create_user()
  12.         data = {"username": "bison", "password": "Pa$$word123"}
  13.         response = self.login(data)
  14.         self.assertEqual(response.status_code, 200)
  15.  
  16.     def test_login_without_password(self):
  17.         """
  18.         Ensure we cannot login without a password.
  19.         """
  20.         self.create_user()
  21.         data = {"username": "bison", "password": ""}
  22.         response = self.login(data)
  23.         self.assertEqual(response.status_code, 400)
  24.  
  25.     def test_login_with_wrong_password(self):
  26.         """
  27.         Ensure we cannot login with a wrong password.
  28.         """
  29.         self.create_user()
  30.         data = {"username": "bison", "password": "12345"}
  31.         response = self.login(data)
  32.         self.assertEqual(response.status_code, 400)
  33.  
  34.     def test_login_with_wrong_username(self):
  35.         """
  36.         Ensure we cannot login with a wrong username.
  37.         """
  38.         self.create_user()
  39.         data = {"username": "bisonlou", "password": "Pa$$word123"}
  40.         response = self.login(data)
  41.         self.assertEqual(response.status_code, 400)
  42.  
  43.     def create_user(self):
  44.         self.superuser = User.objects.create_superuser(
  45.             "bison", "bisonlou@gmail.com", "Pa$$word123"
  46.         )
  47.  
  48.     def login(self, kwags):
  49.         self.username = kwags.get("username")
  50.         self.password = kwags.get("password")
  51.  
  52.         url = reverse("login")
  53.         data = {"username": self.username, "password": self.password}
  54.         return self.client.post(url, data, format="json")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top