Skip to content

Instantly share code, notes, and snippets.

@johnwayodi
Last active December 19, 2018 11:42
Show Gist options
  • Select an option

  • Save johnwayodi/7f479f252451a601e4c67bfdcf3101db to your computer and use it in GitHub Desktop.

Select an option

Save johnwayodi/7f479f252451a601e4c67bfdcf3101db to your computer and use it in GitHub Desktop.
Test that a User can Login with correct details ​
from django.test import TestCase
from rest_framework.test import APIClient
from rest_framework import status
from django.core.urlresolvers import reverse
class LoginTestCase(TestCase):
"""Test suite for the api login views."""
def setUp(self):
"""Set up test client"""
self.client = APIClient()
self.user_data = {'name': 'JohnDoe', 'password': 'johnDoe2341'}
def test_user_login_valid_credentials(self):
"""User should be able to login when valid
credentials are provided."""
# create the user
self.response = self.client.post(reverse('register'),
self.user_data, format="json")
self.assertEqual(self.response.status_code,
status.HTTP_201_CREATED)
# login the user
self.response = self.client.post(reverse('login'),
self.user_data,
format="json")
self.assertEqual(self.response.status_code, status.HTTP_200_OK)
@waracci

waracci commented Dec 19, 2018

Copy link
Copy Markdown

Make sure to import the TestCase module from django:

from django.test import TestCase

@johnwayodi

Copy link
Copy Markdown
Author

Make sure to import the TestCase module from django:

from django.test import TestCase

Thanks for noticing that, I'll make sure to add that

@Teatoller

Copy link
Copy Markdown

Hi John,

I have noticed a typo that will definately give you a bug.
self.user_data = {'name': 'JohnDoe', 'password': 'johnDoe2341}
missing a single quotation on password.

Regards
Steven Ennis

@sharkdevs

Copy link
Copy Markdown

Hello wayodi,

Check grammar on your comments. Usershould should be users should

@Paulvitalis200

Copy link
Copy Markdown

Update your docstring at the beginning of test_user_login_valid_credentials method to be grammatically
correct

@johnwayodi

Copy link
Copy Markdown
Author

Hi John,

I have noticed a typo that will definately give you a bug.
self.user_data = {'name': 'JohnDoe', 'password': 'johnDoe2341}
missing a single quotation on password.

Regards
Steven Ennis

Thanks for noticing that. lemme correct that

@johnwayodi

Copy link
Copy Markdown
Author

PaulStar200, sharkdevs thanks for the feedback, I'll correct that.

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