def test_get_one_raises_exception_when_service_http_errors( self, requests_mock, response_status, ): """ When the Consent Service responds with a http error, It raises a HTTPError. """ requests_mock.post( f'{settings.CONSENT_SERVICE_BASE_URL}' f'{consent.CONSENT_SERVICE_PERSON_PATH_LOOKUP}', status_code=response_status, ) with pytest.raises(consent.ConsentAPIHTTPError): consent.get_one('*****@*****.**')
def test_get_one(self, requests_mock, accepts_marketing): """ Try to get consent status for a single email address """ matcher = requests_mock.post( f'{settings.CONSENT_SERVICE_BASE_URL}' f'{consent.CONSENT_SERVICE_PERSON_PATH_LOOKUP}', json={ 'results': [ { 'email': '*****@*****.**', 'consents': [ CONSENT_SERVICE_EMAIL_CONSENT_TYPE, ] if accepts_marketing else [], }, ], }, status_code=status.HTTP_200_OK, ) resp = consent.get_one('*****@*****.**') assert resp == accepts_marketing assert matcher.called_once assert matcher.last_request.query == 'limit=1' assert matcher.last_request.json() == {'emails': ['*****@*****.**']}
def to_representation(self, value): """Lookup from consent service api/""" try: representation = consent.get_one(value.email) except consent.ConsentAPIException: representation = False return representation
def test_get_one_raises_exception_on_connection_or_timeout_error( self, requests_mock, exceptions, ): """ When the Consent Service responds with a 4XX error, It raises a ConnectionError or a Timeout Error """ (request_exception, consent_exception) = exceptions requests_mock.post( f'{settings.CONSENT_SERVICE_BASE_URL}' f'{consent.CONSENT_SERVICE_PERSON_PATH_LOOKUP}', exc=request_exception, ) with pytest.raises(consent_exception): consent.get_one('*****@*****.**')
def test_get_one_normalises_emails(self, requests_mock, accepts_marketing): """ Try to get consent status for a single email address """ matcher = requests_mock.get( f'{settings.CONSENT_SERVICE_BASE_URL}' f'{consent.CONSENT_SERVICE_PERSON_PATH_LOOKUP}', text=generate_hawk_response({ 'results': [{ 'email': '*****@*****.**', 'consents': [ CONSENT_SERVICE_EMAIL_CONSENT_TYPE, ] if accepts_marketing else [], }], }), status_code=status.HTTP_200_OK, ) resp = consent.get_one('*****@*****.**') assert resp == accepts_marketing assert matcher.called_once assert matcher.last_request.query == 'email=foo%40bar.com'