Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.
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
19 changes: 9 additions & 10 deletions codecov_auth/commands/owner/interactors/save_terms_agreement.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ class SaveTermsAgreementInteractor(BaseInteractor):
requires_service = False

def validate(self, input: TermsAgreementInput):
if input.terms_agreement is None:
Copy link
Contributor

@ajay-sentry ajay-sentry Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per Rola, condition is never used so this is dead code removal

raise ValidationError("Terms of agreement cannot be null")
if input.customer_intent and input.customer_intent not in [
"Business",
"BUSINESS",
"Personal",
"PERSONAL",
]:
valid_customer_intents = ["Business", "BUSINESS", "Personal", "PERSONAL"]
if (
input.customer_intent
and input.customer_intent not in valid_customer_intents
):
raise ValidationError("Invalid customer intent provided")
if not self.current_user.is_authenticated:
raise Unauthenticated()
Expand All @@ -37,13 +34,15 @@ def update_terms_agreement(self, input: TermsAgreementInput):
self.current_user.terms_agreement = input.terms_agreement
self.current_user.terms_agreement_at = timezone.now()
self.current_user.customer_intent = input.customer_intent
self.current_user.email_opt_in = input.marketing_consent
self.current_user.save()

if input.business_email is not None and input.business_email != "":
if input.business_email and input.business_email != "":
self.current_user.email = input.business_email
self.current_user.save()

self.send_data_to_marketo()
if input.marketing_consent:
self.send_data_to_marketo()

def send_data_to_marketo(self):
event_data = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest.mock import patch

import pytest
from asgiref.sync import async_to_sync
from django.contrib.auth.models import AnonymousUser
Expand All @@ -24,6 +26,7 @@ def execute(
input={
"business_email": None,
"terms_agreement": False,
"marketing_consent": False,
},
):
return SaveTermsAgreementInteractor(None, "github", current_user).execute(
Expand Down Expand Up @@ -75,13 +78,6 @@ def test_update_owner_and_user_when_email_is_not_empty(self):
self.current_user.refresh_from_db()
assert self.current_user.email == "[email protected]"

def test_validation_error_when_terms_is_none(self):
with pytest.raises(ValidationError):
self.execute(
current_user=self.current_user,
input={"terms_agreement": None, "customer_intent": "Business"},
)

def test_validation_error_when_customer_intent_invalid(self):
with pytest.raises(ValidationError):
self.execute(
Expand All @@ -99,3 +95,45 @@ def test_user_is_not_authenticated(self):
"customer_intent": "Business",
},
)

def test_email_opt_in_saved_in_db(self):
self.execute(
current_user=self.current_user,
input={
"terms_agreement": True,
"marketing_consent": True,
"customer_intent": "Business",
},
)
self.current_user.refresh_from_db()
assert self.current_user.email_opt_in == True

@patch(
"codecov_auth.commands.owner.interactors.save_terms_agreement.SaveTermsAgreementInteractor.send_data_to_marketo"
)
def test_marketo_called_only_with_consent(self, mock_send_data_to_marketo):
self.execute(
current_user=self.current_user,
input={
"terms_agreement": True,
"marketing_consent": True,
"customer_intent": "Business",
},
)

mock_send_data_to_marketo.assert_called_once()

@patch(
"codecov_auth.commands.owner.interactors.save_terms_agreement.SaveTermsAgreementInteractor.send_data_to_marketo"
)
def test_marketo_not_called_without_consent(self, mock_send_data_to_marketo):
self.execute(
current_user=self.current_user,
input={
"terms_agreement": True,
"marketing_consent": False,
"customer_intent": "Business",
},
)

mock_send_data_to_marketo.assert_not_called()
Loading