def test_search_officers(self): """Searching for officers by company number works""" responses.add( responses.GET, "https://api.companieshouse.gov.uk/company/12345/officers?" + "access_token=pk.test", match_querystring=True, status=200, body=self.results, content_type="application/json", adding_headers={ 'X-Ratelimit-Reset': '{}'.format(self.current_timestamp()) }) res = chwrapper.Search(access_token="pk.test").officers("12345") assert res.status_code == 200
def test_getting_document(self): """Test for the document requesting method""" responses.add(responses.GET, "https://document-api.companieshouse.gov.uk/document/" + "1234/content?access_token=pk.test", match_querystring=True, status=200, body=self.results, content_type="application/json", adding_headers={ 'X-Ratelimit-Reset': '{}'.format(self.current_timestamp()) }) res = chwrapper.Search(access_token="pk.test").document("1234") assert res.status_code == 200
def get_fresh_company_data(): if file_exists( config.data_cache_filepath) and not file_is_older_than_an_hour( config.data_cache_filepath): return get_json_from_file(config.data_cache_filepath) companies_data = {'companies': []} company_ids = get_company_ids_from_input_file() searcher = chwrapper.Search(access_token=config.get_access_token()) # searcher = chwrapper.Search(access_token=config.access_token) for company_id in company_ids: company_name = None officers_response = None try: company_name = searcher.profile(company_id).json()['company_name'] except HTTPError as e: print("ERROR: Exception thrown when querying API: {}".format(e)) exit(1) try: officers_response = searcher.officers(company_id) except HTTPError as e: print("ERROR: Exception thrown when querying API: {}".format(e)) exit(1) officers_json = officers_response.json() officers = officers_json['items'] active_officer_records = get_active_officer_records(officers) company_record = { 'company_name': company_name, 'active_officers': active_officer_records } companies_data['companies'].append(company_record) with open(config.data_cache_filepath, 'w') as outfile: json.dump(companies_data, outfile) return companies_data
class TestSignificantControl(): """Test the significant control endpoints""" s = chwrapper.Search(access_token="pk.test") def current_timestamp(self): return int(datetime.timestamp(datetime.utcnow())) with open("tests/results.json") as results: results = results.read() items = [ "items", "items_per_page", "kind", "page_number", "start_index", "total_results" ] @responses.activate def test_list_persons_significant_control(self): """Test the list of persons of significant control for a company""" responses.add( responses.GET, ('https://api.companieshouse.gov.uk/company/12345/' + 'persons-with-significant-control?access_token=pk.test'), match_querystring=True, status=200, body=self.results, content_type="application/json", adding_headers={ 'X-Ratelimit-Reset': '{}'.format(self.current_timestamp()) }) res = self.s.persons_significant_control('12345') assert res.status_code == 200 assert sorted(res.json().keys()) == self.items @responses.activate def test_list_persons_significant_control_no_company_number(self): """Tests that correct exception raised when no company number used""" with pytest.raises(TypeError): res = self.s.persons_significant_control() @responses.activate def test_persons_significant_control_statements_true(self): """Test list of persons with significant control statements for a company""" responses.add( responses.GET, ('https://api.companieshouse.gov.uk/company/12345/' + 'persons-with-significant-control-statements?access_token=pk.test' ), match_querystring=True, status=200, body=self.results, content_type="application/json", adding_headers={ 'X-Ratelimit-Reset': '{}'.format(self.current_timestamp()) }) res = self.s.persons_significant_control('12345', statements=True) assert res.status_code == 200 assert sorted(res.json().keys()) == self.items @responses.activate def test_persons_significant_control_statements(self): """Test list of persons with significant control statements for a company when set statements set to False""" responses.add( responses.GET, ('https://api.companieshouse.gov.uk/company/12345/' + 'persons-with-significant-control?access_token=pk.test'), match_querystring=True, status=200, body=self.results, content_type="application/json", adding_headers={ 'X-Ratelimit-Reset': '{}'.format(self.current_timestamp()) }) res = self.s.persons_significant_control('12345', statements=False) assert res.status_code == 200 assert sorted(res.json().keys()) == self.items assert res.url == ('https://api.companieshouse.gov.uk/company/12345/' + 'persons-with-significant-control?' + 'access_token=pk.test') @responses.activate def test_person_significant_control(self): """Test single person of significant control for a company""" responses.add(responses.GET, ( 'https://api.companieshouse.gov.uk/company/12345/' + 'persons-with-significant-control/individual/12345?access_token=pk.test' ), match_querystring=True, status=200, body=self.results, content_type="application/json", adding_headers={ 'X-Ratelimit-Reset': '{}'.format(self.current_timestamp()) }) res = self.s.significant_control('12345', '12345') assert res.status_code == 200 assert sorted(res.json().keys()) == self.items @responses.activate def test_person_significant_control_no_company_number(self): """Tests that correct exception raised when no company number used""" with pytest.raises(TypeError): res = self.s.significant_control() @responses.activate def test_person_significant_control_wrong_entity_string(self): """Tests that correct exception raised when wrong entity string used""" with pytest.raises(Exception): res = self.s.significant_control('12345', '12345', entity_type='hello') @responses.activate def test_legal_persons_significant_control(self): """Test legal person of significant control for a company endpoint""" responses.add(responses.GET, ('https://api.companieshouse.gov.uk/company/12345/' + 'persons-with-significant-control/legal-person/12345' + '?access_token=pk.test'), match_querystring=True, status=200, body=self.results, content_type="application/json", adding_headers={ 'X-Ratelimit-Reset': '{}'.format(self.current_timestamp()) }) res = self.s.significant_control('12345', '12345', 'legal') assert res.status_code == 200 assert sorted(res.json().keys()) == self.items @responses.activate def test_secure_persons_significant_control(self): """Test single secure person of significant control for a company""" responses.add(responses.GET, ('https://api.companieshouse.gov.uk/company/12345/' + 'persons-with-significant-control/super-secure/12345?' + 'access_token=pk.test'), match_querystring=True, status=200, body=self.results, content_type="application/json", adding_headers={ 'X-Ratelimit-Reset': '{}'.format(self.current_timestamp()) }) res = self.s.significant_control('12345', '12345', 'secure') assert res.status_code == 200 assert sorted(res.json().keys()) == self.items @responses.activate def test_corporates_significant_control(self): """Test single corporate entity with significant control for a company""" responses.add( responses.GET, ('https://api.companieshouse.gov.uk/company/12345/' + 'persons-with-significant-control/corporate-entity/12345?' + 'access_token=pk.test'), match_querystring=True, status=200, body=self.results, content_type="application/json", adding_headers={ 'X-Ratelimit-Reset': '{}'.format(self.current_timestamp()) }) res = self.s.significant_control('12345', '12345', 'corporate') assert res.status_code == 200 assert sorted(res.json().keys()) == self.items