Merge pull request #2301 from bookwyrm-social/annual-review-tests

Adds a couple tests to annual review views
This commit is contained in:
Mouse Reeve 2022-09-19 20:33:37 -07:00 committed by GitHub
commit 44ce12405b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 4 deletions

View file

@ -21,6 +21,7 @@ def make_date(*args):
class AnnualSummary(TestCase): class AnnualSummary(TestCase):
"""views""" """views"""
# pylint: disable=invalid-name
def setUp(self): def setUp(self):
"""we need basic test data and mocks""" """we need basic test data and mocks"""
self.factory = RequestFactory() self.factory = RequestFactory()
@ -151,3 +152,27 @@ class AnnualSummary(TestCase):
self.assertEqual(result.status_code, 302) self.assertEqual(result.status_code, 302)
self.assertEqual(result.url, "/user/mouse/2020-in-the-books") 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())

View file

@ -120,7 +120,7 @@ def personal_annual_summary(request, year):
@login_required @login_required
@require_POST @require_POST
def summary_add_key(request): def summary_add_key(request):
"""add summary key""" """Create a shareable token for this annual review year"""
year = request.POST["year"] year = request.POST["year"]
user = request.user user = request.user
@ -134,7 +134,7 @@ def summary_add_key(request):
else: else:
user.summary_keys[year] = new_key user.summary_keys[year] = new_key
user.save() user.save(update_fields=["summary_keys"], broadcast=False)
response = redirect("annual-summary", user.localname, year) response = redirect("annual-summary", user.localname, year)
response["Location"] += f"?key={str(new_key)}" response["Location"] += f"?key={str(new_key)}"
@ -144,7 +144,7 @@ def summary_add_key(request):
@login_required @login_required
@require_POST @require_POST
def summary_revoke_key(request): def summary_revoke_key(request):
"""revoke summary key""" """No longer sharing the annual review"""
year = request.POST["year"] year = request.POST["year"]
user = request.user user = request.user
@ -152,7 +152,7 @@ def summary_revoke_key(request):
if user.summary_keys and year in user.summary_keys: if user.summary_keys and year in user.summary_keys:
user.summary_keys.pop(year) user.summary_keys.pop(year)
user.save() user.save(update_fields=["summary_keys"], broadcast=False)
return redirect("annual-summary", user.localname, year) return redirect("annual-summary", user.localname, year)

View file

@ -13,6 +13,7 @@ shell \
dbshell \ dbshell \
restart_celery \ restart_celery \
pytest \ pytest \
pytest_coverage_report \
collectstatic \ collectstatic \
makemessages \ makemessages \
compilemessages \ compilemessages \

View file

@ -10,6 +10,7 @@ shell
dbshell dbshell
restart_celery restart_celery
pytest pytest
pytest_coverage_report
collectstatic collectstatic
makemessages makemessages
compilemessages compilemessages

View file

@ -12,6 +12,7 @@ shell
dbshell dbshell
restart_celery restart_celery
pytest pytest
pytest_coverage_report
collectstatic collectstatic
makemessages makemessages
compilemessages compilemessages