|
| 1 | +import unittest |
| 2 | + |
| 3 | +import os |
| 4 | +import tensorflow_gcs_config |
| 5 | +from unittest.mock import patch |
| 6 | +from test.support import EnvironmentVarGuard |
| 7 | +from kaggle_secrets import UserSecretsClient |
| 8 | + |
| 9 | +class TestTensorflowCredentials(unittest.TestCase): |
| 10 | + |
| 11 | + @patch('tensorflow_gcs_config.configure_gcs') |
| 12 | + def test_set_tensorflow_credential(self, mock_configure_gcs): |
| 13 | + credential = '{"client_id":"fake_client_id",' \ |
| 14 | + '"client_secret":"fake_client_secret",' \ |
| 15 | + '"refresh_token":"not a refresh token",' \ |
| 16 | + '"type":"authorized_user"}'; |
| 17 | + |
| 18 | + env = EnvironmentVarGuard() |
| 19 | + env.set('HOME', '/tmp') |
| 20 | + env.set('GOOGLE_APPLICATION_CREDENTIALS', '') |
| 21 | + |
| 22 | + # These need to be set to make UserSecretsClient happy, but aren't |
| 23 | + # pertinent to this test. |
| 24 | + env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') |
| 25 | + env.set('KAGGLE_KERNEL_INTEGRATIONS', 'AUTOML') |
| 26 | + |
| 27 | + user_secrets = UserSecretsClient() |
| 28 | + user_secrets.set_tensorflow_credential(credential) |
| 29 | + |
| 30 | + credential_path = '/tmp/gcloud_credential.json' |
| 31 | + self.assertEqual( |
| 32 | + credential_path, os.environ['GOOGLE_APPLICATION_CREDENTIALS']) |
| 33 | + with open(credential_path, 'r') as f: |
| 34 | + saved_cred = f.read() |
| 35 | + self.assertEqual(credential, saved_cred) |
| 36 | + |
| 37 | + mock_configure_gcs.assert_called_with(credentials=credential) |
0 commit comments