File: test_codecommit.py

package info (click to toggle)
awscli 2.31.35-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156,692 kB
  • sloc: python: 213,816; xml: 14,082; makefile: 189; sh: 178; javascript: 8
file content (246 lines) | stat: -rw-r--r-- 10,321 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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 argparse import Namespace

from botocore.auth import SigV4Auth
from botocore.awsrequest import AWSRequest
from botocore.credentials import Credentials
from botocore.session import Session

import awscli
from awscli.compat import StringIO
from awscli.customizations.codecommit import (
    CodeCommitCommand,
    CodeCommitGetCommand,
)
from awscli.customizations.exceptions import ParamValidationError
from awscli.testutils import StringIOWithFileNo, mock, unittest


class TestCodeCommitCredentialHelper(unittest.TestCase):
    PROTOCOL_HOST_PATH = (
        'protocol=https\n'
        'host=git-codecommit.us-east-1.amazonaws.com\n'
        'path=/v1/repos/myrepo'
    )

    PROTOCOL_HOST_PATH_TRAILING_NEWLINE = (
        'protocol=https\n'
        'host=git-codecommit.us-east-1.amazonaws.com\n'
        'path=/v1/repos/myrepo\n'
    )

    PROTOCOL_HOST_PATH_BLANK_LINE = (
        'protocol=https\n'
        'host=git-codecommit.us-east-1.amazonaws.com\n'
        'path=/v1/repos/myrepo\n\n'
    )

    FIPS_PROTOCOL_HOST_PATH = (
        'protocol=https\n'
        'host=git-codecommit-fips.us-east-1.amazonaws.com\n'
        'path=/v1/repos/myrepo'
    )

    VPC_1_PROTOCOL_HOST_PATH = (
        'protocol=https\n'
        'host=vpce-0b47ea360adebf88a-jkl88hez.git-codecommit.us-east-1.vpce.amazonaws.com\n'
        'path=/v1/repos/myrepo'
    )

    VPC_2_PROTOCOL_HOST_PATH = (
        'protocol=https\n'
        'host=vpce-0b47ea360adebf88a-jkl88hez-us-east-1a.git-codecommit.us-east-1.vpce.amazonaws.com\n'
        'path=/v1/repos/myrepo'
    )

    FIPS_VPC_1_PROTOCOL_HOST_PATH = (
        'protocol=https\n'
        'host=vpce-0b47ea360adebf88a-jkl88hez.git-codecommit-fips.us-east-1.vpce.amazonaws.com\n'
        'path=/v1/repos/myrepo'
    )

    FIPS_VPC_2_PROTOCOL_HOST_PATH = (
        'protocol=https\n'
        'host=vpce-0b47ea360adebf88a-jkl88hez-us-west-2b.git-codecommit-fips.us-east-1.vpce.amazonaws.com\n'
        'path=/v1/repos/myrepo'
    )

    NO_REGION_PROTOCOL_HOST_PATH = (
        'protocol=https\n'
        'host=git-codecommit.amazonaws.com\n'
        'path=/v1/repos/myrepo'
    )

    NON_AWS_PROTOCOL_HOST_PATH = (
        'protocol=https\n' 'host=mydomain.com\n' 'path=/v1/repos/myrepo'
    )

    MOCK_STDOUT_CLASS = StringIOWithFileNo

    def setUp(self):
        self.credentials = Credentials('access', 'secret')
        self.args = Namespace()
        self.args.ignore_host_check = False
        self.globals = Namespace()
        self.globals.region = 'us-east-1'
        self.globals.verify_ssl = False
        self.session = mock.MagicMock()
        self.session.get_config_variable.return_value = 'us-east-1'
        self.session.get_credentials.return_value = self.credentials

    @mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)
    @mock.patch('sys.stdin', StringIO(PROTOCOL_HOST_PATH))
    def test_generate_credentials(self, stdout_mock):
        self.get_command = CodeCommitGetCommand(self.session)
        self.get_command._run_main(self.args, self.globals)
        output = stdout_mock.getvalue().strip()
        self.assertRegex(output, 'username={0}\npassword=.+'.format('access'))

    @mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)
    @mock.patch('sys.stdin', StringIO(PROTOCOL_HOST_PATH_TRAILING_NEWLINE))
    def test_generate_credentials_trailing_newline(self, stdout_mock):
        self.get_command = CodeCommitGetCommand(self.session)
        self.get_command._run_main(self.args, self.globals)
        output = stdout_mock.getvalue().strip()
        self.assertRegex(output, 'username={0}\npassword=.+'.format('access'))

    @mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)
    @mock.patch('sys.stdin', StringIO(PROTOCOL_HOST_PATH_BLANK_LINE))
    def test_generate_credentials_blank_line(self, stdout_mock):
        self.get_command = CodeCommitGetCommand(self.session)
        self.get_command._run_main(self.args, self.globals)
        output = stdout_mock.getvalue().strip()
        self.assertRegex(output, 'username={0}\npassword=.+'.format('access'))

    @mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)
    @mock.patch('sys.stdin', StringIO(FIPS_PROTOCOL_HOST_PATH))
    def test_generate_credentials_fips_reads_region_from_url(
        self, stdout_mock
    ):
        self.globals.region = None
        self.session.get_config_variable.return_value = None
        self.get_command = CodeCommitGetCommand(self.session)
        self.get_command._run_main(self.args, self.globals)
        output = stdout_mock.getvalue().strip()
        self.assertRegex(output, 'username={0}\npassword=.+'.format('access'))

    @mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)
    @mock.patch('sys.stdin', StringIO(VPC_1_PROTOCOL_HOST_PATH))
    def test_generate_credentials_vpc_reads_region_from_url(self, stdout_mock):
        self.globals.region = None
        self.session.get_config_variable.return_value = None
        self.get_command = CodeCommitGetCommand(self.session)
        self.get_command._run_main(self.args, self.globals)
        output = stdout_mock.getvalue().strip()
        self.assertRegex(output, 'username={0}\npassword=.+'.format('access'))

    @mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)
    @mock.patch('sys.stdin', StringIO(VPC_2_PROTOCOL_HOST_PATH))
    def test_generate_credentials_vpc_2_reads_region_from_url(
        self, stdout_mock
    ):
        self.globals.region = None
        self.session.get_config_variable.return_value = None
        self.get_command = CodeCommitGetCommand(self.session)
        self.get_command._run_main(self.args, self.globals)
        output = stdout_mock.getvalue().strip()
        self.assertRegex(output, 'username={0}\npassword=.+'.format('access'))

    @mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)
    @mock.patch('sys.stdin', StringIO(FIPS_VPC_1_PROTOCOL_HOST_PATH))
    def test_generate_credentials_fips_vpc_1_reads_region_from_url(
        self, stdout_mock
    ):
        self.globals.region = None
        self.session.get_config_variable.return_value = None
        self.get_command = CodeCommitGetCommand(self.session)
        self.get_command._run_main(self.args, self.globals)
        output = stdout_mock.getvalue().strip()
        self.assertRegex(output, 'username={0}\npassword=.+'.format('access'))

    @mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)
    @mock.patch('sys.stdin', StringIO(FIPS_VPC_2_PROTOCOL_HOST_PATH))
    def test_generate_credentials_fips_vpc_2_reads_region_from_url(
        self, stdout_mock
    ):
        self.globals.region = None
        self.session.get_config_variable.return_value = None
        self.get_command = CodeCommitGetCommand(self.session)
        self.get_command._run_main(self.args, self.globals)
        output = stdout_mock.getvalue().strip()
        self.assertRegex(output, 'username={0}\npassword=.+'.format('access'))

    @mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)
    @mock.patch('sys.stdin', StringIO(NO_REGION_PROTOCOL_HOST_PATH))
    def test_generate_credentials_reads_region_from_session(self, stdout_mock):
        self.get_command = CodeCommitGetCommand(self.session)
        self.get_command._run_main(self.args, self.globals)
        output = stdout_mock.getvalue().strip()
        self.assertRegex(output, 'username={0}\npassword=.+'.format('access'))

    @mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)
    @mock.patch('sys.stdin', StringIO(NON_AWS_PROTOCOL_HOST_PATH))
    def test_does_nothing_for_non_amazon_domain(self, stdout_mock):
        self.get_command = CodeCommitGetCommand(self.session)
        self.get_command._run_main(self.args, self.globals)
        output = stdout_mock.getvalue().strip()
        self.assertEqual('', output)

    def test_raises_validation_error_when_not_provided_any_subcommands(self):
        self.get_command = CodeCommitCommand(self.session)
        with self.assertRaises(ParamValidationError):
            self.get_command._run_main(self.args, self.globals)

    @mock.patch('sys.stdout', new_callable=MOCK_STDOUT_CLASS)
    @mock.patch('sys.stdin', StringIO(PROTOCOL_HOST_PATH))
    def test_generate_session_credentials(self, stdout_mock):
        self.credentials = Credentials('access', 'secret', 'token')
        self.session.get_credentials.return_value = self.credentials
        self.get_command = CodeCommitGetCommand(self.session)
        self.get_command._run_main(self.args, self.globals)
        output = stdout_mock.getvalue().strip()
        self.assertRegex(
            output, 'username={0}%{1}\npassword=.+'.format('access', 'token')
        )

    @mock.patch('sys.stdout', MOCK_STDOUT_CLASS())
    @mock.patch('sys.stdin', StringIO(PROTOCOL_HOST_PATH))
    @mock.patch('botocore.auth.SigV4Auth.string_to_sign')
    @mock.patch('botocore.auth.SigV4Auth.signature')
    def test_generate_credentials_creates_a_valid_request(
        self, signature, string_to_sign
    ):
        self.credentials = Credentials('access', 'secret')
        self.session.get_credentials.return_value = self.credentials
        self.get_command = CodeCommitGetCommand(self.session)
        self.get_command._run_main(self.args, self.globals)
        aws_request = signature.call_args[0][1]
        self.assertEqual('GIT', aws_request.method)
        self.assertEqual(
            'https://git-codecommit.us-east-1.amazonaws.com//v1/repos/myrepo',
            aws_request.url,
        )
        self.assertEqual(
            (
                'GIT\n//v1/repos/myrepo\n\n'
                'host:git-codecommit.us-east-1.amazonaws.com\n\n'
                'host\n'
            ),
            string_to_sign.call_args[0][1],
        )


if __name__ == "__main__":
    unittest.main()