Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Miserlou/Zappa
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich Jones committed Feb 29, 2016
2 parents b3c2d68 + d405512 commit 9f3d9e0
Show file tree
Hide file tree
Showing 27 changed files with 444 additions and 121 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ python:
- "2.7"
# command to install dependencies
install:
- "pip install setuptools --upgrade; pip install coveralls; python setup.py install"
- "pip install setuptools --upgrade; pip install -r test_requirements.txt; python setup.py install"
# command to run tests
script: nosetests --with-coverage --cover-package=zappa
after_success:
Expand Down
9 changes: 4 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
boto3==1.2.3
botocore==1.3.26
base58==0.2.2
boto3==1.2.5
botocore==1.3.30
docutils==0.12
filechunkio==1.6
futures==3.0.4
jmespath==0.9.0
nose==1.3.7
placebo==0.7.2
python-dateutil==2.4.2
requests>=2.9.1
six==1.10.0
tqdm==3.7.1
wheel==0.24.0
wsgi-request-logger==0.4.4
Werkzeug==0.11.4
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
with open(os.path.join(os.path.dirname(__file__), 'requirements.txt')) as f:
required = f.read().splitlines()

with open(os.path.join(os.path.dirname(__file__), 'test_requirements.txt')) as f:
test_required = f.read().splitlines()

setup(
name='zappa',
version='0.11.2',
packages=['zappa'],
install_requires=required,
install_requires=required,
tests_require=test_required,
test_suite='nose.collector',
include_package_data=True,
license='MIT License',
description='Serverless WSGI With AWS Lambda + API Gateway',
Expand Down
4 changes: 4 additions & 0 deletions test_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coveralls==1.1
mock==1.3.0
nose==1.3.7
placebo==0.7.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"status_code": 201,
"data": {
"id": "j27idab94h",
"name": "123",
"description": "123 Zappa",
"ResponseMetadata": {
"HTTPStatusCode": 201,
"RequestId": "ded1717d-dc3a-11e5-afc9-35deb4d0a175"
},
"createdDate": {
"hour": 21,
"__class__": "datetime",
"month": 2,
"second": 50,
"microsecond": 0,
"year": 2016,
"day": 25,
"minute": 41
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"status_code": 200,
"data": {
"items": [
{
"path": "/",
"id": "g1atjiylwd"
}
],
"ResponseMetadata": {
"HTTPStatusCode": 200,
"RequestId": "dee74371-dc3a-11e5-adc8-c7e9cc4e6647"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"status_code": 404,
"data": {
"ResponseMetadata": {
"HTTPStatusCode": 404,
"RequestId": "dec9f7a4-dc3a-11e5-ae49-899567c90292"
},
"Error": {
"Message": "Invalid REST API identifier specified",
"Code": "NotFoundException"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"status_code": 201,
"data": {
"statusCode": "200",
"responseTemplates": {
"text/html": "#set($inputRoot = $input.path('$'))\n$inputRoot.Content"
},
"selectionPattern": "",
"ResponseMetadata": {
"HTTPStatusCode": 201,
"RequestId": "df27f5f3-dc3a-11e5-adc8-c7e9cc4e6647"
},
"responseParameters": {
"method.response.header.Content-Type": "integration.response.body.Content-Type",
"method.response.header.Location": "integration.response.body.Location",
"method.response.header.X-Frame-Options": "integration.response.body.X-Frame-Options",
"method.response.header.Status": "integration.response.body.Status",
"method.response.header.Set-Cookie": "integration.response.body.Set-Cookie"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"status_code": 201,
"data": {
"cacheKeyParameters": [],
"ResponseMetadata": {
"HTTPStatusCode": 201,
"RequestId": "df135c7e-dc3a-11e5-afc9-35deb4d0a175"
},
"uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:12345:function:helloworld/invocations",
"httpMethod": "POST",
"requestTemplates": {
"application/json": "{\n \"body\" : $input.json('$'),\n \"headers\": {\n #foreach($header in $input.params().header.keySet())\n \"$header\": \"$util.escapeJavaScript($input.params().header.get($header))\" #if($foreach.hasNext),#end\n \n #end\n },\n \"method\": \"$context.httpMethod\",\n \"params\": {\n #foreach($param in $input.params().path.keySet())\n \"$param\": \"$util.escapeJavaScript($input.params().path.get($param))\" #if($foreach.hasNext),#end\n \n #end\n },\n \"query\": {\n #foreach($queryParam in $input.params().querystring.keySet())\n \"$queryParam\": \"$util.escapeJavaScript($input.params().querystring.get($queryParam))\" #if($foreach.hasNext),#end\n \n #end\n } \n}",
"application/x-www-form-urlencoded": "#set($rawPostData = $input.path('$'))\n{\n \"body\" : \"$rawPostData\",\n \"headers\": {\n #foreach($header in $input.params().header.keySet())\n \"$header\": \"$util.escapeJavaScript($input.params().header.get($header))\" #if($foreach.hasNext),#end\n \n #end\n },\n \"method\": \"$context.httpMethod\",\n \"params\": {\n #foreach($param in $input.params().path.keySet())\n \"$param\": \"$util.escapeJavaScript($input.params().path.get($param))\" #if($foreach.hasNext),#end\n \n #end\n },\n \"query\": {\n #foreach($queryParam in $input.params().querystring.keySet())\n \"$queryParam\": \"$util.escapeJavaScript($input.params().querystring.get($queryParam))\" #if($foreach.hasNext),#end\n \n #end\n } \n}"
},
"cacheNamespace": "none",
"credentials": "arn:aws:iam::12345:role/ZappaLambdaExecution",
"type": "AWS",
"requestParameters": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"status_code": 201,
"data": {
"responseModels": {
"text/html": "Empty"
},
"statusCode": "200",
"ResponseMetadata": {
"HTTPStatusCode": 201,
"RequestId": "df1e3225-dc3a-11e5-ae49-899567c90292"
},
"responseParameters": {
"method.response.header.Content-Type": false,
"method.response.header.Location": false,
"method.response.header.X-Frame-Options": false,
"method.response.header.Status": false,
"method.response.header.Set-Cookie": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"status_code": 201,
"data": {
"apiKeyRequired": false,
"httpMethod": "GET",
"authorizationType": "NONE",
"ResponseMetadata": {
"HTTPStatusCode": 201,
"RequestId": "df048f72-dc3a-11e5-adc8-c7e9cc4e6647"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"status_code": 200,
"data": {
"Role": {
"AssumeRolePolicyDocument": "%7B%0A%20%20%22Version%22%3A%20%222012-10-17%22%2C%0A%20%20%22Statement%22%3A%20%5B%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%22Sid%22%3A%20%22%22%2C%0A%20%20%20%20%20%20%22Effect%22%3A%20%22Allow%22%2C%0A%20%20%20%20%20%20%22Principal%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Service%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%22lambda.amazonaws.com%22%2C%0A%20%20%20%20%20%20%20%20%20%20%22apigateway.amazonaws.com%22%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%22Action%22%3A%20%22sts%3AAssumeRole%22%0A%20%20%20%20%7D%0A%20%20%5D%0A%7D",
"RoleId": "AROAIJTKAN7OBBD7BF25O",
"CreateDate": {
"hour": 1,
"__class__": "datetime",
"month": 2,
"second": 20,
"microsecond": 221000,
"year": 2016,
"day": 25,
"minute": 28
},
"RoleName": "ZappaLambdaExecution",
"Path": "/",
"Arn": "arn:aws:iam::123:role/ZappaLambdaExecution"
},
"ResponseMetadata": {
"HTTPStatusCode": 200,
"RequestId": "0da2623f-db5f-11e5-9d89-67e11bce6785"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"status_code": 404,
"data": {
"ResponseMetadata": {
"HTTPStatusCode": 404,
"RequestId": "0e11c69f-db5f-11e5-aa20-65c7ceb91dfa"
},
"Error": {
"Message": "The role policy with name zappa-permissions cannot be found.",
"Code": "NoSuchEntity",
"Type": "Sender"
}
}
}
14 changes: 14 additions & 0 deletions tests/placebo/TestZappa.test_create_iam_roles/iam.GetRole_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"status_code": 404,
"data": {
"ResponseMetadata": {
"HTTPStatusCode": 404,
"RequestId": "0d24322f-db5f-11e5-9d89-67e11bce6785"
},
"Error": {
"Message": "The role with name ZappaLambdaExecution cannot be found.",
"Code": "NoSuchEntity",
"Type": "Sender"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {
"HTTPStatusCode": 200,
"RequestId": "0e619422-db5f-11e5-9d89-67e11bce6785"
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
92 changes: 57 additions & 35 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import boto3
import collections
import json
import os
import placebo
import unittest
import mock

from .utils import placebo_session

from zappa.wsgi import create_wsgi_request, common_log
from zappa.zappa import Zappa
from zappa.zappa import Zappa, ASSUME_POLICY, ATTACH_POLICY


class TestZappa(unittest.TestCase):
def get_placebo_session(self):
session = boto3.Session()
placebo_dir = os.path.join(os.path.dirname(__file__), 'placebo')
pill = placebo.attach(session, data_path=placebo_dir)
pill.playback()
return session

##
# Sanity Tests
##
Expand All @@ -39,33 +34,36 @@ def test_create_lambda_package(self):

def test_load_credentials(self):
z = Zappa()
z.aws_region = 'us-east-1'
z.load_credentials()
self.assertEqual(z.boto_session.region_name, 'us-east-1')
self.assertEqual(z.aws_region, 'us-east-1')

z.aws_region = 'eu-west-1'
z.load_credentials()
self.assertEqual(z.boto_session.region_name, 'eu-west-1')
self.assertEqual(z.aws_region, 'eu-west-1')

creds = {
'AWS_ACCESS_KEY_ID': 'AK123',
'AWS_SECRET_ACCESS_KEY': 'JKL456',
'AWS_DEFAULT_REGION': 'us-west-1'
}
with mock.patch.dict('os.environ', creds):
z.aws_region = None
z.load_credentials()
loaded_creds = z.boto_session._session.get_credentials()

credentials = '[default]\naws_access_key_id = AK123\naws_secret_access_key = JKL456'
config = '[default]\noutput = json\nregion = us-east-1'

credentials_file = open('credentials', 'w')
credentials_file.write(credentials)
credentials_file.close()

config_file = open('config', 'w')
config_file.write(config)
config_file.close()

z.load_credentials('credentials', 'config')

os.remove('credentials')
os.remove('config')

self.assertTrue((z.access_key == "AK123"))
self.assertTrue((z.secret_key == "JKL456"))
self.assertTrue((z.aws_region == 'us-east-1'))
self.assertEqual(loaded_creds.access_key, 'AK123')
self.assertEqual(loaded_creds.secret_key, 'JKL456')
self.assertEqual(z.boto_session.region_name, 'us-west-1')

def test_upload_remove_s3(self):
session = self.get_placebo_session()
@placebo_session
def test_upload_remove_s3(self, session):
bucket_name = 'test_zappa_upload_s3'
z = Zappa()
z = Zappa(session)
zip_path = z.create_lambda_zip()
res = z.upload_to_s3(zip_path, bucket_name, session)
res = z.upload_to_s3(zip_path, bucket_name)
os.remove(zip_path)
self.assertTrue(res)
s3 = session.resource('s3')
Expand All @@ -78,9 +76,33 @@ def test_upload_remove_s3(self):
Bucket=bucket_name,
Key=zip_path,
)
res = z.remove_from_s3(zip_path, bucket_name, session)
res = z.remove_from_s3(zip_path, bucket_name)
self.assertTrue(res)

@placebo_session
def test_create_iam_roles(self, session):
z = Zappa(session)
arn = z.create_iam_roles()
self.assertEqual(arn, "arn:aws:iam::123:role/{}".format(z.role_name))

@placebo_session
def test_create_api_gateway_routes(self, session):
z = Zappa(session)
z.parameter_depth = 1
z.integration_response_codes = [200]
z.method_response_codes = [200]
z.http_methods = ['GET']
z.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution'
lambda_arn = 'arn:aws:lambda:us-east-1:12345:function:helloworld'
with mock.patch('time.time', return_value=123.456):
api_id = z.create_api_gateway_routes(lambda_arn)
self.assertEqual(api_id, 'j27idab94h')

def test_policy_json(self):
# ensure the policy docs are valid JSON
json.loads(ASSUME_POLICY)
json.loads(ATTACH_POLICY)

##
# Logging
##
Expand All @@ -89,7 +111,7 @@ def test_logging(self):
"""
TODO
"""
z = Zappa()
Zappa()

##
# WSGI
Expand Down

0 comments on commit 9f3d9e0

Please sign in to comment.