From 4608b2d08a13692ddaf5c54df73af07d1456294e Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 10 Jun 2022 19:13:53 +0530 Subject: [PATCH] feat: test login view --- accounts/tests.py | 79 +++++++++++++++++++++++++++++++++++++++++++++-- accounts/urls.py | 14 +++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/accounts/tests.py b/accounts/tests.py index 7ce503c..5f83a1e 100644 --- a/accounts/tests.py +++ b/accounts/tests.py @@ -1,3 +1,78 @@ -from django.test import TestCase - # Create your tests here. + +# Copyright © 2022 Aravinth Manivannan +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +from django.contrib.auth import get_user_model +from django.urls import reverse +from django.test import TestCase, Client, override_settings +from django.contrib.auth import authenticate + + +class LoginTest(TestCase): + """ + Tests create new app view + """ + + def setUp(self): + self.password = "password121231" + self.username = "create_new_app_tests" + self.email = f"{self.username}@example.org" + self.user = get_user_model().objects.create( + username=self.username, + email=self.email, + ) + self.user.set_password(self.password) + self.user.save() + + def test_login_template_works(self): + """ + Tests if login template renders + """ + resp = self.client.get(reverse("accounts.login")) + self.assertEqual(b"Free Forge Ecosystem" in resp.content, True) + + def test_login_works(self): + """ + Tests if login template renders + """ + c = Client() + + # username login works + payload = { + "login": self.username, + "password": self.password, + } + resp = c.post(reverse("accounts.login"), payload) + self.assertEqual(resp.status_code, 302) + self.assertEqual(resp.headers["location"], reverse("accounts.protected")) + + # email login works + paylaod = { + "login": self.email, + "password": self.password, + } + resp = c.post(reverse("accounts.login"), payload) + self.assertEqual(resp.status_code, 302) + self.assertEqual(resp.headers["location"], reverse("accounts.protected")) + + # authentication failure when wrong credentials are provided + payload = { + "login": self.email, + "password": self.user.email, + } + + resp = self.client.post(reverse("accounts.login"), paylaod) + self.assertEqual(resp.status_code, 401) + self.assertEqual(b"Login Failed" in resp.content, True) diff --git a/accounts/urls.py b/accounts/urls.py index c21d420..75d4caf 100644 --- a/accounts/urls.py +++ b/accounts/urls.py @@ -1,3 +1,17 @@ +# Copyright © 2022 Aravinth Manivannan +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . from django.contrib import admin from django.urls import path, include