def assert_correct_response(response, code): assert (response.status_code == code) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)), dict)) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)).get('message'), str))
def test_method_not_allowed_handler(module_client): client = module_client response = client.patch('api/v1/resources') assert (response.status_code == 405) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)), dict)) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)).get('message'), str))
def false_validation(module_client, module_db, fake_auth_from_oc, fake_algolia_save, fake_validate_resource): # Given the validate_resource method fails to catch errors # When we commit the resource to the database # Then the api returns a 422 response client = module_client first_term = random_string() apikey = get_api_key(client) resource = client.post("/api/v1/resources", json=[ dict( name=f"{first_term}", category="Website", url=f"{first_term}", paid=False, ) ], headers={'x-apikey': apikey}) assert (resource.status_code == 200) id = resource.json['data'].get("id") resource = client.post("/api/v1/resources", json=dict( name=f"{first_term}", category="Website", url=f"{first_term}", paid=False, ), headers={'x-apikey': apikey}) assert (resource.status_code == 422) assert (resource.json['data'].get("errors") is not None) assert (resource.json['data'].get("errors")[get_error_code_from_status( resource.status_code)] == msg_map[resource.status_code]) resource = client.put(f"/api/v1/resources/{id}", json=dict(name="New name", languages=["New language"], category="New Category", url=f"{first_term}", paid=False, notes="New notes"), headers={'x-apikey': apikey}) assert (resource.status_code == 422) assert (resource.json['data'].get("errors") is not None) assert (resource.json['data'].get("errors")[get_error_code_from_status( resource.status_code)] == msg_map[resource.status_code])
def test_get_resources_post_date_failure(module_client): client = module_client # If updated_after date is after today, then should return a 422 ua = datetime.now() + timedelta(days=1) uaString = ua.strftime('%m-%d-%Y') response = client.get(f"/api/v1/resources?updated_after={uaString}") assert (response.status_code == 422) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)), dict)) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)).get('message'), str))
def test_rate_limit(module_client, module_db): client = module_client for _ in range(50): client.get('api/v1/resources') # Response should be a failure on request 51 response = client.get('api/v1/resources') assert (response.status_code == 429) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)), dict)) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)).get('message'), str))
def test_update_votes(module_client, module_db, fake_algolia_save): client = module_client vote_direction = 'upvote' id = 1 # Check voting on a valid resource data = client.get(f"api/v1/resources/{id}").json['data'] response = client.put(f"/api/v1/resources/{id}/{vote_direction}", follow_redirects=True) initial_votes = data.get(f"{vote_direction}s") assert (response.status_code == 200) assert (response.json['data'].get(f"{vote_direction}s") == initial_votes + 1) vote_direction = 'downvote' response = client.put(f"/api/v1/resources/{id}/{vote_direction}", follow_redirects=True) assert (response.status_code == 200) assert (response.json['data'].get(f"{vote_direction}s") == initial_votes + 1) # Check voting on an invalid resource id = 'waffles' response = client.put(f"/api/v1/resources/{id}/{vote_direction}", follow_redirects=True) assert (response.status_code == 404) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)), dict)) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)).get('message'), str)) # Check voting on a resource that doesn't exist too_high = 99999999 response = client.put(f"/api/v1/resources/{too_high}/{vote_direction}", follow_redirects=True) assert (response.status_code == 404) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)), dict)) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)).get('message'), str))
def test_get_resources(module_client, module_db): client = module_client response = client.get('api/v1/resources') # Status should be OK assert (response.status_code == 200) resources = response.json # Default page size should be specified in PaginatorConfig assert (len(resources['data']) == PaginatorConfig.per_page) for resource in resources['data']: assert (isinstance(resource.get('name'), str)) assert (resource.get('name')) assert (isinstance(resource.get('url'), str)) assert (resource.get('url')) assert (isinstance(resource.get('category'), str)) assert (resource.get('category')) assert (isinstance(resource.get('languages'), list)) assert (resources['number_of_pages'] is not None) ua = datetime.now() + timedelta(days=-7) uaString = ua.strftime('%m-%d-%Y') response = client.get(f"/api/v1/resources?updated_after={uaString}") assert (response.status_code == 200) # Test trying to get a page of results that doesn't exist too_far = 99999999 response = client.get(f"api/v1/resources?page_size=100&page={too_far}", follow_redirects=True) assert (response.status_code == 404) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)), dict)) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)).get('message'), str))
def test_internal_server_error_handler(module_client, module_db, fake_paginated_data_error): client = module_client response = client.get('api/v1/resources') assert (response.status_code == 500) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)), dict)) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)).get('message'), str)) response = client.get('api/v1/languages') assert (response.status_code == 500) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)), dict)) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)).get('message'), str)) response = client.get('api/v1/categories') assert (response.status_code == 500) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)), dict)) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)).get('message'), str))
def test_add_click(module_client, module_db): client = module_client # Check clicking on a valid resource id = 1 data = client.get(f"api/v1/resources/{id}").json['data'] response = client.put(f"/api/v1/resources/{id}/click", follow_redirects=True) initial_click_count = data.get(f"times_clicked") assert (response.status_code == 200) assert ( response.json['data'].get(f"times_clicked") == initial_click_count + 1) # Check clicking on an invalid resource id = 'pancakes' response = client.put(f"/api/v1/resources/{id}/click", follow_redirects=True) assert (response.status_code == 404) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)), dict)) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)).get('message'), str)) # Check clicking on a resource that doesn't exist too_high = 99999999 response = client.put(f"/api/v1/resources/{too_high}/click", follow_redirects=True) assert (response.status_code == 404) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)), dict)) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)).get('message'), str))
def test_languages(module_client, module_db): client = module_client response = client.get('api/v1/languages') for language in response.json['data']: assert (isinstance(language.get('id'), int)) assert (isinstance(language.get('name'), str)) assert (language.get('name')) assert (response.json['number_of_pages'] is not None) # Test trying to get a page of results that doesn't exist too_far = 99999999 response = client.get(f"api/v1/languages?page_size=100&page={too_far}", follow_redirects=True) assert (response.status_code == 404) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)), dict)) assert (isinstance( response.json.get('errors').get( get_error_code_from_status(response.status_code)).get('message'), str))