forked from snowflakedb/snowflake-connector-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_unit_auth.py
More file actions
215 lines (187 loc) · 5.28 KB
/
Copy pathtest_unit_auth.py
File metadata and controls
215 lines (187 loc) · 5.28 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2018 Snowflake Computing Inc. All right reserved.
#
import time
from snowflake.connector.auth import Auth
from snowflake.connector.compat import PY2
from snowflake.connector.network import (
CLIENT_NAME, CLIENT_VERSION, SnowflakeRestful)
if PY2:
from mock import MagicMock, Mock, PropertyMock
else:
from unittest.mock import MagicMock, Mock, PropertyMock
def _mock_auth_mfa_rest_response(url, headers, body, **kwargs):
"""
Success case
"""
global mock_cnt
_ = url
_ = headers
_ = body
_ = kwargs.get('dummy')
if mock_cnt == 0:
ret = {
u'success': True,
u'message': None,
u'data': {
u'nextAction': u'EXT_AUTHN_DUO_ALL',
u'inFlightCtx': u'inFlightCtx',
}
}
elif mock_cnt == 1:
ret = {
u'success': True,
u'message': None,
u'data': {
u'token': u'TOKEN',
u'masterToken': u'MASTER_TOKEN',
}
}
mock_cnt += 1
return ret
def _mock_auth_mfa_rest_response_failure(url, headers, body, **kwargs):
"""
Failure case
"""
global mock_cnt
_ = url
_ = headers
_ = body
_ = kwargs.get('dummy')
if mock_cnt == 0:
ret = {
u'success': True,
u'message': None,
u'data': {
u'nextAction': u'EXT_AUTHN_DUO_ALL',
u'inFlightCtx': u'inFlightCtx',
}
}
elif mock_cnt == 1:
ret = {
u'success': True,
u'message': None,
u'data': {
u'nextAction': u'BAD',
u'inFlightCtx': u'inFlightCtx',
}
}
mock_cnt += 1
return ret
def _mock_auth_mfa_rest_response_timeout(url, headers, body, **kwargs):
"""
Timeout case
"""
global mock_cnt
_ = url
_ = headers
_ = body
_ = kwargs.get('dummy')
if mock_cnt == 0:
ret = {
u'success': True,
u'message': None,
u'data': {
u'nextAction': u'EXT_AUTHN_DUO_ALL',
u'inFlightCtx': u'inFlightCtx',
}
}
elif mock_cnt == 1:
time.sleep(10) # should timeout while here
ret = {}
mock_cnt += 1
return ret
def _init_rest(application, post_requset):
connection = MagicMock()
connection._login_timeout = 120
connection.errorhandler = Mock(return_value=None)
type(connection).application = PropertyMock(return_value=application)
type(connection)._internal_application_name = PropertyMock(
return_value=CLIENT_NAME
)
type(connection)._internal_application_version = PropertyMock(
return_value=CLIENT_VERSION
)
rest = SnowflakeRestful(host='testaccount.snowflakecomputing.com',
port=443,
connection=connection)
rest._post_request = post_requset
return rest
def test_auth_mfa():
"""
Authentication by MFA
"""
global mock_cnt
application = 'testapplication'
account = 'testaccount'
user = 'testuser'
password = 'testpassword'
# success test case
mock_cnt = 0
rest = _init_rest(application, _mock_auth_mfa_rest_response)
auth = Auth(rest)
auth.authenticate(None, account, user, password)
assert not rest._connection.errorhandler.called # not error
assert rest.token == 'TOKEN'
assert rest.master_token == 'MASTER_TOKEN'
# failure test case
mock_cnt = 0
rest = _init_rest(application, _mock_auth_mfa_rest_response_failure)
auth = Auth(rest)
auth.authenticate(None, account, user, password)
assert rest._connection.errorhandler.called # error
# timeout 1 second
mock_cnt = 0
rest = _init_rest(application, _mock_auth_mfa_rest_response_timeout)
auth = Auth(rest)
auth.authenticate(None, account, user, password, timeout=1)
assert rest._connection.errorhandler.called # error
def _mock_auth_password_change_rest_response(url, headers, body, **kwargs):
"""
Success case
"""
global mock_cnt
_ = url
_ = headers
_ = body
_ = kwargs.get('dummy')
if mock_cnt == 0:
ret = {
u'success': True,
u'message': None,
u'data': {
u'nextAction': u'PWD_CHANGE',
u'inFlightCtx': u'inFlightCtx',
}
}
elif mock_cnt == 1:
ret = {
u'success': True,
u'message': None,
u'data': {
u'token': u'TOKEN',
u'masterToken': u'MASTER_TOKEN',
}
}
mock_cnt += 1
return ret
def test_auth_password_change():
"""
Password change
"""
global mock_cnt
def _password_callback():
return "NEW_PASSWORD"
application = 'testapplication'
account = 'testaccount'
user = 'testuser'
password = 'testpassword'
# success test case
mock_cnt = 0
rest = _init_rest(application, _mock_auth_password_change_rest_response)
auth = Auth(rest)
auth.authenticate(None, account, user, password,
password_callback=_password_callback)
assert not rest._connection.errorhandler.called # not error