From 4fa042079fecbe7c7cfdc897aff792aef6928246 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20P=C3=A9pin?= <kerl@wkerl.me>
Date: Tue, 12 May 2020 21:21:45 +0200
Subject: [PATCH] First test!

---
 authens/tests/__init__.py     |  0
 authens/tests/test_backend.py | 25 +++++++++++++++++++++++++
 runtests.py                   | 17 +++++++++++++++++
 tests/settings.py             | 22 ++++++++++++++++++++++
 4 files changed, 64 insertions(+)
 create mode 100644 authens/tests/__init__.py
 create mode 100644 authens/tests/test_backend.py
 create mode 100644 runtests.py
 create mode 100644 tests/settings.py

diff --git a/authens/tests/__init__.py b/authens/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/authens/tests/test_backend.py b/authens/tests/test_backend.py
new file mode 100644
index 0000000..eace751
--- /dev/null
+++ b/authens/tests/test_backend.py
@@ -0,0 +1,25 @@
+from django.contrib.auth import get_user_model
+from django.test import TestCase
+
+from authens.backends import ENSCASBackend
+
+UserModel = get_user_model()
+
+
+class TestBackend(TestCase):
+    def setUp(self):
+        UserModel.objects.create(username="toto")
+        UserModel.objects.create(username="toto2")
+
+    def test_usernames_uniqueness(self):
+        backend = ENSCASBackend()
+        for _ in range(10):
+            username = backend.find_available_username("toto")
+            self.assertFalse(UserModel.objects.filter(username=username).exists())
+            UserModel.objects.create(username=username)
+        for _ in range(10):
+            username = backend.find_available_username("tutu")
+            self.assertFalse(UserModel.objects.filter(username=username).exists())
+            UserModel.objects.create(username=username)
+
+    # TODO: https://git.eleves.ens.fr/klub-dev-ens/authens/issues/4
diff --git a/runtests.py b/runtests.py
new file mode 100644
index 0000000..f9079d6
--- /dev/null
+++ b/runtests.py
@@ -0,0 +1,17 @@
+# Source:
+# https://docs.djangoproject.com/en/3.0/topics/testing/advanced/#using-the-django-test-runner-to-test-reusable-applications
+
+import os
+import sys
+
+import django
+from django.conf import settings
+from django.test.utils import get_runner
+
+if __name__ == '__main__':
+    os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
+    django.setup()
+    TestRunner = get_runner(settings)
+    test_runner = TestRunner()
+    failures = test_runner.run_tests(sys.argv[1:])
+    sys.exit(bool(failures))
diff --git a/tests/settings.py b/tests/settings.py
new file mode 100644
index 0000000..999e477
--- /dev/null
+++ b/tests/settings.py
@@ -0,0 +1,22 @@
+from django.urls import reverse_lazy  # noqa
+
+# Minimal settings to run a django app using authens.
+
+
+SECRET_KEY = "dummy"
+
+INSTALLED_APPS = [
+    "django.contrib.contenttypes",
+    "django.contrib.auth",
+    "authens",
+    "tests",
+]
+
+AUTHENTICATION_BACKENDS = [
+    "django.contrib.auth.backends.ModelBackend",
+    "authens.backends.ENSCASBackend",
+]
+
+DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3"}}
+
+LOGIN_URL = reverse_lazy("authens:login")