# Path: ec2stack/helpers.py
# def read_file(name):
#     """
#     Reads the given file.
# 
#     @param name: Filename of the file to read.
#     @return: Contents of the file.
#     """
#     filepath = os.path.join(
#         os.path.dirname(os.path.realpath(__file__)),
#         '../',
#         name
#     )
#     data = open(filepath)
#     return data.read()
# 
# def generate_signature(data=None, method=None, host=None, path=None):
#     """
#     Generates a signature.
# 
#     @param data: Data of the request.
#     @param method: HTTP method used.
#     @param host: HTTP post.
#     @param path: HTTP hort.
#     @return: A signature.
#     """
#     if data is None:
#         data = request.form
# 
#     signature_type = get('SignatureMethod', data)
# 
#     secretkey = get_secretkey(data)
#     request_string = _get_request_string(data, method, host, path)
# 
#     if signature_type == 'HmacSHA1':
#         digestmod = hashlib.sha1
#     else:
#         digestmod = hashlib.sha256
# 
#     signature = hmac.new(
#         key=secretkey,
#         msg=bytes(request_string),
#         digestmod=digestmod
#     ).digest()
# 
#     signature = b64encode(signature)
# 
#     return signature


import json
import mock
from ec2stack.helpers import read_file, generate_signature
from . import Ec2StackAppTestCase
        with mock.patch('requests.get', get):
            with mock.patch(
                    'ec2stack.providers.cloudstack.service_offerings.get_service_offering',
                    get_service_offering
            ):
                with mock.patch(
                        'ec2stack.providers.cloudstack.zones.get_zone',
                        get_zone
                ):
                    response = self.post(
                        '/',
                        data=data
                    )

        self.assert_bad_request(response)
        assert 'InvalidAMIID.NotFound' in response.data

    def test_run_instance_invalid_security_group(self):
        data = self.get_example_data()
        data['Action'] = 'RunInstances'
        data['ImageId'] = 'a32d70ee-95e4-11e3-b2e4-d19c9d3e5e1d'
        data['MinCount'] = '0'
        data['MaxCount'] = '0'
        data['SecurityGroupId.1'] = 'invalid-security-group-id'
        data['SecurityGroup.1'] = 'invalid-security-group-name'
        data['KeyName'] = 'example-ssh-key-name'
        data['UserData'] = 'example-user-data'
        data['Signature'] = generate_signature(data, 'POST', 'localhost', '/')

        get = mock.Mock()

# Path: ec2stack/helpers.py
# def read_file(name):
#     """
#     Reads the given file.
# 
#     @param name: Filename of the file to read.
#     @return: Contents of the file.
#     """
#     filepath = os.path.join(
#         os.path.dirname(os.path.realpath(__file__)),
#         '../',
#         name
#     )
#     data = open(filepath)
#     return data.read()
# 
# def generate_signature(data=None, method=None, host=None, path=None):
#     """
#     Generates a signature.
# 
#     @param data: Data of the request.
#     @param method: HTTP method used.
#     @param host: HTTP post.
#     @param path: HTTP hort.
#     @return: A signature.
#     """
#     if data is None:
#         data = request.form
# 
#     signature_type = get('SignatureMethod', data)
# 
#     secretkey = get_secretkey(data)
#     request_string = _get_request_string(data, method, host, path)
# 
#     if signature_type == 'HmacSHA1':
#         digestmod = hashlib.sha1
#     else:
#         digestmod = hashlib.sha256
# 
#     signature = hmac.new(
#         key=secretkey,
#         msg=bytes(request_string),
#         digestmod=digestmod
#     ).digest()
# 
#     signature = b64encode(signature)
# 
#     return signature


# Path: tests/instances_tests.py
import json
import mock
from ec2stack.helpers import read_file, generate_signature
from . import Ec2StackAppTestCase

        with mock.patch('requests.get', get):
            with mock.patch(
                    'ec2stack.providers.cloudstack.service_offerings.get_service_offering',
                    get_service_offering
            ):
                with mock.patch(
                        'ec2stack.providers.cloudstack.zones.get_zone',
                        get_zone
                ):
                    response = self.post(
                        '/',
                        data=data
                    )

        self.assert_bad_request(response)
        assert 'InvalidAMIID.NotFound' in response.data

    def test_run_instance_invalid_security_group(self):
        data = self.get_example_data()
        data['Action'] = 'RunInstances'
        data['ImageId'] = 'a32d70ee-95e4-11e3-b2e4-d19c9d3e5e1d'
        data['MinCount'] = '0'
        data['MaxCount'] = '0'
        data['SecurityGroupId.1'] = 'invalid-security-group-id'
        data['SecurityGroup.1'] = 'invalid-security-group-name'
        data['KeyName'] = 'example-ssh-key-name'
        data['UserData'] = 'example-user-data'
        data['Signature'] = generate_signature(data, 'POST', 'localhost', '/')

        get = mock.Mock()

        get.return_value.text = read_file(