Пример #1
0
    def test_get_many(self, requests_mock, accepts_marketing, emails):
        """
        Try to get consent status for a list of email addresses
        """
        matcher = requests_mock.post(
            f'{settings.CONSENT_SERVICE_BASE_URL}'
            f'{consent.CONSENT_SERVICE_PERSON_PATH_LOOKUP}',
            text=generate_hawk_response({
                'results': [
                    {
                        'email': email,
                        'consents': [
                            CONSENT_SERVICE_EMAIL_CONSENT_TYPE,
                        ] if accepts_marketing else [],
                    } for email in emails
                ],
            }),
            status_code=status.HTTP_200_OK,
        )
        resp = consent.get_many(emails)
        assert resp == {email: accepts_marketing for email in emails}

        assert matcher.called_once
        assert matcher.last_request.query == f'limit={len(emails)}'
        assert matcher.last_request.json() == {'emails': emails}
Пример #2
0
 def test_get_many_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,
     )
     emails = ['*****@*****.**', '*****@*****.**', '*****@*****.**']
     with pytest.raises(consent.ConsentAPIHTTPError):
         consent.get_many(emails)
Пример #3
0
    def test_get_many_normalises_emails(self, requests_mock, accepts_marketing,
                                        emails):
        """
        Try to get consent status for a list of email addresses
        """
        matcher = requests_mock.get(
            f'{settings.CONSENT_SERVICE_BASE_URL}'
            f'{consent.CONSENT_SERVICE_PERSON_PATH_LOOKUP}',
            text=generate_hawk_response({
                'results': [{
                    'email':
                    email,
                    'consents': [
                        CONSENT_SERVICE_EMAIL_CONSENT_TYPE,
                    ] if accepts_marketing else [],
                } for email in emails],
            }),
            status_code=status.HTTP_200_OK,
        )
        resp = consent.get_many([email.upper() for email in emails])
        assert resp == {email: accepts_marketing for email in emails}

        assert matcher.called_once
        assert matcher.last_request.query == urllib.parse.urlencode(
            {'email': emails}, doseq=True)
Пример #4
0
    def _add_consent_response(self, rows):
        """
        Transforms iterable to add user consent from the consent service.

        The consent lookup makes an external API call to return consent.
        For perfromance reasons the consent amount is limited by consent_page_size.
        Due to this limitaion the iterable are sliced into chunks requesting consent for 100 rows
        at a time.
        """
        # Slice iterable into chunks
        row_chunks = slice_iterable_into_chunks(rows, self.consent_page_size)
        for chunk in row_chunks:
            """
            Loop over the chunks and extract the email and item.
            Save the item because the iterator cannot be used twice.
            """
            rows = list(chunk)
            # Peform constent lookup on emails POST request
            consent_lookups = consent.get_many([
                row['email']
                for row in rows if self._is_valid_email(row['email'])
            ], )
            for row in rows:
                # Assign contact consent boolean to accepts_dit_email_marketing
                # and yield modified result.
                row['accepts_dit_email_marketing'] = consent_lookups.get(
                    row['email'], False)
                yield row
Пример #5
0
 def test_get_many_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,
     )
     emails = ['*****@*****.**', '*****@*****.**', '*****@*****.**']
     with pytest.raises(consent_exception):
         consent.get_many(emails)
Пример #6
0
 def _enrich_data(self, data):
     """
     Get the marketing consent from the consent service
     """
     if is_feature_flag_active(GET_CONSENT_FROM_CONSENT_SERVICE):
         emails = [item['email'] for item in data]
         consent_lookups = consent.get_many(emails)
         for item in data:
             item['accepts_dit_email_marketing'] = consent_lookups.get(
                 item['email'], False)
     return data