Skip to content

Instantly share code, notes, and snippets.

@gitaumoses4
Created September 19, 2018 12:37
Show Gist options
  • Save gitaumoses4/d8dbff3c89778abf9d6ee28308a802dc to your computer and use it in GitHub Desktop.
Save gitaumoses4/d8dbff3c89778abf9d6ee28308a802dc to your computer and use it in GitHub Desktop.
"""
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
@YAHYA-H
Copy link

YAHYA-H commented Sep 19, 2018

Good work. There is docstring included that we forgot about it. 👏

@bevkololi
Copy link

Pep 8, check! Logic, check! That teardown might be unnecessary here though.

@charisschomba
Copy link

Avoid dummy functions. Like tearDown

@gitaumoses4
Copy link
Author

So many years ago. 😂😂😂

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