From 5b99609dc7388b6e6f1f177d1af9ae6dd074bbcb Mon Sep 17 00:00:00 2001 From: Trent Palmer Date: Sun, 18 Apr 2021 16:51:24 -0700 Subject: [PATCH] add accounts/tests/test_log_in_view.py --- accounts/tests/test_log_in_view.py | 75 ++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 accounts/tests/test_log_in_view.py diff --git a/accounts/tests/test_log_in_view.py b/accounts/tests/test_log_in_view.py new file mode 100644 index 0000000..3d164e5 --- /dev/null +++ b/accounts/tests/test_log_in_view.py @@ -0,0 +1,75 @@ +from django.test import TestCase +from django.contrib.auth.models import User +from django.urls import reverse + + +class TestLogInViewTestCase(TestCase): + + def setUp(self): + user_a = User.objects.create(username='user_a') + user_a.set_password('password_user_a') + user_a.save() + + def test_log_in_view_wrong_password(self): + response = self.client.post(reverse('accounts:login'), { + 'username': 'user_a', + 'password': 'password_user_b', + }) + self.assertEquals(response.status_code, 200) + self.assertFalse("Successfully logged in" in response.content.decode('utf-8')) + user_a = User.objects.get(username='user_a') + self.assertFalse(hasattr(user_a, 'account')) + self.assertTemplateUsed(response, 'base_form.html') + self.assertEquals(response.request['PATH_INFO'], '/accounts/login/') + self.assertEquals(response.content.decode('utf-8').count( + "Please enter a correct username and password."), 1) + + def test_log_in_view_wrong_user(self): + response = self.client.post(reverse('accounts:login'), { + 'username': 'user_b', + 'password': 'password_user_a', + }) + self.assertEquals(response.status_code, 200) + self.assertFalse("Successfully logged in" in response.content.decode('utf-8')) + user_a = User.objects.get(username='user_a') + self.assertFalse(hasattr(user_a, 'account')) + self.assertTemplateUsed(response, 'base_form.html') + self.assertEquals(response.request['PATH_INFO'], '/accounts/login/') + self.assertEquals(response.content.decode('utf-8').count( + "Please enter a correct username and password."), 1) + + def test_log_in_view_no_data(self): + response = self.client.post(reverse('accounts:login')) + self.assertEquals(response.status_code, 200) + self.assertFalse("Successfully logged in" in response.content.decode('utf-8')) + user_a = User.objects.get(username='user_a') + self.assertFalse(hasattr(user_a, 'account')) + self.assertTemplateUsed(response, 'base_form.html') + self.assertEquals(response.request['PATH_INFO'], '/accounts/login/') + self.assertEquals(response.content.decode('utf-8').count("This field is required."), 2) + + def test_log_in_view_already_logged_in(self): + self.client.login(username='user_a', password='password_user_a') + response = self.client.post(reverse('accounts:login'), { + 'username': 'user_a', + 'password': 'password_user_a', + }, follow=True) + self.assertEquals(response.status_code, 200) + self.assertFalse("Successfully logged in" in response.content.decode('utf-8')) + user_a = User.objects.get(username='user_a') + self.assertFalse(hasattr(user_a, 'account')) + self.assertTemplateUsed(response, 'audio/index.html') + self.assertEquals(response.request['PATH_INFO'], '/') + + def test_log_in_view(self): + response = self.client.post(reverse('accounts:login'), { + 'username': 'user_a', + 'password': 'password_user_a', + }, follow=True) + self.assertEquals(response.status_code, 200) + self.assertTrue("Successfully logged in" in response.content.decode('utf-8')) + user_a = User.objects.get(username='user_a') + self.assertTrue(hasattr(user_a, 'account')) + self.assertEquals(user_a.account.twitter_handle, '@Twitter') + self.assertTemplateUsed(response, 'audio/index.html') + self.assertEquals(response.request['PATH_INFO'], '/')