forked from googleapis/python-bigquery-pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_auth.py
72 lines (52 loc) · 2.01 KB
/
test_auth.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
# -*- coding: utf-8 -*-
import json
from unittest import mock
import pytest
from pandas_gbq import auth
def test_get_credentials_private_key_raises_notimplementederror(monkeypatch):
private_key = json.dumps(
{
"private_key": "some_key",
"client_email": "[email protected]",
"project_id": "private-key-project",
}
)
with pytest.raises(NotImplementedError, match="private_key"):
auth.get_credentials(private_key=private_key)
def test_get_credentials_default_credentials(monkeypatch):
import google.auth
import google.auth.credentials
import google.cloud.bigquery
def mock_default_credentials(scopes=None, request=None):
return (
mock.create_autospec(google.auth.credentials.Credentials),
"default-project",
)
monkeypatch.setattr(google.auth, "default", mock_default_credentials)
credentials, project = auth.get_credentials()
assert project == "default-project"
assert credentials is not None
def test_get_credentials_load_user_no_default(monkeypatch):
import google.auth
import google.auth.credentials
import pydata_google_auth.cache
def mock_default_credentials(scopes=None, request=None):
return (None, None)
monkeypatch.setattr(google.auth, "default", mock_default_credentials)
mock_user_credentials = mock.create_autospec(
google.auth.credentials.Credentials
)
mock_cache = mock.create_autospec(
pydata_google_auth.cache.CredentialsCache
)
mock_cache.load.return_value = mock_user_credentials
monkeypatch.setattr(auth, "get_credentials_cache", lambda _: mock_cache)
credentials, project = auth.get_credentials()
assert project is None
assert credentials is mock_user_credentials
def test_get_credentials_cache_w_reauth():
import pydata_google_auth.cache
cache = auth.get_credentials_cache(True)
assert isinstance(
cache, pydata_google_auth.cache.WriteOnlyCredentialsCache
)