diff --git a/dips/tests/test_migrations.py b/dips/tests/test_migrations.py new file mode 100644 index 00000000..9614b276 --- /dev/null +++ b/dips/tests/test_migrations.py @@ -0,0 +1,30 @@ +from django.contrib.auth.models import Group +from django.db.migrations.executor import MigrationExecutor +from django.db import connection +from django.test import TransactionTestCase + +from dips.models import StaticContent + + +class TestMigrations(TransactionTestCase): + """Uses TransactionTestCase to perform rollbacks.""" + + def setUp(self): + self.executor = MigrationExecutor(connection) + + def test_rollbacks(self): + """Checks that migration rollbacks run correctly. + + Perform all rollbacks in order in the same test to maintain DB status. + """ + # Initial counts + self.assertEqual(Group.objects.count(), 3) + self.assertEqual(StaticContent.objects.count(), 3) + + # StaticContent removal + self.executor.migrate([("dips", "0009_staticcontent")]) + self.assertEqual(StaticContent.objects.count(), 0) + + # Groups removal + self.executor.migrate([("dips", "0001_initial")]) + self.assertEqual(Group.objects.count(), 0) diff --git a/dips/tests/test_user_access.py b/dips/tests/test_user_access.py index 51d7f4dc..ded64e89 100644 --- a/dips/tests/test_user_access.py +++ b/dips/tests/test_user_access.py @@ -160,6 +160,14 @@ class UserAccessTests(TestCase): ("basic", 302), ("viewer", 302), ], + "static_content": [ + ("unauth", 302), + ("admin", 200), + ("manager", 302), + ("editor", 302), + ("basic", 302), + ("viewer", 302), + ], } @patch("elasticsearch_dsl.DocType.save") diff --git a/dips/tests/test_views.py b/dips/tests/test_views.py index 6d8ed8ac..2ecaf48c 100644 --- a/dips/tests/test_views.py +++ b/dips/tests/test_views.py @@ -1,13 +1,14 @@ from django.test import TestCase from unittest.mock import patch -from dips.models import User +from dips.models import StaticContent, User class ViewsTests(TestCase): def setUp(self): self.user = User.objects.create_superuser("admin", "admin@example.com", "admin") self.client.login(username="admin", password="admin") + self.faq = StaticContent.objects.get(key="03_faq") @patch("elasticsearch_dsl.Search.execute") @patch("elasticsearch_dsl.Search.count", return_value=0) @@ -26,3 +27,76 @@ def test_search_wrong_dates(self, mock_msg_error, mock_es_count, mock_es_exec): self.assertEqual(response.context["filters"], expected_filters) # But the errors should be added to the messages self.assertEqual(mock_msg_error.call_count, 2) + + def test_static_content_get_en(self): + response = self.client.get("/static_content/", HTTP_ACCEPT_LANGUAGE="en") + self.assertEqual(len(response.context["formset"]), 3) + self.assertContains(response, "## Digital Archives Access Interface") + + def test_static_content_get_fr(self): + response = self.client.get("/static_content/", HTTP_ACCEPT_LANGUAGE="fr") + self.assertEqual(len(response.context["formset"]), 3) + self.assertContains( + response, "## Interface d'accès aux archives numériques" + ) + + def test_static_content_post_en(self): + data = { + "form-TOTAL_FORMS": ["3"], + "form-INITIAL_FORMS": ["3"], + "form-MIN_NUM_FORMS": ["0"], + "form-MAX_NUM_FORMS": ["1000"], + "form-0-content": ["New English content"], + "form-0-key": ["01_home"], + "form-1-content": [""], + "form-1-key": ["02_login"], + "form-2-content": [""], + "form-2-key": ["03_faq"], + } + self.client.post("/static_content/", data, HTTP_ACCEPT_LANGUAGE="en") + static_content = StaticContent.objects.get(key="01_home") + self.assertEqual(static_content.content_en, "New English content") + + def test_static_content_post_fr(self): + data = { + "form-TOTAL_FORMS": ["3"], + "form-INITIAL_FORMS": ["3"], + "form-MIN_NUM_FORMS": ["0"], + "form-MAX_NUM_FORMS": ["1000"], + "form-0-content": ["New French content"], + "form-0-key": ["01_home"], + "form-1-content": [""], + "form-1-key": ["02_login"], + "form-2-content": [""], + "form-2-key": ["03_faq"], + } + self.client.post("/static_content/", data, HTTP_ACCEPT_LANGUAGE="fr") + static_content = StaticContent.objects.get(key="01_home") + self.assertEqual(static_content.content_fr, "New French content") + + def test_faq_markdown(self): + self.faq.content_en = "## Header" + self.faq.save() + response = self.client.get("/faq/", HTTP_ACCEPT_LANGUAGE="en") + self.assertContains(response, "