# 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
        describe_item_request.return_value = json.loads(read_file(
            'tests/data/revoke_security_group_search.json'
        ))

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

        self.assert_ok(response)
        assert 'RevokeSecurityGroupEgressResponse' in response.data

    def test_invalid_revoke_security_group(self):
        data = self.get_example_data()
        data['Action'] = 'RevokeSecurityGroupEgress'
        data['GroupId'] = '7ae5b92f-3a0d-4977-bc33-f1aaecee5776'
        data['FromPort'] = '0'
        data['ToPort'] = '0'
        data['IpProtocol'] = 'invalid'
        data['CidrIp'] = '192.168.0.0/24'
        data['Signature'] = generate_signature(data, 'POST', 'localhost', '/')

        get = mock.Mock()
        get.return_value.text = read_file(
            'tests/data/revoke_security_group_egress.json'

# 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/security_group_tests.py
import json
import mock
from ec2stack.helpers import read_file, generate_signature
from . import Ec2StackAppTestCase

        describe_item_request.return_value = json.loads(read_file(
            'tests/data/revoke_security_group_search.json'
        ))

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

        self.assert_ok(response)
        assert 'RevokeSecurityGroupEgressResponse' in response.data

    def test_invalid_revoke_security_group(self):
        data = self.get_example_data()
        data['Action'] = 'RevokeSecurityGroupEgress'
        data['GroupId'] = '7ae5b92f-3a0d-4977-bc33-f1aaecee5776'
        data['FromPort'] = '0'
        data['ToPort'] = '0'
        data['IpProtocol'] = 'invalid'
        data['CidrIp'] = '192.168.0.0/24'
        data['Signature'] = generate_signature(data, 'POST', 'localhost', '/')

        get = mock.Mock()
        get.return_value.text = read_file(
            'tests/data/revoke_security_group_egress.json'

        )