Beispiel #1
0
    def test_get_business_by_id_and_collection_exercise_returns_correct_representation(
            self):
        # Post business and link to sample/collection exercise
        mock_business = MockBusiness().as_business()
        self.post_to_businesses(mock_business, 200)
        sample_id = mock_business['sampleSummaryId']
        put_data = {'collectionExerciseId': 'test_id'}
        self.put_to_businesses_sample_link(sample_id, put_data, 200)

        # Repost business and link to different sample/collection exercise
        mock_business['sampleSummaryId'] = '100000001'
        party_id = self.post_to_businesses(mock_business, 200)['id']
        put_data = {'collectionExerciseId': 'other_test_id'}
        self.put_to_businesses_sample_link('100000001', put_data, 200)

        # Retrieve data for first collection exercise
        response = self.get_business_by_id(
            party_id, query_string={"collection_exercise_id": "test_id"})
        self.assertEqual(len(response.items()), 7)
        self.assertEqual(response.get('id'), party_id)
        self.assertEqual(response.get('sampleSummaryId'), sample_id)
        self.assertEqual(response.get('name'), mock_business.get('name'))
        self.assertEqual(response.get('trading_as'),
                         'Tradstyle-1 Tradstyle-2 Tradstyle-3')

        # Retrieve data for 2nd collection exercise
        response = self.get_business_by_id(
            party_id, query_string={"collection_exercise_id": "other_test_id"})
        self.assertEqual(len(response.items()), 7)
        self.assertEqual(response.get('id'), party_id)
        self.assertEqual(response.get('sampleSummaryId'), '100000001')
        self.assertEqual(response.get('name'), mock_business.get('name'))
        self.assertEqual(response.get('trading_as'),
                         'Tradstyle-1 Tradstyle-2 Tradstyle-3')
Beispiel #2
0
    def test_get_business_by_id_returns_correct_representation_verbose(self):
        mock_business = MockBusiness().as_business()
        party_id = self.post_to_businesses(mock_business, 200)['id']
        self._make_business_attributes_active(mock_business)

        response = self.get_business_by_id(party_id,
                                           query_string={"verbose": "true"})
        self.assertTrue(len(response.items()) >= len(mock_business.items()))
Beispiel #3
0
    def test_get_business_by_id_returns_correct_representation(self):
        mock_business = MockBusiness().as_business()
        party_id = self.post_to_businesses(mock_business, 200)['id']
        self._make_business_attributes_active(mock_business)

        response = self.get_business_by_id(party_id)
        self.assertEqual(len(response.items()), 7)
        self.assertEqual(response.get('id'), party_id)
        self.assertEqual(response.get('sampleSummaryId'),
                         mock_business['sampleSummaryId'])
        self.assertEqual(response.get('name'), mock_business.get('name'))
        self.assertEqual(response.get('trading_as'),
                         'Tradstyle-1 Tradstyle-2 Tradstyle-3')
 def test_post_pending_shares_fail_validation(self):
     # Given
     self.populate_with_respondent(respondent=self.mock_respondent_with_id)  # NOQA
     mock_business = MockBusiness().as_business()
     mock_business['id'] = DEFAULT_BUSINESS_UUID
     self.post_to_businesses(mock_business, 200)
     self._make_business_attributes_active(mock_business=mock_business)
     self.associate_business_and_respondent(business_id=mock_business['id'],
                                            respondent_id=self.mock_respondent_with_id['id'])  # NOQA
     # When
     payload = {"pending_shares": [{
         "business_id": DEFAULT_BUSINESS_UUID,
         "survey_id": DEFAULT_SURVEY_UUID,
         "shared_by": self.mock_respondent_with_id['id']
     },
         {
             "business_id": DEFAULT_BUSINESS_UUID,
             "survey_id": 'cb0711c3-0ac8-41d3-ae0e-567e5ea1ef99',
             "email_address": "*****@*****.**",
             "shared_by": self.mock_respondent_with_id['id']
         }]
     }
     # Then
     response = self.post_share_surveys_fail(payload)
     self.assertEqual(response['description'], ["Required key 'email_address' is missing."])
 def test_post_pending_shares_success(self):
     # Given
     self.populate_with_respondent(respondent=self.mock_respondent_with_id)  # NOQA
     mock_business = MockBusiness().as_business()
     mock_business['id'] = DEFAULT_BUSINESS_UUID
     self.post_to_businesses(mock_business, 200)
     self._make_business_attributes_active(mock_business=mock_business)
     self.associate_business_and_respondent(business_id=mock_business['id'],
                                            respondent_id=self.mock_respondent_with_id['id'])  # NOQA
     # When
     payload = {"pending_shares": [{
         "business_id": DEFAULT_BUSINESS_UUID,
         "survey_id": DEFAULT_SURVEY_UUID,
         "email_address": "*****@*****.**",
         "shared_by": self.mock_respondent_with_id['id']
     },
         {
             "business_id": DEFAULT_BUSINESS_UUID,
             "survey_id": 'cb0711c3-0ac8-41d3-ae0e-567e5ea1ef99',
             "email_address": "*****@*****.**",
             "shared_by": self.mock_respondent_with_id['id']
         }]
     }
     response = self.post_share_surveys(payload)
     # Then
     self.assertEqual(response, {'created': 'success'})
     self.assertTrue(self.is_pending_survey_registered(DEFAULT_BUSINESS_UUID, DEFAULT_SURVEY_UUID))
     self.assertTrue(self.is_pending_survey_registered(DEFAULT_BUSINESS_UUID,
                                                       'cb0711c3-0ac8-41d3-ae0e-567e5ea1ef99'))
Beispiel #6
0
    def test_get_party_by_ref_returns_correct_representation(self):
        mock_party_b = MockBusiness().as_party()
        self.post_to_parties(mock_party_b, 200)
        self._make_business_attributes_active(mock_party_b)
        response = self.get_party_by_ref('B', mock_party_b['sampleUnitRef'])

        del mock_party_b['sampleSummaryId']
        for x in mock_party_b:
            self.assertTrue(x in response)
Beispiel #7
0
    def test_put_business_sample_link_200(self):
        mock_business = MockBusiness().as_business()
        self.post_to_businesses(mock_business, 200)

        self.assertEqual(len(businesses()), 1)

        sample_id = mock_business['sampleSummaryId']
        put_data = {'collectionExerciseId': 'somecollectionexcid'}

        self.put_to_businesses_sample_link(sample_id, put_data, 200)
    def test_get_business_by_search_inactive_business_attributes(self):
        mock_business = MockBusiness().as_business()

        # given there is a business but it's attributes are not linked to a collection exercise (inactive)
        name = self.post_to_businesses(mock_business, 200)['name']

        # when user searches by partial name
        response = self.get_businesses_search(query_string={"query": name})

        # then no businesses returned
        self.assertEqual(len(response['businesses']), 0)
Beispiel #9
0
    def test_get_business_by_ids_with_an_unknown_id_still_returns_correct_representation_for_other_ids(
            self):
        mock_business_1 = MockBusiness().as_business()
        mock_business_2 = MockBusiness().as_business()

        party_id_1 = self.post_to_businesses(mock_business_1, 200)['id']
        party_id_2 = self.post_to_businesses(mock_business_2, 200)['id']
        self._make_business_attributes_active(mock_business_2)
        self._make_business_attributes_active(mock_business_1)

        response = self.get_businesses_by_ids(
            [party_id_1, party_id_2, str(uuid.uuid4())])
        self.assertEqual(len(response), 2)

        res_dict = {res['id']: res for res in response}

        self.assertEqual(res_dict[party_id_1].get('id'), party_id_1)
        self.assertEqual(res_dict[party_id_1].get('sampleSummaryId'),
                         mock_business_1['sampleSummaryId'])
        self.assertEqual(res_dict[party_id_1].get('name'),
                         mock_business_1.get('name'))

        self.assertEqual(res_dict[party_id_2].get('id'), party_id_2)
        self.assertEqual(res_dict[party_id_2].get('sampleSummaryId'),
                         mock_business_2['sampleSummaryId'])
        self.assertEqual(res_dict[party_id_2].get('name'),
                         mock_business_2.get('name'))
 def test_delete_pending_shares_success(self):
     # Given
     self.populate_with_respondent(respondent=self.mock_respondent_with_id)  # NOQA
     mock_business = MockBusiness().as_business()
     mock_business['id'] = DEFAULT_BUSINESS_UUID
     self.post_to_businesses(mock_business, 200)
     self._make_business_attributes_active(mock_business=mock_business)
     self.associate_business_and_respondent(business_id=mock_business['id'],
                                            respondent_id=self.mock_respondent_with_id['id'])  # NOQA
     self.populate_pending_share()
     self.assertTrue(self.is_pending_survey_registered(DEFAULT_BUSINESS_UUID, DEFAULT_SURVEY_UUID))
     # When
     response = self.delete_share_surveys()
 def test_post_pending_shares_fail_invalid_payload_size(self):
     # Given
     self.populate_with_respondent(respondent=self.mock_respondent_with_id)  # NOQA
     mock_business = MockBusiness().as_business()
     mock_business['id'] = DEFAULT_BUSINESS_UUID
     self.post_to_businesses(mock_business, 200)
     self._make_business_attributes_active(mock_business=mock_business)
     self.associate_business_and_respondent(business_id=mock_business['id'],
                                            respondent_id=self.mock_respondent_with_id['id'])  # NOQA
     # When
     payload = {"pending_shares": []}
     # Then
     response = self.post_share_surveys_fail(payload)
     self.assertEqual(response['description'], 'Payload Invalid - pending_shares list is empty')
    def test_get_business_by_search_partial_ru(self):
        mock_business = MockBusiness().as_business()

        # given there is a business to search
        business = self.post_to_businesses(mock_business, 200)
        self._make_business_attributes_active(mock_business)

        # when user searches by partial ru
        response = self.get_businesses_search(
            query_string={"query": business['sampleUnitRef'][4:]})

        # then the correct business is returned
        self.assertEqual(len(response['businesses']), 1)
        self.assertEqual(response['businesses'][0]['ruref'],
                         business['sampleUnitRef'])
 def test_share_survey_users_with_no_pending_share(self):
     # Given
     self.populate_with_respondent(respondent=self.mock_respondent_with_id)  # NOQA
     mock_business = MockBusiness().as_business()
     mock_business['id'] = DEFAULT_BUSINESS_UUID
     self.post_to_businesses(mock_business, 200)
     self._make_business_attributes_active(mock_business=mock_business)
     self.associate_business_and_respondent(business_id=mock_business['id'],
                                            respondent_id=self.mock_respondent_with_id['id'])  # NOQA
     self.populate_with_enrolment()  # NOQA
     # When
     response = self.get_share_survey_users(DEFAULT_BUSINESS_UUID,
                                            DEFAULT_SURVEY_UUID)
     # Then
     self.assertEqual(response, 1)
 def test_share_survey_users_with_pending_share_bad_request(self):
     # Given
     self.populate_with_respondent(respondent=self.mock_respondent_with_id)  # NOQA
     mock_business = MockBusiness().as_business()
     mock_business['id'] = DEFAULT_BUSINESS_UUID
     self.post_to_businesses(mock_business, 200)
     self._make_business_attributes_active(mock_business=mock_business)
     self.associate_business_and_respondent(business_id=mock_business['id'],
                                            respondent_id=self.mock_respondent_with_id['id'])  # NOQA
     self.populate_with_enrolment()  # NOQA
     self.populate_pending_share()
     # When
     response = self.get_share_survey_users_bad_request()
     # Then
     self.assertEqual(response['description'], 'Business id and Survey id is required for this request.')
Beispiel #15
0
 def test_get_party_by_survey_id_and_enrolment_statuses_with_invalid_enrolment(
         self):
     self.populate_with_respondent(respondent=self.mock_respondent_with_id)
     mock_business = MockBusiness().as_business()
     mock_business['id'] = DEFAULT_BUSINESS_UUID
     self.post_to_businesses(mock_business, 200)
     self._make_business_attributes_active(mock_business=mock_business)
     self.associate_business_and_respondent(
         business_id=mock_business['id'],
         respondent_id=self.mock_respondent_with_id['id'])
     self.populate_with_enrolment(
         enrolment=self.mock_enrolment_disabled)  # NOQA
     self.get_party_by_id_filtered_by_survey_and_enrolment(
         'B', mock_business['id'], DEFAULT_SURVEY_UUID,
         ['ENABLED', 'PENDING'])
 def test_share_survey_users_with_pending_share_not_found(self):
     # Given
     self.populate_with_respondent(respondent=self.mock_respondent_with_id)  # NOQA
     mock_business = MockBusiness().as_business()
     mock_business['id'] = DEFAULT_BUSINESS_UUID
     self.post_to_businesses(mock_business, 200)
     self._make_business_attributes_active(mock_business=mock_business)
     self.associate_business_and_respondent(business_id=mock_business['id'],
                                            respondent_id=self.mock_respondent_with_id['id'])  # NOQA
     self.populate_with_enrolment()  # NOQA
     self.populate_pending_share()
     # When
     response = self.get_share_survey_users_not_found('3b136c4b-7a14-4904-9e01-13364dd7b973',
                                                      DEFAULT_SURVEY_UUID)
     # Then
     self.assertEqual(response['description'], 'Business with party id does not exist')
Beispiel #17
0
    def test_existing_party_can_be_updated(self):
        mock_party = MockBusiness().attributes(version=1)

        response_1 = self.post_to_parties(mock_party.as_party(), 200)
        self.assertEqual(len(businesses()), 1)
        self.assertEqual(response_1['attributes']['version'], 1)

        mock_party.attributes(version=2)
        response_2 = self.post_to_parties(mock_party.as_party(), 200)
        self.assertEqual(len(businesses()), 1)
        self.assertEqual(response_2['attributes']['version'], 2)
Beispiel #18
0
    def test_get_business_by_ref_returns_correct_representation(self):
        with open(
                f'{project_root}/test/test_data/business/get_business_by_ref.json'
        ) as json_data:
            expected = json.load(json_data)
        mock_business = MockBusiness().as_business()
        self.post_to_businesses(mock_business, 200)
        self._make_business_attributes_active(mock_business)

        ru_ref = mock_business['sampleUnitRef']
        actual = self.get_business_by_ref(ru_ref)

        # Overwrite provided ru_ref because creating a new business object via the mock creates a random ru_ref
        expected['attributes']['ruref'] = ru_ref
        expected['sampleSummaryId'] = ru_ref
        expected['sampleUnitRef'] = ru_ref

        # Delete as id is random each time.
        del actual['id']
        del expected['id']
        self.assertDictEqual(expected, actual)
    def _set_up_businesses(self, count):
        """set up multiple businesses with unique ru refs and names and trading as starting in <n>-"""
        for i in range(count):
            attribs = copy.deepcopy(DEFAULT_ATTRIBUTES)
            attribs["runame1"] = f"{i}-Runame-1"
            attribs["runame2"] = f"{i}-Runame-2"
            attribs["runame3"] = f"{i}-Runame-3"
            attribs[
                "name"] = f'{attribs["runame1"]} {attribs["runame2"]} {attribs["runame3"]}'

            attribs["tradstyle1"] = f"{i}-Tradstyle-1"
            attribs["tradstyle2"] = f"{i}-Tradstyle-2"
            attribs["tradstyle3"] = f"{i}-Tradstyle-3"
            attribs[
                "trading_as"] = f'{attribs["tradstyle1"]} {attribs["tradstyle2"]} {attribs["tradstyle3"]}'

            mock_business = MockBusiness(attribs) \
                .as_business()

            self.post_to_businesses(mock_business, 200)
            self._make_business_attributes_active(mock_business)
    def test_get_business_by_search_key_words_in_name(self):
        mock_business = MockBusiness().as_business()

        # given there is a business to search
        business = self.post_to_businesses(mock_business, 200)
        self._make_business_attributes_active(mock_business)

        # when user searches by multiple key words in name
        response = self.get_businesses_search(
            query_string={
                "query":
                f"{business['attributes']['runame1']}"
                f" {business['attributes']['runame3']}"
            })

        # then the correct business is returned
        self.assertEqual(len(response['businesses']), 1)
        self.assertEqual(response['businesses'][0]['ruref'],
                         business['sampleUnitRef'])
        self.assertEqual(response['businesses'][0]['name'], business['name'])
        self.assertEqual(response['businesses'][0]['trading_as'],
                         business['trading_as'])
    def test_get_business_by_search_distinct_multi_names(self):
        mock_business = MockBusiness().as_business()

        # given there is a business with multiple names to search
        self.post_to_businesses(mock_business, 200)
        name_1 = self.post_to_businesses(mock_business, 200)['name']
        mock_business['runame2'] = 'another1'
        name_2 = self.post_to_businesses(mock_business, 200)['name']
        mock_business['runame3'] = 'another1'
        name_3 = self.post_to_businesses(mock_business, 200)['name']
        self._make_business_attributes_active(mock_business)

        # when user searches by partial name
        response = self.get_businesses_search(
            query_string={"query": mock_business['runame1']})

        # then distinct variations of the correct business is returned
        names = [business['name'] for business in response['businesses']]
        self.assertEqual(len(response['businesses']), 3)
        self.assertIn(name_1, names)
        self.assertIn(name_2, names)
        self.assertIn(name_3, names)
Beispiel #22
0
    def test_get_business_by_id_with_no_active_attributes_returns_404(self):
        mock_business = MockBusiness().as_business()
        party_id = self.post_to_businesses(mock_business, 200)['id']

        self.get_business_by_id(party_id, 404)
Beispiel #23
0
    def test_post_valid_party_adds_to_db(self):
        mock_party = MockBusiness().as_party()
        self.post_to_parties(mock_party, 200)

        self.assertEqual(len(businesses()), 1)
Beispiel #24
0
    def test_post_valid_business_adds_to_db(self):
        mock_business = MockBusiness().as_business()
        self.post_to_businesses(mock_business, 200)

        self.assertEqual(len(businesses()), 1)
Beispiel #25
0
 def test_post_invalid_party(self):
     mock_party = MockBusiness().as_party()
     del mock_party['sampleUnitRef']
     self.post_to_parties(mock_party, 400)
 def populate_with_business(self, business_id=DEFAULT_BUSINESS_UUID):
     mock_business = MockBusiness().as_business()
     mock_business['id'] = business_id
     self.post_to_businesses(mock_business, 200)
Beispiel #27
0
    def test_get_party_by_id_no_active_attributes_returns_404(self):
        mock_party_b = MockBusiness().as_party()
        party_id_b = self.post_to_parties(mock_party_b, 200)['id']

        self.get_party_by_id('B', party_id_b, 404)
Beispiel #28
0
 def test_put_business_sample_link_returns_400_when_no_ce(self):
     mock_business = MockBusiness().as_business()
     sample_id = mock_business['sampleSummaryId']
     self.put_to_businesses_sample_link(sample_id, {}, 400)