def test_project_with_connected_account_default_credentials(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'BIGQUERY') with env: client = bigquery.Client(project='ANOTHER_PROJECT') self._test_proxy(client, should_use_proxy=False)
def test_project_with_connected_account(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') with env: client = bigquery.Client( project='ANOTHER_PROJECT', credentials=KaggleKernelCredentials()) self._test_proxy(client, should_use_proxy=False)
def _test_client(self, client_func, expected_path, expected_body, secret=None, success=True): _request = {} class AccessTokenHandler(UserSecretsHTTPHandler): def set_request(self): _request['path'] = self.path content_len = int(self.headers.get('Content-Length')) _request['body'] = json.loads(self.rfile.read(content_len)) _request['headers'] = self.headers def get_response(self): if success: return { 'result': { 'secret': secret, 'secretType': 'refreshToken', 'secretProvider': 'google', 'expiresInSeconds': 3600 }, 'wasSuccessful': "true" } else: return { 'wasSuccessful': "false", 'errors': ['No user secrets exist for kernel'] } env = EnvironmentVarGuard() env.set(_KAGGLE_USER_SECRETS_TOKEN_ENV_VAR_NAME, _TEST_JWT) with env: with HTTPServer( (self.SERVER_ADDRESS.hostname, self.SERVER_ADDRESS.port), AccessTokenHandler) as httpd: threading.Thread(target=httpd.serve_forever).start() try: client_func() finally: httpd.shutdown() path, headers, body = _request['path'], _request[ 'headers'], _request['body'] self.assertEqual( path, expected_path, msg= "Fake server did not receive the right request from the UserSecrets client." ) self.assertEqual( body, expected_body, msg= "Fake server did not receive the right body from the UserSecrets client." )
def test_project_with_connected_account_default_credentials(self, mock_access_token, ApiUrlMock): self._setup_mocks(ApiUrlMock) env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'BIGQUERY') with env: client = bigquery.Client(project='ANOTHER_PROJECT') self._test_integration(client)
def test_can_manage_config_with_interpolated_variables_coming_from_environment_variables(self): project_dir = os.path.join(FIXTURE_ROOT, 'project_with_dynamic_variables') env = EnvironmentVarGuard() with env: env.set('DUMMY_VAR_01', 'test01') config = Config.from_base_dir(project_dir) assert config['provisioning'][0]['inline'] == 'touch /opt/test01' assert config['provisioning'][1]['inline'] == 'echo $thisisatest'
def test_project_with_connected_account_unrelated_integrations(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'GCS:ANOTHER_ONE') with env: client = bigquery.Client( project='ANOTHER_PROJECT', credentials=KaggleKernelCredentials()) self._test_proxy(client, should_use_proxy=False)
def test_monkeypatching_succeed(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'CLOUDAI') with env: init_natural_language() client = language.LanguageServiceClient.__init__ self.assertTrue("kaggle_gcp" in inspect.getsourcefile(client))
def test_annonymous_client(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'GCS') with env: init_gcs() anonymous = storage.Client.create_anonymous_client() self.assertIsNotNone(anonymous)
def test_monkeypatching_succeed(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'CLOUDAI') with env: init_vision() client = vision.ImageAnnotatorClient.__init__ self.assertTrue("kaggle_gcp" in inspect.getsourcefile(client))
def test_default_credentials_gcs_enabled(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'GCS') with env: init_gcs() client = storage.Client(project="xyz") self.assertIsInstance(client._credentials, KaggleKernelCredentials)
def test_project_with_empty_integrations(self): env = EnvironmentVarGuard() env.set('KAGGLE_BQ_USER_JWT', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', '') with env: client = bigquery.Client(project='ANOTHER_PROJECT', credentials=KaggleKernelCredentials()) self._test_proxy(client, should_use_proxy=False)
def test_monkeypatching_succeed(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'TRANSLATION') with env: init_video_intelligence() client = videointelligence.VideoIntelligenceServiceClient.__init__ self.assertTrue("kaggle_gcp" in inspect.getsourcefile(client))
def test_magics_without_connected_account(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') with env: import sitecustomize sitecustomize.init() from google.cloud.bigquery import magics self.assertIsNone(magics.context._credentials)
def test_project_with_connected_account_default_credentials(self): env = EnvironmentVarGuard() env.set('KAGGLE_BQ_USER_JWT', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'GCS:ANOTHER_ONE') with env: client = bigquery.Client(project='ANOTHER_PROJECT', credentials=KaggleKernelCredentials()) self._test_proxy(client, should_use_proxy=False)
def test_project_with_connected_account(self, mock_access_token, ApiUrlMock): self._setup_mocks(ApiUrlMock) env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') with env: client = bigquery.Client( project='ANOTHER_PROJECT', credentials=KaggleKernelCredentials()) self._test_integration(client)
def test_default_credentials_prediction_client(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'AUTOML') with env: prediction_client = automl.PredictionServiceClient() self.assertIsNotNone(prediction_client.credentials) self.assertIsInstance(prediction_client.credentials, KaggleKernelCredentials)
def test_default_credentials(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'VIDEO_INTELLIGENCE') with env: init_video_intelligence() client = videointelligence.VideoIntelligenceServiceClient() self.assertIsNotNone(client.credentials) self.assertIsInstance(client.credentials, KaggleKernelCredentials)
def test_default_credentials_v3(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'CLOUDAI') with env: init_translation_v3() client = translate.TranslationServiceClient() self.assertIsNotNone(client.credentials) self.assertIsInstance(client.credentials, KaggleKernelCredentials)
def test_aud_claim(self): """Test aud claim function.""" env = EnvironmentVarGuard() env.set("JWT_AUD", "aud1,aud2") result = verify_aud_claim() # Because it is false we expect it not to be parsed expected = (False, []) self.assertEqual(result, expected) env.unset("JWT_AUD")
def test_default_credentials(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'CLOUDAI') with env: init_natural_language() client = language.LanguageServiceClient() self.assertIsNotNone(client.credentials) self.assertIsInstance(client.credentials, KaggleKernelCredentials)
def test_project_with_connected_account(self, mock_access_token): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') with env: client = bigquery.Client( project='ANOTHER_PROJECT', credentials=KaggleKernelCredentials(), client_options={"api_endpoint": TestBigQuery.API_BASE_URL}) self._test_integration(client)
def test_magics_with_connected_account_default_credentials(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'BIGQUERY') with env: init_bigquery() from google.cloud.bigquery import magics self.assertEqual(type(magics.context._credentials), KaggleKernelCredentials) magics.context.credentials = None
def test_default_credentials_prediction_client(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'CLOUDAI') with env: prediction_client = automl.PredictionServiceClient() self.assertIsNotNone(prediction_client.credentials) self.assertIsInstance(prediction_client.credentials, KaggleKernelCredentials) self.assertTrue(prediction_client._connection.user_agent.startswith("kaggle-gcp-client/1.0"))
def test_tables_client_credentials(self): credentials = _make_credentials() env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'CLOUDAI') with env: init_automl() tables_client = automl_v1beta1.TablesClient(credentials=credentials) self.assertEqual(tables_client.auto_ml_client.credentials, credentials)
def test_monkeypatching_idempotent(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'CLOUDAI') with env: client1 = automl.AutoMlClient.__init__ init_automl() client2 = automl.AutoMlClient.__init__ self.assertEqual(client1, client2)
def test_no_project_with_connected_account(self): env = EnvironmentVarGuard() env.set('KAGGLE_BQ_USER_JWT', 'foobar') with env: with self.assertRaises(DefaultCredentialsError): # TODO(vimota): Handle this case, either default to Kaggle Proxy or use some default project # by the user or throw a custom exception. client = bigquery.Client(credentials=KaggleKernelCredentials()) self._test_proxy(client, should_use_proxy=False)
def test_default_credentials(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'CLOUDAI') with env: init_vision() client = vision.ImageAnnotatorClient() self.assertIsNotNone(client.credentials) self.assertIsInstance(client.credentials, KaggleKernelCredentials)
def test_monkeypatching_idempotent(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'GCS') with env: client1 = storage.Client.__init__ init_gcs() client2 = storage.Client.__init__ self.assertEqual(client1, client2)
def test_user_provided_credentials(self): credentials = _make_credentials() env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'CLOUDAI') with env: init_automl() client = automl.AutoMlClient(credentials=credentials) self.assertNotIsInstance(client.credentials, KaggleKernelCredentials) self.assertIsNotNone(client.credentials)
def test_monkeypatching_idempotent(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'CLOUDAI') with env: init_vision() client1 = vision.ImageAnnotatorClient.__init__ init_vision() client2 = vision.ImageAnnotatorClient.__init__ self.assertEqual(client1, client2)
def test_user_provided_credentials_v3(self): credentials = _make_credentials() env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'TRANSLATION') with env: init_translation_v3() client = translate.TranslationServiceClient(credentials=credentials) self.assertIsNotNone(client.credentials) self.assertNotIsInstance(client.credentials, KaggleKernelCredentials)
def test_no_project_with_connected_account(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'BIGQUERY') with env: with self.assertRaises(DefaultCredentialsError): # TODO(vimota): Handle this case, either default to Kaggle Proxy or use some default project # by the user or throw a custom exception. client = bigquery.Client() self._test_integration(client)
def test_monkeypatching_idempotent(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'VIDEO_INTELLIGENCE') with env: init_video_intelligence() client1 = videointelligence.VideoIntelligenceServiceClient.__init__ init_video_intelligence() client2 = videointelligence.VideoIntelligenceServiceClient.__init__ self.assertEqual(client1, client2)
def test_no_project_with_connected_account(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') env.set('KAGGLE_KERNEL_INTEGRATIONS', 'BIGQUERY') with env: with self.assertRaises(DefaultCredentialsError): # TODO(vimota): Handle this case, either default to Kaggle Proxy or use some default project # by the user or throw a custom exception. client = bigquery.Client() self._test_proxy(client, should_use_proxy=False)
def test_simultaneous_clients(self): env = EnvironmentVarGuard() env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') with env: proxy_client = bigquery.Client() self._test_proxy(proxy_client, should_use_proxy=True) bq_client = bigquery.Client( project='ANOTHER_PROJECT', credentials=KaggleKernelCredentials()) self._test_proxy(bq_client, should_use_proxy=False) # Verify that proxy client is still going to proxy to ensure global Connection # isn't being modified. self._test_proxy(proxy_client, should_use_proxy=True)
def _test_client(self, client_func, expected_path, expected_body, secret=None, success=True): _request = {} class AccessTokenHandler(UserSecretsHTTPHandler): def set_request(self): _request['path'] = self.path content_len = int(self.headers.get('Content-Length')) _request['body'] = json.loads(self.rfile.read(content_len)) _request['headers'] = self.headers def get_response(self): if success: return {'result': {'secret': secret, 'secretType': 'refreshToken', 'secretProvider': 'google', 'expiresInSeconds': 3600}, 'wasSuccessful': "true"} else: return {'wasSuccessful': "false"} env = EnvironmentVarGuard() env.set(_KAGGLE_USER_SECRETS_TOKEN_ENV_VAR_NAME, _TEST_JWT) with env: with HTTPServer((self.SERVER_ADDRESS.hostname, self.SERVER_ADDRESS.port), AccessTokenHandler) as httpd: threading.Thread(target=httpd.serve_forever).start() try: client_func() finally: httpd.shutdown() path, headers, body = _request['path'], _request['headers'], _request['body'] self.assertEqual( path, expected_path, msg="Fake server did not receive the right request from the UserSecrets client.") self.assertEqual( body, expected_body, msg="Fake server did not receive the right body from the UserSecrets client.")
class TestEnv(unittest.TestCase): def setUp(self): self.env = EnvironmentVarGuard() self.env.set('SECRET_KEY', 'xoxo') self.env.set( 'DATABASE_URL', 'mysql2://myuser:mypass@myhost:3306/my_database_name' ) self.env.set('STATIC', '/path/to/static') def test_secret_key(self): from project_name import settings self.assertEqual(settings.SECRET_KEY,'xoxo') def test_db_url(self): from project_name import settings self.assertEqual( settings.DATABASES['default']['HOST'], 'myhost' ) def test_db_name(self): from project_name import settings self.assertEqual( settings.DATABASES['default']['NAME'], 'my_database_name' ) def test_static(self): from project_name import settings self.assertEqual( settings.STATIC_ROOT, '/path/to/static' ) self.assertEqual( settings.STATICFILES_STORAGE, 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage' )
import unittest from test.support import EnvironmentVarGuard from environ import settings """ Unit tests """ class TestSettings(unittest.TestCase): def test_my_custom_param(self): self.assertEqual('Hello World!', settings('MY_CUSTOM_PARAM')) if __name__ == '__main__': env = EnvironmentVarGuard() env.set('SETTINGS_MODULE', '{{cookiecutter.repo_name}}.settings.unittest') with env: unittest.main()