Fix tests and functionality of get_access_key_last_used()

This commit is contained in:
martynaspaulikas 2018-11-27 16:12:41 +00:00
parent 7189d019df
commit 9418a6916d
4 changed files with 43 additions and 17 deletions

View File

@ -19,6 +19,7 @@ test: lint
rm -f .coverage
rm -rf cover
@nosetests -sv --with-coverage --cover-html ./tests/ $(TEST_EXCLUDE)
test_server:
@TEST_SERVER_MODE=true nosetests -sv --with-coverage --cover-html ./tests/

View File

@ -355,10 +355,6 @@ class User(BaseModel):
def get_all_access_keys(self):
return self.access_keys
def get_access_key_last_used(self, access_key_id):
key = self.get_access_key_by_id(access_key_id)
return key.last_used
def delete_access_key(self, access_key_id):
key = self.get_access_key_by_id(access_key_id)
self.access_keys.remove(key)
@ -845,10 +841,23 @@ class IAMBackend(BaseBackend):
user = self.get_user(user_name)
user.update_access_key(access_key_id, status)
def get_access_key_last_used(self, user_name, access_key_id):
user = self.get_user(user_name)
last_used = user.get_access_key_last_used(access_key_id)
return last_used
def get_access_key_last_used(self, access_key_id):
access_keys_list = self.get_all_access_keys_for_all_users()
for key in access_keys_list:
if key.access_key_id == access_key_id:
return {
'user_name': key.user_name,
'last_used': key.last_used
}
else:
raise IAMNotFoundException(
"The Access Key with id {0} cannot be found".format(access_key_id))
def get_all_access_keys_for_all_users(self):
access_keys_list = []
for user_name in self.users:
access_keys_list += self.get_all_access_keys(user_name)
return access_keys_list
def get_all_access_keys(self, user_name, marker=None, max_items=None):
user = self.get_user(user_name)

View File

@ -455,15 +455,13 @@ class IamResponse(BaseResponse):
return template.render(name='UpdateAccessKey')
def get_access_key_last_used(self):
user_name = self._get_param('UserName')
access_key_id = self._get_param('AccessKeyId')
iam_backend.get_access_key_last_used(user_name, access_key_id)
template = self.response_template(GENERIC_EMPTY_TEMPLATE)
return template.render(name='GetAccessKeyLastUsed')
last_used_response = iam_backend.get_access_key_last_used(access_key_id)
template = self.response_template(GET_ACCESS_KEY_LAST_USED_TEMPLATE)
return template.render(user_name=last_used_response["user_name"], last_used=last_used_response["last_used"])
def list_access_keys(self):
user_name = self._get_param('UserName')
keys = iam_backend.get_all_access_keys(user_name)
template = self.response_template(LIST_ACCESS_KEYS_TEMPLATE)
return template.render(user_name=user_name, keys=keys)
@ -1247,6 +1245,18 @@ LIST_ACCESS_KEYS_TEMPLATE = """<ListAccessKeysResponse>
</ResponseMetadata>
</ListAccessKeysResponse>"""
GET_ACCESS_KEY_LAST_USED_TEMPLATE = """
<GetAccessKeyLastUsedResponse>
<GetAccessKeyLastUsedResult>
<UserName>{{ user_name }}</UserName>
<AccessKeyLastUsed>
<LastUsedDate>{{ last_used }}</LastUsedDate>
</AccessKeyLastUsed>
</GetAccessKeyLastUsedResult>
</GetAccessKeyLastUsedResponse>
"""
CREDENTIAL_REPORT_GENERATING = """
<GenerateCredentialReportResponse>
<GenerateCredentialReportResult>

View File

@ -11,6 +11,7 @@ from moto.iam.models import aws_managed_policies
from nose.tools import assert_raises, assert_equals
from nose.tools import raises
from datetime import datetime
from tests.helpers import requires_boto_gte
@ -702,10 +703,15 @@ def test_get_access_key_last_used():
username = 'test-user'
iam.create_user(UserName=username)
with assert_raises(ClientError):
client.get_access_key_last_used(UserName=username, AccessKeyId='non-existent-key')
key = client.create_access_key(UserName=username)['AccessKey']
resp = client.get_access_key_last_used(UserName=username, AccessKeyId=key['AccessKeyId'])
resp.should.equal(key.last_used)
client.get_access_key_last_used(AccessKeyId='non-existent-key-id')
create_key_response = client.create_access_key(UserName=username)['AccessKey']
resp = client.get_access_key_last_used(AccessKeyId=create_key_response['AccessKeyId'])
datetime.strftime(resp["AccessKeyLastUsed"]["LastUsedDate"], "%Y-%m-%d").should.equal(datetime.strftime(
datetime.utcnow(),
"%Y-%m-%d"
))
resp["UserName"].should.equal(create_key_response["UserName"])
@mock_iam