def test_error_unauthenticated(client): response = client.get('/widgets') assert response.status_code == 401 assert helpers.get_errors(response) == [{ 'code': 'invalid_credentials.missing', }]
def test_error_update_unauthorized(client): not_found_response = helpers.request( client, 'PATCH', '/widgets/1?user_id=bar', { 'id': '1', 'owner_id': 'bar', 'name': "Updated", }, ) assert not_found_response.status_code == 404 forbidden_response = helpers.request( client, 'PATCH', '/widgets/3?user_id=bar', { 'id': '3', 'owner_id': 'bar', 'name': "Updated", }, ) assert forbidden_response.status_code == 403 assert helpers.get_errors(forbidden_response) == [{ 'code': 'invalid_user' }]
def test_error_invalid_page_value(client): response = client.get('/page_widgets?page=-1') assert response.status_code == 400 assert helpers.get_errors(response) == [{ 'code': 'invalid_page', 'source': {'parameter': 'page'}, }]
def test_error_invalid_field(client): response = client.get("/widgets?size_min=foo") assert response.status_code == 400 errors = helpers.get_errors(response) for error in errors: assert error.pop("detail", None) is not None assert errors == [{"code": "invalid_filter", "source": {"parameter": "size_min"}}]
def test_error_invalid_cursor_length(client): response = client.get('/id_cursor_widgets?cursor=WzEsIDFd') assert response.status_code == 400 assert helpers.get_errors(response) == [{ 'code': 'invalid_cursor.length', 'source': {'parameter': 'cursor'}, }]
def test_error_invalid_cursor_encoding(client): response = client.get('/id_cursor_widgets?cursor=foo') assert response.status_code == 400 assert helpers.get_errors(response) == [{ 'code': 'invalid_cursor.encoding', 'source': {'parameter': 'cursor'}, }]
def test_error_invalid_offset_value(client): response = client.get('/limit_offset_widgets?offset=-1') assert response.status_code == 400 assert helpers.get_errors(response) == [{ 'code': 'invalid_offset', 'source': {'parameter': 'offset'}, }]
def test_error_delete_unauthorized(client): not_found_response = client.delete('/widgets/1?user_id=bar') assert not_found_response.status_code == 404 forbidden_response = client.delete('/widgets/3?user_id=bar') assert forbidden_response.status_code == 403 assert helpers.get_errors(forbidden_response) == [{ 'code': 'invalid_user' }]
def test_invalid_body(client): response = client.post( '/widgets', data='foo', ) assert response.status_code == 400 assert helpers.get_errors(response) == [{ 'code': 'invalid_body', }]
def test_data_missing(client): response = client.post( '/widgets', content_type='application/json', data='{}', ) assert response.status_code == 400 assert helpers.get_errors(response) == [{ 'code': 'invalid_data.missing', }]
def test_error_invalid_cursor_field(client): response = client.get('/id_cursor_widgets?cursor=WyJmb28iXQ%3D%3D') assert response.status_code == 400 errors = helpers.get_errors(response) for error in errors: assert error.pop('detail', None) is not None assert errors == [{ 'code': 'invalid_cursor', 'source': {'parameter': 'cursor'}, }]
def test_error_invalid_token(client, invalid_token): response = client.get( '/widgets', headers={ 'Authorization': 'Bearer {}'.format(invalid_token), }, ) assert response.status_code == 401 assert helpers.get_errors(response) == [{ 'code': 'invalid_token', }]
def test_error_invalid_authorization_scheme(client): response = client.get( '/widgets', headers={ 'Authorization': 'foo bar', }, ) assert response.status_code == 401 assert helpers.get_errors(response) == [{ 'code': 'invalid_authorization.scheme', }]
def test_id_missing(client): response = helpers.request( client, 'PATCH', '/widgets/1', { 'name': "Bar", }, ) assert response.status_code == 422 assert helpers.get_errors(response) == [{ 'code': 'invalid_id.missing', }]
def test_commit_conflict(client): response = helpers.request( client, 'POST', '/widgets', { 'name': "Foo", }, ) assert response.status_code == 409 assert helpers.get_errors(response) == [{ 'code': 'invalid_data.conflict', }]
def test_id_forbidden(client): response = helpers.request( client, 'POST', '/widgets', { 'id': '2', 'name': "Bar", }, ) assert response.status_code == 403 assert helpers.get_errors(response) == [{ 'code': 'invalid_id.forbidden', }]
def test_error_create_unauthorized(client): response = helpers.request( client, 'POST', '/widgets?user_id=bar', { 'owner_id': 'foo', 'name': "Created", }, ) assert response.status_code == 403 assert helpers.get_errors(response) == [{ 'code': 'invalid_user' }]
def test_error_missing_id(client): response = helpers.request( client, 'PUT', '/children/1', { 'id': '1', 'name': "Updated Child", 'parent': {}, }, ) assert response.status_code == 422 assert helpers.get_errors(response) == [{ 'code': 'invalid_related.missing_id', 'source': {'pointer': '/data/parent'}, }]
def test_deserializer_errors(client): response = helpers.request( client, 'POST', '/widgets', { 'nested': {'value': 'three'}, 'nested_many': [ {'value': 'four'}, {'value': 5}, {'value': 'six'}, ], }, ) assert response.status_code == 422 errors = helpers.get_errors(response) for error in errors: assert error.pop('detail', None) is not None errors.sort(key=lambda error: error['source']['pointer']) assert errors == [ { 'code': 'invalid_data', 'source': {'pointer': '/data/name'}, }, { 'code': 'invalid_data', 'source': {'pointer': '/data/nested/value'}, }, { 'code': 'invalid_data', 'source': {'pointer': '/data/nested_many/0/value'}, }, { 'code': 'invalid_data', 'source': {'pointer': '/data/nested_many/2/value'}, }, ]