|
| 1 | +from unittest import TestCase |
| 2 | +from mock import Mock, patch |
| 3 | + |
| 4 | +import serverlessrepo.permission_helper as permission_helper |
| 5 | +from serverlessrepo.application_policy import ApplicationPolicy |
| 6 | +from serverlessrepo.exceptions import InvalidApplicationPolicyError |
| 7 | + |
| 8 | + |
| 9 | +class TestPermissionHelper(TestCase): |
| 10 | + |
| 11 | + def setUp(self): |
| 12 | + self.patcher = patch('serverlessrepo.permission_helper.boto3') |
| 13 | + self.boto3_mock = self.patcher.start() |
| 14 | + self.serverlessrepo_mock = Mock() |
| 15 | + self.boto3_mock.client.return_value = self.serverlessrepo_mock |
| 16 | + self.application_id = 'arn:aws:serverlessrepo:us-east-1:123456789012:applications/test-app' |
| 17 | + self.account_ids = ['123456789012'] |
| 18 | + |
| 19 | + def test_make_application_public_succeeded(self): |
| 20 | + permission_helper.make_application_public(self.application_id) |
| 21 | + self.serverlessrepo_mock.put_application_policy.assert_called_with( |
| 22 | + ApplicationId=self.application_id, |
| 23 | + Statements=[{ |
| 24 | + 'Principals': ['*'], |
| 25 | + 'Actions': [ApplicationPolicy.DEPLOY] |
| 26 | + }] |
| 27 | + ) |
| 28 | + |
| 29 | + def test_make_application_public_exception_with_empty_application_id(self): |
| 30 | + with self.assertRaises(ValueError) as context: |
| 31 | + permission_helper.make_application_public('') |
| 32 | + |
| 33 | + message = str(context.exception) |
| 34 | + expected = 'Require application id to make the app public' |
| 35 | + self.assertEqual(expected, message) |
| 36 | + |
| 37 | + def test_make_application_private_succeeded(self): |
| 38 | + permission_helper.make_application_private(self.application_id) |
| 39 | + self.serverlessrepo_mock.put_application_policy.assert_called_with( |
| 40 | + ApplicationId=self.application_id, |
| 41 | + Statements=[] |
| 42 | + ) |
| 43 | + |
| 44 | + def test_make_application_private_exception_with_empty_application_id(self): |
| 45 | + with self.assertRaises(ValueError) as context: |
| 46 | + permission_helper.make_application_private('') |
| 47 | + |
| 48 | + message = str(context.exception) |
| 49 | + expected = 'Require application id to make the app private' |
| 50 | + self.assertEqual(expected, message) |
| 51 | + |
| 52 | + def test_share_application_with_accounts_succeeded(self): |
| 53 | + permission_helper.share_application_with_accounts(self.application_id, self.account_ids) |
| 54 | + self.serverlessrepo_mock.put_application_policy.assert_called_with( |
| 55 | + ApplicationId=self.application_id, |
| 56 | + Statements=[{ |
| 57 | + 'Principals': self.account_ids, |
| 58 | + 'Actions': [ApplicationPolicy.DEPLOY] |
| 59 | + }] |
| 60 | + ) |
| 61 | + |
| 62 | + def test_share_application_with_accounts_exception_with_empty_application_id(self): |
| 63 | + with self.assertRaises(ValueError) as context: |
| 64 | + permission_helper.share_application_with_accounts('', self.account_ids) |
| 65 | + |
| 66 | + message = str(context.exception) |
| 67 | + expected = 'Require application id and list of AWS account IDs to share the app' |
| 68 | + self.assertEqual(expected, message) |
| 69 | + |
| 70 | + def test_share_application_with_accounts_exception_with_empty_account_ids(self): |
| 71 | + with self.assertRaises(ValueError) as context: |
| 72 | + permission_helper.share_application_with_accounts(self.application_id, []) |
| 73 | + |
| 74 | + message = str(context.exception) |
| 75 | + expected = 'Require application id and list of AWS account IDs to share the app' |
| 76 | + self.assertEqual(expected, message) |
| 77 | + |
| 78 | + def test_share_application_with_accounts_exception_with_invalid_account_ids(self): |
| 79 | + with self.assertRaises(InvalidApplicationPolicyError) as context: |
| 80 | + permission_helper.share_application_with_accounts(self.application_id, ['123', '456']) |
| 81 | + |
| 82 | + message = str(context.exception) |
| 83 | + expected = 'principal should be 12-digit AWS account ID or "*"' |
| 84 | + self.assertTrue(expected in message) |
| 85 | + |
| 86 | + def tearDown(self): |
| 87 | + self.patcher.stop() |
0 commit comments