diff --git a/.github/workflows/ci-tests.yml b/.github/workflows/ci-tests.yml index a058f03b7..73256fccb 100644 --- a/.github/workflows/ci-tests.yml +++ b/.github/workflows/ci-tests.yml @@ -1,4 +1,3 @@ -# TODO(user): Fix or skip tests that fail on GitHub name: ci-tests on: [ push, diff --git a/python/ee/tests/ee_test.py b/python/ee/tests/ee_test.py index 59f924b09..2c3379a82 100644 --- a/python/ee/tests/ee_test.py +++ b/python/ee/tests/ee_test.py @@ -53,6 +53,53 @@ def MockAlgorithms(): self.assertEqual(ee.ApiFunction._api, {}) self.assertFalse(ee.Image._initialized) + def testProjectInitialization(self): + """Verifies that we can fetch the client project from many locations. + + This also exercises the logic in data.get_persistent_credentials. + """ + + cred_args = dict(refresh_token='rt', quota_project_id='qp1') + google_creds = credentials.Credentials(token=None, quota_project_id='qp2') + expected_project = None + + def CheckDataInit(**kwargs): + self.assertEqual(expected_project, kwargs.get('project')) + + moc = mock.patch.object + with (moc(ee.oauth, 'get_credentials_arguments', new=lambda: cred_args), + moc(ee.oauth, 'is_valid_credentials', new=lambda _: True), + moc(google.auth, 'default', new=lambda: (google_creds, None)), + moc(ee.data, 'initialize', side_effect=CheckDataInit) as inits): + expected_project = 'qp0' + ee.Initialize(project='qp0') + + expected_project = 'qp1' + ee.Initialize() + + cred_args['refresh_token'] = None + ee.Initialize() + + cred_args['quota_project_id'] = None + expected_project = 'qp2' + ee.Initialize() + + google_creds = google_creds.with_quota_project(None) + expected_project = None + ee.Initialize() + self.assertEqual(5, inits.call_count) + + msg = 'Earth Engine API has not been used in project 764086051850 before' + with moc(ee.ApiFunction, 'initialize', side_effect=ee.EEException(msg)): + with self.assertRaisesRegex(ee.EEException, '.*no project found..*'): + ee.Initialize() + + cred_args['client_id'] = '764086051850-xxx' # dummy usable-auth client + cred_args['refresh_token'] = 'rt' + with self.assertRaisesRegex(ee.EEException, '.*no project found..*'): + ee.Initialize() + self.assertEqual(6, inits.call_count) + def testCallAndApply(self): """Verifies library initialization."""