def test_get_list(self): client = Client(WSGIDispatcher([ApiApp]), response_wrapper=BaseResponse) resp = client.get("/address/") self.assertEqual(resp.status_code, 200) self.assertIsInstance(json.loads(resp.data)["object_list"], list)
def test_base_pagination_offset(self): client = Client(WSGIDispatcher([ApiApp]), response_wrapper=BaseResponse) resp = client.get("/address/?offset=2") self.assertEqual( json.loads(resp.data)["object_list"][0]['ressource_uri'], "/address/2/")
def test_auth_unique_uri(self): from rest_api_framework.pagination import Pagination ressources = [{"id": "azerty"}] authentication = ApiKeyAuthentication( PythonListDataStore(ressources, ApiModel)) class ApiAppAuth(ApiApp): controller = { "list_verbs": ["GET", "POST"], "unique_verbs": ["GET", "PUT", "DElETE"], "options": { "pagination": Pagination(20), "authentication": authentication, "authorization": ApiKeyAuthorization } } client = Client(WSGIDispatcher([ApiAppAuth]), response_wrapper=BaseResponse) resp = client.get("/address/1/?apikey=azerty") self.assertEqual(resp.status_code, 200) resp = client.get("/address/1/?apikey=querty") self.assertEqual(resp.status_code, 401)
def test_post_auth(self): """ Test a read only api. GET should be ok, POST and PUT should not """ from rest_api_framework.pagination import Pagination ressources = [{"id": "azerty"}] authentication = ApiKeyAuthentication( PythonListDataStore(ressources, ApiModel)) class ApiAppAuth(ApiApp): controller = { "list_verbs": ["GET"], "unique_verbs": ["GET"], "options": { "pagination": Pagination(20), "authentication": authentication, "authorization": ApiKeyAuthorization } } client = Client(WSGIDispatcher([ApiAppAuth]), response_wrapper=BaseResponse) resp = client.get("/address/1/?apikey=azerty") self.assertEqual(resp.status_code, 200) resp = client.post("/address/?apikey=azerty", data=json.dumps({ 'name': 'bob', 'age': 34 })) self.assertEqual(resp.status_code, 405)
def test_get(self): client = Client(WSGIDispatcher([ApiApp]), response_wrapper=BaseResponse) resp = client.get("/address/1/") self.assertEqual(resp.status_code, 200) resp = client.get("/400/") self.assertEqual(resp.status_code, 404)
def test_say_hello(self): client = Client(WSGIDispatcher([FormatedApp]), response_wrapper=BaseResponse) resp = client.get('/') self.assertEqual(resp.status_code, 200) self.assertEqual({ "version": "devel", "name": "PRAF" }, json.loads(resp.data))
def test_get_partial_list(self): client = Client(WSGIDispatcher([PartialApiApp]), response_wrapper=BaseResponse) resp = client.get("/address/?fields=age") # we only want "age". get_list add id, JsonResponse add ressource_uri self.assertEqual(len( json.loads(resp.data)["object_list"][0].keys()), 3) resp = client.get("/address/") self.assertEqual(resp.status_code, 200)
def test_create(self): client = Client(WSGIDispatcher([FormatedApp]), response_wrapper=BaseResponse) resp = client.post("/address/", data=json.dumps({ 'name': 'bob', 'age': "34" })) self.assertEqual(resp.status_code, 201)
def test_get_formated_list(self): client = Client(WSGIDispatcher([FormatedApp]), response_wrapper=BaseResponse) resp = client.get("/address/") self.assertNotIn("id", json.loads(resp.data)['object_list'][0]) self.assertIn("ressource_uri", json.loads(resp.data)['object_list'][0]) self.assertEqual(resp.status_code, 200) self.assertIsInstance(json.loads(resp.data)["object_list"], list)
def test_get_partial_sql_raise(self): client = Client(WSGIDispatcher([PartialSQLApp]), response_wrapper=BaseResponse) for i in range(100): client.post("/address/", data=json.dumps({"name": "bob", "age": 34})) response = client.get("/address/?fields=wrongkey") self.assertEqual(response.status_code, 400) os.remove("test.db")
def test_rxjson_spore(self): rx = Rx.Factory({'register_core_types': True}) client = Client(WSGIDispatcher([ApiApp], name='ApiApp', version='1.0', base_url='http://apiapp.com'), response_wrapper=BaseResponse) resp = client.get("/spore/") with open(os.path.join(HERE, 'spore_validation.rx')) as f: spore_json_schema = json.loads(f.read()) spore_schema = rx.make_schema(spore_json_schema) self.assertTrue(spore_schema.check(json.loads(resp.data)))
def test_ratelimit(self): client = Client(WSGIDispatcher([RateLimitApiApp]), response_wrapper=BaseResponse) resp = client.get("/address/") self.assertEqual(resp.status_code, 401) resp = client.get("/address/?apikey=azerty") self.assertEqual(resp.status_code, 200) resp = client.get("/address/?apikey=azerty") self.assertEqual(resp.status_code, 429) time.sleep(1) resp = client.get("/address/?apikey=azerty") self.assertEqual(resp.status_code, 200)
def test_base_pagination_count(self): client = Client(WSGIDispatcher([SQLiteApp]), response_wrapper=BaseResponse) for i in range(100): client.post("/address/", data=json.dumps({ "name": "bob", "age": 34 })) resp = client.get("/address/?count=2") self.assertEqual(len(json.loads(resp.data)), 2)
def test_base_pagination_offset(self): client = Client(WSGIDispatcher([SQLiteApp]), response_wrapper=BaseResponse) for i in range(100): client.post("/address/", data=json.dumps({ "name": "bob", "age": 34 })) resp = client.get("/address/?offset=2") self.assertEqual(json.loads(resp.data)["object_list"][0]['id'], 2) os.remove("test.db")
def test_get_partial_sql(self): client = Client(WSGIDispatcher([PartialSQLApp]), response_wrapper=BaseResponse) for i in range(100): client.post("/address/", data=json.dumps({"name": "bob", "age": 34})) resp = client.get("/address/?fields=age") # we only want "age". get_list add id, JsonResponse add ressource_uri self.assertEqual( len(json.loads(resp.data)["object_list"][0].keys()), 3) resp = client.get("/address/") self.assertEqual(resp.status_code, 200) os.remove("test.db")
def test_create(self): client = Client(WSGIDispatcher([ApiApp]), response_wrapper=BaseResponse) resp = client.post("/address/", data=json.dumps({ 'name': 'bob', 'age': 34 })) self.assertEqual(resp.status_code, 201) self.assertEqual(resp.headers['Location'], "http://localhost/address/101/") resp = client.post("/address/", data={"test": datetime.datetime.now()}) self.assertEqual(resp.status_code, 400)
def test_spore(self): client = Client(WSGIDispatcher([ApiApp], name='ApiApp', version='1.0', base_url='http://apiapp.com'), response_wrapper=BaseResponse) resp = client.get("/spore/") self.assertEqual(resp.status_code, 200) spore = json.loads(resp.data) # basic fields self.assertEqual(spore['name'], 'ApiApp') self.assertEqual(spore['base_url'], 'http://apiapp.com') self.assertEqual(spore['version'], '1.0') # methods self.assertIn('list_address', spore['methods']) self.assertEqual('/address/:identifier/', spore['methods']['get_address']['path']) self.assertIn('identifier', spore['methods']['get_address']['required_params'])
def test_basic_auth(self): from base64 import b64encode class BasicModel(models.Model): fields = [ models.StringPkField(name="user", required=True), models.StringField(name="password", required=True) ] ressources = [{"user": "******", "password": "******"}] authentication = BasicAuthentication( PythonListDataStore(ressources, ApiModel)) class ApiAppAuth(ApiApp): controller = { "list_verbs": ["GET"], "unique_verbs": ["GET"], "options": { "authentication": authentication, "authorization": Authorization } } client = Client(WSGIDispatcher([ApiAppAuth]), response_wrapper=BaseResponse) credentials = b64encode("login:pass") headers = Headers([('Authorization: Basic', credentials)]) resp = client.get("/address/1/", headers=headers) self.assertEqual(resp.status_code, 200) credentials = b64encode("login:hackme") headers = Headers([('Authorization: Basic', credentials)]) resp = client.get("/address/1/", headers=headers) self.assertEqual(resp.status_code, 401) resp = client.get("/address/1/") self.assertEqual(resp.status_code, 401)
def test_base_pagination_count_overflow(self): client = Client(WSGIDispatcher([ApiApp]), response_wrapper=BaseResponse) resp = client.get("/address/?count=200") self.assertEqual(len(json.loads(resp.data)["object_list"]), 20)
def test_updated(self): client = Client(WSGIDispatcher([ApiApp]), response_wrapper=BaseResponse) resp = client.put("/address/1/", data=json.dumps({'name': 'boby mc queen'})) self.assertEqual(resp.status_code, 200)
def test_get_partial_raise(self): client = Client(WSGIDispatcher([PartialApiApp]), response_wrapper=BaseResponse) response = client.get("/address/?fields=wrongkey") self.assertEqual(response.status_code, 400)
def test_delete(self): client = Client(WSGIDispatcher([ApiApp]), response_wrapper=BaseResponse) resp = client.delete("/address/4/") self.assertEqual(resp.status_code, 204)
fields = [ models.StringField(name="first_name", required=True), models.StringField(name="last_name", required=True), models.PkField(name="id", required=True) ] class UserEndPoint(Controller): ressource = { "ressource_name": "users", "ressource": { "name": "adress_book.db", "table": "users" }, "model": UserModel, "datastore": SQLiteDataStore } controller = { "list_verbs": ["GET", "POST"], "unique_verbs": ["GET", "PUT", "DELETE"] } view = {"response_class": JsonResponse} if __name__ == '__main__': from werkzeug.serving import run_simple from rest_api_framework.controllers import WSGIDispatcher app = WSGIDispatcher([UserEndPoint]) run_simple('127.0.0.1', 5000, app, use_debugger=True, use_reloader=True)
view = {"response_class": JsonResponse} class SQLiteApp(Controller): ressource = { "ressource_name": "address", "ressource": { "name": ":memory:", "table": "address" }, "model": ApiModel, "datastore": SQLiteDataStore } controller = { "list_verbs": ["GET", "POST"], "unique_verbs": ["GET", "PUT", "DELETE"], "options": { "pagination": Pagination(20) } } view = {"response_class": JsonResponse} if __name__ == '__main__': from werkzeug.serving import run_simple app = WSGIDispatcher([SQLiteApp]) run_simple('127.0.0.1', 5000, app, use_debugger=True, use_reloader=True)
def test_max_pagination_count(self): client = Client(WSGIDispatcher([ApiApp]), response_wrapper=BaseResponse) resp = client.get("/address/?offset=82") self.assertEqual(json.loads(resp.data)["meta"]['next'], "null") self.assertEqual(len(json.loads(resp.data)["object_list"]), 19)