forked from freelawproject/courtlistener
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request freelawproject#4275 from freelawproject/4204-feat-…
…retry-neon-creation-task feat(neon): Adds retry logic for Neon account creation
- Loading branch information
Showing
3 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from collections import defaultdict | ||
from datetime import timedelta | ||
from http import HTTPStatus | ||
from unittest.mock import patch | ||
|
||
from asgiref.sync import sync_to_async | ||
from django.core import mail | ||
from django.test import override_settings | ||
from django.test.client import AsyncClient, Client | ||
|
@@ -17,6 +19,7 @@ | |
from cl.lib.test_helpers import UserProfileWithParentsFactory | ||
from cl.tests.cases import TestCase | ||
from cl.users.models import UserProfile | ||
from cl.users.utils import create_stub_account | ||
|
||
|
||
class EmailCommandTest(TestCase): | ||
|
@@ -376,3 +379,59 @@ async def test_uses_insensitive_match_for_emails( | |
|
||
# Check the neon_account_id was updated properly | ||
self.assertEqual(membership.user.profile.neon_account_id, "9524") | ||
|
||
@patch( | ||
"cl.lib.neon_utils.NeonClient.get_acount_by_id", | ||
) | ||
@patch.object( | ||
MembershipWebhookViewSet, "_store_webhook_payload", return_value=None | ||
) | ||
async def test_updates_account_with_recent_login( | ||
self, mock_store_webhook, mock_get_account | ||
) -> None: | ||
# Create two profile records - one stub, one regular user, | ||
_, stub_profile = await sync_to_async(create_stub_account)( | ||
{ | ||
"email": "[email protected]", | ||
"first_name": "test", | ||
"last_name": "test", | ||
}, | ||
defaultdict(lambda: ""), | ||
) | ||
|
||
user_profile = await sync_to_async(UserProfileWithParentsFactory)( | ||
user__email="[email protected]" | ||
) | ||
user = user_profile.user | ||
# Updates last login field for the regular user | ||
user.last_login = now() | ||
await user.asave() | ||
|
||
# mocks the Neon API response | ||
mock_get_account.return_value = { | ||
"accountId": "1246", | ||
"primaryContact": { | ||
"email1": "[email protected]", | ||
"firstName": "test", | ||
"lastName": "test", | ||
}, | ||
} | ||
|
||
self.data["eventTrigger"] = "createMembership" | ||
self.data["data"]["membership"]["accountId"] = "1246" | ||
r = await self.async_client.post( | ||
reverse("membership-webhooks-list", kwargs={"version": "v3"}), | ||
data=self.data, | ||
content_type="application/json", | ||
) | ||
self.assertEqual(r.status_code, HTTPStatus.CREATED) | ||
|
||
# Refresh both profiles to ensure updated data | ||
await stub_profile.arefresh_from_db() | ||
await user_profile.arefresh_from_db() | ||
|
||
# Verify stub account remains untouched | ||
self.assertEqual(stub_profile.neon_account_id, "") | ||
|
||
# Verify regular user account is updated with Neon data | ||
self.assertEqual(user_profile.neon_account_id, "1246") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters