示例#1
0
    def test_client_clears_active_tenant_if_login_fails_on_initialization(
        self, patch_post, cloud_api
    ):
        post = patch_post(
            {
                "errors": [
                    {
                        "message": "",
                        "locations": [],
                        "path": ["tenant"],
                        "extensions": {"code": "UNAUTHENTICATED"},
                    }
                ]
            }
        )

        # create a client just so we can use its settings methods to store settings
        client = Client()
        settings = client._load_local_settings()
        settings.update(api_token="API_TOKEN", active_tenant_id=str(uuid.uuid4()))
        client._save_local_settings(settings)

        # this initialization will fail with the patched error
        client = Client()
        client._init_tenant()
        settings = client._load_local_settings()
        assert "active_tenant_id" not in settings
    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_client_throws_error_during_inference_if_non_single_tenant_is_returned(
            self, patch_posts, cloud_api, tenants):
        patch_posts([
            # First, raise an UNAUTHENTICATED error
            {
                "errors": [{
                    "message": "",
                    "locations": [],
                    "path": ["tenant"],
                    "extensions": {
                        "code": "UNAUTHENTICATED"
                    },
                }]
            },
            # Then, return tenant ids
            {
                "data": {
                    "tenant": tenants
                }
            },
        ])

        # create a client just so we can use its settings methods to store settings
        client = Client()
        client._save_local_settings(
            dict(api_token="API_TOKEN", active_tenant_id=str(uuid.uuid4())))

        # this initialization will fail to login to the active tenant then load the
        # correct tenant from the API
        client = Client(api_token="API_TOKEN")
        with pytest.raises(ValueError, match="Failed to authorize"):
            client._init_tenant()
示例#4
0
    def test_client_token_initializes_from_file(selfmonkeypatch, cloud_api):
        with tempfile.TemporaryDirectory() as tmp:
            with set_temporary_config({"home_dir": tmp, "cloud.graphql": "xyz"}):
                path = Path(tmp) / "client" / "xyz" / "settings.toml"
                path.parent.mkdir(parents=True)
                with path.open("w") as f:
                    toml.dump(dict(api_token="FILE_TOKEN"), f)

                client = Client()
                client._init_tenant()
        assert client._api_token == "FILE_TOKEN"
    def test_load_local_api_token_is_called_when_the_client_is_initialized_without_token(
            self, cloud_api):
        with tempfile.TemporaryDirectory() as tmp:
            with set_temporary_config({"home_dir": tmp}):
                client = Client(api_token="a")
                client._init_tenant()
                client.save_api_token()

                client = Client(api_token="b")
                assert client._api_token == "b"
                client_local_api = Client()
                client_local_api._init_tenant()
                assert client_local_api._api_token == "a"
    def test_client_infers_correct_tenant_if_a_token_is_not_user_scoped(
            self, patch_posts, cloud_api):
        patch_posts([
            # First, raise an UNAUTHENTICATED error
            {
                "errors": [{
                    "message": "",
                    "locations": [],
                    "path": ["tenant"],
                    "extensions": {
                        "code": "UNAUTHENTICATED"
                    },
                }]
            },
            # Then, return a tenant id
            {
                "data": {
                    "tenant": [{
                        "id": "tenant-id"
                    }]
                }
            },
        ])

        # create a client just so we can use its settings methods to store settings
        disk_tenant = str(uuid.uuid4())
        client = Client()
        client._save_local_settings(
            dict(api_token="API_TOKEN", active_tenant_id=disk_tenant))

        # this initialization will fail to login to the active tenant then load the
        # correct tenant from the API
        client = Client(api_token="API_TOKEN")
        client._init_tenant()
        assert client._tenant_id == "tenant-id"

        # Disk is unchanged
        settings = client._load_local_settings()
        assert settings["active_tenant_id"] == disk_tenant