def test_logout_clears_access_token_and_tenant(self, patch_post): tenant_id = str(uuid.uuid4()) post = patch_post({ "data": { "tenant": [{ "id": tenant_id }], "switchTenant": { "accessToken": "ACCESS_TOKEN", "expiresAt": "2100-01-01", "refreshToken": "REFRESH_TOKEN", }, } }) client = Client() client.login_to_tenant(tenant_id=tenant_id) assert client._access_token is not None assert client._refresh_token is not None assert client._active_tenant_id is not None client.logout_from_tenant() assert client._access_token is None assert client._refresh_token is None assert client._active_tenant_id is None # new client doesn't load the active tenant assert Client()._active_tenant_id is None
def test_graphql_uses_access_token_after_login(self, patch_post): tenant_id = str(uuid.uuid4()) post = patch_post( { "data": { "tenant": [{"id": tenant_id}], "switch_tenant": { "access_token": "ACCESS_TOKEN", "expires_at": "2100-01-01", "refresh_token": "REFRESH_TOKEN", }, } } ) client = Client(api_token="api") client.graphql({}) assert client.get_auth_token() == "api" assert post.call_args[1]["headers"] == { "Authorization": "Bearer api", "X-PREFECT-CORE-VERSION": str(prefect.__version__), } client.login_to_tenant(tenant_id=tenant_id) client.graphql({}) assert client.get_auth_token() == "ACCESS_TOKEN" assert post.call_args[1]["headers"] == { "Authorization": "Bearer ACCESS_TOKEN", "X-PREFECT-CORE-VERSION": str(prefect.__version__), }
def test_login_to_tenant_writes_tenant_and_reloads_it_when_token_is_reloaded( self, patch_post): tenant_id = str(uuid.uuid4()) post = patch_post({ "data": { "tenant": [{ "id": tenant_id }], "switchTenant": { "accessToken": "ACCESS_TOKEN", "expiresAt": "2100-01-01", "refreshToken": "REFRESH_TOKEN", }, } }) client = Client(api_token="abc") assert client._active_tenant_id is None client.login_to_tenant(tenant_id=tenant_id) client.save_api_token() assert client._active_tenant_id == tenant_id # new client loads the active tenant and token assert Client()._active_tenant_id == tenant_id assert Client()._api_token == "abc"
def test_login_to_tenant_writes_tenant_and_reloads_it_when_token_is_reloaded( self, patch_post, cloud_api ): tenant_id = str(uuid.uuid4()) post = patch_post( { "data": { "tenant": [{"id": tenant_id}], "switch_tenant": { "access_token": "ACCESS_TOKEN", "expires_at": "2100-01-01", "refresh_token": "REFRESH_TOKEN", }, } } ) client = Client(api_token="abc") assert client._active_tenant_id is None client.login_to_tenant(tenant_id=tenant_id) client.save_api_token() assert client.active_tenant_id == tenant_id # new client loads the active tenant and token client_load_active_tenant = Client() # The tenant is initialized by calling the property active_tenant_id assert client_load_active_tenant.active_tenant_id == tenant_id assert client_load_active_tenant._api_token == "abc"
def test_login_to_client_doesnt_reload_active_tenant_when_token_isnt_loaded( self, patch_post, cloud_api): tenant_id = str(uuid.uuid4()) post = patch_post({ "data": { "tenant": [{ "id": tenant_id }], "switch_tenant": { "access_token": "ACCESS_TOKEN", "expires_at": "2100-01-01", "refresh_token": "REFRESH_TOKEN", }, } }) client = Client(api_token="abc") assert client.tenant_id is None client.login_to_tenant(tenant_id=tenant_id) assert client.tenant_id == tenant_id # new client doesn't load the active tenant because there's no api token loaded client = Client() client._init_tenant() assert client._tenant_id is None
def test_login_uses_api_token(self, patch_post): tenant_id = str(uuid.uuid4()) post = patch_post({ "data": { "tenant": [{ "id": tenant_id }], "switchTenant": { "accessToken": "ACCESS_TOKEN", "expiresAt": "2100-01-01", "refreshToken": "REFRESH_TOKEN", }, } }) client = Client(api_token="api") client.login_to_tenant(tenant_id=tenant_id) assert post.call_args[1]["headers"] == dict(Authorization="Bearer api")
def test_login_to_client_sets_access_token(self, patch_post): tenant_id = str(uuid.uuid4()) post = patch_post({ "data": { "tenant": [{ "id": tenant_id }], "switchTenant": { "accessToken": "ACCESS_TOKEN", "expiresAt": "2100-01-01", "refreshToken": "REFRESH_TOKEN", }, } }) client = Client() assert client._access_token is None assert client._refresh_token is None client.login_to_tenant(tenant_id=tenant_id) assert client._access_token == "ACCESS_TOKEN" assert client._refresh_token == "REFRESH_TOKEN"
def test_graphql_uses_access_token_after_login(self, patch_post): tenant_id = str(uuid.uuid4()) post = patch_post( { "data": { "tenant": [{"id": tenant_id}], "switchTenant": { "accessToken": "ACCESS_TOKEN", "expiresIn": 600, "refreshToken": "REFRESH_TOKEN", }, } } ) client = Client(api_token="api") client.graphql({}) assert client.get_auth_token() == "api" assert post.call_args[1]["headers"] == dict(Authorization="Bearer api") client.login_to_tenant(tenant_id=tenant_id) client.graphql({}) assert client.get_auth_token() == "ACCESS_TOKEN" assert post.call_args[1]["headers"] == dict(Authorization="Bearer ACCESS_TOKEN")
def test_login_to_tenant_requires_valid_uuid(self): client = Client() with pytest.raises(ValueError, match="valid UUID"): client.login_to_tenant(tenant_id="a")
def test_login_to_tenant_requires_argument(self): client = Client() with pytest.raises(ValueError, match="At least one"): client.login_to_tenant()