Skip to content

Instantly share code, notes, and snippets.

@makmac213
Last active December 12, 2015 04:09
Show Gist options
  • Save makmac213/4712441 to your computer and use it in GitHub Desktop.
Save makmac213/4712441 to your computer and use it in GitHub Desktop.
Just some few basics for me on learning tests.
# Mark Allan B. Meriales
# Client testing basic
# If you want to generate fixtures out of your
# existing models and database contents you may
# try my manage.py command below
# https://gist.github.com/makmac213/4671624
import unittest
from django.test.client import Client
from django.contrib.auth.models import User, Group
"""
Method Checks that
assertEqual(a, b) a == b
assertNotEqual(a, b) a != b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIs(a, b) a is b
assertIsNot(a, b) a is not b
assertIsNone(x) x is None
assertIsNotNone(x) x is not None
assertIn(a, b) a in b
assertNotIn(a, b) a not in b
assertIsInstance(a, b) isinstance(a, b)
assertNotIsInstance(a, b) not isinstance(a, b)
"""
TESTUSERS = {
'superuser': {
'username': 'admin.test@mailcatch.com',
#'email': 'admin.test@mailcatch.com',
'password': '123456'
},
'member': {
'username': 'member.test@mailcatch.com',
#'email': 'member.test@mailcatch.com',
'password': '123456'
}
}
class MyTestCase(unittest.TestCase):
def setUp(self):
"""
Initialize here.
Will run on every test.
"""
# initialize the test client
self.client = Client()
# If your app needs some user you can create some here or
# via fixtures. I will create some for login purposes.
# Create a superuser for backoffice/admin testing
self._init_superuser()
# Create a member for frontend/profile testing
self._init_memberuser()
def tearDown(self):
"""
cleanup goes here
"""
self.client.logout()
# privates
def _init_superuser(self):
"""
Initialize test superuser
"""
try:
# check to see if our superuser already exist
self.superuser = User.objects.get(username=TESTUSERS['superuser']['username'])
except:
# a DoesNotExist exception is raised, create superuser
self.superuser = User.objects.create_superuser(
username = TESTUSERS['superuser']['username'],
email = TESTUSERS['superuser']['username'],
password = TESTUSERS['superuser']['password']
)
def _init_memberuser(self):
"""
Initialize test member
"""
try:
# check to see if our superuser already exist
self.member = User.objects.get(username=TESTUSERS['member']['username'])
except:
# a DoesNotExist exception is raised, create superuser
self.member = User.objects.create_user(
username = TESTUSERS['member']['username'],
email = TESTUSERS['member']['username'],
password = TESTUSERS['member']['password']
)
def _login(self, user):
return self.client.login(
username = user['username'],
password = user['password']
)
# Testing starts from here #
# The order of the tests is alphabetical/numerical order.
def superuser_can_login(self):
# if user can't login, test will fail
self.assertTrue(self._login(TESTUSERS['superuser']))
def memberuser_can_login(self):
# if user can't login, test will fail
self.assertTrue(self._login(TESTUSERS['member']))
def superuser_page_not_restricted(self):
"""
A sample test for checking if
superuser is allowed for a certain page
"""
is_logged_in = self._login(TESTUSERS['superuser'])
# this is our message if access is denied
# i just assumed there is a deny message
# TODO: if no message, check response status code
# or content
deny_msg = "Access Denied"
# follow redirect
# refer to urls.py for the path
response = self.client.get(
'/path/',
follow = True
)
# if deny_msg is found in the response content
# the test will fail
self.assertTrue(deny_msg in response.content)
def memberuser_page_restricted(self):
"""
A sample test for checking if
memberuser is not allowed for a certain page
"""
is_logged_in = self._login(TESTUSERS['member'])
# this is our message if access is denied
# i just assumed there is a deny message
# TODO: if no message, check response status code
# or content
deny_msg = "Access Denied"
# follow redirect
# refer to urls.py for the path
response = self.client.get(
'/path/',
follow = True
)
# if deny_msg is not found in the response content
# the test will fail
self.assertFalse(deny_msg in response.content)
def superuser_create_member(self):
"""
A sample test to check if
superuser can create a user
"""
is_logged_in = self._login(self._login(TESTUSERS['superuser'])
testuser = 'new_user@mailcatch.com'
# we post a request to create-user
# that view will create a user
response = self.client.post(
'/create-user/',
{
'username': testuser,
'password': '123456',
'email': testuser
},
follow = True
)
user = User.objects.filter(username=testuser)
# If the view failed to create the requested
# user, user will be None and test will fail
self.assertIsNotNone(user)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment