From 3a73ced5fa126b5fff95ca803f16d0752a021091 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 19 Sep 2022 14:38:30 -0700 Subject: [PATCH] Adds a couple tests to annual review views Also I tweaked the save functions so they don't broadcast --- bookwyrm/tests/views/test_annual_summary.py | 25 +++++++++++++++++++++ bookwyrm/views/annual_summary.py | 8 +++---- complete_bwdev.fish | 1 + complete_bwdev.sh | 1 + complete_bwdev.zsh | 1 + 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/bookwyrm/tests/views/test_annual_summary.py b/bookwyrm/tests/views/test_annual_summary.py index aeb70794b..aaba0aac6 100644 --- a/bookwyrm/tests/views/test_annual_summary.py +++ b/bookwyrm/tests/views/test_annual_summary.py @@ -21,6 +21,7 @@ def make_date(*args): class AnnualSummary(TestCase): """views""" + # pylint: disable=invalid-name def setUp(self): """we need basic test data and mocks""" self.factory = RequestFactory() @@ -151,3 +152,27 @@ class AnnualSummary(TestCase): self.assertEqual(result.status_code, 302) self.assertEqual(result.url, "/user/mouse/2020-in-the-books") + + def test_summary_add_key(self, *_): + """add shareable key""" + self.assertFalse("2022" in self.local_user.summary_keys.keys()) + + request = self.factory.post("", {"year": "2022"}) + request.user = self.local_user + + result = views.summary_add_key(request) + + self.assertEqual(result.status_code, 302) + self.assertIsNotNone(self.local_user.summary_keys["2022"]) + + def test_summary_revoke_key(self, *_): + """add shareable key""" + self.assertTrue("2020" in self.local_user.summary_keys.keys()) + + request = self.factory.post("", {"year": "2020"}) + request.user = self.local_user + + result = views.summary_revoke_key(request) + + self.assertEqual(result.status_code, 302) + self.assertFalse("2020" in self.local_user.summary_keys.keys()) diff --git a/bookwyrm/views/annual_summary.py b/bookwyrm/views/annual_summary.py index 5d8e14089..4f9c0a794 100644 --- a/bookwyrm/views/annual_summary.py +++ b/bookwyrm/views/annual_summary.py @@ -120,7 +120,7 @@ def personal_annual_summary(request, year): @login_required @require_POST def summary_add_key(request): - """add summary key""" + """Create a shareable token for this annual review year""" year = request.POST["year"] user = request.user @@ -134,7 +134,7 @@ def summary_add_key(request): else: user.summary_keys[year] = new_key - user.save() + user.save(update_fields=["summary_keys"], broadcast=False) response = redirect("annual-summary", user.localname, year) response["Location"] += f"?key={str(new_key)}" @@ -144,7 +144,7 @@ def summary_add_key(request): @login_required @require_POST def summary_revoke_key(request): - """revoke summary key""" + """No longer sharing the annual review""" year = request.POST["year"] user = request.user @@ -152,7 +152,7 @@ def summary_revoke_key(request): if user.summary_keys and year in user.summary_keys: user.summary_keys.pop(year) - user.save() + user.save(update_fields=["summary_keys"], broadcast=False) return redirect("annual-summary", user.localname, year) diff --git a/complete_bwdev.fish b/complete_bwdev.fish index ac87739f2..fbdae2492 100644 --- a/complete_bwdev.fish +++ b/complete_bwdev.fish @@ -13,6 +13,7 @@ shell \ dbshell \ restart_celery \ pytest \ +pytest_coverage_report \ collectstatic \ makemessages \ compilemessages \ diff --git a/complete_bwdev.sh b/complete_bwdev.sh index 3bfc95a67..92b102362 100644 --- a/complete_bwdev.sh +++ b/complete_bwdev.sh @@ -10,6 +10,7 @@ shell dbshell restart_celery pytest +pytest_coverage_report collectstatic makemessages compilemessages diff --git a/complete_bwdev.zsh b/complete_bwdev.zsh index 963597231..fa6dbfa3f 100644 --- a/complete_bwdev.zsh +++ b/complete_bwdev.zsh @@ -12,6 +12,7 @@ shell dbshell restart_celery pytest +pytest_coverage_report collectstatic makemessages compilemessages