|
43 | 43 |
|
44 | 44 | SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, "service_account.json") |
45 | 45 |
|
| 46 | +CLIENT_SECRETS_FILE = os.path.join(DATA_DIR, "client_secrets.json") |
| 47 | + |
46 | 48 | with open(SERVICE_ACCOUNT_FILE) as fh: |
47 | 49 | SERVICE_ACCOUNT_FILE_DATA = json.load(fh) |
48 | 50 |
|
49 | 51 | LOAD_FILE_PATCH = mock.patch( |
50 | | - "google.auth._default._load_credentials_from_file", |
| 52 | + "google.auth._default.load_credentials_from_file", |
51 | 53 | return_value=(mock.sentinel.credentials, mock.sentinel.project_id), |
52 | 54 | autospec=True, |
53 | 55 | ) |
54 | 56 |
|
55 | 57 |
|
56 | | -def test__load_credentials_from_missing_file(): |
| 58 | +def test_load_credentials_from_missing_file(): |
57 | 59 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
58 | | - _default._load_credentials_from_file("") |
| 60 | + _default.load_credentials_from_file("") |
59 | 61 |
|
60 | 62 | assert excinfo.match(r"not found") |
61 | 63 |
|
62 | 64 |
|
63 | | -def test__load_credentials_from_file_invalid_json(tmpdir): |
| 65 | +def test_load_credentials_from_file_invalid_json(tmpdir): |
64 | 66 | jsonfile = tmpdir.join("invalid.json") |
65 | 67 | jsonfile.write("{") |
66 | 68 |
|
67 | 69 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
68 | | - _default._load_credentials_from_file(str(jsonfile)) |
| 70 | + _default.load_credentials_from_file(str(jsonfile)) |
69 | 71 |
|
70 | 72 | assert excinfo.match(r"not a valid json file") |
71 | 73 |
|
72 | 74 |
|
73 | | -def test__load_credentials_from_file_invalid_type(tmpdir): |
| 75 | +def test_load_credentials_from_file_invalid_type(tmpdir): |
74 | 76 | jsonfile = tmpdir.join("invalid.json") |
75 | 77 | jsonfile.write(json.dumps({"type": "not-a-real-type"})) |
76 | 78 |
|
77 | 79 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
78 | | - _default._load_credentials_from_file(str(jsonfile)) |
| 80 | + _default.load_credentials_from_file(str(jsonfile)) |
79 | 81 |
|
80 | 82 | assert excinfo.match(r"does not have a valid type") |
81 | 83 |
|
82 | 84 |
|
83 | | -def test__load_credentials_from_file_authorized_user(): |
84 | | - credentials, project_id = _default._load_credentials_from_file(AUTHORIZED_USER_FILE) |
| 85 | +def test_load_credentials_from_file_authorized_user(): |
| 86 | + credentials, project_id = _default.load_credentials_from_file(AUTHORIZED_USER_FILE) |
85 | 87 | assert isinstance(credentials, google.oauth2.credentials.Credentials) |
86 | 88 | assert project_id is None |
87 | 89 |
|
88 | 90 |
|
89 | | -def test__load_credentials_from_file_authorized_user_bad_format(tmpdir): |
| 91 | +def test_load_credentials_from_file_no_type(tmpdir): |
| 92 | + # use the client_secrets.json, which is valid json but not a |
| 93 | + # loadable credentials type |
| 94 | + with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
| 95 | + _default.load_credentials_from_file(CLIENT_SECRETS_FILE) |
| 96 | + |
| 97 | + assert excinfo.match(r"does not have a valid type") |
| 98 | + assert excinfo.match(r"Type is None") |
| 99 | + |
| 100 | + |
| 101 | +def test_load_credentials_from_file_authorized_user_bad_format(tmpdir): |
90 | 102 | filename = tmpdir.join("authorized_user_bad.json") |
91 | 103 | filename.write(json.dumps({"type": "authorized_user"})) |
92 | 104 |
|
93 | 105 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
94 | | - _default._load_credentials_from_file(str(filename)) |
| 106 | + _default.load_credentials_from_file(str(filename)) |
95 | 107 |
|
96 | 108 | assert excinfo.match(r"Failed to load authorized user") |
97 | 109 | assert excinfo.match(r"missing fields") |
98 | 110 |
|
99 | 111 |
|
100 | | -def test__load_credentials_from_file_authorized_user_cloud_sdk(): |
| 112 | +def test_load_credentials_from_file_authorized_user_cloud_sdk(): |
101 | 113 | with pytest.warns(UserWarning, match="Cloud SDK"): |
102 | | - credentials, project_id = _default._load_credentials_from_file( |
| 114 | + credentials, project_id = _default.load_credentials_from_file( |
103 | 115 | AUTHORIZED_USER_CLOUD_SDK_FILE |
104 | 116 | ) |
105 | 117 | assert isinstance(credentials, google.oauth2.credentials.Credentials) |
106 | 118 | assert project_id is None |
107 | 119 |
|
108 | 120 | # No warning if the json file has quota project id. |
109 | | - credentials, project_id = _default._load_credentials_from_file( |
| 121 | + credentials, project_id = _default.load_credentials_from_file( |
110 | 122 | AUTHORIZED_USER_CLOUD_SDK_WITH_QUOTA_PROJECT_ID_FILE |
111 | 123 | ) |
112 | 124 | assert isinstance(credentials, google.oauth2.credentials.Credentials) |
113 | 125 | assert project_id is None |
114 | 126 |
|
115 | 127 |
|
116 | | -def test__load_credentials_from_file_service_account(): |
117 | | - credentials, project_id = _default._load_credentials_from_file(SERVICE_ACCOUNT_FILE) |
| 128 | +def test_load_credentials_from_file_authorized_user_cloud_sdk_with_scopes(): |
| 129 | + with pytest.warns(UserWarning, match="Cloud SDK"): |
| 130 | + credentials, project_id = _default.load_credentials_from_file( |
| 131 | + AUTHORIZED_USER_CLOUD_SDK_FILE, |
| 132 | + scopes=["https://www.google.com/calendar/feeds"], |
| 133 | + ) |
| 134 | + assert isinstance(credentials, google.oauth2.credentials.Credentials) |
| 135 | + assert project_id is None |
| 136 | + assert credentials.scopes == ["https://www.google.com/calendar/feeds"] |
| 137 | + |
| 138 | + |
| 139 | +def test_load_credentials_from_file_service_account(): |
| 140 | + credentials, project_id = _default.load_credentials_from_file(SERVICE_ACCOUNT_FILE) |
| 141 | + assert isinstance(credentials, service_account.Credentials) |
| 142 | + assert project_id == SERVICE_ACCOUNT_FILE_DATA["project_id"] |
| 143 | + |
| 144 | + |
| 145 | +def test_load_credentials_from_file_service_account_with_scopes(): |
| 146 | + credentials, project_id = _default.load_credentials_from_file( |
| 147 | + SERVICE_ACCOUNT_FILE, scopes=["https://www.google.com/calendar/feeds"] |
| 148 | + ) |
118 | 149 | assert isinstance(credentials, service_account.Credentials) |
119 | 150 | assert project_id == SERVICE_ACCOUNT_FILE_DATA["project_id"] |
| 151 | + assert credentials.scopes == ["https://www.google.com/calendar/feeds"] |
120 | 152 |
|
121 | 153 |
|
122 | | -def test__load_credentials_from_file_service_account_bad_format(tmpdir): |
| 154 | +def test_load_credentials_from_file_service_account_bad_format(tmpdir): |
123 | 155 | filename = tmpdir.join("serivce_account_bad.json") |
124 | 156 | filename.write(json.dumps({"type": "service_account"})) |
125 | 157 |
|
126 | 158 | with pytest.raises(exceptions.DefaultCredentialsError) as excinfo: |
127 | | - _default._load_credentials_from_file(str(filename)) |
| 159 | + _default.load_credentials_from_file(str(filename)) |
128 | 160 |
|
129 | 161 | assert excinfo.match(r"Failed to load service account") |
130 | 162 | assert excinfo.match(r"missing fields") |
|
0 commit comments