Merge pull request #1717 from fewstera/usage-plans

APIGateway: Added API for usage plans and usage plan keys
This commit is contained in:
Steve Pulec 2018-07-19 10:40:14 -04:00 committed by GitHub
commit 2c1aa8a63d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 299 additions and 3 deletions

View file

@ -995,3 +995,92 @@ def test_api_keys():
response = client.get_api_keys()
len(response['items']).should.equal(1)
@mock_apigateway
def test_usage_plans():
region_name = 'us-west-2'
client = boto3.client('apigateway', region_name=region_name)
response = client.get_usage_plans()
len(response['items']).should.equal(0)
usage_plan_name = 'TEST-PLAN'
payload = {'name': usage_plan_name}
response = client.create_usage_plan(**payload)
usage_plan = client.get_usage_plan(usagePlanId=response['id'])
usage_plan['name'].should.equal(usage_plan_name)
usage_plan['apiStages'].should.equal([])
usage_plan_name = 'TEST-PLAN-2'
usage_plan_description = 'Description'
usage_plan_quota = {'limit': 10, 'period': 'DAY', 'offset': 0}
usage_plan_throttle = {'rateLimit': 2, 'burstLimit': 1}
usage_plan_api_stages = [{'apiId': 'foo', 'stage': 'bar'}]
payload = {'name': usage_plan_name, 'description': usage_plan_description, 'quota': usage_plan_quota, 'throttle': usage_plan_throttle, 'apiStages': usage_plan_api_stages}
response = client.create_usage_plan(**payload)
usage_plan_id = response['id']
usage_plan = client.get_usage_plan(usagePlanId=usage_plan_id)
usage_plan['name'].should.equal(usage_plan_name)
usage_plan['description'].should.equal(usage_plan_description)
usage_plan['apiStages'].should.equal(usage_plan_api_stages)
usage_plan['throttle'].should.equal(usage_plan_throttle)
usage_plan['quota'].should.equal(usage_plan_quota)
response = client.get_usage_plans()
len(response['items']).should.equal(2)
client.delete_usage_plan(usagePlanId=usage_plan_id)
response = client.get_usage_plans()
len(response['items']).should.equal(1)
@mock_apigateway
def test_usage_plan_keys():
region_name = 'us-west-2'
usage_plan_id = 'test_usage_plan_id'
client = boto3.client('apigateway', region_name=region_name)
usage_plan_id = "test"
# Create an API key so we can use it
key_name = 'test-api-key'
response = client.create_api_key(name=key_name)
key_id = response["id"]
key_value = response["value"]
# Get current plan keys (expect none)
response = client.get_usage_plan_keys(usagePlanId=usage_plan_id)
len(response['items']).should.equal(0)
# Create usage plan key
key_type = 'API_KEY'
payload = {'usagePlanId': usage_plan_id, 'keyId': key_id, 'keyType': key_type }
response = client.create_usage_plan_key(**payload)
usage_plan_key_id = response["id"]
# Get current plan keys (expect 1)
response = client.get_usage_plan_keys(usagePlanId=usage_plan_id)
len(response['items']).should.equal(1)
# Get a single usage plan key and check it matches the created one
usage_plan_key = client.get_usage_plan_key(usagePlanId=usage_plan_id, keyId=usage_plan_key_id)
usage_plan_key['name'].should.equal(key_name)
usage_plan_key['id'].should.equal(key_id)
usage_plan_key['type'].should.equal(key_type)
usage_plan_key['value'].should.equal(key_value)
# Delete usage plan key
client.delete_usage_plan_key(usagePlanId=usage_plan_id, keyId=key_id)
# Get current plan keys (expect none)
response = client.get_usage_plan_keys(usagePlanId=usage_plan_id)
len(response['items']).should.equal(0)
@mock_apigateway
def test_create_usage_plan_key_non_existent_api_key():
region_name = 'us-west-2'
usage_plan_id = 'test_usage_plan_id'
client = boto3.client('apigateway', region_name=region_name)
usage_plan_id = "test"
# Attempt to create a usage plan key for a API key that doesn't exists
payload = {'usagePlanId': usage_plan_id, 'keyId': 'non-existent', 'keyType': 'API_KEY' }
client.create_usage_plan_key.when.called_with(**payload).should.throw(ClientError)

View file

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