Path: blob/develop/tests/functional/rds/test_generate_db_auth_token.py
1567 views
# Copyright 2016 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.12import datetime1314from dateutil.tz import tzutc15from botocore.compat import urlparse, parse_qs1617from awscli.testutils import mock, BaseAWSCommandParamsTest181920class TestGenerateDBAuthToken(BaseAWSCommandParamsTest):2122prefix = 'rds generate-db-auth-token'2324def _urlparse(self, url):25if isinstance(url, bytes):26# Not really necessary, but it helps to reduce noise on Python 2.x27url = url.decode('utf8')28return urlparse(url)2930def assert_url_equal(self, url1, url2):31parts1 = self._urlparse(url1)32parts2 = self._urlparse(url2)3334# Because the query string ordering isn't relevant, we have to parse35# every single part manually and then handle the query string.36self.assertEqual(parts1.scheme, parts2.scheme)37self.assertEqual(parts1.netloc, parts2.netloc)38self.assertEqual(parts1.path, parts2.path)39self.assertEqual(parts1.params, parts2.params)40self.assertEqual(parts1.fragment, parts2.fragment)41self.assertEqual(parts1.username, parts2.username)42self.assertEqual(parts1.password, parts2.password)43self.assertEqual(parts1.hostname, parts2.hostname)44self.assertEqual(parts1.port, parts2.port)45self.assertEqual(parse_qs(parts1.query), parse_qs(parts2.query))4647def test_generate_simple_token(self):48command = self.prefix + ' --hostname host.us-east-1.amazonaws.com'49command += ' --port 3306 --username mySQLUser'50clock = datetime.datetime(2016, 11, 7, 17, 39, 33, tzinfo=tzutc())5152with mock.patch('datetime.datetime') as dt:53dt.now.return_value = clock54stdout, _, _ = self.run_cmd(command, expected_rc=0)5556expected = (57'host.us-east-1.amazonaws.com:3306/?DBUser=mySQLUser&'58'Action=connect&X-Amz-Credential=access_key%2F20161107%2Fus-east-1'59'%2Frds-db%2Faws4_request&X-Amz-Expires=900&X-Amz-Date=20161107T173'60'933Z&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-SignedHeaders=host&'61'X-Amz-Signature=87ab58107ef49f1c311a412f98b7f976b0b5152ffb559f0d'62'36c6c9a0c5e0e362'63)6465self.assert_url_equal(66'https://' + stdout.strip('\n'), 'https://' + expected)676869