Beispiel #1
0
 def test_to_representation(
     self,
     requests_mock,
     accepts_marketing,
 ):
     """
     Test accepts_dit_email_marketing fields is populated by the consent service
     when the feature flag is enabled.
     """
     contact = self._make_contact()
     hawk_response = HawkMockJSONResponse(
         api_id=settings.COMPANY_MATCHING_HAWK_ID,
         api_key=settings.COMPANY_MATCHING_HAWK_KEY,
         response={
             'results': [{
                 'email':
                 contact.email,
                 'consents': [
                     CONSENT_SERVICE_EMAIL_CONSENT_TYPE,
                 ] if accepts_marketing else [],
             }],
         },
     )
     requests_mock.post(
         f'{settings.CONSENT_SERVICE_BASE_URL}'
         f'{CONSENT_SERVICE_PERSON_PATH_LOOKUP}',
         status_code=200,
         text=hawk_response,
     )
     contact_serialized = ContactDetailSerializer(instance=contact)
     assert contact_serialized.data[
         'accepts_dit_email_marketing'] is accepts_marketing
     assert requests_mock.call_count == 1
def generate_hawk_response(payload):
    """Mocks HAWK server validation for content."""
    return HawkMockJSONResponse(
        api_id=settings.COMPANY_MATCHING_HAWK_ID,
        api_key=settings.COMPANY_MATCHING_HAWK_KEY,
        response=payload,
    )
    def test_no_match_id_404_conditions(
        self,
        requests_mock,
        match_response,
    ):
        """Test that any issues with company matching service results in a 200 empty list."""
        company_dynamic_response = HawkMockJSONResponse(
            api_id=settings.COMPANY_MATCHING_HAWK_ID,
            api_key=settings.COMPANY_MATCHING_HAWK_KEY,
            response=match_response,
        )
        requests_mock.post(
            '/api/v1/company/match/',
            status_code=status.HTTP_200_OK,
            text=company_dynamic_response,
        )

        user = create_test_user(
            permission_codenames=(
                CompanyPermission.view_export_win,
            ),
        )
        api_client = self.create_api_client(user=user)
        company = CompanyFactory()
        url = reverse('api-v4:company:export-win', kwargs={'pk': company.id})
        response = api_client.get(url)
        assert response.status_code == status.HTTP_200_OK
        assert response.json() == {
            'count': 0,
            'next': None,
            'previous': None,
            'results': [],
        }
    def test_model_to_match_payload(
        self,
        requests_mock,
        build_company,
        expected_result,
    ):
        """
        Test that the function maps the Company object to JSON correctly
        also stripping out falsy values.
        """
        company = build_company()
        dynamic_response = HawkMockJSONResponse(
            api_id=settings.COMPANY_MATCHING_HAWK_ID,
            api_key=settings.COMPANY_MATCHING_HAWK_KEY,
        )
        matcher = requests_mock.post(
            '/api/v1/company/match/',
            status_code=status.HTTP_200_OK,
            text=dynamic_response,
        )
        match_company(company)

        assert matcher.called_once
        assert matcher.last_request.json() == {
            'descriptions': [expected_result],
        }
    def test_get_export_wins_one_merged_company_success(
        self,
        requests_mock,
    ):
        """Test get wins for both source and target, when a company was merged."""
        source_company = CompanyFactory()
        target_company = CompanyFactory()
        user = create_test_user(
            permission_codenames=(
                CompanyPermission.view_export_win,
            ),
        )
        merge_companies(source_company, target_company, user)

        requests_mock.register_uri(
            'POST',
            '/api/v1/company/match/',
            [
                {'text': self._get_matching_service_response([1, 2]), 'status_code': 200},
            ],
        )

        export_wins_response = {
            'count': 2,
            'next': None,
            'previous': None,
            'results': [
                {
                    'id': 'e3013078-7b3e-4359-83d9-cd003a515521',
                    'date': '2016-05-25',
                    'created': '2020-02-18T15:36:02.782000Z',
                    'country': 'CA',
                },
                {
                    'id': 'e3013078-7b3e-4359-83d9-cd003a515234',
                    'date': '2016-07-25',
                    'created': '2020-04-18T15:36:02.782000Z',
                    'country': 'US',
                },
            ],
        }
        export_wins_dynamic_response = HawkMockJSONResponse(
            api_id=settings.EXPORT_WINS_HAWK_ID,
            api_key=settings.EXPORT_WINS_HAWK_KEY,
            response=export_wins_response,
        )
        requests_mock.get(
            '/wins/match?match_id=1,2',
            status_code=200,
            text=export_wins_dynamic_response,
        )

        api_client = self.create_api_client(user=user)
        url = reverse('api-v4:company:export-win', kwargs={'pk': target_company.id})
        response = api_client.get(url)
        assert response.status_code == 200
        assert response.json() == export_wins_response
 def _get_matching_service_response(self, match_ids):
     company_matching_reponse = {
         'matches': [{
             'id': 1,
             'match_id': match_id,
             'similarity': '100000',
         } for match_id in match_ids],
     }
     company_dynamic_response = HawkMockJSONResponse(
         api_id=settings.COMPANY_MATCHING_HAWK_ID,
         api_key=settings.COMPANY_MATCHING_HAWK_KEY,
         response=company_matching_reponse,
     )
     return company_dynamic_response
    def test_get_call_invoked_multiple_match_ids(
        self,
        requests_mock,
    ):
        """
        Check GET call will be made
        """
        dynamic_response = HawkMockJSONResponse(
            api_id=settings.EXPORT_WINS_HAWK_ID,
            api_key=settings.EXPORT_WINS_HAWK_KEY,
        )
        matcher = requests_mock.get(
            '/wins/match?match_id=1234,2345',
            status_code=status.HTTP_200_OK,
            text=dynamic_response,
        )
        get_export_wins([1234, 2345])

        assert matcher.called_once
Beispiel #8
0
    def test_accepts_dit_email_marketing_consent_service(
        self,
        accepts_marketing,
        requests_mock,
    ):
        """
        Tests accepts_dit_email_marketing field is populated from the consent service.
        """
        contact = ContactFactory()
        hawk_response = HawkMockJSONResponse(
            api_id=settings.COMPANY_MATCHING_HAWK_ID,
            api_key=settings.COMPANY_MATCHING_HAWK_KEY,
            response={
                'results': [{
                    'email':
                    contact.email,
                    'consents': [
                        CONSENT_SERVICE_EMAIL_CONSENT_TYPE,
                    ] if accepts_marketing else [],
                }],
            },
        )
        requests_mock.post(
            f'{settings.CONSENT_SERVICE_BASE_URL}'
            f'{CONSENT_SERVICE_PERSON_PATH_LOOKUP}',
            status_code=200,
            text=hawk_response,
        )
        api_client = self.create_api_client()

        url = reverse('api-v3:contact:detail', kwargs={'pk': contact.id})
        response = api_client.get(url)

        assert requests_mock.call_count == 1
        assert response.status_code == status.HTTP_200_OK
        assert response.json(
        )['accepts_dit_email_marketing'] == accepts_marketing
    def test_get_export_wins_success(
        self,
        requests_mock,
    ):
        """Test get wins in a successful scenario."""
        requests_mock.post(
            '/api/v1/company/match/',
            status_code=200,
            text=self._get_matching_service_response([1]),
        )

        export_wins_response = {
            'count': 1,
            'next': None,
            'previous': None,
            'results': [
                {
                    'id': 'e3013078-7b3e-4359-83d9-cd003a515521',
                    'date': '2016-05-25',
                    'created': '2020-02-18T15:36:02.782000Z',
                    'country': 'CA',
                    'sector': 251,
                    'business_potential': 1,
                    'business_type': '',
                    'name_of_export': '',
                    'officer': {
                        'name': 'lead officer name',
                        'email': '',
                        'team': {
                            'type': 'tcp',
                            'sub_type': 'tcp:12',
                        },
                    },
                    'contact': {
                        'name': 'customer name',
                        'email': '*****@*****.**',
                        'job_title': 'customer job title',
                    },
                    'value': {
                        'export': {
                            'value': 100000,
                            'breakdowns': [],
                        },
                    },
                    'customer': 'Some Company Limited',
                    'response': None,
                    'hvc': {
                        'code': 'E24116',
                        'name': 'AER-01',
                    },
                },
            ],
        }
        export_wins_dynamic_response = HawkMockJSONResponse(
            api_id=settings.EXPORT_WINS_HAWK_ID,
            api_key=settings.EXPORT_WINS_HAWK_KEY,
            response=export_wins_response,
        )
        requests_mock.get(
            '/wins/match?match_id=1',
            status_code=200,
            text=export_wins_dynamic_response,
        )

        user = create_test_user(
            permission_codenames=(
                CompanyPermission.view_export_win,
            ),
        )
        api_client = self.create_api_client(user=user)
        company = CompanyFactory()
        url = reverse('api-v4:company:export-win', kwargs={'pk': company.id})
        response = api_client.get(url)
        assert response.status_code == 200
        assert response.json() == export_wins_response