def test_make_resource_serialization_get(client, app, admin_auth, complex_type, complex_item): model = make_model(complex_type) resource = make_resource(model)() app.add_route('/endpoint/{id}', resource) response = client.get('/endpoint/%s' % (complex_item.id), headers={'authorization': admin_auth}) assert response.status == falcon.HTTP_OK
def test_make_resource_make_model(client, app, dummy_type, custom_field, method): """ Tests whether make_resource can correctly generate a resource using make_model. """ model = make_model(dummy_type) resource = make_resource(model) assert hasattr(resource, method)
def test_make_resource_serialization_patch(client, app, admin_auth, complex_type, complex_item): model = make_model(complex_type) resource = make_resource(model)() body = {'datefield': '2016-12-30', 'intfield': 5} app.add_route('/endpoint/{id}', resource) response = client.patch('/endpoint/%s' % (complex_item.id), body=body, headers={ 'authorization': admin_auth, 'Content-Type': 'application/json' }) assert response.status == falcon.HTTP_OK
def test_make_resource_make_model_delete(client, app, admin_auth, custom_field, dummy_type, dummy_admin): # setup model = make_model(dummy_type) item = model(owner=dummy_admin, f='someval') item.save() item_id = item.id # test resource = make_resource(model)() app.add_route('/endpoint/{id}', resource) response = client.delete('/endpoint/%s' % (item_id), headers={'authorization': admin_auth}) assert response.status == falcon.HTTP_NO_CONTENT assert is_deleted(model, item_id) == True
def test_make_resource_unathorized(client, app, model, method): """ Tests the behaviour of a generated resource when a GET or DELETE request is performed. """ resource = make_resource(model)() app.add_route('/endpoint/{id}', resource) if method == 'get': response = client.get('/endpoint/1') elif method == 'delete': response = client.delete('/endpoint/1') elif method == 'patch': response = client.patch('/endpoint/1', body='') assert response.status == falcon.HTTP_UNAUTHORIZED assert response.__dict__['headers']['www-authenticate'] != None
def test_make_resource_delete_item(client, app, admin_auth, item_with_model): """ Tests the behaviour of a generated resource when a DELETE request that includes a basic auth header is performed and an item is deleted. """ # setup model = item_with_model[1] item = item_with_model[0] item_id = item.id # test resource = make_resource(model)() app.add_route('/endpoint/{id}', resource) response = client.delete('/endpoint/%s' % (item_id), headers={'authorization': admin_auth}) assert response.status == falcon.HTTP_NO_CONTENT assert is_deleted(model, item_id) == True
def test_make_resource_access_rules_get(client, app, user_auth, item_with_model): """ Verifies that make_resource correctly implements permissions on GET requests. """ item = item_with_model[0] model = item_with_model[1] resource = make_resource(model)() app.add_route('/endpoint/{id}', resource) response = client.get('/endpoint/%s' % (item.id), headers={'authorization': user_auth}) body = json.loads(response.body) description = 'You do not have the required permissions for this action' assert response.status == falcon.HTTP_FORBIDDEN assert body['title'] == 'Forbidden access' assert body['description'] == description
def test_make_resource_get_item(client, app, admin_auth, item_with_model): """ Tests the behaviour of a generated resource when a GET request that includes a basic auth header is performed and an item is retrieved. """ item = item_with_model[0] model = item_with_model[1] resource = make_resource(model)() app.add_route('/endpoint/{id}', resource) response = client.get('/endpoint/%s' % (item.id), headers={'authorization': admin_auth}) assert response.status == falcon.HTTP_OK model_fields = model_columns(model) body = json.loads(response.body)['properties'] for i in model_fields: assert body[i] == getattr(item, i)
def test_make_resource_patch_item(client, app, admin_auth, item_with_model): """ Tests the behaviour of a generated resource when a PATCH request that includes a basic auth header is performed and an item is retrieved. """ item = item_with_model[0] model = item_with_model[1] resource = make_resource(model)() app.add_route('/endpoint/{id}', resource) body = model_body(model) response = client.patch('/endpoint/%s' % (item.id), body, headers={ 'authorization': admin_auth, 'Content-Type': 'application/json' }) assert response.status == falcon.HTTP_OK response_body = json.loads(response.body)['properties'] for key in body: assert body[key] == response_body[key]
def test_make_resource_make_model_get(client, app, admin_auth, dummy_type, dummy_admin, custom_field): """ Verifies that make_resource can use make_model's generated models and return a 200 when auth is sent. """ model = make_model(dummy_type) item = model(owner=dummy_admin, f='someval') item.save() resource = make_resource(model)() app.add_route('/endpoint/{id}', resource) response = client.get('/endpoint/%s' % (item.id), headers={'authorization': admin_auth}) assert response.status == falcon.HTTP_OK model_fields = model_columns(model) body = json.loads(response.body)['properties'] for i in model_fields: assert body[i] == getattr(item, i) # teardown item.delete_instance()
def test_make_resource_not_found(client, app, admin_auth, model, method): """ Tests the behaviour of a generated resource when a GET or DELETE request that includes a basic auth header is performed. """ resource = make_resource(model)() app.add_route('/endpoint/{id}', resource) if method == 'get': response = client.get('/endpoint/1234', headers={'authorization': admin_auth}) elif method == 'delete': response = client.delete('/endpoint/1234', headers={'authorization': admin_auth}) elif method == 'patch': response = client.patch('/endpoint/1234', headers={'authorization': admin_auth}, body='') body = json.loads(response.body) description = 'The resource you are looking for does not exist' assert response.status == falcon.HTTP_NOT_FOUND assert body['title'] == 'Not found' assert body['description'] == description
def test_make_resource_make_model_patch(client, app, admin_auth, custom_field, dummy_type, dummy_admin): model = make_model(dummy_type) item = model(owner=dummy_admin, f='someval') item.save() resource = make_resource(model)() app.add_route('/endpoint/{id}', resource) body = {} body[custom_field.name] = 'myval' check = {} check[custom_field.name] = 'myval' response = client.patch('/endpoint/%s' % (item.id), body=body, headers={ 'authorization': admin_auth, 'Content-Type': 'application/json' }) assert response.status == falcon.HTTP_OK response_body = json.loads(response.body)['properties'] for key in body: assert body[key] == response_body[key] # teardown item.delete_instance()
def test_make_resource(model, method): """ Tests whether make_resource can correctly generate a resource. """ resource = make_resource(model) assert hasattr(resource, method)