def test_agencies_endpoint_fields_filtering(self, testapp, swagger): res = testapp.get('/agencies?fields=ori,agency_name') validate_api_call(swagger, raw_request=res.request, raw_response=res) assert len(res.json['results']) > 0 assert 'ori' in res.json['results'][0] assert 'agency_name' in res.json['results'][0] assert 'city_name' not in res.json['results'][0]
def test_state_endpoint_no_year_in_request(self, testapp, swagger): res = testapp.get('/hc/count/states/3/bias_name/offenses') assert 'pagination' in res.json assert res.status_code == 200 validate_api_call(swagger, raw_request=res.request, raw_response=res) for r in res.json['results']: assert 'count' in r
def test_state_endpoint_no_year_in_request(self, testapp, swagger): res = testapp.get('/victims/count/states/3/race_code/offenses') assert 'pagination' in res.json assert res.status_code == 200 for r in res.json['results']: assert r['race_code'] or r['race_code'] is None validate_api_call(swagger, raw_request=res.request, raw_response=res)
def test_offenses_endpoint_with_state_year_explorer_offense(self, testapp, swagger, variable, explorer_offense): url = '/offenses/count/states/43/{}/offenses?explorer_offense={}&year=2014'.format(variable, explorer_offense) res = testapp.get(url) validate_api_call(swagger, raw_request=res.request, raw_response=res) assert 'pagination' in res.json for r in res.json['results']: assert 'count' in r
def test_state_participation_endpoint(self, testapp, swagger): res = testapp.get('/participation/states/NY') assert res.status_code == 200 validate_api_call(swagger, raw_request=res.request, raw_response=res) res = testapp.get('/participation/states/ny') assert res.status_code == 200
def test_invalid_api_call_with_polymorphism(): request_payload = """{ "events": [ { "eventType": "Impression", "timestamp": 12312312 } ] }""" responses.add(responses.POST, "http://test.com/poly/report", body="{}", status=200, content_type="application/json") response = requests.post("http://test.com/poly/report", json=json.loads(request_payload)) schema = load(os.path.join(BASE_DIR, 'schemas', 'polymorphism.yaml')) with pytest.raises(ValidationError) as err: validate_api_call(schema, raw_request=response.request, raw_response=response) assert_message_in_errors( MESSAGES['required']['required'], err.value.detail, )
def test_validate_api_call(httpbin): schema = load(os.path.join(BASE_DIR, 'schemas', 'httpbin.yaml')) response = requests.get(urlparse.urljoin(httpbin.url, '/get')) print response.text validate_api_call(schema, raw_request=response.request, raw_response=response)
def test_states_endpoint(self, testapp, swagger): res = testapp.get('/ht/states?state_abbr=RI') assert res.status_code == 200 validate_api_call(swagger, raw_request=res.request, raw_response=res) res = testapp.get('/ht/states?year=2015&state_abbr=RI') assert res.status_code == 200 validate_api_call(swagger, raw_request=res.request, raw_response=res)
def test_victims_offenses_endpoint_with_just_state_year_and_postal_code( self, testapp, swagger): url = '/hc/count/states/NY/bias_name/offenses?year=2014' res = testapp.get(url) validate_api_call(swagger, raw_request=res.request, raw_response=res) assert 'pagination' in res.json for r in res.json['results']: assert 'count' in r
def test_victims_offenses_endpoint_with_just_state_year( self, testapp, swagger, variable): url = '/victims/count/states/3/{}/offenses?year=2014'.format(variable) res = testapp.get(url) validate_api_call(swagger, raw_request=res.request, raw_response=res) assert 'pagination' in res.json for r in res.json['results']: assert (variable in r or r[variable] == None)
def test_state_endpoint_count_with_postal_code(self, testapp, swagger): url = '/offenses/count/states/AR/weapon_name?year=2014' res = testapp.get(url) assert res.status_code == 200 validate_api_call(swagger, raw_request=res.request, raw_response=res) assert 'pagination' in res.json for r in res.json['results']: assert 'count' in r
def test_codes_endpoint_exists(self, testapp, swagger, table, id_col): res = testapp.get('/codes/{0}'.format(table)) validate_api_call(swagger, raw_request=res.request, raw_response=res) assert res.status_code == 200 assert id_col in res.json[0] res = testapp.get('/codes/{0}.csv'.format(table)) assert res.status_code == 200
def test_victims_offenses_endpoint_with_state_postal_code( self, testapp, swagger): url = '/victims/count/states/AR/race_code/offenses?year=2014' res = testapp.get(url) validate_api_call(swagger, raw_request=res.request, raw_response=res) assert 'pagination' in res.json for r in res.json['results']: assert r['race_code'] or r['race_code'] == None
def test_national_endpoint_count(self, testapp, swagger, variable): url = '/offenses/count/national/{}?year=2014'.format(variable) res = testapp.get(url) assert res.status_code == 200 validate_api_call(swagger, raw_request=res.request, raw_response=res) assert 'pagination' in res.json for r in res.json['results']: assert 'count' in r
def test_national_endpoint_count(self, testapp, swagger): url = '/hc/count/national/bias_name?year=2014' res = testapp.get(url) assert res.status_code == 200 validate_api_call(swagger, raw_request=res.request, raw_response=res) assert 'pagination' in res.json for r in res.json['results']: assert 'count' in r
def test_victims_offenses_endpoint_with_state_year_offense( self, testapp, swagger, variable): url = '/victims/count/states/43/{}/offenses?offense_name=Aggravated+Assault&year=2014'.format( variable) res = testapp.get(url) validate_api_call(swagger, raw_request=res.request, raw_response=res) assert 'pagination' in res.json for r in res.json['results']: assert variable in r
def test_offenders_offenses_endpoint_with_state_postal_code( self, testapp, swagger, variable): url = '/offenders/count/states/AR/{}/offenses?year=2014'.format( variable) res = testapp.get(url) validate_api_call(swagger, raw_request=res.request, raw_response=res) assert 'pagination' in res.json for r in res.json['results']: assert 'count' in r
def validate(self, response): validate_api_call(self.master_schema, raw_request=response.request, raw_response=response) self.code = str(response.status_code) if response.content != u'': self.data = response.json() self.compare() self.logger.debug(u'Api request %s is valid' % self.uri) return True
def request(self, url, method='GET', params=None, data=None, headers=None): import base64 from urllib import urlencode import flask content_type = None if data is not None: data = json.dumps(data) content_type = 'application/json' if params is not None: params = {k: v for k, v in params.items() if v is not None} url = url + '?' + urlencode(params, True) if headers is None: headers = {} headers['Authorization'] = 'Basic ' + base64.b64encode(self.username + ':' + self.password) r = self.test_app.open(url, method=method, headers=headers, data=data, content_type=content_type) if self.validate_api_call: try: old_r_data = r.data r.data = json.dumps(strip_none(json.loads(r.data))) except ValueError: print(r.data) else: with self.app.test_request_context(url, method=method, headers=headers, data=data, content_type=content_type): try: validate_api_call(self.schema, raw_request=flask.request, raw_response=r) except ValidationError as e: context = { 'url': url, 'method': method, 'params': params, 'data': data, 'headers': headers, 'r.data': json.loads(r.data), } print(json.dumps(context, indent=4)) raise e r.data = old_r_data return r
def test_agencies_offenses(self, testapp, swagger): res = testapp.get( '/agencies/count/RI0040200/offenses?explorer_offense=robbery') validate_api_call(swagger, raw_request=res.request, raw_response=res) assert len(res.json['results']) == 1 result = res.json['results'][0] assert result['ori'] == 'RI0040200' assert result['offense_code'] == 'SUM_ROB' assert result['offense_name'] == 'Robbery' assert result['reported'] == 22 assert result['actual'] == 22 assert result['cleared'] == 12 assert result['juvenile_cleared'] == 1
def test_post_request(request_path): schema = load('swagger.awsexport.json') URL = 'https://' + schema['host'] + schema['basePath'] + request_path lambdaArn = 'arn:aws:lambda:us-east-1:769482945897:function:' \ 'anon-service-Lambda-1CNEQSHUD7Z25' time = str(datetime.now() + relativedelta(months=1)) postdata = json.dumps({"lambdaArn": lambdaArn, "time": time}) headers = {'Content-Type': 'application/json'} # r => response r = requests.post(URL, data=postdata, headers=headers) validate_api_call(schema, raw_request=r.request, raw_response=r)
def test_agencies_classification(self, testapp, swagger): res = testapp.get( '/agencies/count/RI0040200/offenses?explorer_offense=property') validate_api_call(swagger, raw_request=res.request, raw_response=res) assert len(res.json['results']) == 1 result = res.json['results'][0] assert result['ori'] == 'RI0040200' assert result['classification'] == 'Property' assert result['reported'] == 1595 assert result['unfounded'] == 0 assert result['actual'] == 1595 assert result['cleared'] == 419 assert result['juvenile_cleared'] == 43
def test_agencies_arson(self, testapp, swagger): res = testapp.get( '/agencies/count/RI0010100/offenses?explorer_offense=arson') validate_api_call(swagger, raw_request=res.request, raw_response=res) assert len(res.json['results']) == 1 result = res.json['results'][0] assert result['pub_agency_name'] == 'Barrington Police Department' assert result['ori'] == 'RI0010100' assert result['offense_code'] == 'X_ARS' assert result['offense_name'] == 'Arson' assert result['reported'] == 1 assert result['actual'] == 1 assert result['cleared'] == 1 assert result['juvenile_cleared'] == 0
def test_valid_api_call_with_polymorphism(): request_payload = """{ "events": [ { "eventType": "Impression", "advertisementId" : "ad7", "timestamp": 12312312 } ] }""" responses.add(responses.POST, "http://test.com/poly/report", body="{}", status=200, content_type="application/json") response = requests.post("http://test.com/poly/report", json=json.loads(request_payload)) schema = load(os.path.join(BASE_DIR, 'schemas', 'polymorphism.yaml')) validate_api_call(schema, raw_request=response.request, raw_response=response)
def test_agencies_endpoint_with_filter_comparison(self, testapp, swagger, filter, value): res = testapp.get('/participation/agencies?{}<={}'.format(filter, value)) assert res.status_code == 200 assert int(res.json['results'][0][filter]) <= value validate_api_call(swagger, raw_request=res.request, raw_response=res) res = testapp.get('/participation/agencies?{}={}'.format(filter, value)) assert res.status_code == 200 assert int(res.json['results'][0][filter]) == value validate_api_call(swagger, raw_request=res.request, raw_response=res) res = testapp.get('/participation/agencies?{}>={}'.format(filter, value)) assert res.status_code == 200 assert int(res.json['results'][0][filter]) >= value validate_api_call(swagger, raw_request=res.request, raw_response=res)
def validate_call(resp, schema): validate_api_call(schema, normalize_request(), normalize_response(resp))
def test_agencies_endpoint_exists(self, testapp, swagger): res = testapp.get('/agencies') assert res.status_code == 200 validate_api_call(swagger, raw_request=res.request, raw_response=res)
def test_state_estimates_lowercase(self, testapp, swagger): res = testapp.get('/estimates/states/ne?per_page=100') assert res.status_code == 200 validate_api_call(swagger, raw_request=res.request, raw_response=res) assert len(res.json['results']) > 0
def test_validate_api_call(httpbin): schema = load(os.path.join(BASE_DIR, 'schemas', 'httpbin.yaml')) response = requests.get(urlparse.urljoin(httpbin.url, '/get')) validate_api_call(schema, raw_request=response.request, raw_response=response)
def test_codes_index_exists(self, testapp, swagger): res = testapp.get('/codes') assert res.status_code == 200 validate_api_call(swagger, raw_request=res.request, raw_response=res)
def _assert_spec(self, spec, response): request = response.request try: validate_api_call(spec, raw_request=request, raw_response=response) except ValueError as e: raise AssertionError(e)