Path: blob/develop/tests/unit/customizations/test_codecommit.py
1567 views
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.1213import awscli1415from argparse import Namespace16from botocore.session import Session17from botocore.credentials import Credentials18from awscli.customizations.codecommit import CodeCommitGetCommand19from awscli.customizations.codecommit import CodeCommitCommand20from awscli.testutils import mock, unittest, StringIOWithFileNo21from awscli.compat import StringIO2223from botocore.auth import SigV4Auth24from botocore.awsrequest import AWSRequest252627class TestCodeCommitCredentialHelper(unittest.TestCase):2829PROTOCOL_HOST_PATH = ('protocol=https\n'30'host=git-codecommit.us-east-1.amazonaws.com\n'31'path=/v1/repos/myrepo')3233PROTOCOL_HOST_PATH_TRAILING_NEWLINE = ('protocol=https\n'34'host=git-codecommit.us-east-1.amazonaws.com\n'35'path=/v1/repos/myrepo\n')3637PROTOCOL_HOST_PATH_BLANK_LINE = ('protocol=https\n'38'host=git-codecommit.us-east-1.amazonaws.com\n'39'path=/v1/repos/myrepo\n\n')4041FIPS_PROTOCOL_HOST_PATH = ('protocol=https\n'42'host=git-codecommit-fips.us-east-1.amazonaws.com\n'43'path=/v1/repos/myrepo')4445VPC_1_PROTOCOL_HOST_PATH = ('protocol=https\n'46'host=vpce-0b47ea360adebf88a-jkl88hez.git-codecommit.us-east-1.vpce.amazonaws.com\n'47'path=/v1/repos/myrepo')4849VPC_2_PROTOCOL_HOST_PATH = ('protocol=https\n'50'host=vpce-0b47ea360adebf88a-jkl88hez-us-east-1a.git-codecommit.us-east-1.vpce.amazonaws.com\n'51'path=/v1/repos/myrepo')5253FIPS_VPC_1_PROTOCOL_HOST_PATH = ('protocol=https\n'54'host=vpce-0b47ea360adebf88a-jkl88hez.git-codecommit-fips.us-east-1.vpce.amazonaws.com\n'55'path=/v1/repos/myrepo')5657FIPS_VPC_2_PROTOCOL_HOST_PATH = ('protocol=https\n'58'host=vpce-0b47ea360adebf88a-jkl88hez-us-west-2b.git-codecommit-fips.us-east-1.vpce.amazonaws.com\n'59'path=/v1/repos/myrepo')6061NO_REGION_PROTOCOL_HOST_PATH = ('protocol=https\n'62'host=git-codecommit.amazonaws.com\n'63'path=/v1/repos/myrepo')6465NON_AWS_PROTOCOL_HOST_PATH = ('protocol=https\n'66'host=mydomain.com\n'67'path=/v1/repos/myrepo')6869MOCK_STDOUT_CLASS = StringIOWithFileNo7071def setUp(self):72self.credentials = Credentials('access', 'secret')73self.args = Namespace()74self.args.ignore_host_check = False75self.globals = Namespace()76self.globals.region = 'us-east-1'77self.globals.verify_ssl = False78self.session = mock.MagicMock()79self.session.get_config_variable.return_value = 'us-east-1'80self.session.get_credentials.return_value = self.credentials8182@mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)83@mock.patch('sys.stdin', StringIO(PROTOCOL_HOST_PATH))84def test_generate_credentials(self, stdout_mock):85self.get_command = CodeCommitGetCommand(self.session)86self.get_command._run_main(self.args, self.globals)87output = stdout_mock.getvalue().strip()88self.assertRegex(89output, 'username={0}\npassword=.+'.format('access'))9091@mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)92@mock.patch('sys.stdin', StringIO(PROTOCOL_HOST_PATH_TRAILING_NEWLINE))93def test_generate_credentials_trailing_newline(self, stdout_mock):94self.get_command = CodeCommitGetCommand(self.session)95self.get_command._run_main(self.args, self.globals)96output = stdout_mock.getvalue().strip()97self.assertRegex(98output, 'username={0}\npassword=.+'.format('access'))99100@mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)101@mock.patch('sys.stdin', StringIO(PROTOCOL_HOST_PATH_BLANK_LINE))102def test_generate_credentials_blank_line(self, stdout_mock):103self.get_command = CodeCommitGetCommand(self.session)104self.get_command._run_main(self.args, self.globals)105output = stdout_mock.getvalue().strip()106self.assertRegex(107output, 'username={0}\npassword=.+'.format('access'))108109@mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)110@mock.patch('sys.stdin', StringIO(FIPS_PROTOCOL_HOST_PATH))111def test_generate_credentials_fips_reads_region_from_url(self, stdout_mock):112self.globals.region = None113self.session.get_config_variable.return_value = None114self.get_command = CodeCommitGetCommand(self.session)115self.get_command._run_main(self.args, self.globals)116output = stdout_mock.getvalue().strip()117self.assertRegex(118output, 'username={0}\npassword=.+'.format('access'))119120@mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)121@mock.patch('sys.stdin', StringIO(VPC_1_PROTOCOL_HOST_PATH))122def test_generate_credentials_vpc_reads_region_from_url(self, stdout_mock):123self.globals.region = None124self.session.get_config_variable.return_value = None125self.get_command = CodeCommitGetCommand(self.session)126self.get_command._run_main(self.args, self.globals)127output = stdout_mock.getvalue().strip()128self.assertRegex(129output, 'username={0}\npassword=.+'.format('access'))130131@mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)132@mock.patch('sys.stdin', StringIO(VPC_2_PROTOCOL_HOST_PATH))133def test_generate_credentials_vpc_2_reads_region_from_url(self, stdout_mock):134self.globals.region = None135self.session.get_config_variable.return_value = None136self.get_command = CodeCommitGetCommand(self.session)137self.get_command._run_main(self.args, self.globals)138output = stdout_mock.getvalue().strip()139self.assertRegex(140output, 'username={0}\npassword=.+'.format('access'))141142@mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)143@mock.patch('sys.stdin', StringIO(FIPS_VPC_1_PROTOCOL_HOST_PATH))144def test_generate_credentials_fips_vpc_1_reads_region_from_url(self, stdout_mock):145self.globals.region = None146self.session.get_config_variable.return_value = None147self.get_command = CodeCommitGetCommand(self.session)148self.get_command._run_main(self.args, self.globals)149output = stdout_mock.getvalue().strip()150self.assertRegex(151output, 'username={0}\npassword=.+'.format('access'))152153@mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)154@mock.patch('sys.stdin', StringIO(FIPS_VPC_2_PROTOCOL_HOST_PATH))155def test_generate_credentials_fips_vpc_2_reads_region_from_url(self, stdout_mock):156self.globals.region = None157self.session.get_config_variable.return_value = None158self.get_command = CodeCommitGetCommand(self.session)159self.get_command._run_main(self.args, self.globals)160output = stdout_mock.getvalue().strip()161self.assertRegex(162output, 'username={0}\npassword=.+'.format('access'))163164@mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)165@mock.patch('sys.stdin', StringIO(NO_REGION_PROTOCOL_HOST_PATH))166def test_generate_credentials_reads_region_from_session(self, stdout_mock):167self.get_command = CodeCommitGetCommand(self.session)168self.get_command._run_main(self.args, self.globals)169output = stdout_mock.getvalue().strip()170self.assertRegex(171output, 'username={0}\npassword=.+'.format('access'))172173@mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)174@mock.patch('sys.stdin', StringIO(NON_AWS_PROTOCOL_HOST_PATH))175def test_does_nothing_for_non_amazon_domain(self, stdout_mock):176self.get_command = CodeCommitGetCommand(self.session)177self.get_command._run_main(self.args, self.globals)178output = stdout_mock.getvalue().strip()179self.assertEqual('', output)180181def test_raises_value_error_when_not_provided_any_subcommands(self):182self.get_command = CodeCommitCommand(self.session)183with self.assertRaises(ValueError):184self.get_command._run_main(self.args, self.globals)185186@mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)187@mock.patch('sys.stdin', StringIO(PROTOCOL_HOST_PATH))188def test_generate_session_credentials(self, stdout_mock):189self.credentials = Credentials('access', 'secret', 'token')190self.session.get_credentials.return_value = self.credentials191self.get_command = CodeCommitGetCommand(self.session)192self.get_command._run_main(self.args, self.globals)193output = stdout_mock.getvalue().strip()194self.assertRegex(195output,196'username={0}%{1}\npassword=.+'.format('access', 'token'))197198@mock.patch('sys.stdout', MOCK_STDOUT_CLASS())199@mock.patch('sys.stdin', StringIO(PROTOCOL_HOST_PATH))200@mock.patch('botocore.auth.SigV4Auth.string_to_sign')201@mock.patch('botocore.auth.SigV4Auth.signature')202def test_generate_credentials_creates_a_valid_request(self, signature,203string_to_sign):204self.credentials = Credentials('access', 'secret')205self.session.get_credentials.return_value = self.credentials206self.get_command = CodeCommitGetCommand(self.session)207self.get_command._run_main(self.args, self.globals)208aws_request = signature.call_args[0][1]209self.assertEqual('GIT', aws_request.method)210self.assertEqual(211'https://git-codecommit.us-east-1.amazonaws.com//v1/repos/myrepo',212aws_request.url)213self.assertEqual(214('GIT\n//v1/repos/myrepo\n\n'215'host:git-codecommit.us-east-1.amazonaws.com\n\n'216'host\n'),217string_to_sign.call_args[0][1])218219if __name__ == "__main__":220unittest.main()221222223