Skip to content

Commit

Permalink
fix: Revert changes that need to be rolled out later in phase
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin-codecov committed Jan 16, 2025
1 parent 6a8ac58 commit f28e35c
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 6 deletions.
23 changes: 19 additions & 4 deletions codecov_auth/commands/owner/interactors/save_terms_agreement.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Any
from typing import Any, Optional

from django.utils import timezone

Expand All @@ -11,15 +11,26 @@

@dataclass
class TermsAgreementInput:
business_email: str
name: str
business_email: Optional[str] = None
name: Optional[str] = None
terms_agreement: bool = False
marketing_consent: bool = False
customer_intent: Optional[str] = None


class SaveTermsAgreementInteractor(BaseInteractor):
requires_service = False

def validate_deprecated(self, input: TermsAgreementInput) -> None:
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()

def validate(self, input: TermsAgreementInput) -> None:
if not input.business_email:
raise ValidationError("Email is required")
Expand Down Expand Up @@ -54,7 +65,11 @@ def execute(self, input: Any) -> None:
business_email=input.get("business_email", ""),
terms_agreement=input.get("terms_agreement", False),
marketing_consent=input.get("marketing_consent", False),
customer_intent=input.get("customer_intent"),
name=input.get("name", ""),
)
self.validate(typed_input)
if input.get("customer_intent"):
self.validate_deprecated(typed_input)
else:
self.validate(typed_input)
return self.update_terms_agreement(typed_input)
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,119 @@ def execute(self, current_user, input=None):
input=real_input,
)

#### Start of older tests

@freeze_time("2022-01-01T00:00:00")
def test_deprecated_update_user_when_agreement_is_false(self):
self.execute(
current_user=self.current_user,
input={"terms_agreement": False, "customer_intent": "Business"},
)
before_refresh_business_email = self.current_user.email

assert self.current_user.terms_agreement == False
assert self.current_user.terms_agreement_at == self.updated_at

self.current_user.refresh_from_db()
assert self.current_user.email == before_refresh_business_email

@freeze_time("2022-01-01T00:00:00")
def test_deprecated_update_user_when_agreement_is_true(self):
self.execute(
current_user=self.current_user,
input={"terms_agreement": True, "customer_intent": "Business"},
)
before_refresh_business_email = self.current_user.email

assert self.current_user.terms_agreement == True
assert self.current_user.terms_agreement_at == self.updated_at

self.current_user.refresh_from_db()
assert self.current_user.email == before_refresh_business_email

@freeze_time("2022-01-01T00:00:00")
def test_deprecated_update_owner_and_user_when_email_is_not_empty(self):
self.execute(
current_user=self.current_user,
input={
"business_email": "[email protected]",
"terms_agreement": True,
"customer_intent": "Business",
},
)

assert self.current_user.terms_agreement == True
assert self.current_user.terms_agreement_at == self.updated_at

self.current_user.refresh_from_db()
assert self.current_user.email == "[email protected]"

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

def test_deprecated_user_is_not_authenticated(self):
with pytest.raises(Unauthenticated):
self.execute(
current_user=AnonymousUser(),
input={
"business_email": "[email protected]",
"terms_agreement": True,
"customer_intent": "Business",
},
)

def test_deprecated_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_deprecated_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_deprecated_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()

#### End of older tests

@freeze_time("2022-01-01T00:00:00")
def test_update_user_when_agreement_is_false(self):
self.execute(
Expand Down
39 changes: 39 additions & 0 deletions graphql_api/tests/mutation/test_save_terms_agreement.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@


class SaveTermsAgreementMutationTest(GraphQLTestHelper, TransactionTestCase):
def _request_deprecated(self, owner=None):
return self.gql_request(
query,
variables={"input": {"termsAgreement": True, "customerIntent": "Business"}},
owner=owner,
)

def _request_invalid_customer_intent_deprecated(self, owner=None):
return self.gql_request(
query,
variables={"input": {"termsAgreement": True, "customerIntent": "invalid"}},
owner=owner,
)

def _request(self, owner=None):
return self.gql_request(
query,
Expand All @@ -31,6 +45,31 @@ def _request(self, owner=None):
owner=owner,
)

def test_invalid_customer_intent_deprecated(self):
owner = OwnerFactory()
assert self._request_invalid_customer_intent_deprecated(owner=owner) == {
"saveTermsAgreement": {
"error": {
"__typename": "ValidationError",
"message": "Invalid customer intent provided",
}
}
}

def test_unauthenticated_deprecated(self):
assert self._request_deprecated() == {
"saveTermsAgreement": {
"error": {
"__typename": "UnauthenticatedError",
"message": "You are not authenticated",
}
}
}

def test_authenticated_deprecated(self):
owner = OwnerFactory()
assert self._request_deprecated(owner=owner) == {"saveTermsAgreement": None}

def test_unauthenticated(self):
assert self._request() == {
"saveTermsAgreement": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ type SaveTermsAgreementPayload {
}

input SaveTermsAgreementInput {
businessEmail: String!
businessEmail: String
termsAgreement: Boolean!
marketingConsent: Boolean
name: String!
name: String
customerIntent: String
}

0 comments on commit f28e35c

Please sign in to comment.