Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jraddaoui committed Jun 2, 2019
1 parent 8c7efb7 commit 1b497f6
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
30 changes: 30 additions & 0 deletions dips/tests/test_migrations.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions dips/tests/test_user_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
76 changes: 75 additions & 1 deletion dips/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -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", "[email protected]", "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)
Expand All @@ -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, "<h2>Header</h2>")

def test_faq_html(self):
self.faq.content_en = "<h2>Header</h2>"
self.faq.save()
response = self.client.get("/faq/", HTTP_ACCEPT_LANGUAGE="en")
self.assertContains(response, "<h2>Header</h2>")

def test_faq_langs(self):
self.faq.content_en = "English content"
self.faq.save()
# English
response = self.client.get("/faq/", HTTP_ACCEPT_LANGUAGE="en")
self.assertContains(response, "English content")
# Fallback
response = self.client.get("/faq/", HTTP_ACCEPT_LANGUAGE="fr")
self.assertContains(response, "English content")
# French
self.faq.content_fr = "French content"
self.faq.save()
response = self.client.get("/faq/", HTTP_ACCEPT_LANGUAGE="fr")
self.assertContains(response, "French content")

0 comments on commit 1b497f6

Please sign in to comment.