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



class PasswordTestCase(Ec2StackAppTestCase):

    def test_get_password_data(self):
        data = self.get_example_data()
        data['Action'] = 'GetPasswordData'
        data['InstanceId'] = 'Test'
        data['Signature'] = generate_signature(data, 'POST', 'localhost', '/')

        get = mock.Mock()
        get.return_value.text = read_file(
            'tests/data/valid_instance_get_password.json'
        )
        get.return_value.status_code = 200

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

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

    def test_invalid_get_password(self):
        data = self.get_example_data()
        data['Action'] = 'GetPasswordData'
        data['InstanceId'] = 'Test'

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




class PasswordTestCase(Ec2StackAppTestCase):

    def test_get_password_data(self):
        data = self.get_example_data()
        data['Action'] = 'GetPasswordData'
        data['InstanceId'] = 'Test'
        data['Signature'] = generate_signature(data, 'POST', 'localhost', '/')

        get = mock.Mock()
        get.return_value.text = read_file(
            'tests/data/valid_instance_get_password.json'
        )
        get.return_value.status_code = 200

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

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

    def test_invalid_get_password(self):
        data = self.get_example_data()
        data['Action'] = 'GetPasswordData'
        data['InstanceId'] = 'Test'

        data['Signature'] = generate_signature(data, 'POST', 'localhost', '/')