moto/tests/test_apigateway/test_server.py

104 lines
3.3 KiB
Python
Raw Normal View History

2016-03-01 17:03:59 +00:00
from __future__ import unicode_literals
import sure # noqa
2018-07-10 13:58:02 +00:00
import json
2016-03-01 17:03:59 +00:00
import moto.server as server
2019-10-31 15:44:26 +00:00
"""
2016-03-01 17:03:59 +00:00
Test the different server responses
2019-10-31 15:44:26 +00:00
"""
2016-03-01 17:03:59 +00:00
def test_list_apis():
2019-10-31 15:44:26 +00:00
backend = server.create_backend_app("apigateway")
2016-03-01 17:03:59 +00:00
test_client = backend.test_client()
2019-10-31 15:44:26 +00:00
res = test_client.get("/restapis")
2016-03-01 17:03:59 +00:00
res.data.should.equal(b'{"item": []}')
2018-07-10 13:58:02 +00:00
2019-10-31 15:44:26 +00:00
2018-07-10 13:58:02 +00:00
def test_usage_plans_apis():
2019-10-31 15:44:26 +00:00
backend = server.create_backend_app("apigateway")
2018-07-10 13:58:02 +00:00
test_client = backend.test_client()
2018-07-11 16:17:58 +00:00
# List usage plans (expect empty)
2019-10-31 15:44:26 +00:00
res = test_client.get("/usageplans")
2018-07-10 13:58:02 +00:00
json.loads(res.data)["item"].should.have.length_of(0)
2018-07-11 16:17:58 +00:00
# Create usage plan
2019-10-31 15:44:26 +00:00
res = test_client.post("/usageplans", data=json.dumps({"name": "test"}))
2018-07-10 13:58:02 +00:00
created_plan = json.loads(res.data)
2019-10-31 15:44:26 +00:00
created_plan["name"].should.equal("test")
2018-07-10 13:58:02 +00:00
2018-07-11 16:17:58 +00:00
# List usage plans (expect 1 plan)
2019-10-31 15:44:26 +00:00
res = test_client.get("/usageplans")
2018-07-10 13:58:02 +00:00
json.loads(res.data)["item"].should.have.length_of(1)
2018-07-11 16:17:58 +00:00
# Get single usage plan
2019-10-31 15:44:26 +00:00
res = test_client.get("/usageplans/{0}".format(created_plan["id"]))
2018-07-10 13:58:02 +00:00
fetched_plan = json.loads(res.data)
fetched_plan.should.equal(created_plan)
2018-07-11 16:17:58 +00:00
# Delete usage plan
2019-10-31 15:44:26 +00:00
res = test_client.delete("/usageplans/{0}".format(created_plan["id"]))
res.data.should.equal(b"{}")
2018-07-10 13:58:02 +00:00
2018-07-11 16:17:58 +00:00
# List usage plans (expect empty again)
2019-10-31 15:44:26 +00:00
res = test_client.get("/usageplans")
2018-07-10 13:58:02 +00:00
json.loads(res.data)["item"].should.have.length_of(0)
2018-07-11 16:17:58 +00:00
2019-10-31 15:44:26 +00:00
2018-07-11 16:17:58 +00:00
def test_usage_plans_keys():
2019-10-31 15:44:26 +00:00
backend = server.create_backend_app("apigateway")
2018-07-11 16:17:58 +00:00
test_client = backend.test_client()
2019-10-31 15:44:26 +00:00
usage_plan_id = "test_plan_id"
2018-07-11 16:17:58 +00:00
# Create API key to be used in tests
2019-10-31 15:44:26 +00:00
res = test_client.post("/apikeys", data=json.dumps({"name": "test"}))
2018-07-11 16:17:58 +00:00
created_api_key = json.loads(res.data)
# List usage plans keys (expect empty)
2019-10-31 15:44:26 +00:00
res = test_client.get("/usageplans/{0}/keys".format(usage_plan_id))
2018-07-11 16:17:58 +00:00
json.loads(res.data)["item"].should.have.length_of(0)
# Create usage plan key
2019-10-31 15:44:26 +00:00
res = test_client.post(
"/usageplans/{0}/keys".format(usage_plan_id),
data=json.dumps({"keyId": created_api_key["id"], "keyType": "API_KEY"}),
)
2018-07-11 16:17:58 +00:00
created_usage_plan_key = json.loads(res.data)
# List usage plans keys (expect 1 key)
2019-10-31 15:44:26 +00:00
res = test_client.get("/usageplans/{0}/keys".format(usage_plan_id))
2018-07-11 16:17:58 +00:00
json.loads(res.data)["item"].should.have.length_of(1)
# Get single usage plan key
2019-10-31 15:44:26 +00:00
res = test_client.get(
"/usageplans/{0}/keys/{1}".format(usage_plan_id, created_api_key["id"])
)
2018-07-11 16:17:58 +00:00
fetched_plan_key = json.loads(res.data)
fetched_plan_key.should.equal(created_usage_plan_key)
# Delete usage plan key
2019-10-31 15:44:26 +00:00
res = test_client.delete(
"/usageplans/{0}/keys/{1}".format(usage_plan_id, created_api_key["id"])
)
res.data.should.equal(b"{}")
2018-07-11 16:17:58 +00:00
# List usage plans keys (expect to be empty again)
2019-10-31 15:44:26 +00:00
res = test_client.get("/usageplans/{0}/keys".format(usage_plan_id))
2018-07-11 16:17:58 +00:00
json.loads(res.data)["item"].should.have.length_of(0)
2019-10-31 15:44:26 +00:00
2018-07-11 16:17:58 +00:00
def test_create_usage_plans_key_non_existent_api_key():
2019-10-31 15:44:26 +00:00
backend = server.create_backend_app("apigateway")
2018-07-11 16:17:58 +00:00
test_client = backend.test_client()
2019-10-31 15:44:26 +00:00
usage_plan_id = "test_plan_id"
2018-07-11 16:17:58 +00:00
# Create usage plan key with non-existent api key
2019-10-31 15:44:26 +00:00
res = test_client.post(
"/usageplans/{0}/keys".format(usage_plan_id),
data=json.dumps({"keyId": "non-existent", "keyType": "API_KEY"}),
)
2018-07-11 16:17:58 +00:00
res.status_code.should.equal(404)