def test_request_body_with_errors(): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) data = { 'id': 'invalid integer', 'is_god': False, 'status': 'invalid status', 'invalid_key': 'data', 'items': ['invalid integer'], } expected_data = { 'id': 'Cannot decode "invalid integer" to integer', 'status': 'Value not in allowed values("active", "super_active"): "invalid status"', 'items': 'Cannot decode "invalid integer" to integer', 'non_field_error': 'Missing fields: "name"', } data = json.dumps(data) # Act response = client.post('/with-request-data/', data=data, content_type='application/json') assert response.status_code == HTTPStatus.BAD_REQUEST assert response.json() == expected_data
def test_request_body_as_list(): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) data = [{ 'id': 1, 'name': 'test name', 'is_god': True, 'status': 'active', 'items': [1, 2], }] expected_data = [{ 'id': 1, 'with_default': 5, 'name': 'test name', 'is_god': True, 'status': 'active', 'optional_status': None, 'items': [1, 2], 'optional_items': None, }] data = json.dumps(data) # Act response = client.post('/with-request-data/many/', data=data, content_type='application/json') assert response.status_code == HTTPStatus.OK assert response.json() == expected_data
def test_interceptor_headers(hello_world_query, hello_world_header): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) url = f'/winter-simple/get/{hello_world_query}' response = client.get(url) assert response.get('x-method') == 'SimpleController.get' assert response.get('x-hello-world') == hello_world_header
def test_controller_with_limits_redirects_to_default_limit(): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) response = client.get('/with-limits/') assert response.status_code == HTTPStatus.FOUND assert response['Location'] == '/with-limits/?limit=20'
def test_controller_with_limits_fails_if_maximum_limit_is_exceeded(): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) response = client.get('/with-limits/?limit=120') assert response.status_code == HTTPStatus.BAD_REQUEST assert response.json() == 'Maximum limit value is exceeded: 100'
def test_controller_with_limits_does_not_redirect_if_limit_is_set(): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) response = client.get('/with-limits/?limit=30') assert response.status_code == HTTPStatus.OK assert response.json() == {'limit': 30, 'offset': None}
def test_header_without_annotation(): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) with pytest.raises(ArgumentNotSupported): # Act client.get('/with-response-headers/header-without-annotation/', content_type='application/json')
def test_controller_with_output_template(): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) response = client.get('/with-output-template/?name=John') assert response.status_code == HTTPStatus.OK assert response.content == b'Hello, John!'
def test_controller_with_serializer_context(): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) expected_data = {'number': 123} response = client.get('/with-serializer/with-context/') assert response.status_code == HTTPStatus.OK assert response.json() == expected_data
def test_int_response_header(): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) # Act response = client.get('/with-response-headers/int-header/', content_type='application/json') assert response.status_code == HTTPStatus.OK assert response.json() == 'OK' assert response['x-header'] == '123'
def test_controller_with_serializer(): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) data = {'number': 1} expected_data = {'number': 2} response = client.post('/with-serializer/', data=data) assert response.status_code == HTTPStatus.OK assert response.json() == expected_data
def test_uuid_response_header(): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) uid = uuid.uuid4() # Act response = client.get(f'/with-response-headers/uuid-header/?uid={uid}', content_type='application/json') assert response.status_code == HTTPStatus.OK assert response.json() == 'OK' assert response['x-header'] == str(uid)
def test_controller_with_limits_fails_if_maximum_limit_is_exceeded(): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) response = client.get('/with-limits/?limit=120') assert response.status_code == HTTPStatus.BAD_REQUEST assert response.json() == { 'status': 400, 'detail': 'Maximum limit value is exceeded: 100', 'title': 'Maximum limit value exceeded', 'type': 'urn:problem-type:maximum-limit-value-exceeded', }
def test_datetime_isoformat_response_header(): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) now = datetime.datetime.now() # Act response = client.get( f'/with-response-headers/datetime-isoformat-header/?now={now.timestamp()}', content_type='application/json', ) assert response.status_code == HTTPStatus.OK assert response.json() == 'OK' assert response['x-header'] == now.isoformat()
def test_last_modified_response_header(): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) now = datetime.datetime.now() # Act response = client.get( f'/with-response-headers/last-modified-header/?now={now.timestamp()}', content_type='application/json', ) assert response.status_code == HTTPStatus.OK assert response.json() == 'OK' assert response['last-modified'] == now.astimezone( pytz.utc).strftime('%a, %d %b %Y %X GMT')
def test_query_parameter(date, date_time, boolean, optional_boolean, array, string): client = APIClient() user = AuthorizedUser() client.force_authenticate(user) expected_data = { 'date': parser.parse(date).date(), 'date_time': parser.parse(date_time), 'boolean': boolean == 'true', 'optional_boolean': optional_boolean == 'true' if optional_boolean is not None else None, 'array': array, 'expanded_array': list(map(str, array)), 'string': string, } base_uri = URITemplate( '/with-query-parameter/' '{?date,date_time,boolean,optional_boolean,array,expanded_array*,string}', ) query_params = { 'date': date, 'date_time': date_time, 'boolean': boolean, 'array': ','.join(map(str, array)), 'expanded_array': array, 'string': string, } if optional_boolean is not None: query_params['optional_boolean'] = optional_boolean base_uri = base_uri.expand(**query_params) # Act http_response = client.get(base_uri) assert http_response.data == expected_data