Skip to content

Commit 7b0bbad

Browse files
authored
Merge pull request #3780 from freelawproject/3755-fix-error-code-404-when-updating-neon-accounts
fix(users): Adds a check for the Neon account ID before firing tasks
2 parents 25ab4c2 + 7906e94 commit 7b0bbad

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

cl/users/tests.py

+54
Original file line numberDiff line numberDiff line change
@@ -3454,3 +3454,57 @@ async def test_can_create_neon_account(self, mock_neon_client) -> None:
34543454

34553455
await up.arefresh_from_db()
34563456
self.assertEqual(up.neon_account_id, "9876")
3457+
3458+
3459+
@override_settings(DEVELOPMENT=False)
3460+
@patch("cl.users.views.create_neon_account")
3461+
@patch("cl.users.views.update_neon_account")
3462+
class NeonAccountUpdateTest(TestCase):
3463+
3464+
def setUp(self) -> None:
3465+
self.client = AsyncClient()
3466+
self.up = UserProfileWithParentsFactory.create(
3467+
user__username="pandora",
3468+
user__password=make_password("password"),
3469+
)
3470+
3471+
async def test_can_call_update_task_when_account_id_found(
3472+
self, update_account_mock, create_account_mock
3473+
) -> None:
3474+
"""Tests whether we use the update task when the account has a neon_account_id"""
3475+
self.up.neon_account_id = "12345"
3476+
await self.up.asave()
3477+
3478+
await self.client.alogin(username="pandora", password="password")
3479+
r = await self.client.post(
3480+
reverse("view_settings"),
3481+
{
3482+
"first_name": "test_name",
3483+
"last_name": "test_last_name",
3484+
"email": self.up.user.email,
3485+
},
3486+
follow=True,
3487+
)
3488+
3489+
self.assertEqual(r.status_code, HTTP_200_OK)
3490+
update_account_mock.delay.assert_called_once_with(self.up.user.pk)
3491+
create_account_mock.delay.assert_not_called()
3492+
3493+
async def test_can_call_create_task_when_no_account_id_found(
3494+
self, update_account_mock, create_account_mock
3495+
) -> None:
3496+
"""Tests whether we use the create task when the account does not have a neon_account_id"""
3497+
await self.client.alogin(username="pandora", password="password")
3498+
r = await self.client.post(
3499+
reverse("view_settings"),
3500+
{
3501+
"first_name": "test_name",
3502+
"last_name": "test_last_name",
3503+
"email": self.up.user.email,
3504+
},
3505+
follow=True,
3506+
)
3507+
3508+
self.assertEqual(r.status_code, HTTP_200_OK)
3509+
create_account_mock.delay.assert_called_once_with(self.up.user.pk)
3510+
update_account_mock.delay.assert_not_called()

cl/users/views.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,10 @@ def view_settings(request: AuthenticatedHttpRequest) -> HttpResponse:
354354
user_form.save()
355355

356356
if not settings.DEVELOPMENT:
357-
update_neon_account.delay(user.pk)
357+
if up.neon_account_id:
358+
update_neon_account.delay(user.pk)
359+
else:
360+
create_neon_account.delay(user.pk)
358361

359362
return HttpResponseRedirect(reverse("view_settings"))
360363

@@ -603,7 +606,10 @@ def confirm_email(request, activation_key):
603606
up.email_confirmed = True
604607
up.save()
605608
if not settings.DEVELOPMENT:
606-
create_neon_account.delay(up.user.pk)
609+
if up.neon_account_id:
610+
update_neon_account.delay(up.user.pk)
611+
else:
612+
create_neon_account.delay(up.user.pk)
607613

608614
return TemplateResponse(
609615
request, "register/confirm.html", {"success": True, "private": True}

0 commit comments

Comments
 (0)