From a17135d85382e9094344981de0322448c974a1b5 Mon Sep 17 00:00:00 2001 From: RulaKhaled Date: Mon, 14 Oct 2024 18:58:06 +0200 Subject: [PATCH] Update with email opt in --- .../owner/interactors/save_terms_agreement.py | 19 ++++--- .../tests/test_save_terms_agreement.py | 52 ++++++++++++++++--- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/codecov_auth/commands/owner/interactors/save_terms_agreement.py b/codecov_auth/commands/owner/interactors/save_terms_agreement.py index 113c899c8c..82243720f2 100644 --- a/codecov_auth/commands/owner/interactors/save_terms_agreement.py +++ b/codecov_auth/commands/owner/interactors/save_terms_agreement.py @@ -21,14 +21,11 @@ class SaveTermsAgreementInteractor(BaseInteractor): requires_service = False def validate(self, input: TermsAgreementInput): - if input.terms_agreement is None: - 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() @@ -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 = { diff --git a/codecov_auth/commands/owner/interactors/tests/test_save_terms_agreement.py b/codecov_auth/commands/owner/interactors/tests/test_save_terms_agreement.py index 257e649764..98a6271633 100644 --- a/codecov_auth/commands/owner/interactors/tests/test_save_terms_agreement.py +++ b/codecov_auth/commands/owner/interactors/tests/test_save_terms_agreement.py @@ -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 @@ -24,6 +26,7 @@ def execute( input={ "business_email": None, "terms_agreement": False, + "marketing_consent": False, }, ): return SaveTermsAgreementInteractor(None, "github", current_user).execute( @@ -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 == "something@email.com" - 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( @@ -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()