Skip to content

Commit

Permalink
Add a simple testsuite
Browse files Browse the repository at this point in the history
Closes #3.
  • Loading branch information
matthiask committed Jul 4, 2024
1 parent 6b977e5 commit 85dc6c3
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 98 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Python application

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.12
uses: actions/setup-python@v4
with:
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install uv
uv pip install --system -r requirements.txt
- name: Test with the Django testsuite
env:
ALLOWED_HOSTS: '["*"]'
DATABASE_URL: "sqlite://traduire.db"
DEBUG: "True"
LIVE: "False"
NAMESPACE: "test"
SECRET_KEY: "unsafe"
TESTING: "True"

run: |
python manage.py test -v2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ app/static/app/dist
/*.sql
/.vscode
.tox
.coverage
7 changes: 4 additions & 3 deletions app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@


DEBUG = env("DEBUG", required=True)
TESTING = any(r in sys.argv for r in ("test",))
TESTING = env("TESTING", default="test" in sys.argv)
LIVE = env("LIVE", default=False)
SECURE_SSL_HOST = env("SECURE_SSL_HOST")
SECURE_SSL_REDIRECT = env("SECURE_SSL_REDIRECT", default=False)
ALLOWED_HOSTS = env("ALLOWED_HOSTS", required=True)
DEBUG_TOOLBAR = DEBUG and not TESTING

FORMS_URLFIELD_ASSUME_HTTPS = True

Expand Down Expand Up @@ -75,7 +76,7 @@
m
for m in [
"" if DEBUG or LIVE else "curtains.middleware.basic_auth",
"debug_toolbar.middleware.DebugToolbarMiddleware" if DEBUG else "",
"debug_toolbar.middleware.DebugToolbarMiddleware" if DEBUG_TOOLBAR else "",
"canonical_domain.middleware.canonical_domain",
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
Expand Down Expand Up @@ -129,7 +130,7 @@
"app",
"projects",
"canonical_domain",
"debug_toolbar" if DEBUG else "",
"debug_toolbar" if DEBUG_TOOLBAR else "",
]
if app
]
Expand Down
2 changes: 1 addition & 1 deletion app/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load markcurrent i18n static webpack_assets %}
{% load i18n static webpack_assets %}
<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE }}">
<head>
Expand Down
89 changes: 0 additions & 89 deletions app/templatetags/markcurrent.py

This file was deleted.

7 changes: 3 additions & 4 deletions projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,16 @@ def __str__(self):
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
if not self.token:
self.cycle_token(save=True)
self.cycle_token()

save.alters_data = True

def get_absolute_url(self):
return reverse("projects:project", kwargs={"slug": self.slug})

def cycle_token(self, *, save=True):
def cycle_token(self):
self.token = f"{get_random_string(60)}-{self.pk}"
if save:
self.save()
self.save()


class CatalogQuerySet(models.QuerySet):
Expand Down
65 changes: 65 additions & 0 deletions projects/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from authlib.little_auth.models import User
from django.test import TestCase

from projects.models import Project


class ProjectsTest(TestCase):
def test_smoke(self):
user = User.objects.create_superuser("admin@example.com", "admin")
self.client.force_login(user)

p = Project.objects.create(name="test", slug="test")
c = p.catalogs.create(
language_code="fr",
domain="djangojs",
pofile="""\
#: conf/strings.js frontend/intro/intro.js frontend/people/person.js
msgid "Continue"
msgstr "Continuer"
#: conf/strings.js
msgid "Copied code!"
msgstr "Code copié !"
#: fmw/dashboard/classes/forms.py
#, python-format
msgid "Successfully reset the password of %(count)s student."
msgid_plural "Successfully reset passwords of %(count)s students."
msgstr[0] "Réinitialisation du mot de passe de %(count)s élève."
msgstr[1] "Réinitialisation des mots de passe de %(count)s élèves ."
""",
)

r = self.client.get("/", headers={"accept-language": "en"})

self.assertContains(r, '<a href="/project/test/">test</a>')

r = self.client.get(p.get_absolute_url(), headers={"accept-language": "en"})
self.assertContains(r, p.token)
self.assertContains(
r,
'<a href="/project/test/catalog/fr/djangojs/">French, djangojs (100%)</a>',
)

r = self.client.get(c.get_absolute_url(), headers={"accept-language": "en"})
self.assertContains(
r,
'<input type="hidden" name="msgid_1" value="Copied code!" id="id_msgid_1">',
)

# API test
r = self.client.get("/api/pofile/fr/djangojs/")
self.assertEqual(r.status_code, 403)

r = self.client.get(
"/api/pofile/fr/djangojs/", headers={"x-project-token": p.token}
)
self.assertEqual(r.content.decode("utf-8"), c.pofile)

r = self.client.get(
"/api/pofile/de/djangojs/", headers={"x-project-token": p.token}
)
self.assertEqual(r.status_code, 404)

# print(r, r.content.decode("utf-8"))
4 changes: 3 additions & 1 deletion projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
@login_required
def projects(request):
return render(
request, "projects/projects.html", {"projects": request.user.projects.all()}
request,
"projects/projects.html",
{"projects": Project.objects.for_user(request.user)},
)


Expand Down
8 changes: 8 additions & 0 deletions runtests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
set -ex
.venv/bin/coverage erase
.venv/bin/python -Wall .venv/bin/coverage run ./manage.py test -v 2 --keepdb $*

if [ $# -eq 0 ]; then
.venv/bin/coverage report
fi

0 comments on commit 85dc6c3

Please sign in to comment.