Skip to content

Commit

Permalink
Merge pull request freelawproject#3450 from ttys0dev/more-async
Browse files Browse the repository at this point in the history
More async test/view migrations
  • Loading branch information
mlissner authored Dec 11, 2023
2 parents b30749e + 03f5d52 commit b78774b
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 190 deletions.
55 changes: 31 additions & 24 deletions cl/alerts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
from urllib.parse import parse_qs, urlparse

import time_machine
from asgiref.sync import sync_to_async
from django.conf import settings
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User
from django.core import mail
from django.core.management import call_command
from django.test import Client, override_settings
from django.test import AsyncClient, override_settings
from django.urls import reverse
from django.utils import timezone
from django.utils.timezone import now
Expand Down Expand Up @@ -96,49 +97,55 @@ def setUp(self) -> None:
def tearDown(self) -> None:
Alert.objects.all().delete()

def test_create_alert(self) -> None:
async def test_create_alert(self) -> None:
"""Can we create an alert by sending a post?"""
self.assertTrue(
self.client.login(username="pandora", password="password")
await self.async_client.alogin(
username="pandora", password="password"
)
)
r = self.client.post(
r = await self.async_client.post(
reverse("show_results"), self.alert_params, follow=True
)
self.assertEqual(r.redirect_chain[0][1], 302)
self.assertIn("successfully", r.content.decode())
self.client.logout()
await self.async_client.alogout()

def test_fail_gracefully(self) -> None:
async def test_fail_gracefully(self) -> None:
"""Do we fail gracefully when an invalid alert form is sent?"""
# Use a copy to shield other tests from changes.
bad_alert_params = self.alert_params.copy()
# Break the form
bad_alert_params.pop("query", None)
self.assertTrue(
self.client.login(username="pandora", password="password")
await self.async_client.alogin(
username="pandora", password="password"
)
)
r = self.client.post("/", bad_alert_params, follow=True)
r = await self.async_client.post("/", bad_alert_params, follow=True)
self.assertEqual(r.status_code, 200)
self.assertIn("error creating your alert", r.content.decode())
self.client.logout()
await self.async_client.alogout()

def test_new_alert_gets_secret_key(self) -> None:
"""When you create a new alert, does it get a secret key?"""
self.assertTrue(self.alert.secret_key)

def test_are_alerts_disabled_when_the_link_is_visited(self) -> None:
async def test_are_alerts_disabled_when_the_link_is_visited(self) -> None:
self.assertEqual(self.alert.rate, self.alert_params["rate"])
self.client.get(reverse("disable_alert", args=[self.alert.secret_key]))
self.alert.refresh_from_db()
await self.async_client.get(
reverse("disable_alert", args=[self.alert.secret_key])
)
await self.alert.arefresh_from_db()
self.assertEqual(self.alert.rate, "off")

def test_are_alerts_enabled_when_the_link_is_visited(self) -> None:
async def test_are_alerts_enabled_when_the_link_is_visited(self) -> None:
self.assertEqual(self.alert.rate, self.alert_params["rate"])
self.alert.rate = "off"
new_rate = "wly"
path = reverse("enable_alert", args=[self.alert.secret_key])
self.client.get(f"{path}?rate={new_rate}")
self.alert.refresh_from_db()
await self.async_client.get(f"{path}?rate={new_rate}")
await self.alert.arefresh_from_db()
self.assertEqual(self.alert.rate, new_rate)


Expand Down Expand Up @@ -326,7 +333,7 @@ class AlertSeleniumTest(BaseSeleniumTest):

def setUp(self) -> None:
# Set up some handy variables
self.client = Client()
self.async_client = AsyncClient()
self.alert_params = {
"query": "q=asdf",
"name": "dummy alert",
Expand Down Expand Up @@ -1185,13 +1192,13 @@ def test_old_docket_alert_report_timeline(self):
self.assertEqual(len(report.disabled_alerts), 1)
self.assertEqual(active_docket_alerts.count(), 0)

def test_toggle_docket_alert_date_update(self):
async def test_toggle_docket_alert_date_update(self):
"""Does the docket alert toggle view properly update docket alerts
date_modified when toggling the alert_type?
"""

# A docket alert is created today for a case terminated on 2020-01-01
da = DocketAlertWithParentsFactory(
da = await sync_to_async(DocketAlertWithParentsFactory)(
docket__source=Docket.RECAP,
docket__date_terminated="2020-01-01",
docket__date_last_filing=None,
Expand All @@ -1201,38 +1208,38 @@ def test_toggle_docket_alert_date_update(self):
)

self.assertTrue(
self.client.login(
await self.async_client.alogin(
username=self.user_profile.user.username, password="password"
)
)
post_data = {"id": da.docket.pk}
header = {"HTTP_X_REQUESTED_WITH": "XMLHttpRequest"}
header = {"X_REQUESTED_WITH": "XMLHttpRequest"}

# Send an AJAX request to toggle_docket_alert view and confirm the
# is disabled and the date_modified updated.
sixty_days_ahead = now() + timedelta(days=60)
with time_machine.travel(sixty_days_ahead, tick=False):
self.client.post(
await self.async_client.post(
reverse("toggle_docket_alert"),
data=post_data,
follow=True,
**header,
)
da.refresh_from_db()
await da.arefresh_from_db()
self.assertEqual(da.alert_type, DocketAlert.UNSUBSCRIPTION)
self.assertEqual(da.date_modified, sixty_days_ahead)

# Send an AJAX request to toggle_docket_alert view and confirm the
# is enabled and the date_modified updated.
eighty_five_days_ahead = now() + timedelta(days=85)
with time_machine.travel(eighty_five_days_ahead, tick=False):
self.client.post(
await self.async_client.post(
reverse("toggle_docket_alert"),
data=post_data,
follow=True,
**header,
)
da.refresh_from_db()
await da.arefresh_from_db()
self.assertEqual(da.alert_type, DocketAlert.SUBSCRIPTION)
self.assertEqual(da.date_modified, eighty_five_days_ahead)

Expand Down
14 changes: 7 additions & 7 deletions cl/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Dict
from unittest import mock

from asgiref.sync import async_to_sync, sync_to_async
from asgiref.sync import async_to_sync
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import Permission
from django.contrib.humanize.templatetags.humanize import intcomma, ordinal
Expand Down Expand Up @@ -388,7 +388,7 @@ class DRFJudgeApiFilterTests(
@async_to_sync
async def setUp(self) -> None:
self.assertTrue(
await sync_to_async(self.async_client.login)(
await self.async_client.alogin(
username="pandora", password="password"
)
)
Expand Down Expand Up @@ -592,7 +592,7 @@ def setUpTestData(cls) -> None:
@async_to_sync
async def setUp(self) -> None:
self.assertTrue(
await sync_to_async(self.async_client.login)(
await self.async_client.alogin(
username="recap-user", password="password"
)
)
Expand Down Expand Up @@ -748,7 +748,7 @@ def setUpTestData(cls) -> None:
@async_to_sync
async def setUp(self) -> None:
self.assertTrue(
await sync_to_async(self.async_client.login)(
await self.async_client.alogin(
username="recap-user", password="password"
)
)
Expand Down Expand Up @@ -903,7 +903,7 @@ async def test_only_some_fields_returned(self) -> None:
fields_to_return = ["educations", "date_modified", "slug"]
q = {"fields": ",".join(fields_to_return)}
self.assertTrue(
await sync_to_async(self.async_client.login)(
await self.async_client.alogin(
username="pandora", password="password"
)
)
Expand Down Expand Up @@ -980,7 +980,7 @@ def setUpTestData(cls) -> None:
async def test_has_access(self) -> None:
"""Does the RECAP user have access to all of the RECAP endpoints?"""
self.assertTrue(
await sync_to_async(self.async_client.login)(
await self.async_client.alogin(
username="recap-user", password="password"
)
)
Expand All @@ -993,7 +993,7 @@ async def test_has_access(self) -> None:
async def test_lacks_access(self) -> None:
"""Does a normal user lack access to the RECPAP endpoints?"""
self.assertTrue(
await sync_to_async(self.async_client.login)(
await self.async_client.alogin(
username="pandora", password="password"
)
)
Expand Down
6 changes: 3 additions & 3 deletions cl/audio/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ def setUpTestData(cls) -> None:
duration=5,
)

def test_do_jurisdiction_podcasts_have_good_content(self) -> None:
async def test_do_jurisdiction_podcasts_have_good_content(self) -> None:
"""Can we simply load a jurisdiction podcast page?"""

# Test jurisdiction_podcast for a court.
response = self.client.get(
response = await self.async_client.get(
reverse(
"jurisdiction_podcast",
kwargs={"court": self.court_1.id},
Expand Down Expand Up @@ -108,7 +108,7 @@ def test_do_jurisdiction_podcasts_have_good_content(self) -> None:
)

# Test all_jurisdictions_podcast
response = self.client.get(
response = await self.async_client.get(
reverse(
"all_jurisdictions_podcast",
)
Expand Down
14 changes: 7 additions & 7 deletions cl/citations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,9 +815,9 @@ def _tree_has_content(self, content, expected_count):
)
self.assertEqual(count, expected_count)

def test_basic_cited_by_feed(self) -> None:
async def test_basic_cited_by_feed(self) -> None:
"""Can we load the cited-by feed and does it have content?"""
r = self.client.get(
r = await self.async_client.get(
reverse("search_feed", args=["search"]),
{"q": f"cites:{self.opinion_1.pk}"},
)
Expand All @@ -826,18 +826,18 @@ def test_basic_cited_by_feed(self) -> None:
expected_count = 1
self._tree_has_content(r.content, expected_count)

def test_unicode_content(self) -> None:
async def test_unicode_content(self) -> None:
"""Does the citation feed continue working even when we have a unicode
case name?
"""
new_case_name = (
"MAC ARTHUR KAMMUELLER, \u2014 v. LOOMIS, FARGO & " "CO., \u2014"
)
OpinionCluster.objects.filter(pk=self.opinion_cluster_1.pk).update(
case_name=new_case_name
)
await OpinionCluster.objects.filter(
pk=self.opinion_cluster_1.pk
).aupdate(case_name=new_case_name)

r = self.client.get(
r = await self.async_client.get(
reverse("search_feed", args=["search"]),
{"q": f"cites:{self.opinion_1.pk}"},
)
Expand Down
Loading

0 comments on commit b78774b

Please sign in to comment.