dashboard/accounts/tests.py

202 lines
6.7 KiB
Python

# Create your tests here.
# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
#
# 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 <http://www.gnu.org/licenses/>.
import time
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
from django.conf import settings
from .models import AccountConfirmChallenge
from .management.commands.rm_unverified_users import (
Command as CleanUnverifiedUsersCommand,
)
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.home"))
# 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.home"))
# 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)
class RegistrationTest(TestCase):
def test_register_template_works(self):
"""
Tests if register template renders
"""
resp = self.client.get(reverse("accounts.register"))
self.assertEqual(b"Free Forge Ecosystem" in resp.content, True)
def test_register_works(self):
"""
Tests if register works
"""
c = Client()
# passwords don't match
msg = {
"username": "register_user",
"password": "password",
"email": "register_user@example.com",
"confirm_password": "foo@example.com",
}
resp = c.post(reverse("accounts.register"), msg)
self.assertEqual(resp.status_code, 400)
# register user
msg["confirm_password"] = msg["password"]
resp = c.post(reverse("accounts.register"), msg)
self.assertEqual(resp.status_code, 302)
user = get_user_model().objects.get(username=msg["username"])
self.assertEqual(user.is_active, False)
challenge = AccountConfirmChallenge.objects.get(owned_by=user)
pending_url = challenge.pending_url()
self.assertEqual(resp.headers["location"], pending_url)
resp = c.post(reverse("accounts.verify.resend", args=(challenge.public_ref,)))
self.assertEqual(resp.status_code, 302)
self.assertEqual(resp.headers["location"], pending_url)
resp = c.post(challenge.verification_link())
self.assertEqual(resp.status_code, 302)
self.assertEqual(resp.headers["location"], reverse("accounts.login"))
user.refresh_from_db()
self.assertEqual(user.is_active, True)
old_email = msg["email"]
# duplicate username
msg["email"] = "new_register_user_email@example.com"
resp = c.post(reverse("accounts.register"), msg)
self.assertEqual(resp.status_code, 400)
self.assertEqual(b"Username is already registered" in resp.content, True)
# duplicate email
msg["username"] = "new_register_user_email"
msg["email"] = old_email
resp = c.post(reverse("accounts.register"), msg)
self.assertEqual(resp.status_code, 400)
self.assertEqual(b"Email is already registered" in resp.content, True)
class UnverifiedAccountCleanupTets(TestCase):
"""
Tests account clean up management command
"""
@override_settings(HOSTEA={"ACCOUNTS": {"MAX_VERIFICATION_TOLERANCE_PERIOD": 0}})
def test_register_works(self):
"""
Tests if register works
"""
c = Client()
username1 = "cleanup_user1"
username2 = "cleanup_user2"
# passwords don't match
msg = {
"username": username1,
"password": "password",
"email": f"{username1}@example.com",
"confirm_password": "password",
}
# register user
resp = c.post(reverse("accounts.register"), msg)
self.assertEqual(resp.status_code, 302)
msg["username"] = username2
msg["email"] = f"{username2}@example.org"
resp = c.post(reverse("accounts.register"), msg)
self.assertEqual(resp.status_code, 302)
user1 = get_user_model().objects.get(username=username1)
user1.is_active = True
user1.save()
if settings.HOSTEA["ACCOUNTS"]["MAX_VERIFICATION_TOLERANCE_PERIOD"] > 10:
raise Exception(
"This test requires MAX_VERIFICATION_TOLERANCE_PERIOD to be less, ideally less 10"
)
time.sleep(settings.HOSTEA["ACCOUNTS"]["MAX_VERIFICATION_TOLERANCE_PERIOD"] + 2)
cmd = CleanUnverifiedUsersCommand()
cmd.handle()
self.assertEqual(
get_user_model().objects.filter(username=username1).exists(), True
)
self.assertEqual(
get_user_model().objects.filter(username=username2).exists(), False
)