Skip to content

Commit

Permalink
fix: Set Hubspot cookie name from request body (#4880)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachaysan authored Dec 3, 2024
1 parent 81c96f4 commit 7d3b253
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
32 changes: 15 additions & 17 deletions api/integrations/lead_tracking/hubspot/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,21 @@


def register_hubspot_tracker(request: Request) -> None:
hubspot_cookie = request.COOKIES.get(HUBSPOT_COOKIE_NAME)
hubspot_cookie = request.data.get(HUBSPOT_COOKIE_NAME)

# TODO: Remove this temporary debugging logger statement.
logger.info(f"Request cookies for user {request.user.email}: {request.COOKIES}")

if hubspot_cookie:
logger.info(
f"Creating HubspotTracker instance for user {request.user.email} with cookie {hubspot_cookie}"
)

HubspotTracker.objects.update_or_create(
user=request.user,
defaults={
"hubspot_cookie": hubspot_cookie,
},
)
else:
if not hubspot_cookie:
logger.info(
f"Could not create HubspotTracker instance for user {request.user.email} since no cookie"
f"Request did not included Hubspot data for user {request.user.email}"
)
return

logger.info(
f"Creating HubspotTracker instance for user {request.user.email} with cookie {hubspot_cookie}"
)

HubspotTracker.objects.update_or_create(
user=request.user,
defaults={
"hubspot_cookie": hubspot_cookie,
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from organisations.invites.models import Invite, InviteLink
from organisations.models import Organisation, OrganisationRole, Subscription
from users.models import FFAdminUser
from users.models import FFAdminUser, HubspotTracker


def test_create_invite_link(
Expand Down Expand Up @@ -166,13 +166,17 @@ def test_join_organisation_with_permission_groups(
subscription.save()

url = reverse("api-v1:users:user-join-organisation", args=[invite.hash])
data = {"hubspotutk": "somehubspotdata"}

# When
response = test_user_client.post(url)
response = test_user_client.post(url, data)
test_user.refresh_from_db()

# Then
assert response.status_code == status.HTTP_200_OK
hubspot_tracker = HubspotTracker.objects.first()
assert hubspot_tracker.user == test_user

assert organisation in test_user.organisations.all()
assert user_permission_group in test_user.permission_groups.all()
# and invite is deleted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ def test_non_superuser_can_create_new_organisation_by_default(
data = {
"name": org_name,
"webhook_notification_email": webhook_notification_email,
HUBSPOT_COOKIE_NAME: "test_cookie_tracker",
}
staff_client.cookies[HUBSPOT_COOKIE_NAME] = "test_cookie_tracker"

assert not HubspotTracker.objects.filter(user=staff_user).exists()

# When
Expand Down
8 changes: 4 additions & 4 deletions api/tests/unit/users/test_unit_users_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ def test_join_organisation(
organisation = Organisation.objects.create(name="test org")
invite = Invite.objects.create(email=staff_user.email, organisation=organisation)
url = reverse("api-v1:users:user-join-organisation", args=[invite.hash])
staff_client.cookies[HUBSPOT_COOKIE_NAME] = "test_cookie_tracker"
data = {HUBSPOT_COOKIE_NAME: "test_cookie_tracker"}
assert not HubspotTracker.objects.filter(user=staff_user).exists()

# When
response = staff_client.post(url)
response = staff_client.post(url, data)
staff_user.refresh_from_db()

# Then
Expand All @@ -56,11 +56,11 @@ def test_join_organisation_via_link(
organisation = Organisation.objects.create(name="test org")
invite = InviteLink.objects.create(organisation=organisation)
url = reverse("api-v1:users:user-join-organisation-link", args=[invite.hash])
staff_client.cookies[HUBSPOT_COOKIE_NAME] = "test_cookie_tracker"
data = {HUBSPOT_COOKIE_NAME: "test_cookie_tracker"}
assert not HubspotTracker.objects.filter(user=staff_user).exists()

# When
response = staff_client.post(url)
response = staff_client.post(url, data)
staff_user.refresh_from_db()

# Then
Expand Down

0 comments on commit 7d3b253

Please sign in to comment.