generated from salesforce/oss-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_credentials.py
More file actions
420 lines (356 loc) · 16.7 KB
/
test_credentials.py
File metadata and controls
420 lines (356 loc) · 16.7 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
from __future__ import annotations
import configparser
import os
from unittest.mock import mock_open, patch
import pytest
from datacustomcode.credentials import AuthType, Credentials
class TestCredentials:
"""Test suite for Credentials class supporting multiple auth types."""
# ============== OAuth Tokens Tests (Default) ==============
def test_from_env_oauth_tokens_default(self):
"""Test loading OAuth Tokens credentials from env vars (default)."""
env_vars = {
"SFDC_LOGIN_URL": "https://test.login.url",
"SFDC_CLIENT_ID": "test_client_id",
"SFDC_CLIENT_SECRET": "test_secret",
"SFDC_REFRESH_TOKEN": "test_refresh_token",
"SFDC_ACCESS_TOKEN": "test_access_token",
}
with patch.dict(os.environ, env_vars, clear=True):
creds = Credentials.from_env()
assert creds.auth_type == AuthType.OAUTH_TOKENS
assert creds.client_secret == "test_secret"
assert creds.refresh_token == "test_refresh_token"
assert creds.access_token == "test_access_token"
assert creds.client_id == "test_client_id"
assert creds.login_url == "https://test.login.url"
def test_from_env_oauth_tokens_explicit(self):
"""Test loading OAuth Tokens credentials with explicit auth type."""
env_vars = {
"SFDC_LOGIN_URL": "https://test.login.url",
"SFDC_CLIENT_ID": "test_client_id",
"SFDC_AUTH_TYPE": "oauth_tokens",
"SFDC_CLIENT_SECRET": "test_secret",
"SFDC_REFRESH_TOKEN": "test_refresh_token",
}
with patch.dict(os.environ, env_vars, clear=True):
creds = Credentials.from_env()
assert creds.auth_type == AuthType.OAUTH_TOKENS
assert creds.client_secret == "test_secret"
assert creds.refresh_token == "test_refresh_token"
def test_from_ini_oauth_tokens(self):
"""Test loading OAuth Tokens credentials from an INI file."""
ini_content = """
[oauth_profile]
auth_type = oauth_tokens
login_url = https://oauth.login.url
client_id = oauth_client_id
client_secret = oauth_secret
refresh_token = oauth_refresh_token
access_token = oauth_access_token
"""
with (
patch("os.path.exists", return_value=True),
patch("builtins.open", mock_open(read_data=ini_content)),
):
mock_config = configparser.ConfigParser()
mock_config.read_string(ini_content)
with patch.object(configparser, "ConfigParser", return_value=mock_config):
creds = Credentials.from_ini(
profile="oauth_profile", ini_file="fake_path"
)
assert creds.auth_type == AuthType.OAUTH_TOKENS
assert creds.client_secret == "oauth_secret"
assert creds.refresh_token == "oauth_refresh_token"
assert creds.access_token == "oauth_access_token"
assert creds.client_id == "oauth_client_id"
def test_from_ini_default_auth_type(self):
"""Test that INI files without auth_type default to oauth_tokens."""
ini_content = """
[default]
login_url = https://ini.login.url
client_id = ini_client_id
client_secret = ini_secret
refresh_token = ini_refresh_token
"""
with (
patch("os.path.exists", return_value=True),
patch("builtins.open", mock_open(read_data=ini_content)),
):
mock_config = configparser.ConfigParser()
mock_config.read_string(ini_content)
with patch.object(configparser, "ConfigParser", return_value=mock_config):
creds = Credentials.from_ini(profile="default", ini_file="fake_path")
assert creds.auth_type == AuthType.OAUTH_TOKENS
assert creds.client_secret == "ini_secret"
assert creds.refresh_token == "ini_refresh_token"
def test_from_ini_client_credentials(self):
"""Test loading client credentials auth from an INI file."""
ini_content = """
[default]
auth_type = client_credentials
login_url = https://ini.login.url
client_id = ini_client_id
client_secret = ini_secret
"""
with (
patch("os.path.exists", return_value=True),
patch("builtins.open", mock_open(read_data=ini_content)),
):
mock_config = configparser.ConfigParser()
mock_config.read_string(ini_content)
with patch.object(configparser, "ConfigParser", return_value=mock_config):
creds = Credentials.from_ini(profile="default", ini_file="fake_path")
assert creds.auth_type == AuthType.CLIENT_CREDENTIALS
assert creds.client_id == "ini_client_id"
assert creds.client_secret == "ini_secret"
assert creds.login_url == "https://ini.login.url"
def test_oauth_tokens_missing_refresh_token(self):
"""Test that OAuth Tokens auth requires refresh token."""
with pytest.raises(ValueError, match="refresh_token"):
Credentials(
login_url="https://test.login.url",
client_id="test_client_id",
auth_type=AuthType.OAUTH_TOKENS,
client_secret="test_secret",
)
def test_oauth_tokens_missing_client_secret(self):
"""Test that OAuth Tokens auth requires client secret."""
with pytest.raises(TypeError, match="client_secret"):
Credentials(
login_url="https://test.login.url",
client_id="test_client_id",
auth_type=AuthType.OAUTH_TOKENS,
refresh_token="test_refresh_token",
)
def test_from_env_missing_vars(self):
"""Test that missing environment variables raise appropriate error."""
with patch.dict(os.environ, {}, clear=True):
with pytest.raises(ValueError, match="SFDC_LOGIN_URL and SFDC_CLIENT_ID"):
Credentials.from_env()
def test_from_env_client_credentials(self):
"""Test loading client credentials auth from environment variables."""
env_vars = {
"SFDC_LOGIN_URL": "https://test.login.url",
"SFDC_CLIENT_ID": "test_client_id",
"SFDC_AUTH_TYPE": "client_credentials",
"SFDC_CLIENT_SECRET": "test_secret",
}
with patch.dict(os.environ, env_vars, clear=True):
creds = Credentials.from_env()
assert creds.auth_type == AuthType.CLIENT_CREDENTIALS
assert creds.client_id == "test_client_id"
assert creds.client_secret == "test_secret"
# ============== from_available Tests ==============
def test_from_available_env(self):
"""Test that from_available uses environment variables when available."""
env_vars = {
"SFDC_LOGIN_URL": "https://test.login.url",
"SFDC_CLIENT_ID": "test_client_id",
"SFDC_CLIENT_SECRET": "test_secret",
"SFDC_REFRESH_TOKEN": "test_refresh_token",
}
with (
patch.dict(os.environ, env_vars, clear=True),
patch("os.path.exists", return_value=False),
):
creds = Credentials.from_available()
assert creds.auth_type == AuthType.OAUTH_TOKENS
assert creds.client_id == "test_client_id"
assert creds.client_secret == "test_secret"
assert creds.refresh_token == "test_refresh_token"
assert creds.login_url == "https://test.login.url"
def test_from_available_ini(self):
"""Test that from_available uses INI file when env vars not available."""
ini_content = """
[default]
auth_type = oauth_tokens
login_url = https://ini.login.url
client_id = ini_client_id
client_secret = ini_secret
refresh_token = ini_refresh_token
"""
with (
patch.dict(os.environ, {}, clear=True),
patch("os.path.exists", return_value=True),
patch("builtins.open", mock_open(read_data=ini_content)),
):
mock_config = configparser.ConfigParser()
mock_config.read_string(ini_content)
with patch.object(configparser, "ConfigParser", return_value=mock_config):
creds = Credentials.from_available()
assert creds.auth_type == AuthType.OAUTH_TOKENS
assert creds.client_id == "ini_client_id"
assert creds.client_secret == "ini_secret"
assert creds.refresh_token == "ini_refresh_token"
assert creds.login_url == "https://ini.login.url"
def test_from_available_no_creds(self):
"""Test that from_available raises error when no credentials are found."""
with (
patch.dict(os.environ, {}, clear=True),
patch("os.path.exists", return_value=False),
):
with pytest.raises(ValueError, match="Credentials not found"):
Credentials.from_available()
# ============== update_ini Tests ==============
def test_update_ini_oauth_tokens(self):
"""Test updating OAuth Tokens credentials in an INI file."""
ini_content = """
[default]
auth_type = oauth_tokens
login_url = https://old.login.url
client_id = old_client_id
client_secret = old_secret
refresh_token = old_refresh_token
"""
creds = Credentials(
login_url="https://new.login.url",
client_id="new_client_id",
auth_type=AuthType.OAUTH_TOKENS,
client_secret="new_secret",
refresh_token="new_refresh_token",
access_token="new_access_token",
)
mock_file = mock_open(read_data=ini_content)
with (
patch("os.path.expanduser", return_value="/fake/expanded/path"),
patch("os.path.exists", return_value=True),
patch("os.makedirs"),
patch("builtins.open", mock_file),
):
mock_config = configparser.ConfigParser()
mock_config.read_string(ini_content)
with patch.object(configparser, "ConfigParser", return_value=mock_config):
creds.update_ini(profile="default", ini_file="~/fake_path")
mock_file.assert_called_with("/fake/expanded/path", "w")
assert mock_config["default"]["auth_type"] == "oauth_tokens"
assert mock_config["default"]["client_id"] == "new_client_id"
assert mock_config["default"]["client_secret"] == "new_secret"
assert mock_config["default"]["refresh_token"] == "new_refresh_token"
assert mock_config["default"]["access_token"] == "new_access_token"
assert mock_config["default"]["login_url"] == "https://new.login.url"
def test_update_ini_client_credentials(self):
"""Test updating client credentials auth in an INI file."""
ini_content = """
[default]
auth_type = client_credentials
login_url = https://old.login.url
client_id = old_client_id
client_secret = old_secret
"""
creds = Credentials(
login_url="https://new.login.url",
client_id="new_client_id",
auth_type=AuthType.CLIENT_CREDENTIALS,
client_secret="new_secret",
)
mock_file = mock_open(read_data=ini_content)
with (
patch("os.path.expanduser", return_value="/fake/expanded/path"),
patch("os.path.exists", return_value=True),
patch("os.makedirs"),
patch("builtins.open", mock_file),
):
mock_config = configparser.ConfigParser()
mock_config.read_string(ini_content)
with patch.object(configparser, "ConfigParser", return_value=mock_config):
creds.update_ini(profile="default", ini_file="~/fake_path")
mock_file.assert_called_with("/fake/expanded/path", "w")
assert mock_config["default"]["auth_type"] == "client_credentials"
assert mock_config["default"]["client_id"] == "new_client_id"
assert mock_config["default"]["client_secret"] == "new_secret"
assert mock_config["default"]["login_url"] == "https://new.login.url"
def test_update_ini_new_profile(self):
"""Test updating credentials with a new profile."""
ini_content = """
[existing]
auth_type = oauth_tokens
login_url = https://existing.login.url
client_id = existing_client_id
client_secret = existing_secret
refresh_token = existing_refresh_token
"""
creds = Credentials(
login_url="https://new.profile.login.url",
client_id="new_profile_client_id",
auth_type=AuthType.OAUTH_TOKENS,
client_secret="new_profile_secret",
refresh_token="new_profile_refresh_token",
)
mock_file = mock_open(read_data=ini_content)
with (
patch("os.path.expanduser", return_value="/fake/expanded/path"),
patch("os.path.exists", return_value=True),
patch("os.makedirs"),
patch("builtins.open", mock_file),
):
mock_config = configparser.ConfigParser()
mock_config.read_string(ini_content)
with patch.object(configparser, "ConfigParser", return_value=mock_config):
creds.update_ini(profile="new_profile", ini_file="~/fake_path")
assert "new_profile" in mock_config
assert (
mock_config["new_profile"]["client_id"] == "new_profile_client_id"
)
assert (
mock_config["new_profile"]["client_secret"] == "new_profile_secret"
)
assert (
mock_config["new_profile"]["refresh_token"]
== "new_profile_refresh_token"
)
assert (
mock_config["new_profile"]["login_url"]
== "https://new.profile.login.url"
)
assert (
mock_config["existing"]["refresh_token"] == "existing_refresh_token"
)
def test_from_available_with_custom_profile(self):
"""Test that from_available uses custom profile when specified."""
ini_content = """
[default]
auth_type = oauth_tokens
login_url = https://default.login.url
client_id = default_client_id
client_secret = default_secret
refresh_token = default_refresh_token
[custom_profile]
auth_type = oauth_tokens
login_url = https://custom.login.url
client_id = custom_client_id
client_secret = custom_secret
refresh_token = custom_refresh_token
"""
with (
patch("datacustomcode.credentials.INI_FILE", "fake_path"),
patch("os.path.exists", return_value=True),
patch("builtins.open", mock_open(read_data=ini_content)),
):
mock_config = configparser.ConfigParser()
mock_config.read_string(ini_content)
with patch.object(configparser, "ConfigParser", return_value=mock_config):
creds_default = Credentials.from_available()
assert creds_default.client_secret == "default_secret"
assert creds_default.refresh_token == "default_refresh_token"
assert creds_default.login_url == "https://default.login.url"
creds_custom = Credentials.from_available(profile="custom_profile")
assert creds_custom.client_id == "custom_client_id"
assert creds_custom.client_secret == "custom_secret"
assert creds_custom.refresh_token == "custom_refresh_token"
assert creds_custom.login_url == "https://custom.login.url"
# ============== AuthType Enum Tests ==============
def test_auth_type_values(self):
"""Test AuthType enum values."""
assert AuthType.OAUTH_TOKENS.value == "oauth_tokens"
assert AuthType.CLIENT_CREDENTIALS.value == "client_credentials"
def test_invalid_auth_type_from_env(self):
"""Test that invalid auth type from env raises error."""
env_vars = {
"SFDC_LOGIN_URL": "https://test.login.url",
"SFDC_CLIENT_ID": "test_client_id",
"SFDC_AUTH_TYPE": "invalid_auth_type",
}
with patch.dict(os.environ, env_vars, clear=True):
with pytest.raises(ValueError, match="Invalid SFDC_AUTH_TYPE"):
Credentials.from_env()