Skip to content

Instantly share code, notes, and snippets.

@alexphelps
Last active January 3, 2016 08:30
Show Gist options
  • Save alexphelps/6c65c68109e7cce3c62b to your computer and use it in GitHub Desktop.
Save alexphelps/6c65c68109e7cce3c62b to your computer and use it in GitHub Desktop.
Example Tests
class PasswordResetViewTest(TestCase):
fixtures = [
'auth_initial_data.json',
]
def setUp(self):
url = '/password/reset/'
self.url = url
def test_password_reset_get(self):
response = self.client.get(self.url)
self.assertTemplateUsed(response, 'password-reset.html')
self.assertEqual(response.status_code, 200)
self.assertTrue('form' in response.context)
def test_password_reset_post_success(self):
form_data = {
'email': 'jennifer@calltracker.io',
}
response = self.client.post(self.url, form_data)
self.assertTemplateUsed(response, 'password-reset.html')
self.assertEqual(response.status_code, 200)
self.assertTrue('form' in response.context)
expected = '<div class="alert alert-success" role="alert">'
expected += 'An email has been sent to jennifer@calltracker.io. Follow'
expected += ' the link in the email to reset your password.</div>'
self.assertContains(response, expected)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject, 'Password Reset')
def test_password_reset_post_no_user(self):
form_data = {
'email': 'wrongemail@admin.com',
}
response = self.client.post(self.url, form_data)
self.assertTemplateUsed(response, 'password-reset.html')
self.assertEqual(response.status_code, 200)
self.assertTrue('form' in response.context)
expected = '<div class="alert alert-danger" role="alert">'
expected += 'This email does not exist in the system.</div>'
self.assertContains(response, expected)
def test_password_reset_post_missing_data(self):
form_data = {
'email': '',
}
response = self.client.post(self.url, form_data)
self.assertTemplateUsed(response, 'password-reset.html')
self.assertTrue('form' in response.context)
self.assertEqual(response.status_code, 200)
self.assertFormError(
response,
'form',
'email',
'This field is required.'
)
class PasswordResetConfirmView(TestCase):
fixtures = [
'auth_initial_data.json',
]
def setUp(self):
email = 'jennifer@calltracker.io'
user = User.objects.get(username=email)
uid = urlsafe_base64_encode(force_bytes(user.email))
token = default_token_generator.make_token(user)
url = '/password/reset/confirm/' + uid + '/' + token + '/'
self.url = url
def test_password_reset_confirm_get(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'password-reset-confirm.html')
self.assertTrue('form' in response.context)
def test_password_reset_confirm_post_success(self):
form_data = {
'new_password': 'newpassword',
'new_password2': 'newpassword'
}
response = self.client.post(self.url, form_data)
self.assertEqual(response.status_code, 302)
def test_password_reset_confirm_post_passwords_dont_match(self):
form_data = {
'new_password': 'newpassword',
'new_password2': 'differentpassword'
}
response = self.client.post(self.url, form_data)
self.assertEqual(response.status_code, 200)
expected = '<div class="alert alert-danger" role="alert">'
expected += 'Passwords do not match.</div>'
self.assertContains(response, expected)
def test_password_reset_confirm_post_missing_data(self):
form_data = {
}
response = self.client.post(self.url, form_data)
self.assertEqual(response.status_code, 200)
self.assertFormError(
response,
'form',
'new_password',
'This field is required.'
)
self.assertFormError(
response,
'form',
'new_password2',
'This field is required.'
)
def test_password_reset_confirm_post_wrong_url(self):
url = '/password/reset/confirm/AAAA/1234567890/'
form_data = {
'new_password': 'newpassword',
'new_password2': 'newpassword'
}
response = self.client.post(url, form_data)
self.assertEqual(response.status_code, 200)
expected = '<div class="alert alert-danger" role="alert">'
expected += 'The reset password link is no longer valid.</div>'
self.assertContains(response, expected)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment