Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/functional/iam/test_create_virtual_mfa_device.py
1567 views
1
#!/usr/bin/env python
2
# Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License"). You
5
# may not use this file except in compliance with the License. A copy of
6
# the License is located at
7
#
8
# http://aws.amazon.com/apache2.0/
9
#
10
# or in the "license" file accompanying this file. This file is
11
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
12
# ANY KIND, either express or implied. See the License for the specific
13
# language governing permissions and limitations under the License.
14
from awscli.testutils import BaseAWSCommandParamsTest
15
import os
16
17
18
class TestCreateVirtualMFADevice(BaseAWSCommandParamsTest):
19
20
prefix = 'iam create-virtual-mfa-device'
21
22
def setUp(self):
23
super(TestCreateVirtualMFADevice, self).setUp()
24
self.parsed_response = {
25
'ResponseMetadata': {
26
'HTTPStatusCode': 200,
27
'RequestId': 'requset-id'
28
},
29
"VirtualMFADevice": {
30
"Base32StringSeed": (
31
"VFpYTVc2V1lIUFlFRFczSVhLUlpRUTJRVFdUSFRNRDNTQ0c3"
32
"TkZDUVdQWDVETlNWM0IyUENaQVpWTEpQTlBOTA=="),
33
"SerialNumber": "arn:aws:iam::419278470775:mfa/fiebaz",
34
"QRCodePNG": (
35
"iVBORw0KGgoAAAANSUhEUgAAAPoAAAD6CAIAAAAHjs1qAAAFi"
36
"ElEQVR42u3bQW7jMBAEwPz/07vHvS0QeLpnZFVf7cgyWRTgJv"
37
"PzR+Q1+TEEgrsI7iK4i+AugrsI7iK4i+AugrsI7iK4C+4iuIv"
38
"gLoK7CO4iuIvgLoK7CO4iBe4/rfz/c391k7lLffK5v/r6tZu8"
39
"Ofu444477rjjjjvuuOOOO+6xwcoZ/WTl5D53cNXlZqG2VPpXx"
40
"h133HHHHXfccccdd9xxD09/rU7ZylZdVnvo5BY/7rjjjjvuuO"
41
"OOO+64447713H/RFIOZW0944477rjjjjvuuOOOO+64417ZVM8"
42
"ZPbLHXiOLO+6444477rjjjjvuuOP+fO5bC2lwMeQKoiN/ew0l"
43
"7rjjjjvuuOOOO+644457uCLYGmivHuxeBmcfd6/ijrtXccfdq"
44
"7jj7lXcH5han3Bkeedana9SgTvuuOOOO+6444477ri/hXttc7"
45
"umMLc2tm5ycI4GnzJfWETijjvuuOOOO+6444477s9oZnKDlZv"
46
"vGp2tnf/BNx8/NYA77rjjjjvuuOOOO+64v5V7rZkZHKzalXOw"
47
"cg3J1qorFGK444477rjjjjvuuOOO+1u5fwLrSG9T6zEGxyr3F"
48
"WqPwkeemcEdd9xxxx133HHHHXfcj3Kv4chNUo17rU7ZmtDjax"
49
"J33HHHHXfccccdd9xxfw33rWYm12McOY/wBQupZgN33HHHHXf"
50
"ccccdd9xxx/2DwTpy0Pzm2sh9oyOgc+sZd9xxxx133HHHHXfc"
51
"ccc9liOVSE3SkYIot2KPFES444477rjjjjvuuOOO+/u4DzYzg"
52
"xoGpyH35q3R2Nrb758awB133HHHHXfccccdd9zfyj23NmpX3j"
53
"omkBuc3N7+4DIr+MYdd9xxxx133HHHHXfc38q91gkcUZi7jcG"
54
"d/9xIHnkG4Y477rjjjjvuuOOOO+6430gOR20hbVUTW4cIrg0O"
55
"7rjjjjvuuOOOO+644/4a7keKi8G+6At273Nv3vpc3HHHHXfcc"
56
"ccdd9xxxx332LjXyocanUfgqJ0pqNU4uOOOO+6444477rjjjj"
57
"vuS13E1l53jc5Ndjf144477rjjjjvuuOOOO+64x77Skf352mZ"
58
"+7VhEbo62DiBE6jvccccdd9xxxx133HHHHffs9A8WNbnprz0a"
59
"tv7rYPDrP/IQAe6444477rjjjjvuuON+lPvguK//ci+TrS3Rw"
60
"Z7q5nMEd9xxxx133HHHHXfccce9VSDkippcY5A7nrBVaxyp2n"
61
"DHHXfccccdd9xxxx133FuTVFs5tUttlVq5pZJ7cCy0arjjjjv"
62
"uuOOOO+6444477tmd8K0SIDdnNXZHirj+YwV33HHHHXfccccd"
63
"d9xxfw33mu+bHUiObK4By7E7jgF33HHHHXfccccdd9xxfw33W"
64
"hcx+Kt/a1Zyn5tbZltkcccdd9xxxx133HHHHXfcb4DOnRp4Yg"
65
"eSQ/l9jRDuuOOOO+6444477rjjjntM4WCBMGhlcHByt1GrcbZ"
66
"OSeCOO+6444477rjjjjvuuD8htUMENcFbN1mrcXIfhDvuuOOO"
67
"O+6444477rjjvvSrP3cbg3NWG7pcMTV4OuPtRSTuuOOOO+644"
68
"4477rjjfoV7rXvZWjlbVr6+5CkUNbjjjjvuuOOOO+6444477q"
69
"sacn+7VeMcaYRyVdsjDxHgjjvuuOOOO+6444477rinNNR6myf"
70
"exlbXpJnBHXfccccdd9xxxx133J/PvbAFPT5nN4upLQy44447"
71
"7rjjjjvuuOOOO+4t7rWx27pybY/9iLMjvQ3uuOOOO+6444477"
72
"rjjjvvSfB/Zr86t58H53qpxBpdoYZxxxx133HHHHXfccccd99"
73
"dwF3lKcBfcRXAXwV0EdxHcRXAXwV0EdxHcRXAXwV1wF8FdBHc"
74
"R3EVwF8FdBHcR3EVwF8Fd5F/+AgASajf850wfAAAAAElFTkSu"
75
"QmCC"
76
),
77
}
78
}
79
80
def getpath(self, filename):
81
return os.path.join(os.path.abspath(os.path.dirname(__file__)),
82
filename)
83
84
def remove_file_if_exists(self, filename):
85
if os.path.isfile(filename):
86
os.remove(filename)
87
88
def test_base32(self):
89
outfile = self.getpath('fiebaz.b32')
90
self.addCleanup(self.remove_file_if_exists, outfile)
91
cmdline = self.prefix
92
cmdline += ' --virtual-mfa-device-name fiebaz'
93
cmdline += (
94
' --outfile %s --bootstrap-method Base32StringSeed' % outfile)
95
result = {"VirtualMFADeviceName": 'fiebaz'}
96
self.assert_params_for_cmd(cmdline, result)
97
self.assertTrue(os.path.exists(outfile))
98
99
def test_qrcode(self):
100
outfile = self.getpath('fiebaz.png')
101
self.addCleanup(self.remove_file_if_exists, outfile)
102
cmdline = self.prefix
103
cmdline += ' --virtual-mfa-device-name fiebaz'
104
cmdline += ' --outfile %s --bootstrap-method QRCodePNG' % outfile
105
result = {"VirtualMFADeviceName": 'fiebaz'}
106
self.assert_params_for_cmd(cmdline, result)
107
self.assertTrue(os.path.exists(outfile))
108
109
def test_bad_filename(self):
110
outfile = '/some/bad/filename.png'
111
self.addCleanup(self.remove_file_if_exists, outfile)
112
cmdline = self.prefix
113
cmdline += ' --virtual-mfa-device-name fiebaz'
114
cmdline += ' --outfile %s --bootstrap-method QRCodePNG' % outfile
115
self.assert_params_for_cmd(cmdline, expected_rc=255)
116
117
def test_relative_filename(self):
118
outfile = 'filename.png'
119
self.addCleanup(self.remove_file_if_exists, outfile)
120
cmdline = self.prefix
121
cmdline += ' --virtual-mfa-device-name fiebaz'
122
cmdline += ' --outfile %s --bootstrap-method QRCodePNG' % outfile
123
result = {"VirtualMFADeviceName": 'fiebaz'}
124
self.assert_params_for_cmd(cmdline, result)
125
self.assertTrue(os.path.exists(outfile))
126
127
def test_bad_relative_filename(self):
128
outfile = 'some/bad/filename.png'
129
self.addCleanup(self.remove_file_if_exists, outfile)
130
cmdline = self.prefix
131
cmdline += ' --virtual-mfa-device-name fiebaz'
132
cmdline += ' --outfile %s --bootstrap-method QRCodePNG' % outfile
133
self.assert_params_for_cmd(cmdline, expected_rc=255)
134
135
def test_bad_response(self):
136
# This can happen if you run the create-virtual-mfa-device
137
# command multiple times with the same name. You'll get
138
# an "already exists" error and we should handle that case
139
# gracefully.
140
self.parsed_response = {
141
'Error': {
142
'Code': 'EntityAlreadyExists',
143
'Message': 'MFADevice entity at the and name already exists.',
144
'Type': 'Sender',
145
},
146
'ResponseMetadata': {
147
'HTTPStatusCode': 409,
148
'RequestId': 'requset-id'}
149
}
150
self.http_response.status_code = 409
151
cmdline = self.prefix
152
cmdline += ' --virtual-mfa-device-name fiebaz'
153
cmdline += ' --outfile foo --bootstrap-method QRCodePNG'
154
# The error message should be in the stderr.
155
self.assert_params_for_cmd(
156
cmdline,
157
stderr_contains=self.parsed_response['Error']['Message'],
158
expected_rc=255)
159
160