test cleanup

This commit is contained in:
Mateusz Sikora 2014-05-01 22:32:30 +02:00
parent 4be5debc5f
commit 2bde528074
2 changed files with 116 additions and 304 deletions

View File

@ -1,294 +0,0 @@
import requests
import json
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.conf import settings
from rest_framework.serializers import _resolve_model
from registration.models import RegistrationProfile
# Get the UserProfile model from the setting value
user_profile_model = _resolve_model(
getattr(settings, 'REST_PROFILE_MODULE', None))
# Get the REST Registration Backend for django-registration
registration_backend = getattr(settings, 'REST_REGISTRATION_BACKEND',
'rest_auth.backends.rest_registration.RESTRegistrationView')
class RegistrationAndActivationTestCase(TestCase):
"""
Unit Test for registering and activating a new user
This test case assumes that the local server runs at port 8000.
"""
def setUp(self):
self.url = "http://localhost:8000/rest_auth/register/"
self.headers = {"content-type": "application/json"}
def test_successful_registration(self):
print 'Registering a new user'
payload = {"username": "person", "password": "person",
"email": "person@world.com", "newsletter_subscribe": "false"}
print 'The request will attempt to register:'
print 'Django User object'
print 'Username: %s\nPassword: %s\nEmail: %s\n' % ('person', 'person', 'person@world.com')
print 'Django UserProfile object'
print 'newsletter_subscribe: false'
print 'Sending a POST request to register API'
r = requests.post(self.url, data=json.dumps(payload),
headers=self.headers)
if self.assertEqual(r.status_code, 201):
print r.content
print 'Activating a new user'
# Get the latest activation key from RegistrationProfile model
activation_key = RegistrationProfile.objects.latest(
'id').activation_key
# Set the url and GET the request to verify and activate a new user
url = "http://localhost:8000/rest_auth/verify-email/" + \
activation_key + "/"
r = requests.get(url)
print "Sending a GET request to activate the user from verify-email API"
if self.assertEqual(r.status_code, 200):
print r.content
# Get the latest User object
new_user = get_user_model().objects.latest('id')
print "Got the new user %s" % new_user.username
try:
print "Got the new user profile %s" % (user_profile_model.objects.get(user=new_user))
except user_profile_model.DoesNotExist:
pass
def test_successful_registration_without_userprofile_model(self):
print 'Registering a new user'
payload = {"username": "person1", "password":
"person1", "email": "person1@world.com"}
print 'The request will attempt to register:'
print 'Django User object'
print 'Username: %s\nPassword: %s\nEmail: %s\n' % ('person1', 'person1', 'person1@world.com')
print 'No Django UserProfile object'
print 'Sending a POST request to register API'
r = requests.post(self.url, data=json.dumps(payload),
headers=self.headers)
if self.assertEqual(r.status_code, 201):
print r.content
print 'Activating a new user'
# Get the latest activation key from RegistrationProfile model
activation_key = RegistrationProfile.objects.latest(
'id').activation_key
# Set the url and GET the request to verify and activate a new user
url = "http://localhost:8000/rest_auth/verify-email/" + \
activation_key + "/"
r = requests.get(url)
print "Sending a GET request to activate the user from verify-email API"
if self.assertEqual(r.status_code, 200):
print r.content
# Get the latest User object
new_user = get_user_model().objects.latest('id')
print "Got the new user %s" % new_user.username
try:
print "Got the new user profile %s" % (user_profile_model.objects.get(user=new_user))
except user_profile_model.DoesNotExist:
pass
def test_required_fields_for_registration(self):
print 'Registering a new user'
payload = {}
print 'The request will attempt to register with no data provided.'
print 'Sending a POST request to register API'
r = requests.post(self.url, data=json.dumps(payload),
headers=self.headers)
if self.assertEqual(r.status_code, 400):
print r.content
class LoginTestCase(TestCase):
"""
Unit Test for logging in
This test case assumes that the local server runs at port 8000.
"""
def setUp(self):
self.url = "http://localhost:8000/rest_auth/login/"
self.headers = {"content-type": "application/json"}
def test_successful_login(self):
print 'Logging in as a new user'
payload = {"username": "person", "password": "person"}
print 'The request will attempt to login:'
print 'Username: %s\nPassword: %s' % ('person', 'person')
print 'Sending a POST request to login API'
r = requests.post(self.url, data=json.dumps(payload),
headers=self.headers)
if self.assertEqual(r.status_code, 200):
print r.content
print "Got the REST Token: " + r.json()['key']
def test_invalid_login(self):
print 'Logging in as a new user'
payload = {"username": "person", "password": "person32"}
print 'The request will attempt to login:'
print 'Username: %s\nPassword: %s' % ('person', 'person32')
print 'Sending a POST request to login API'
r = requests.post(self.url, data=json.dumps(payload),
headers=self.headers)
if self.assertEqual(r.status_code, 401):
print r.content
def test_required_fields_for_login(self):
print 'Logging in as a new user'
payload = {}
print 'The request will attempt to login with no data provided.'
print 'Sending a POST request to login API'
r = requests.post(self.url, data=json.dumps(payload),
headers=self.headers)
if self.assertEqual(r.status_code, 400):
print r.content
class PasswordChangeCase(TestCase):
"""
Unit Test for changing the password while logged in
This test case assumes that the local server runs at port 8000.
"""
def setUp(self):
self.url = "http://localhost:8000/rest_auth/password/change/"
self.headers = {"content-type": "application/json"}
def test_successful_password_change(self):
print 'Logging in'
payload = {"username": "person", "password": "person"}
login_url = "http://localhost:8000/rest_auth/login/"
print 'Sending a POST request to login API'
r = requests.post(login_url, data=json.dumps(payload),
headers=self.headers)
if self.assertEqual(r.status_code, 200):
print r.content
print "Got the REST Token: " + r.json()['key']
self.token = r.json()['key']
self.headers['authorization'] = "Token " + r.json()['key']
payload = {"new_password1": "new_person",
"new_password2": "new_person"}
print 'Sending a POST request to password change API'
r = requests.post(self.url, data=json.dumps(payload),
headers=self.headers)
if self.assertEqual(r.status_code, 200):
print r.content
payload = {"new_password1": "person",
"new_password2": "person"}
print 'Sending a POST request to password change API'
r = requests.post(
self.url, data=json.dumps(payload), headers=self.headers)
if self.assertEqual(r.status_code, 200):
print r.content
def test_invalid_password_change(self):
print 'Logging in'
payload = {"username": "person", "password": "person"}
login_url = "http://localhost:8000/rest_auth/login/"
print 'Sending a POST request to login API'
r = requests.post(login_url, data=json.dumps(payload),
headers=self.headers)
if self.assertEqual(r.status_code, 200):
print r.content
print "Got the REST Token: " + r.json()['key']
self.token = r.json()['key']
self.headers['authorization'] = "Token " + r.json()['key']
payload = {"new_password1": "new_person",
"new_password2": "wrong_person"}
print 'Sending a POST request to password change API'
r = requests.post(self.url, data=json.dumps(payload),
headers=self.headers)
if self.assertEqual(r.status_code, 400):
print r.content
def test_required_fields_for_password_change(self):
print 'Logging in'
payload = {"username": "person", "password": "person"}
login_url = "http://localhost:8000/rest_auth/login/"
print 'Sending a POST request to login API'
r = requests.post(login_url, data=json.dumps(payload),
headers=self.headers)
if self.assertEqual(r.status_code, 200):
print r.content
print "Got the REST Token: " + r.json()['key']
self.token = r.json()['key']
self.headers['authorization'] = "Token " + r.json()['key']
payload = {}
print 'The request will attempt to login with no data provided.'
print 'Sending a POST request to password change API'
r = requests.post(self.url, data=json.dumps(payload),
headers=self.headers)
if self.assertEqual(r.status_code, 400):
print r.content

View File

@ -1,13 +1,16 @@
import json
import os
from datetime import datetime, date, time
from pprint import pprint
from django.conf import settings
from django.test.client import Client, MULTIPART_CONTENT
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from registration.models import RegistrationProfile
from django.contrib.auth import get_user_model
from rest_framework.serializers import _resolve_model
class APIClient(Client):
@ -113,6 +116,10 @@ class BaseAPITestCase(object):
# -----------------------
# T E S T H E R E
# -----------------------
user_profile_model = _resolve_model(
getattr(settings, 'REST_PROFILE_MODULE', None))
class LoginAPITestCase(TestCase, BaseAPITestCase):
"""
@ -122,9 +129,12 @@ class LoginAPITestCase(TestCase, BaseAPITestCase):
USERNAME = 'person'
PASS = 'person'
def setUp(self):
self.init()
self.login_url = reverse('rest_login')
self.password_change_url = reverse('rest_password_change')
self.register_url = reverse('rest_register')
def test_login(self):
payload = {
@ -134,21 +144,117 @@ class LoginAPITestCase(TestCase, BaseAPITestCase):
# there is no users in db so it should throw error (401)
self.post(self.login_url, data=payload, status_code=401)
# you can easily print response
pprint(self.response.json)
self.post(self.password_change_url, status_code=403)
# create user
user = User.objects.create_user(self.USERNAME, '', self.PASS)
self.post(self.login_url, data=payload, status_code=200)
self.assertEqual('key' in self.response.json.keys(), True)
self.token = self.response.json['key']
# TODO:
# now all urls that required token should be available
# would be perfect to test one of
self.post(self.password_change_url, status_code=400)
# test inactive user
user.is_active = False
user.save()
self.post(self.login_url, data=payload, status_code=401)
# test wrong username/password
payload = {
"username": self.USERNAME+'?',
"password": self.PASS
}
self.post(self.login_url, data=payload, status_code=401)
# test empty payload
self.post(self.login_url, data={}, status_code=400)
# TODO:
# another case to test - make user inactive and test if login is
# impossible
def test_password_change(self):
login_payload = {
"username": self.USERNAME,
"password": self.PASS
}
user = User.objects.create_user(self.USERNAME, '', self.PASS)
self.post(self.login_url, data=login_payload, status_code=200)
self.token = self.response.json['key']
new_password_payload = {
"new_password1": "new_person",
"new_password2": "new_person"
}
self.post(self.password_change_url, data=new_password_payload,
status_code=200)
# user should not be able to login using old password
self.post(self.login_url, data=login_payload, status_code=401)
# new password should work
login_payload['password'] = new_password_payload['new_password1']
self.post(self.login_url, data=login_payload, status_code=200)
# pass1 and pass2 are not equal
new_password_payload = {
"new_password1": "new_person1",
"new_password2": "new_person"
}
self.post(self.password_change_url, data=new_password_payload,
status_code=400)
# send empty payload
self.post(self.password_change_url, data={}, status_code=400)
def test_registration_user_with_profile(self):
payload = {
"username": self.USERNAME,
"password": self.PASS,
"email": "person@world.com",
"newsletter_subscribe": "false"
}
# test empty payload
self.post(self.register_url, data={}, status_code=400)
self.post(self.register_url, data=payload, status_code=201)
activation_key = RegistrationProfile.objects.latest('id').activation_key
verify_url = reverse('verify_email',
kwargs={'activation_key': activation_key})
# new user at this point shouldn't be active
new_user = get_user_model().objects.latest('id')
self.assertEqual(new_user.is_active, False)
# let's active new user and check is_active flag
self.get(verify_url)
new_user = get_user_model().objects.latest('id')
self.assertEqual(new_user.is_active, True)
user_profile = user_profile_model.objects.get(user=new_user)
self.assertIsNotNone(user_profile)
def test_registration_user_without_profile(self):
payload = {
"username": self.USERNAME,
"password": self.PASS,
"email": "person1@world.com"
}
self.post(self.register_url, data=payload, status_code=201)
activation_key = RegistrationProfile.objects.latest('id').activation_key
verify_url = reverse('verify_email',
kwargs={'activation_key': activation_key})
# new user at this point shouldn't be active
new_user = get_user_model().objects.latest('id')
self.assertEqual(new_user.is_active, False)
# let's active new user and check is_active flag
self.get(verify_url)
new_user = get_user_model().objects.latest('id')
self.assertEqual(new_user.is_active, True)
user_profile = user_profile_model.objects.get(user=new_user)
self.assertIsNotNone(user_profile)