dashboard/support/tests.py

139 lines
4.7 KiB
Python

# 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/>.
from django.contrib.auth import get_user_model
from django.utils.http import urlencode
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 .utils import IssueTracker
hostea_issue_tracker_settings = settings.HOSTEA
hostea_issue_tracker_settings["META"] = {
"GITEA_INSTANCE": "https://gitea.hostea.org",
"GITEA_ORG_NAME": "Hostea",
"SUPPORT_REPOSITORY": "support",
}
@override_settings(HOSTEA=hostea_issue_tracker_settings)
class IssueTrackerTests(TestCase):
"""
Test IssueTracker utility
"""
def test_defaults(self):
"""
Verify default credentials; all further tests are based on defaults set
"""
it = IssueTracker()
self.assertEqual(it.config["GITEA_INSTANCE"], "https://gitea.hostea.org")
self.assertEqual(it.config["GITEA_ORG_NAME"], "Hostea")
self.assertEqual(it.config["SUPPORT_REPOSITORY"], "support")
def test_uri_builders(self):
"""
Verify default credentials; all further tests are based on defaults set
"""
it = IssueTracker()
self.assertEqual(
it.get_issue_tracker(), "https://gitea.hostea.org/Hostea/support/issues"
)
self.assertEqual(
it.open_issue(), "https://gitea.hostea.org/Hostea/support/issues/new"
)
class SupportWorks(TestCase):
"""
Tests create new app view
"""
def setUp(self):
self.password = "password121231"
self.username = "suport_user"
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_dash_is_protected(self):
"""
Tests if support templates render
"""
# default LOGIN redirect URI that is used by @login_required decorator is
# /accounts/login. There's a redirection endpoint at /accounts/login/ that
# will redirect user to /login. Hence the /accounts prefix
def redirect_login_uri(uri: str) -> str:
return f"/accounts{reverse('accounts.login')}?next={uri}"
urls = [
reverse("support.home"),
reverse("support.new"),
reverse("support.view"),
]
for i in urls:
print(f"[*] Testing URI: {i}")
resp = self.client.get(i)
self.assertEqual(resp.status_code, 302)
expected = redirect_login_uri(i)
self.assertEqual(resp.headers["location"], expected)
def test_dash_home_renders(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"))
urls = [
reverse("support.home"),
reverse("support.new"),
reverse("support.view"),
]
for i in urls:
print(f"[*] Testing URI: {i}")
resp = c.get(i)
self.assertEqual(resp.status_code, 200)
self.assertEqual(b"Billing" in resp.content, True)
self.assertEqual(b"Support" in resp.content, True)
self.assertEqual(b"Logout" in resp.content, True)
# new issue view
resp = c.get(reverse("support.new"))
self.assertEqual(resp.status_code, 200)
it = IssueTracker()
new_issue = str.encode(it.open_issue())
self.assertEqual(new_issue in resp.content, True)
# list issues view
resp = c.get(reverse("support.new"))
self.assertEqual(resp.status_code, 200)
issue_tracker = str.encode(it.get_issue_tracker())
self.assertEqual(issue_tracker in resp.content, True)