From eb45a9dc5d47ffa41b85b09983f61cff16352d21 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" Date: Tue, 28 May 2019 13:59:48 +0000 Subject: [PATCH 01/11] Bump markdown from 3.1 to 3.1.1 Bumps [markdown](https://github.com/Python-Markdown/markdown) from 3.1 to 3.1.1. - [Release notes](https://github.com/Python-Markdown/markdown/releases) - [Commits](https://github.com/Python-Markdown/markdown/compare/3.1...3.1.1) --- Pipfile.lock | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Pipfile.lock b/Pipfile.lock index a2344f9b8..d213a3c1b 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -406,11 +406,11 @@ }, "markdown": { "hashes": [ - "sha256:fc4a6f69a656b8d858d7503bda633f4dd63c2d70cf80abdc6eafa64c4ae8c250", - "sha256:fe463ff51e679377e3624984c829022e2cfb3be5518726b06f608a07a3aad680" + "sha256:2e50876bcdd74517e7b71f3e7a76102050edec255b3983403f1a63e7c8a41e7a", + "sha256:56a46ac655704b91e5b7e6326ce43d5ef72411376588afa1dd90e881b83c7e8c" ], "index": "pypi", - "version": "==3.1" + "version": "==3.1.1" }, "more-itertools": { "hashes": [ @@ -692,10 +692,10 @@ }, "aspy.yaml": { "hashes": [ - "sha256:ae249074803e8b957c83fdd82a99160d0d6d26dff9ba81ba608b42eebd7d8cd3", - "sha256:c7390d79f58eb9157406966201abf26da0d56c07e0ff0deadc39c8f4dbc13482" + "sha256:463372c043f70160a9ec950c3f1e4c3a82db5fca01d334b6bc89c7164d744bdc", + "sha256:e7c742382eff2caed61f87a39d13f99109088e5e93f04d76eb8d4b28aa143f45" ], - "version": "==1.2.0" + "version": "==1.3.0" }, "astroid": { "hashes": [ @@ -750,10 +750,10 @@ }, "cfgv": { "hashes": [ - "sha256:6e9f2feea5e84bc71e56abd703140d7a2c250fc5ba38b8702fd6a68ed4e3b2ef", - "sha256:e7f186d4a36c099a9e20b04ac3108bd8bb9b9257e692ce18c8c3764d5cb12172" + "sha256:32edbe09de6f4521224b87822103a8c16a614d31a894735f7a5b3bcf0eb3c37e", + "sha256:3bd31385cd2bebddbba8012200aaf15aa208539f1b33973759b4d02fc2148da5" ], - "version": "==1.6.0" + "version": "==2.0.0" }, "cfn-lint": { "hashes": [ @@ -930,10 +930,10 @@ }, "importlib-metadata": { "hashes": [ - "sha256:2f2e54cbf6b06b16351e4c40a6adb0860cab6cfb95a0c0fcb58bb789c4b450f5", - "sha256:37bbea81dec44d1ff72d58a1b5c1599a9f3436537f33e9e26f276610064c4830" + "sha256:027cfc6524613de726789072f95d2e4cc64dd1dee8096d42d13f2ead5bd302f5", + "sha256:0d05199e1f0b1a8707a1b9c46476d4a49807fb56cb1b0737db1d37feb42fe31d" ], - "version": "==0.12" + "version": "==0.15" }, "invoke": { "hashes": [ @@ -1182,10 +1182,10 @@ }, "zipp": { "hashes": [ - "sha256:46dfd547d9ccbf8bdc26ecea52818046bb28509f12bb6a0de1cd66ab06e9a9be", - "sha256:d7ac25f895fb65bff937b381353c14eb1fa23d35f40abd72a5342cd57eb57fd1" + "sha256:8c1019c6aad13642199fbe458275ad6a84907634cc9f0989877ccc4a2840139d", + "sha256:ca943a7e809cc12257001ccfb99e3563da9af99d52f261725e96dfe0f9275bc3" ], - "version": "==0.5.0" + "version": "==0.5.1" } } } From 28865e0845c0602bb8e597fa0d3f5d714f8cadc7 Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Tue, 28 May 2019 16:16:57 -0400 Subject: [PATCH 02/11] Refactor test modules This splits out some of the legacy structure into files with better names and a little more clarity of purpose --- concordia/tests/test_account_views.py | 108 ++++++++++++++++++ ...level_views.py => test_top_level_views.py} | 29 +++-- .../tests/{test_view.py => test_views.py} | 98 +--------------- 3 files changed, 134 insertions(+), 101 deletions(-) create mode 100644 concordia/tests/test_account_views.py rename concordia/tests/{test_1st_level_views.py => test_top_level_views.py} (66%) rename concordia/tests/{test_view.py => test_views.py} (91%) diff --git a/concordia/tests/test_account_views.py b/concordia/tests/test_account_views.py new file mode 100644 index 000000000..9d063f8fe --- /dev/null +++ b/concordia/tests/test_account_views.py @@ -0,0 +1,108 @@ +""" +Tests for user account-related views +""" +from django.test import TestCase, override_settings +from django.urls import reverse + +from concordia.models import User + +from .utils import JSONAssertMixin + + +@override_settings(RATELIMIT_ENABLE=False) +class ConcordiaViewTests(JSONAssertMixin, TestCase): + """ + This class contains the unit tests for the view in the concordia app. + """ + + def login_user(self): + """ + Create a user and log the user in + """ + + # create user and login + self.user = User.objects.create_user( + username="tester", email="tester@example.com" + ) + self.user.set_password("top_secret") + self.user.save() + + self.client.login(username="tester", password="top_secret") + + def test_AccountProfileView_get(self): + """ + Test the http GET on route account/profile + """ + + self.login_user() + + response = self.client.get(reverse("user-profile")) + + # validate the web page has the "tester" and "tester@example.com" as values + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, template_name="account/profile.html") + + def test_AccountProfileView_post(self): + """ + This unit test tests the post entry for the route account/profile + :param self: + """ + test_email = "tester@example.com" + + self.login_user() + + response = self.client.post( + reverse("user-profile"), {"email": test_email, "username": "tester"} + ) + + self.assertEqual(response.status_code, 302) + self.assertEqual(response.url, reverse("user-profile")) + + # Verify the User was correctly updated + updated_user = User.objects.get(email=test_email) + self.assertEqual(updated_user.email, test_email) + + def test_AccountProfileView_post_invalid_form(self): + """ + This unit test tests the post entry for the route account/profile but + submits an invalid form + """ + self.login_user() + + response = self.client.post(reverse("user-profile"), {"first_name": "Jimmy"}) + + self.assertEqual(response.status_code, 200) + + # Verify the User was not changed + updated_user = User.objects.get(id=self.user.id) + self.assertEqual(updated_user.first_name, "") + + def test_ajax_session_status_anon(self): + resp = self.client.get(reverse("ajax-session-status")) + data = self.assertValidJSON(resp) + self.assertEqual(data, {}) + + def test_ajax_session_status(self): + self.login_user() + + resp = self.client.get(reverse("ajax-session-status")) + data = self.assertValidJSON(resp) + + self.assertIn("links", data) + self.assertIn("username", data) + + self.assertEqual(data["username"], self.user.username) + + self.assertIn("private", resp["Cache-Control"]) + + def test_ajax_messages(self): + self.login_user() + + resp = self.client.get(reverse("ajax-messages")) + data = self.assertValidJSON(resp) + + self.assertIn("messages", data) + + # This view cannot be cached because the messages would be displayed + # multiple times: + self.assertIn("no-cache", resp["Cache-Control"]) diff --git a/concordia/tests/test_1st_level_views.py b/concordia/tests/test_top_level_views.py similarity index 66% rename from concordia/tests/test_1st_level_views.py rename to concordia/tests/test_top_level_views.py index 389fba8d2..4fd4e91f5 100644 --- a/concordia/tests/test_1st_level_views.py +++ b/concordia/tests/test_top_level_views.py @@ -1,22 +1,22 @@ +""" +Tests for for the top-level & “CMS” views +""" + from django.test import TestCase from django.urls import reverse +from concordia.models import SimplePage -class ViewTest_1st_level(TestCase): - """ - This is a test case for testing all the first level views originated - from home pages. - """ +class TopLevelViewTests(JSONAssertMixin, TestCase): def test_contact_us_get(self): - response = self.client.get(reverse("contact")) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "contact.html") - def test_contact_us_get_pre_populate(self): + def test_contact_us_with_referrer(self): test_http_referrer = "http://foo/bar" response = self.client.get(reverse("contact"), HTTP_REFERER=test_http_referrer) @@ -54,3 +54,18 @@ def test_contact_us_post_invalid(self): self.assertEqual( {"email": ["Enter a valid email address."]}, response.context["form"].errors ) + + def test_simple_page(self): + s = SimplePage.objects.create( + title="Help Center 123", + body="not the real body", + path=reverse("help-center"), + ) + + resp = self.client.get(reverse("help-center")) + self.assertEqual(200, resp.status_code) + self.assertEqual(s.title, resp.context["title"]) + self.assertEqual( + [(reverse("help-center"), s.title)], resp.context["breadcrumbs"] + ) + self.assertEqual(resp.context["body"], f"

{s.body}

") diff --git a/concordia/tests/test_view.py b/concordia/tests/test_views.py similarity index 91% rename from concordia/tests/test_view.py rename to concordia/tests/test_views.py index c637704d3..066d43417 100644 --- a/concordia/tests/test_view.py +++ b/concordia/tests/test_views.py @@ -1,3 +1,7 @@ +""" +Tests for the core application features +""" + from datetime import datetime, timedelta from captcha.models import CaptchaStore @@ -9,7 +13,6 @@ from concordia.models import ( Asset, AssetTranscriptionReservation, - SimplePage, Transcription, TranscriptionStatus, User, @@ -45,54 +48,6 @@ def login_user(self): self.client.login(username="tester", password="top_secret") - def test_AccountProfileView_get(self): - """ - Test the http GET on route account/profile - """ - - self.login_user() - - response = self.client.get(reverse("user-profile")) - - # validate the web page has the "tester" and "tester@example.com" as values - self.assertEqual(response.status_code, 200) - self.assertTemplateUsed(response, template_name="account/profile.html") - - def test_AccountProfileView_post(self): - """ - This unit test tests the post entry for the route account/profile - :param self: - """ - test_email = "tester@example.com" - - self.login_user() - - response = self.client.post( - reverse("user-profile"), {"email": test_email, "username": "tester"} - ) - - self.assertEqual(response.status_code, 302) - self.assertEqual(response.url, reverse("user-profile")) - - # Verify the User was correctly updated - updated_user = User.objects.get(email=test_email) - self.assertEqual(updated_user.email, test_email) - - def test_AccountProfileView_post_invalid_form(self): - """ - This unit test tests the post entry for the route account/profile but - submits an invalid form - """ - self.login_user() - - response = self.client.post(reverse("user-profile"), {"first_name": "Jimmy"}) - - self.assertEqual(response.status_code, 200) - - # Verify the User was not changed - updated_user = User.objects.get(id=self.user.id) - self.assertEqual(updated_user.first_name, "") - def test_campaign_list_view(self): """ Test the GET method for route /campaigns @@ -316,51 +271,6 @@ def test_campaign_report(self): self.assertEqual(ctx["title"], item.project.campaign.title) self.assertEqual(ctx["total_asset_count"], 10) - def test_simple_page(self): - s = SimplePage.objects.create( - title="Help Center 123", - body="not the real body", - path=reverse("help-center"), - ) - - resp = self.client.get(reverse("help-center")) - self.assertEqual(200, resp.status_code) - self.assertEqual(s.title, resp.context["title"]) - self.assertEqual( - [(reverse("help-center"), s.title)], resp.context["breadcrumbs"] - ) - self.assertEqual(resp.context["body"], f"

{s.body}

") - - def test_ajax_session_status_anon(self): - resp = self.client.get(reverse("ajax-session-status")) - data = self.assertValidJSON(resp) - self.assertEqual(data, {}) - - def test_ajax_session_status(self): - self.login_user() - - resp = self.client.get(reverse("ajax-session-status")) - data = self.assertValidJSON(resp) - - self.assertIn("links", data) - self.assertIn("username", data) - - self.assertEqual(data["username"], self.user.username) - - self.assertIn("private", resp["Cache-Control"]) - - def test_ajax_messages(self): - self.login_user() - - resp = self.client.get(reverse("ajax-messages")) - data = self.assertValidJSON(resp) - - self.assertIn("messages", data) - - # This view cannot be cached because the messages would be displayed - # multiple times: - self.assertIn("no-cache", resp["Cache-Control"]) - @override_settings(RATELIMIT_ENABLE=False) class TransactionalViewTests(JSONAssertMixin, TransactionTestCase): From b9cfafcdb82f26e55654a12eaa3df4aa02aa52d1 Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Tue, 28 May 2019 16:26:26 -0400 Subject: [PATCH 03/11] Test: basic checks on health-check endpoint --- concordia/tests/test_top_level_views.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/concordia/tests/test_top_level_views.py b/concordia/tests/test_top_level_views.py index 4fd4e91f5..fca74b228 100644 --- a/concordia/tests/test_top_level_views.py +++ b/concordia/tests/test_top_level_views.py @@ -7,8 +7,21 @@ from concordia.models import SimplePage +from .utils import JSONAssertMixin + class TopLevelViewTests(JSONAssertMixin, TestCase): + def test_healthz(self): + data = self.assertValidJSON(self.client.get("/healthz")) + + for k in ( + "current_time", + "load_average", + "debug", + "database_has_data", + "application_version", + ): + self.assertIn(k, data) def test_contact_us_get(self): response = self.client.get(reverse("contact")) From 5d58a33bb792e7a011ae31a48c78bdec3f333f68 Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Tue, 28 May 2019 16:44:04 -0400 Subject: [PATCH 04/11] Add a test for the homepage view --- concordia/tests/test_top_level_views.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/concordia/tests/test_top_level_views.py b/concordia/tests/test_top_level_views.py index fca74b228..8ceeedfba 100644 --- a/concordia/tests/test_top_level_views.py +++ b/concordia/tests/test_top_level_views.py @@ -23,6 +23,12 @@ def test_healthz(self): ): self.assertIn(k, data) + def test_homepage(self): + response = self.client.get(reverse("homepage")) + + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, "home.html") + def test_contact_us_get(self): response = self.client.get(reverse("contact")) From 9cd4592ef3e06cc1a29acb5f931d7555a459a077 Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Tue, 28 May 2019 16:46:57 -0400 Subject: [PATCH 05/11] Tests: update simple page view * Add a test for the nested page breadcrumb support * Correct docstring * Confirm basic Markdown functionality --- concordia/tests/test_top_level_views.py | 22 ++++++++++++++++++++++ concordia/views.py | 8 +++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/concordia/tests/test_top_level_views.py b/concordia/tests/test_top_level_views.py index 8ceeedfba..1bad16f5d 100644 --- a/concordia/tests/test_top_level_views.py +++ b/concordia/tests/test_top_level_views.py @@ -88,3 +88,25 @@ def test_simple_page(self): [(reverse("help-center"), s.title)], resp.context["breadcrumbs"] ) self.assertEqual(resp.context["body"], f"

{s.body}

") + + def test_nested_simple_page(self): + l1 = SimplePage.objects.create( + title="Help Center", body="not the real body", path=reverse("help-center") + ) + + l2 = SimplePage.objects.create( + title="Help Center Welcome Guide", + body="This is _not_ the real page", + path=reverse("welcome-guide"), + ) + + resp = self.client.get(reverse("welcome-guide")) + self.assertEqual(200, resp.status_code) + self.assertEqual(l2.title, resp.context["title"]) + self.assertEqual( + resp.context["breadcrumbs"], + [(reverse("help-center"), l1.title), (reverse("welcome-guide"), l2.title)], + ) + self.assertHTMLEqual( + resp.context["body"], f"

This is not the real page

" + ) diff --git a/concordia/views.py b/concordia/views.py index c35ab928d..8b48e0c0e 100644 --- a/concordia/views.py +++ b/concordia/views.py @@ -115,13 +115,11 @@ def healthz(request): @default_cache_control def simple_page(request, path=None): """ - Serve static content from Markdown files + Basic content management using Markdown managed in the SimplePage model - Expects the request path with the addition of ".md" to match a file under - the top-level static-pages directory or the url dispatcher configuration to - pass a base_name parameter: + This expects a pre-existing URL path matching the path specified in the database:: - path("foobar/", static_page, {"base_name": "some-weird-filename.md"}) + path("about/", views.simple_page, name="about"), """ if not path: From cc743e5ee3a4de9bd69f482a8452943cc27263af Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Tue, 28 May 2019 16:52:15 -0400 Subject: [PATCH 06/11] Tests: refactor test account code We had the same code in too many places --- concordia/tests/test_account_views.py | 18 ++--------- concordia/tests/test_top_level_views.py | 4 +-- concordia/tests/test_views.py | 40 ++++--------------------- concordia/tests/utils.py | 32 +++++++++++++++++++- 4 files changed, 40 insertions(+), 54 deletions(-) diff --git a/concordia/tests/test_account_views.py b/concordia/tests/test_account_views.py index 9d063f8fe..dd1612957 100644 --- a/concordia/tests/test_account_views.py +++ b/concordia/tests/test_account_views.py @@ -6,29 +6,15 @@ from concordia.models import User -from .utils import JSONAssertMixin +from .utils import CreateTestUsers, JSONAssertMixin @override_settings(RATELIMIT_ENABLE=False) -class ConcordiaViewTests(JSONAssertMixin, TestCase): +class ConcordiaViewTests(CreateTestUsers, JSONAssertMixin, TestCase): """ This class contains the unit tests for the view in the concordia app. """ - def login_user(self): - """ - Create a user and log the user in - """ - - # create user and login - self.user = User.objects.create_user( - username="tester", email="tester@example.com" - ) - self.user.set_password("top_secret") - self.user.save() - - self.client.login(username="tester", password="top_secret") - def test_AccountProfileView_get(self): """ Test the http GET on route account/profile diff --git a/concordia/tests/test_top_level_views.py b/concordia/tests/test_top_level_views.py index 1bad16f5d..c5d3460d5 100644 --- a/concordia/tests/test_top_level_views.py +++ b/concordia/tests/test_top_level_views.py @@ -7,10 +7,10 @@ from concordia.models import SimplePage -from .utils import JSONAssertMixin +from .utils import CreateTestUsers, JSONAssertMixin -class TopLevelViewTests(JSONAssertMixin, TestCase): +class TopLevelViewTests(JSONAssertMixin, CreateTestUsers, TestCase): def test_healthz(self): data = self.assertValidJSON(self.client.get("/healthz")) diff --git a/concordia/tests/test_views.py b/concordia/tests/test_views.py index 066d43417..1c00ee75e 100644 --- a/concordia/tests/test_views.py +++ b/concordia/tests/test_views.py @@ -15,11 +15,11 @@ AssetTranscriptionReservation, Transcription, TranscriptionStatus, - User, ) from concordia.utils import get_anonymous_user from .utils import ( + CreateTestUsers, JSONAssertMixin, create_asset, create_campaign, @@ -29,25 +29,11 @@ @override_settings(RATELIMIT_ENABLE=False) -class ConcordiaViewTests(JSONAssertMixin, TestCase): +class ConcordiaViewTests(CreateTestUsers, JSONAssertMixin, TestCase): """ This class contains the unit tests for the view in the concordia app. """ - def login_user(self): - """ - Create a user and log the user in - """ - - # create user and login - self.user = User.objects.create_user( - username="tester", email="tester@example.com" - ) - self.user.set_password("top_secret") - self.user.save() - - self.client.login(username="tester", password="top_secret") - def test_campaign_list_view(self): """ Test the GET method for route /campaigns @@ -273,21 +259,7 @@ def test_campaign_report(self): @override_settings(RATELIMIT_ENABLE=False) -class TransactionalViewTests(JSONAssertMixin, TransactionTestCase): - def login_user(self): - """ - Create a user and log the user in - """ - - # create user and login - self.user = User.objects.create_user( - username="tester", email="tester@example.com" - ) - self.user.set_password("top_secret") - self.user.save() - - self.client.login(username="tester", password="top_secret") - +class TransactionalViewTests(CreateTestUsers, JSONAssertMixin, TransactionTestCase): def completeCaptcha(self, key=None): """Submit a CAPTCHA response using the provided challenge key""" @@ -778,12 +750,10 @@ def test_duplicate_tag_submission(self): ) data = self.assertValidJSON(resp, expected_status=200) - second_user = User.objects.create_user( + second_user = self.create_test_user( username="second_tester", email="second_tester@example.com" ) - second_user.set_password("secret") - second_user.save() - self.client.login(username="second_tester", password="secret") + self.client.login(username=second_user.username, password=second_user.password) resp = self.client.post( reverse("submit-tags", kwargs={"asset_pk": asset.pk}), diff --git a/concordia/tests/utils.py b/concordia/tests/utils.py index f0d642113..5175653fc 100644 --- a/concordia/tests/utils.py +++ b/concordia/tests/utils.py @@ -1,9 +1,10 @@ import json from functools import wraps +from secrets import token_hex from django.utils.text import slugify -from concordia.models import Asset, Campaign, Item, MediaType, Project +from concordia.models import Asset, Campaign, Item, MediaType, Project, User def ensure_slug(original_function): @@ -132,3 +133,32 @@ def assertValidJSON(self, response, expected_status=200): raise return data + + +class CreateTestUsers(object): + def login_user(self): + """ + Create a user and log the user in + """ + + if not hasattr(self, "user"): + self.user = self.create_test_user("tester") + + self.client.login(username=self.user.username, password=self.user.password) + + def create_test_user(self, username, **kwargs): + """ + Creates a test User account + """ + + if "email" not in kwargs: + kwargs["email"] = f"{username}@example.com" + + user = User.objects.create_user(username=username, **kwargs) + fake_pw = token_hex(24) + user.set_password(fake_pw) + user.save() + + user.password = fake_pw + + return user From e1db62e47a8a7746db61094be1f8b3f3422d6772 Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Tue, 28 May 2019 17:04:09 -0400 Subject: [PATCH 07/11] =?UTF-8?q?Tests:=20verify=20that=20logged-in=20user?= =?UTF-8?q?s=E2=80=99=20email=20is=20prefilled?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- concordia/tests/test_top_level_views.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/concordia/tests/test_top_level_views.py b/concordia/tests/test_top_level_views.py index c5d3460d5..2e4a55dc6 100644 --- a/concordia/tests/test_top_level_views.py +++ b/concordia/tests/test_top_level_views.py @@ -47,6 +47,20 @@ def test_contact_us_with_referrer(self): response.context["form"].initial["referrer"], test_http_referrer ) + def test_contact_us_as_a_logged_in_user(self): + """ + The contact form should pre-fill your email address if you're logged in + """ + + self.login_user() + + response = self.client.get(reverse("contact")) + + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, "contact.html") + + self.assertEqual(response.context["form"].initial["email"], self.user.email) + def test_contact_us_post(self): post_data = { "email": "nobody@example.com", From 69bb051362b9ed248338945b5710718a076d018f Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Tue, 28 May 2019 17:11:24 -0400 Subject: [PATCH 08/11] Tests: more explicit tracking of responses which should not be cached --- concordia/tests/test_account_views.py | 27 +++++++++++++++---------- concordia/tests/test_top_level_views.py | 10 +++++++-- concordia/tests/utils.py | 11 ++++++++++ 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/concordia/tests/test_account_views.py b/concordia/tests/test_account_views.py index dd1612957..398c5e1b3 100644 --- a/concordia/tests/test_account_views.py +++ b/concordia/tests/test_account_views.py @@ -6,11 +6,13 @@ from concordia.models import User -from .utils import CreateTestUsers, JSONAssertMixin +from .utils import CacheControlAssertions, CreateTestUsers, JSONAssertMixin @override_settings(RATELIMIT_ENABLE=False) -class ConcordiaViewTests(CreateTestUsers, JSONAssertMixin, TestCase): +class ConcordiaViewTests( + CreateTestUsers, JSONAssertMixin, CacheControlAssertions, TestCase +): """ This class contains the unit tests for the view in the concordia app. """ @@ -26,6 +28,7 @@ def test_AccountProfileView_get(self): # validate the web page has the "tester" and "tester@example.com" as values self.assertEqual(response.status_code, 200) + self.assertUncacheable(response) self.assertTemplateUsed(response, template_name="account/profile.html") def test_AccountProfileView_post(self): @@ -42,6 +45,7 @@ def test_AccountProfileView_post(self): ) self.assertEqual(response.status_code, 302) + self.assertUncacheable(response) self.assertEqual(response.url, reverse("user-profile")) # Verify the User was correctly updated @@ -58,37 +62,38 @@ def test_AccountProfileView_post_invalid_form(self): response = self.client.post(reverse("user-profile"), {"first_name": "Jimmy"}) self.assertEqual(response.status_code, 200) + self.assertUncacheable(response) # Verify the User was not changed updated_user = User.objects.get(id=self.user.id) self.assertEqual(updated_user.first_name, "") def test_ajax_session_status_anon(self): - resp = self.client.get(reverse("ajax-session-status")) - data = self.assertValidJSON(resp) + response = self.client.get(reverse("ajax-session-status")) + self.assertCachePrivate(response) + data = self.assertValidJSON(response) self.assertEqual(data, {}) def test_ajax_session_status(self): self.login_user() - resp = self.client.get(reverse("ajax-session-status")) - data = self.assertValidJSON(resp) + response = self.client.get(reverse("ajax-session-status")) + self.assertCachePrivate(response) + data = self.assertValidJSON(response) self.assertIn("links", data) self.assertIn("username", data) self.assertEqual(data["username"], self.user.username) - self.assertIn("private", resp["Cache-Control"]) - def test_ajax_messages(self): self.login_user() - resp = self.client.get(reverse("ajax-messages")) - data = self.assertValidJSON(resp) + response = self.client.get(reverse("ajax-messages")) + data = self.assertValidJSON(response) self.assertIn("messages", data) # This view cannot be cached because the messages would be displayed # multiple times: - self.assertIn("no-cache", resp["Cache-Control"]) + self.assertUncacheable(response) diff --git a/concordia/tests/test_top_level_views.py b/concordia/tests/test_top_level_views.py index 2e4a55dc6..4fd2fb7fb 100644 --- a/concordia/tests/test_top_level_views.py +++ b/concordia/tests/test_top_level_views.py @@ -7,10 +7,12 @@ from concordia.models import SimplePage -from .utils import CreateTestUsers, JSONAssertMixin +from .utils import CacheControlAssertions, CreateTestUsers, JSONAssertMixin -class TopLevelViewTests(JSONAssertMixin, CreateTestUsers, TestCase): +class TopLevelViewTests( + JSONAssertMixin, CreateTestUsers, CacheControlAssertions, TestCase +): def test_healthz(self): data = self.assertValidJSON(self.client.get("/healthz")) @@ -33,6 +35,7 @@ def test_contact_us_get(self): response = self.client.get(reverse("contact")) self.assertEqual(response.status_code, 200) + self.assertUncacheable(response) self.assertTemplateUsed(response, "contact.html") def test_contact_us_with_referrer(self): @@ -41,6 +44,7 @@ def test_contact_us_with_referrer(self): response = self.client.get(reverse("contact"), HTTP_REFERER=test_http_referrer) self.assertEqual(response.status_code, 200) + self.assertUncacheable(response) self.assertTemplateUsed(response, "contact.html") self.assertEqual( @@ -57,6 +61,7 @@ def test_contact_us_as_a_logged_in_user(self): response = self.client.get(reverse("contact")) self.assertEqual(response.status_code, 200) + self.assertUncacheable(response) self.assertTemplateUsed(response, "contact.html") self.assertEqual(response.context["form"].initial["email"], self.user.email) @@ -72,6 +77,7 @@ def test_contact_us_post(self): response = self.client.post(reverse("contact"), post_data) self.assertEqual(response.status_code, 302) + self.assertUncacheable(response) def test_contact_us_post_invalid(self): post_data = { diff --git a/concordia/tests/utils.py b/concordia/tests/utils.py index 5175653fc..ddb726504 100644 --- a/concordia/tests/utils.py +++ b/concordia/tests/utils.py @@ -162,3 +162,14 @@ def create_test_user(self, username, **kwargs): user.password = fake_pw return user + + +class CacheControlAssertions(object): + def assertUncacheable(self, response): + self.assertIn("Cache-Control", response) + self.assertIn("no-cache", response["Cache-Control"]) + self.assertIn("no-store", response["Cache-Control"]) + + def assertCachePrivate(self, response): + self.assertIn("Cache-Control", response) + self.assertIn("private", response["Cache-Control"]) From 35116160bea237ba789d4fdb612acc92d02a7708 Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Tue, 28 May 2019 17:23:11 -0400 Subject: [PATCH 09/11] Tests: replace misleading comment with actual test --- concordia/tests/test_account_views.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/concordia/tests/test_account_views.py b/concordia/tests/test_account_views.py index 398c5e1b3..f3ae25621 100644 --- a/concordia/tests/test_account_views.py +++ b/concordia/tests/test_account_views.py @@ -26,11 +26,14 @@ def test_AccountProfileView_get(self): response = self.client.get(reverse("user-profile")) - # validate the web page has the "tester" and "tester@example.com" as values self.assertEqual(response.status_code, 200) self.assertUncacheable(response) self.assertTemplateUsed(response, template_name="account/profile.html") + self.assertEqual(response.context["user"], self.user) + self.assertContains(response, self.user.username) + self.assertContains(response, self.user.email) + def test_AccountProfileView_post(self): """ This unit test tests the post entry for the route account/profile From c75ec2bccdde328b47871ef9e94b6d3bd70172d7 Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Tue, 28 May 2019 17:25:04 -0400 Subject: [PATCH 10/11] =?UTF-8?q?Naming=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exporter/tests/{test_view.py => test_views.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename exporter/tests/{test_view.py => test_views.py} (100%) diff --git a/exporter/tests/test_view.py b/exporter/tests/test_views.py similarity index 100% rename from exporter/tests/test_view.py rename to exporter/tests/test_views.py From 0fd6799e760e3ab38fd30885e1effbceaa44ae8e Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Wed, 29 May 2019 10:08:22 -0400 Subject: [PATCH 11/11] Test: basic coverage for the activity UI --- concordia/tests/test_views.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/concordia/tests/test_views.py b/concordia/tests/test_views.py index 1c00ee75e..71aed1f27 100644 --- a/concordia/tests/test_views.py +++ b/concordia/tests/test_views.py @@ -257,6 +257,18 @@ def test_campaign_report(self): self.assertEqual(ctx["title"], item.project.campaign.title) self.assertEqual(ctx["total_asset_count"], 10) + @override_settings(FLAGS={"ACTIVITY_UI_ENABLED": [("boolean", True)]}) + def test_activity_ui_anonymous(self): + response = self.client.get(reverse("action-app")) + self.assertRedirects(response, "/account/login/?next=/act/") + + @override_settings(FLAGS={"ACTIVITY_UI_ENABLED": [("boolean", True)]}) + def test_activity_ui_logged_in(self): + self.login_user() + response = self.client.get(reverse("action-app")) + self.assertTemplateUsed(response, "action-app.html") + self.assertContains(response, "new ActionApp") + @override_settings(RATELIMIT_ENABLE=False) class TransactionalViewTests(CreateTestUsers, JSONAssertMixin, TransactionTestCase):