1

run tests, add debug toolbar

This commit is contained in:
Motiejus Jakštys 2024-01-12 00:05:57 +02:00
parent 2fc1b17f43
commit 518dcf469a
6 changed files with 32 additions and 14 deletions

View File

@ -14,11 +14,15 @@ DEBUG = True
ALLOWED_HOSTS = [] ALLOWED_HOSTS = []
INTERNAL_IPS = ["127.0.0.1"]
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
'debug_toolbar',
'compressor', 'compressor',
'signup.apps.SignupConfig', 'signup.apps.SignupConfig',
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',
@ -28,6 +32,8 @@ INSTALLED_APPS = [
] ]
MIDDLEWARE = [ MIDDLEWARE = [
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.security.SecurityMiddleware', 'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',

View File

@ -20,5 +20,6 @@ from django.urls import include, path
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path("__debug__/", include("debug_toolbar.urls")),
path('', include('signup.urls')), path('', include('signup.urls')),
] ]

View File

@ -78,9 +78,7 @@
<fieldset> <fieldset>
{% csrf_token %} {% csrf_token %}
{% if error_message %} {% if request.GET.success %}
<span class="pure-form-message message-error">{{ error_message }}</span>
{% elif request.GET.success %}
<span class="pure-form-message message-success"> <span class="pure-form-message message-success">
{% if request.GET.already_subscribed %} {% if request.GET.already_subscribed %}
Looks like you were already subscribed. We share your excitement! Looks like you were already subscribed. We share your excitement!
@ -88,11 +86,21 @@
You are now subscribed! You are now subscribed!
{% endif %} {% endif %}
</span> </span>
{% else %}
{% if error_message %}
<span class="pure-form-message message-error">{{ error_message }}</span>
{% endif %}
<input type="email"
class="subscribe-email"
name="email"
id="email"
{% if request.POST.email %}value="{{ request.POST.email }}"{% endif %}
placeholder="your@email.com"
required
/>
<button type="submit" class="pure-button pure-button-primary">Inform Me</button>
{% endif %} {% endif %}
<input type="email" class="subscribe-email" name="email" id="email" placeholder="your@email.com" required />
<button type="submit" class="pure-button pure-button-primary">Inform Me</button>
</fieldset> </fieldset>
</form> </form>
</section> </section>

View File

@ -1,14 +1,15 @@
from django.test import TestCase, Client from django.test import TestCase, Client
from django.urls import reverse from django.urls import reverse
def SignupViewTests(TestCase): from .models import Signup
class SignupViewTest(TestCase):
def setUp(self): def setUp(self):
self.client = Client() self.client = Client()
def test_index(self): def test_index(self):
resp = self.client.get(reverse("index")) resp = self.client.get(reverse("index"))
self.assertEqual(response.status_code, 200) self.assertEqual(resp.status_code, 200)
self.assertContains(resp, "11sync is a privacy-respecting way") self.assertContains(resp, "11sync is a privacy-respecting way")
def test_ok_signup(self): def test_ok_signup(self):
@ -16,6 +17,6 @@ def SignupViewTests(TestCase):
follow = True, follow = True,
HTTP_USER_AGENT = "foo-agent", HTTP_USER_AGENT = "foo-agent",
REMOTE_ADDR = "127.0.0.2") REMOTE_ADDR = "127.0.0.2")
self.assertEqual(resp.redirect_chain, ('http://testserver/?success', 302)) self.assertEqual(resp.redirect_chain, [('/?success', 302)])
#print(str(resp.content).replace('\\n', '\n'))
self.assertContains(resp, "You are now subscribed!") self.assertContains(resp, "You are now subscribed!")

View File

@ -1,6 +1,7 @@
import logging import logging
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.urls import reverse
from django.db import IntegrityError from django.db import IntegrityError
from lib.anonymize_ip import anonymize_ip from lib.anonymize_ip import anonymize_ip
@ -20,7 +21,7 @@ def index(request):
ip = request.META.get('REMOTE_ADDR') ip = request.META.get('REMOTE_ADDR')
anonymous_ip = anonymize_ip(ip) anonymous_ip = anonymize_ip(ip)
e = Email( e = Signup(
email = request.POST.get("email"), email = request.POST.get("email"),
anonymized_ip = anonymous_ip, anonymized_ip = anonymous_ip,
user_agent = request.META["HTTP_USER_AGENT"], user_agent = request.META["HTTP_USER_AGENT"],
@ -37,12 +38,12 @@ def index(request):
e.save() e.save()
except IntegrityError: except IntegrityError:
# email already registered, presumably # email already registered, presumably
return redirect("index") + "?already_subscribed" return redirect(reverse("index") + "?already_subscribed")
except DatabaseError: except DatabaseError:
logger.exception("database error when registering an email") logger.exception("database error when registering an email")
err = "Sorry, database error. Please come back later." err = "Sorry, database error. Please come back later."
return render(request, "signup/index.html", return render(request, "signup/index.html",
{"error_message": err}) {"error_message": err})
return redirect("index") + "?success" return redirect(reverse("index") + "?success")
return render(request, "signup/index.html", {}) return render(request, "signup/index.html", {})

View File

@ -18,6 +18,7 @@
pkgs = import nixpkgs {inherit system;}; pkgs = import nixpkgs {inherit system;};
devDeps = with pkgs; [ devDeps = with pkgs; [
python3Packages.django_5 python3Packages.django_5
python3Packages.django-debug-toolbar
python3Packages.django-compressor python3Packages.django-compressor
dart-sass dart-sass