# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest import TestCase

from aws.sts import *

__author__ = 'Douglas Anastasia'


class TestGetArnFromGoogleGroup(TestCase):
    def setUp(self):
        self.result = get_arn_from_google_group('aws_test_testgroup')

    def test_arn_format(self):
        regexTxt = 'arn:aws:iam::([0-9]{12}):role/testgroup'
        self.assertRegexpMatches(self.result, regexTxt)


class TestGetAppArnFromAccount(TestCase):
    def setUp(self):
        self.result = get_app_arn_from_account('test')

    def test_arn_format(self):
        regexTxt = 'arn:aws:iam::([0-9]{12}):role/' + settings.AWS_CONFIG['accounts']['test']['app_role_name']
        self.assertRegexpMatches(self.result, regexTxt)


class TestAssumeRole(TestCase):
    def setUp(self):
        test_arn = get_arn_from_google_group(settings.AWS_CONFIG['accounts']['test']['deploy_group'])
        self.test_role = assume_role(test_arn, 'deployment')

    def test_access_key(self):
        self.assertIsNotNone(self.test_role['access_key'], 'Access Key is None')
        self.assertEqual(len(self.test_role['access_key']), 20, 'Invalid Access Key')

    def test_url(self):
        self.assertIsNotNone(self.test_role['url'], 'URL is None')

    def test_secret_key(self):
        self.assertIsNotNone(self.test_role['secret_key'], 'Secret Key is None')
        self.assertEqual(len(self.test_role['secret_key']), 40, 'Invalid Secret Key')

    def test_session_token(self):
        self.assertIsNotNone(self.test_role['sessionToken'], 'Session Token is None')
