-
Notifications
You must be signed in to change notification settings - Fork 32
/
test_oneloginAWS_saml.py
120 lines (98 loc) · 3.78 KB
/
test_oneloginAWS_saml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from argparse import Namespace
from unittest import TestCase, mock
from unittest.mock import MagicMock, patch
from onelogin_aws_cli import OneloginAWS
class _MockSection(Namespace):
"""
Used to mock `onelogin_aws_cli.configuration.Section` objects
"""
def __getitem__(self, item):
return self.__getattribute__(item)
def get(self, item):
if hasattr(self, item):
return self[item]
return None
class TestOneloginSAML(TestCase):
def setUp(self):
"""
Set up mock SAML and base OneLoginAWS object
"""
self.ol = OneloginAWS(
_MockSection(
base_uri="https://api.us.onelogin.com/",
client_id='mock-id',
client_secret='mock-secret',
aws_app_id='mock-app-id',
subdomain='example',
can_save_password=False,
username='mock-username',
duration_seconds=2600
),
)
self.ol.password = "mock-password"
self.get_saml_assertion_mock = MagicMock(return_value=Namespace(
mfa=Namespace(
devices=[Namespace(type='mock1', id='mock-id-1'), ],
state_token='mock-token'
),
))
self.get_saml_assertion_verifying_mock = MagicMock(
return_value='mock-saml-response'
)
self.ol.ol_client = Namespace(
get_saml_assertion=self.get_saml_assertion_mock,
get_saml_assertion_verifying=self.get_saml_assertion_verifying_mock
)
@mock.patch('getpass.getpass')
def test_get_saml_assertion_single(self, getpw):
getpw.return_value = 'mock-password'
with patch('builtins.input', side_effect=['123456']):
self.ol.get_saml_assertion()
self.assertEqual(self.ol.saml, 'mock-saml-response')
self.get_saml_assertion_mock.assert_called_with(
'mock-username', 'mock-password',
'mock-app-id', 'example'
)
self.get_saml_assertion_verifying_mock.assert_called_with(
'mock-app-id', 'mock-id-1',
'mock-token', '123456'
)
@mock.patch('getpass.getpass')
def test_get_saml_assertion_multiple(self, getpw):
getpw.return_value = 'mock-password'
self.get_saml_assertion_mock = MagicMock(return_value=Namespace(
mfa=Namespace(
devices=[
Namespace(type='mock1', id='mock-id-1'),
Namespace(type='mock2', id='mock-id-2'),
Namespace(type='mock3', id='mock-id-3')
],
state_token='mock-token'
),
))
self.ol.ol_client.get_saml_assertion = self.get_saml_assertion_mock
with patch('builtins.input', side_effect=['2', '123456']):
self.ol.get_saml_assertion()
self.assertEqual(self.ol.saml, 'mock-saml-response')
self.get_saml_assertion_mock.assert_called_with(
'mock-username', 'mock-password',
'mock-app-id', 'example'
)
self.get_saml_assertion_verifying_mock.assert_called_with(
'mock-app-id', 'mock-id-2',
'mock-token', '123456'
)
@patch('builtins.input', side_effect=['123456'])
@mock.patch('getpass.getpass')
def test_username_prompt(self, getpw, input):
getpw.return_value = 'mock-password'
self.ol.username = None
self.ol.get_saml_assertion()
self.assertEqual(self.ol.user_credentials.username, 'mock-username')
self.assertEqual(self.ol.user_credentials.password, 'mock-password')
def tearDown(self):
"""
Reset MagicMocks
"""
self.get_saml_assertion_verifying_mock.reset_mock()
self.get_saml_assertion_mock.reset_mock()