From 6bdbd5d2d87ceec265e2e6378f73285db633ccef Mon Sep 17 00:00:00 2001 From: Nicholas Romero Date: Mon, 18 Jul 2016 00:19:24 -0500 Subject: [PATCH] Adds additional tests to accounts and profiles apps --- .travis.yml | 2 +- src/accounts/tests.py | 67 +++++++++++++++++++++++++++++++++++++++++-- src/profiles/tests.py | 36 ++++++++++++++++++++--- 3 files changed, 98 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 742988f9..2833a4f0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,4 @@ install: - cp my_proj/settings/local.sample.env my_proj/settings/local.env - python manage.py migrate script: - - python manage.py test profiles \ No newline at end of file + - python manage.py test profiles accounts \ No newline at end of file diff --git a/src/accounts/tests.py b/src/accounts/tests.py index 7ce503c2..087b729a 100644 --- a/src/accounts/tests.py +++ b/src/accounts/tests.py @@ -1,3 +1,66 @@ -from django.test import TestCase +from django.contrib.auth import get_user_model +from django.core import mail +from django.core.urlresolvers import reverse +from django.test import TestCase, RequestFactory -# Create your tests here. +User = get_user_model() + + +class AccountsLogViewTests(TestCase): + def setUp(self): + self.factory = RequestFactory() + self.user = User.objects.create_user( + email='test@user.com', + password='top_secret' + ) + + def test_logged_in_user_login_view_redirects_to_home(self): + self.client.login(username='test@user.com', + password='top_secret') + response = self.client.get(reverse('accounts:login')) + self.assertRedirects(response, reverse('profiles:show_self')) + + def test_login_in_view_success(self): + response = self.client.post(reverse('accounts:login'), { + 'username': 'test@user.com', + 'password': 'top_secret', + 'remember_me': True + }) + self.assertRedirects(response, reverse('profiles:show_self')) + + def test_sign_up_view_success(self): + response = self.client.post(reverse('accounts:signup'), { + 'email': 'new@user.com', + 'name': 'New User', + 'password1': 'top_secret', + 'password2': 'top_secret', + }) + self.assertRedirects(response, reverse('home'), ) + + def test_password_change_view_success(self): + self.client.login(username='test@user.com', + password='top_secret') + response = self.client.post(reverse('accounts:password-change'), { + 'old_password': 'top_secret', + 'new_password1': 'new_password', + 'new_password2': 'new_password', + }) + self.assertRedirects(response, reverse('home')) + + def test_password_reset_view_success(self): + response = self.client.post(reverse('accounts:password-reset'), { + 'email': 'test@user.com', + }) + self.assertRedirects(response, reverse('accounts:password-reset-done')) + self.assertEqual(len(mail.outbox), 1) + + # Now, here's the kicker: we get the token and userid from the response + + token = response.context[0]['token'] + uid = response.context[0]['uid'] + # Now we can use the token to get the password change form + # Now we post to the same url with our new password: + response = self.client.post(reverse('accounts:password-reset-confirm', + kwargs={'token': token, 'uidb64': uid}), + {'new_password1': 'pass', 'new_password2': 'pass'}) + self.assertEqual(response.status_code, 302) diff --git a/src/profiles/tests.py b/src/profiles/tests.py index 06f528e8..803c82b5 100644 --- a/src/profiles/tests.py +++ b/src/profiles/tests.py @@ -1,7 +1,8 @@ from __future__ import unicode_literals -from django.test import TestCase -from django.core.urlresolvers import resolve, reverse + from django.contrib.auth import get_user_model +from django.core.urlresolvers import reverse +from django.test import TestCase class PageOpenTestCase(TestCase): @@ -20,6 +21,33 @@ def test_about_page_exists(self): class ProfileTestCase(TestCase): + def setUp(self): + self.user = User.objects.create_user( + email="dummy@example.com", + password='password' + ) + def test_profiles_created(self): - u = User.objects.create_user(email="dummy@example.com") - self.assertIsNotNone(u.profile) + self.assertIsNotNone(self.user.profile) + + def test_profiles_without_created(self): + self.assertIsNotNone(self.user.profile) + + def test_edit_profile_success_view(self): + self.client.login(username='dummy@example.com', + password='password') + response = self.client.post(reverse('profiles:edit_self'), { + 'name': 'test user', + 'bio': 'bio', + }) + self.assertRedirects(response, reverse('profiles:show_self')) + + def test_edit_profile_invalid_view(self): + self.client.login(username='dummy@example.com', + password='password') + response = self.client.post(reverse('profiles:edit_self'), { + 'name': '', + }) + self.assertEqual(response.status_code, 200) + message = list(response.context['messages'])[0] + self.assertEqual(message.message, 'There was a problem with the form. Please check the details.')