Skip to content

Commit

Permalink
Requests migration and testing logic (#13)
Browse files Browse the repository at this point in the history
* Moved to requests instead of botocore.vendored requests and started implementing response testing

* typo in docs

Implements #11
  • Loading branch information
taylorb-syd authored Jan 8, 2020
1 parent 424d983 commit b3c6168
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 12 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2017 Taylor Bertie
Copyright 2019 Taylor Bertie

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ To install:
pip3 install accustom
```

To create a Lambda Code Zip with accustom included:
To create a Lambda Code Zip with accustom and dependencies including (`requests`):

```bash
pip3 install accustom -t . --no-deps
pip3 install accustom -t .
zip code.zip function.py accustom -r
```

Expand Down
59 changes: 59 additions & 0 deletions accustom/Testing/test_response.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,72 @@
from accustom import is_valid_event
from accustom import cfnresponse
from accustom import ResponseObject
from accustom import Status
from accustom import RequestType
from accustom.Exceptions import DataIsNotDictException
from accustom.Exceptions import NoPhysicalResourceIdException
from accustom.Exceptions import InvalidResponseStatusException
from accustom.Exceptions import FailedToSendResponseException

import requests_mock
from unittest import TestCase, main as umain

class valid_event_tests(TestCase):
def test_missing_field(self):
event = {
"RequestType" : RequestType.CREATE,
"ResponseURL" : "https://test.url",
"StackId" : None,
"RequestId" : None,
"ResourceType" : None,
}
self.assertFalse(is_valid_event(event))

def test_no_valid_request_type(self):
event = {
"RequestType" : "DESTROY",
"ResponseURL" : "https://test.url",
"StackId" : None,
"RequestId" : None,
"ResourceType" : None,
"LogicalResourceId" : None
}
self.assertFalse(is_valid_event(event))

def test_invalid_url(self):
event = {
"RequestType" : RequestType.CREATE,
"ResponseURL" : "ftp://test.url",
"StackId" : None,
"RequestId" : None,
"ResourceType" : None,
"LogicalResourceId" : None
}
self.assertFalse(is_valid_event(event))

def test_missing_physical(self):
event = {
"RequestType" : RequestType.UPDATE,
"ResponseURL" : "https://test.url",
"StackId" : None,
"RequestId" : None,
"ResourceType" : None,
"LogicalResourceId" : None
}
self.assertFalse(is_valid_event(event))

def test_included_physical(self):
event = {
"RequestType" : RequestType.DELETE,
"ResponseURL" : "https://test.url",
"StackId" : None,
"RequestId" : None,
"ResourceType" : None,
"LogicalResourceId" : None,
"PhysicalResourceId" : None
}
self.assertTrue(is_valid_event(event))

class cfnresponseTests(TestCase):
pass

Expand Down
9 changes: 8 additions & 1 deletion accustom/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@
import six
from boto3 import client
from botocore.client import Config
from botocore.vendored import requests

logger = logging.getLogger(__name__)

# Import Requests
try:
import requests
except ImportError:
from botocore.vendored import requests
logger.warning("botocore.vendored version of requests is deprecated. Please include requests in your code bundle.")


# Time in milliseconds to set the alarm for (in milliseconds)
# Should be set to twice the worst case response time to send to S3
# Setting to 4 seconds for safety
Expand Down
11 changes: 8 additions & 3 deletions accustom/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@
import sys
import six
from urllib.parse import urlparse
from botocore.vendored import requests

logger = logging.getLogger(__name__)

# Import Requests
try:
import requests
except ImportError:
from botocore.vendored import requests
logger.warning("botocore.vendored version of requests is deprecated. Please include requests in your code bundle.")

def is_valid_event(event: dict) -> bool:
"""This function takes in a CloudFormation Request Object and checks for the required fields as per:
Expand All @@ -37,14 +42,14 @@ def is_valid_event(event: dict) -> bool:
bool: If the request object is a valid request object
"""
if not all(v in event for v in [
if not (all(v in event for v in (
'RequestType',
'ResponseURL',
'StackId',
'RequestId',
'ResourceType',
'LogicalResourceId'
]):
))):
# Check we have all the required fields
return False

Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
botocore>=1.10
boto3>=1.8
boto3>=1.8
requests>=2.0
requests-mock>=1.5
7 changes: 3 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from distutils.core import setup
from setuptools import find_packages

setup(
name = 'accustom',
version = '1.1.0',
Expand Down Expand Up @@ -30,9 +32,6 @@
'Programming Language :: Python :: 3.6',
],
packages = ['accustom', 'accustom.Exceptions'],
install_requires=[
'botocore>=1.10'
'boto3>=1.8'
],
install_requires=find_packages(),
python_requires='>=3, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*, <4',
)

0 comments on commit b3c6168

Please sign in to comment.