Created
September 19, 2018 12:37
-
-
Save gitaumoses4/d8dbff3c89778abf9d6ee28308a802dc to your computer and use it in GitHub Desktop.
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
""" | |
LOGIN TEST | |
Tests the following functionalities: | |
1. User can login with their valid username and password | |
2. User cannot login with a username that does not exist in the system | |
""" | |
from django.test import TestCase | |
from rest_framework import status | |
from rest_framework.test import APIClient | |
class LoginTestCase(TestCase): | |
""" | |
Tests the login functionality | |
""" | |
def setUp(self): | |
self.client = APIClient() | |
self.user = { | |
"username": "Moses Gitau", | |
"password": "password" | |
} | |
def login(self, user): | |
""" | |
Encapsulate the login method | |
""" | |
return self.client.post("/api/users", user, format="json") | |
def test_successful_login(self): | |
""" | |
Tests whether the login is successful | |
""" | |
response = self.login(self.user) | |
self.assertEqual(response.data['message'], "Login successful") | |
self.assertEqual(response.response_code, status.HTTP_200_OK) | |
def test_cannot_login_with_invalid_username(self): | |
""" | |
Tests that the user cannot login with an invalid username | |
An invalid username - a username that does not exist in the system | |
""" | |
response = self.login({"username": "maljdfad", "password": "password"}) | |
self.assertEqual(response.data['error'], "Invalid username") | |
def tearDown(self): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So many years ago. 😂😂😂