Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Set Hubspot cookie name from request body #4880

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -82,8 +82,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
Loading