def execute(self, application_id: int) -> None: beneficiary_pre_subscription = get_application_by_id(application_id) user = find_user_by_email(beneficiary_pre_subscription.email) try: validate(beneficiary_pre_subscription, preexisting_account=user) except CantRegisterBeneficiary as cant_register_beneficiary_exception: self.beneficiary_repository.reject( beneficiary_pre_subscription, detail=str(cant_register_beneficiary_exception)) send_rejection_email_to_beneficiary_pre_subscription( beneficiary_pre_subscription=beneficiary_pre_subscription, beneficiary_is_eligible=isinstance( cant_register_beneficiary_exception, BeneficiaryIsADuplicate), send_email=send_raw_email, ) else: if not user: beneficiary = self.beneficiary_repository.save( beneficiary_pre_subscription) else: beneficiary = users_api.activate_beneficiary( user, beneficiary_pre_subscription.deposit_source) users_api.attach_beneficiary_import_details( user, beneficiary_pre_subscription) send_activation_email(user=beneficiary, send_email=send_raw_email)
def execute(self, application_id: int) -> None: beneficiary_pre_subscription = get_application_by_id(application_id) user = find_user_by_email(beneficiary_pre_subscription.email) try: validate(beneficiary_pre_subscription, preexisting_account=user) except CantRegisterBeneficiary as cant_register_beneficiary_exception: self.beneficiary_repository.reject( beneficiary_pre_subscription, detail=str(cant_register_beneficiary_exception)) send_rejection_email_to_beneficiary_pre_subscription( beneficiary_pre_subscription=beneficiary_pre_subscription, beneficiary_is_eligible=isinstance( cant_register_beneficiary_exception, BeneficiaryIsADuplicate), ) else: beneficiary = self.beneficiary_repository.save( beneficiary_pre_subscription, user=user) if user is None: send_activation_email(user=beneficiary) else: send_accepted_as_beneficiary_email(user=beneficiary)
def test_doesnt_raise_if_no_exact_duplicate(app): # Given first_name = "John" last_name = "Doe" date_of_birth = datetime(1993, 2, 2) existing_user1 = create_user(first_name="Joe", last_name=last_name, date_of_birth=date_of_birth, email="*****@*****.**") existing_user2 = create_user(first_name=first_name, last_name="Trump", date_of_birth=date_of_birth, email="*****@*****.**") existing_user3 = create_user(first_name=first_name, last_name=last_name, date_of_birth=datetime(1992, 2, 2), email="*****@*****.**") repository.save(existing_user1, existing_user2, existing_user3) beneficiary_pre_subcription = create_domain_beneficiary_pre_subcription( first_name=first_name, last_name=last_name, date_of_birth=date_of_birth) try: # When validate(beneficiary_pre_subcription) except CantRegisterBeneficiary: # Then assert pytest.fail( "Should not raise an exception when email not given")
def test_validates_for_non_beneficiary_with_same_mail(app): email = "*****@*****.**" existing_user = create_user(email=email, is_beneficiary=False, is_email_validated=True) repository.save(existing_user) beneficiary_pre_subcription = create_domain_beneficiary_pre_subcription(email=email) # Should not raise an exception validate(beneficiary_pre_subcription, preexisting_account=existing_user)
def test_should_not_raise_exception_for_valid_beneficiary(app): # Given beneficiary_pre_subcription = create_domain_beneficiary_pre_subcription() try: # When validate(beneficiary_pre_subcription) except CantRegisterBeneficiary: # Then assert pytest.fail("Should not raise an exception when email not given")
def test_should_not_raise_if_eligible(app, postal_code): # Given beneficiary_pre_subcription = create_domain_beneficiary_pre_subcription(postal_code=postal_code) try: # When validate(beneficiary_pre_subcription) except CantRegisterBeneficiary: # Then assert pytest.fail("Should not raise when postal code is eligible")
def test_raises_if_not_eligible(app, postal_code): # Given beneficiary_pre_subcription = create_domain_beneficiary_pre_subcription(postal_code=postal_code) # When with pytest.raises(BeneficiaryIsNotEligible) as error: validate(beneficiary_pre_subcription) # Then assert str(error.value) == f"Postal code {postal_code} is not eligible."
def test_doesnt_raise_if_email_not_taken(app): # Given existing_user = create_user(email="*****@*****.**") repository.save(existing_user) beneficiary_pre_subcription = create_domain_beneficiary_pre_subcription(email="*****@*****.**") try: # When validate(beneficiary_pre_subcription) except CantRegisterBeneficiary: # Then assert pytest.fail("Should not raise an exception when email not given")
def test_raises_if_email_already_taken_by_beneficiary(app): # Given email = "*****@*****.**" existing_user = create_user(email=email) repository.save(existing_user) beneficiary_pre_subcription = create_domain_beneficiary_pre_subcription(email=email) # When with pytest.raises(BeneficiaryIsADuplicate) as error: validate(beneficiary_pre_subcription, preexisting_account=existing_user) # Then assert str(error.value) == f"Email {email} is already taken."
def test_raises_if_duplicate(app): # Given first_name = "John" last_name = "Doe" date_of_birth = datetime(1993, 2, 2) existing_user = create_user(first_name=first_name, last_name=last_name, date_of_birth=date_of_birth) repository.save(existing_user) beneficiary_pre_subcription = create_domain_beneficiary_pre_subcription( first_name=first_name, last_name=last_name, date_of_birth=date_of_birth ) # When with pytest.raises(BeneficiaryIsADuplicate) as error: validate(beneficiary_pre_subcription) # Then assert str(error.value) == f"User with id {existing_user.id} is a duplicate."