Showing
1 changed file
with
79 additions
and
0 deletions
tests/unit/test_role.py
0 → 100644
1 | +# -*- coding: utf-8 -*- | ||
2 | +# Copyright (c) 2015 Mitch Garnaat http://garnaat.org/ | ||
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 | + | ||
15 | +import unittest | ||
16 | +import random | ||
17 | +import string | ||
18 | +from mock import Mock, call | ||
19 | + | ||
20 | +from kappa.role import Role | ||
21 | + | ||
22 | + | ||
23 | +def randomword(length): | ||
24 | + return ''.join(random.choice(string.lowercase) for i in range(length)) | ||
25 | + | ||
26 | + | ||
27 | +class TestRole(unittest.TestCase): | ||
28 | + | ||
29 | + def setUp(self): | ||
30 | + self.iam_client = Mock() | ||
31 | + policy = type('Policy', (object,), { | ||
32 | + 'arn': None | ||
33 | + }) | ||
34 | + self.context = type('Context', (object,), { | ||
35 | + 'name': randomword(10), | ||
36 | + 'environment': randomword(10), | ||
37 | + 'policy': policy | ||
38 | + }) | ||
39 | + self.role_record = { | ||
40 | + 'RoleName': '%s_%s' % (self.context.name, | ||
41 | + self.context.environment), | ||
42 | + 'Arn': randomword(10) | ||
43 | + } | ||
44 | + self.role = Role(self.context, None, iam_client=self.iam_client) | ||
45 | + | ||
46 | + def _expect_get_role(self): | ||
47 | + get_role_resp = {'Role': self.role_record} | ||
48 | + self.iam_client.configure_mock(**{ | ||
49 | + 'call.return_value': get_role_resp | ||
50 | + }) | ||
51 | + return get_role_resp | ||
52 | + | ||
53 | + def test_delete_no_ops_if_role_not_found(self): | ||
54 | + self.iam_client.configure_mock(**{ | ||
55 | + 'call.return_value': None | ||
56 | + }) | ||
57 | + self.assertEquals(None, self.role.delete()) | ||
58 | + self.assertEquals(1, self.iam_client.call.call_count) | ||
59 | + | ||
60 | + def test_delete_when_role_exists(self): | ||
61 | + get_role_resp = self._expect_get_role() | ||
62 | + self.assertEquals(get_role_resp, self.role.delete()) | ||
63 | + self.iam_client.call.assert_has_calls([call('get_role', | ||
64 | + RoleName=self.role.name), | ||
65 | + call('delete_role', | ||
66 | + RoleName=self.role.name)]) | ||
67 | + | ||
68 | + def test_delete_policy_when_context_arn_exists(self): | ||
69 | + self.context.policy.arn = randomword(10) | ||
70 | + get_role_resp = self._expect_get_role() | ||
71 | + self.assertEquals(get_role_resp, self.role.delete()) | ||
72 | + calls = [call('get_role', | ||
73 | + RoleName=self.role.name), | ||
74 | + call('detach_role_policy', | ||
75 | + RoleName=self.role.name, | ||
76 | + PolicyArn=self.context.policy.arn), | ||
77 | + call('delete_role', | ||
78 | + RoleName=self.role.name)] | ||
79 | + self.iam_client.call.assert_has_calls(calls) |
-
Please register or login to post a comment