def post(self, request): """ A wrapper around the investigation API endpoint for dnb-service. """ investigation_serializer = DNBCompanyInvestigationSerializer( data=request.data) investigation_serializer.is_valid(raise_exception=True) data = {'company_details': investigation_serializer.validated_data} company = data['company_details'].pop('company') try: response = create_investigation(data) except ( DNBServiceConnectionError, DNBServiceTimeoutError, DNBServiceError, ) as exc: raise APIUpstreamException(str(exc)) company.dnb_investigation_id = response['id'] company.pending_dnb_investigation = True company.save() return Response(response)
def test_investigation_serializer_no_investigation_data_valid( db, investigation_payload, investigation_data_override, override_function, ): """ Test if DNBCompanyInvestigationSerializer is valid without dnb_investigation_data """ data = override_function( format_dnb_company_investigation(investigation_payload, ), investigation_data_override, ) serializer = DNBCompanyInvestigationSerializer(data=data) assert serializer.is_valid()
def test_investigation_serializer_invalid( db, investigation_payload, investigation_payload_override, expected_error, ): """ Test if DNBCompanyInvestigationSerializer fails given an invalid payload. """ serializer = DNBCompanyInvestigationSerializer( data=format_dnb_company_investigation( { **investigation_payload, **investigation_payload_override }, ), ) assert not serializer.is_valid() assert serializer.errors == expected_error
def test_investigation_serializer_valid( db, investigation_payload, investigation_payload_override, ): """ Test if DNBCompanyInvestigationSerializer saves a Company record given a valid payload. """ data = format_dnb_company_investigation( { **investigation_payload, **investigation_payload_override, }, ) serializer = DNBCompanyInvestigationSerializer(data=data) assert serializer.is_valid() company = serializer.save() company.refresh_from_db() assert_company_data(company, data)
def post(self, request): """ Given a minimal set of fields that may be necessary for DNB investigation, create a Company record in DataHub. """ formatted_company_data = format_dnb_company_investigation(request.data) company_serializer = DNBCompanyInvestigationSerializer( data=formatted_company_data) try: company_serializer.is_valid(raise_exception=True) except serializers.ValidationError: message = 'Company investigation payload failed serializer validation' extra_data = { 'formatted_dnb_company_data': formatted_company_data, 'dh_company_serializer_errors': company_serializer.errors, } logger.error(message, extra=extra_data) raise datahub_company = company_serializer.save( created_by=request.user, modified_by=request.user, pending_dnb_investigation=True, ) statsd.incr(f'dnb.create.investigation') return Response( company_serializer.to_representation(datahub_company), )
def test_investigation_serializer_null_fields_invalid( db, investigation_payload, investigation_payload_override, override_function, ): """ If website as well as telephone_number are not given, the serializer should return an error. """ data = format_dnb_company_investigation( override_function( investigation_payload, investigation_payload_override, ), ) serializer = DNBCompanyInvestigationSerializer(data=data) assert not serializer.is_valid() assert serializer.errors == { 'non_field_errors': [ 'Either website or telephone_number must be provided.', ], }
def test_investigation_serializer_null_fields_valid( db, investigation_payload, investigation_payload_override, override_function, ): """ Test if DNBCompanyInvestigationSerializer saves a Company record given a valid payload. A valid payload has wither a valid URL for website or valid telephone_number: country_code and number. """ data = format_dnb_company_investigation( override_function( investigation_payload, investigation_payload_override, ), ) serializer = DNBCompanyInvestigationSerializer(data=data) assert serializer.is_valid() company = serializer.save() company.refresh_from_db() assert_company_data(company, data)