From 0c52da08d6ebb9748f32dddf746acecbec42d808 Mon Sep 17 00:00:00 2001 From: Hostea dashboard Date: Sun, 4 Sep 2022 13:53:25 +0530 Subject: [PATCH] feat: basic settings.py validation --- accounts/tests.py | 8 +++- .../management/commands/generate_invoice.py | 2 - dashboard/settings.py | 15 +++++++- dashboard/utils.py | 37 +++++++++++++++++++ 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 dashboard/utils.py diff --git a/accounts/tests.py b/accounts/tests.py index 95d7840..06cd182 100644 --- a/accounts/tests.py +++ b/accounts/tests.py @@ -77,7 +77,9 @@ class LoginTest(TestCase): Tests if login template renders """ resp = self.client.get(reverse("accounts.login")) - self.assertEqual(b"A free forge ecosystem for free developers" in resp.content, True) + self.assertEqual( + b"A free forge ecosystem for free developers" in resp.content, True + ) def test_login_works(self): """ @@ -234,7 +236,9 @@ class RegistrationTest(TestCase): Tests if register template renders """ resp = self.client.get(reverse("accounts.register")) - self.assertEqual(b"A free forge ecosystem for free developers." in resp.content, True) + self.assertEqual( + b"A free forge ecosystem for free developers." in resp.content, True + ) def test_register_works(self): """ diff --git a/billing/management/commands/generate_invoice.py b/billing/management/commands/generate_invoice.py index 76983e4..8a53130 100644 --- a/billing/management/commands/generate_invoice.py +++ b/billing/management/commands/generate_invoice.py @@ -24,8 +24,6 @@ from accounts.utils import gen_secret from dash.models import Instance from billing.utils import generate_invoice, payment_fullfilled -Application = get_application_model() - class Command(BaseCommand): help = "Generate invoices, should be run from cronjob scheduled for daily execution" diff --git a/dashboard/settings.py b/dashboard/settings.py index d2e6361..aa66625 100644 --- a/dashboard/settings.py +++ b/dashboard/settings.py @@ -11,6 +11,8 @@ https://docs.djangoproject.com/en/4.0/ref/settings/ """ from pathlib import Path +from .utils import is_url, is_email + # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -206,7 +208,7 @@ EMAIL_USE_SSL = False EMAIL_PORT = 10025 EMAIL_HOST_USER = "admin" EMAIL_HOST_PASSWORD = "password" -DEFAULT_FROM_EMAIL: "no-reply@hostea.org" +DEFAULT_FROM_EMAIL = "no-reply@hostea.org" try: from dashboard.local_settings import * @@ -214,3 +216,14 @@ try: print("Found local_settings") except ModuleNotFoundError: pass + + +def verifiy_settings(): + assert is_url(HOSTEA["META"]["GITEA_INSTANCE"]) + assert is_url(PAYMENT_HOST) + + assert is_email(DEFAULT_FROM_EMAIL) + assert is_email(HOSTEA["INSTANCE_MAINTAINER_CONTACT"]) + + +verifiy_settings() diff --git a/dashboard/utils.py b/dashboard/utils.py new file mode 100644 index 0000000..f337ffe --- /dev/null +++ b/dashboard/utils.py @@ -0,0 +1,37 @@ +# 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.core.validators import URLValidator, EmailValidator + + +def is_url(val: str) -> bool: + """ + Validate if the given value is a URL + """ + try: + URLValidator()(val) + except: + return False + return True + + +def is_email(val: str) -> bool: + """ + Validate if the given value is an email + """ + try: + EmailValidator()(val) + except: + return False + return True