async def test_get_non_existant(jp_fetch, labserverapp):
    id = "foo"

    r = await jp_fetch("lab", "api", "workspaces", id)
    validate_request(r)
    data = json.loads(r.body.decode())

    r2 = await jp_fetch("lab", "api", "workspaces", id, method="PUT", body=json.dumps(data))
    validate_request(r2)

    r3 = await jp_fetch("lab", "api", "workspaces", id)
    validate_request(r3)
    data = json.loads(r3.body.decode())
    first_metadata = data["metadata"]
    first_created = rfc3339_to_timestamp(first_metadata["created"])
    first_modified = rfc3339_to_timestamp(first_metadata["last_modified"])

    r4 = await jp_fetch("lab", "api", "workspaces", id, method="PUT", body=json.dumps(data))
    validate_request(r4)

    r5 = await jp_fetch("lab", "api", "workspaces", id)
    validate_request(r5)
    data = json.loads(r5.body.decode())
    second_metadata = data["metadata"]
    second_created = rfc3339_to_timestamp(second_metadata["created"])
    second_modified = rfc3339_to_timestamp(second_metadata["last_modified"])

    assert first_created <= second_created
    assert first_modified < second_modified
async def test_get_settings_overrides_d_dicts(jp_fetch, labserverapp, ext):
    # Check that values that are dictionaries in overrides.d/*.json are
    # merged with the schema.
    id = "@jupyterlab/apputils-extension:themes"
    overrides_d = Path(labserverapp.app_settings_dir) / "overrides.d"
    overrides_d.mkdir(exist_ok=True, parents=True)
    for i in range(10):
        text = json.dumps(
            {id: {
                "codeCellConfig": {
                    "cursorBlinkRate": 530 + i
                }
            }})
        if ext == "json5":
            text += "\n// a comment"
        (overrides_d / f"foo-{i}.{ext}").write_text(text, encoding="utf-8")
    r = await jp_fetch("lab", "api", "settings", id)
    validate_request(r)
    res = r.body.decode()
    data = json.loads(res)
    assert data["id"] == id
    schema = data["schema"]
    # Check that the last overrides.d/*.json file is respected.
    assert schema["properties"]["codeCellConfig"]["default"][
        "cursorBlinkRate"] == 539
async def test_listing(jp_fetch, labserverapp):
    # ID fields are from workspaces/*.jupyterlab-workspace
    listing = {"foo", "f/o/o/"}
    r = await jp_fetch("lab", "api", "workspaces/")
    validate_request(r)
    res = r.body.decode()
    data = json.loads(res)
    output = set(data["workspaces"]["ids"])
    assert output == listing
async def test_patch(jp_fetch, labserverapp):
    id = "@jupyterlab/shortcuts-extension:plugin"

    r = await jp_fetch("lab",
                       "api",
                       "settings",
                       id,
                       method="PUT",
                       body=json.dumps(dict(raw=json5.dumps(dict()))))
    validate_request(r)

    r = await jp_fetch(
        "lab",
        "api",
        "settings",
        id,
        method="GET",
    )
    validate_request(r)
    data = json.loads(r.body.decode())
    first_created = rfc3339_to_timestamp(data["created"])
    first_modified = rfc3339_to_timestamp(data["last_modified"])

    r = await jp_fetch("lab",
                       "api",
                       "settings",
                       id,
                       method="PUT",
                       body=json.dumps(dict(raw=json5.dumps(dict()))))
    validate_request(r)

    r = await jp_fetch(
        "lab",
        "api",
        "settings",
        id,
        method="GET",
    )
    validate_request(r)
    data = json.loads(r.body.decode())
    second_created = rfc3339_to_timestamp(data["created"])
    second_modified = rfc3339_to_timestamp(data["last_modified"])

    assert first_created <= second_created
    assert first_modified < second_modified

    r = await jp_fetch(
        "lab",
        "api",
        "settings/",
        method="GET",
    )
    validate_request(r)
    data = json.loads(r.body.decode())
    listing = data["settings"]
    list_data = [item for item in listing if item["id"] == id][0]
async def test_get_settings(jp_fetch, labserverapp):
    id = "@jupyterlab/apputils-extension:themes"
    r = await jp_fetch("lab", "api", "settings", id)
    validate_request(r)
    res = r.body.decode()
    data = json.loads(res)
    assert data["id"] == id
    schema = data["schema"]
    # Check that overrides.json file is respected.
    assert schema["properties"]["theme"]["default"] == "JupyterLab Dark"
    assert "raw" in res
async def test_get_locale(jp_fetch):
    locale = "es_CO"
    r = await jp_fetch("lab", "api", "translations", locale)
    validate_request(r)
    data = json.loads(r.body.decode())["data"]
    assert "jupyterlab" in data
    assert data["jupyterlab"][""]["language"] == locale

    assert "jupyterlab_some_package" in data
    assert data["jupyterlab_some_package"][""]["version"] == "0.1.0"
    assert data["jupyterlab_some_package"][""]["language"] == locale
async def test_get_settings_overrides_dicts(jp_fetch, labserverapp):
    # Check that values that are dictionaries in overrides.json are
    # merged with the schema.
    id = "@jupyterlab/apputils-extension:themes"
    r = await jp_fetch("lab", "api", "settings", id)
    validate_request(r)
    res = r.body.decode()
    data = json.loads(res)
    assert data["id"] == id
    schema = data["schema"]
    # Check that overrides.json file is respected.
    assert schema["properties"]["codeCellConfig"]["default"][
        "lineNumbers"] is True
    assert len(schema["properties"]["codeCellConfig"]["default"]) == 15
async def test_get(jp_fetch, labserverapp):
    id = "foo"
    r = await jp_fetch("lab", "api", "workspaces", id)
    validate_request(r)
    data = json.loads(r.body.decode())
    metadata = data["metadata"]
    assert metadata["id"] == id
    assert rfc3339_to_timestamp(metadata["created"])
    assert rfc3339_to_timestamp(metadata["last_modified"])

    r2 = await jp_fetch("lab", "api", "workspaces", id)
    validate_request(r2)
    data = json.loads(r.body.decode())
    assert data["metadata"]["id"] == id
async def test_delete(jp_fetch, labserverapp):
    orig = "f/o/o"
    copy = "baz"
    r = await jp_fetch("lab", "api", "workspaces", orig)
    validate_request(r)
    res = r.body.decode()
    data = json.loads(res)
    data["metadata"]["id"] = copy
    r2 = await jp_fetch("lab", "api", "workspaces", copy, method="PUT", body=json.dumps(data))
    assert r2.code == 204
    r3 = await jp_fetch(
        "lab",
        "api",
        "workspaces",
        copy,
        method="DELETE",
    )
    assert r3.code == 204
async def test_patch_unicode(jp_fetch, labserverapp):
    id = "@jupyterlab/unicode-extension:plugin"
    settings = dict(comment=big_unicode_string[::-1])
    payload = dict(raw=json5.dumps(settings))

    r = await jp_fetch("lab",
                       "api",
                       "settings",
                       id,
                       method="PUT",
                       body=json.dumps(payload))
    validate_request(r)

    r = await jp_fetch(
        "lab",
        "api",
        "settings",
        id,
        method="GET",
    )
    validate_request(r)
    data = json.loads(r.body.decode())
    assert data["settings"]["comment"] == big_unicode_string[::-1]
async def test_listing(jp_fetch, labserverapp):
    ids = [
        "@jupyterlab/apputils-extension:themes",
        "@jupyterlab/apputils-extension-federated:themes",
        "@jupyterlab/codemirror-extension:commands",
        "@jupyterlab/codemirror-extension-federated:commands",
        "@jupyterlab/shortcuts-extension:plugin",
        "@jupyterlab/translation-extension:plugin",
        "@jupyterlab/unicode-extension:plugin",
    ]
    versions = ["N/A", "N/A", "test-version"]
    r = await jp_fetch("lab", "api", "settings/")
    validate_request(r)
    res = r.body.decode()
    response = json.loads(res)
    response_ids = [item["id"] for item in response["settings"]]
    response_schemas = [item["schema"] for item in response["settings"]]
    response_versions = [item["version"] for item in response["settings"]]
    assert set(response_ids) == set(ids)
    assert all(response_schemas)
    assert set(response_versions) == set(versions)
    last_modifieds = [item["last_modified"] for item in response["settings"]]
    createds = [item["created"] for item in response["settings"]]
    assert {None} == set(last_modifieds + createds)
async def test_get_locale_not_valid(jp_fetch):
    r = await jp_fetch("lab", "api", "translations", "foo_BAR")
    validate_request(r)
    result = json.loads(r.body.decode())
    assert "not valid" in result["message"]
    assert result["data"] == {}
async def test_get_federated(jp_fetch, labserverapp):
    id = "@jupyterlab/apputils-extension-federated:themes"
    r = await jp_fetch("lab", "api", "settings", id)
    validate_request(r)
    res = r.body.decode()
    assert "raw" in res
async def test_get_locale_bad(jp_fetch):
    r = await jp_fetch("lab", "api", "translations", "foo_BAR")
    validate_request(r)
    data = json.loads(r.body.decode())["data"]
    assert data == {}
async def test_get_theme(jp_fetch, labserverapp):
    r = await jp_fetch("lab", "api", "themes", "@jupyterlab", "foo", "index.css")
    validate_request(r)
Esempio n. 16
0
async def test_get_listing(jp_fetch, labserverapp):
    url = r"lab/api/listings/@jupyterlab/extensionmanager-extension/listings.json"
    r = await jp_fetch(*url.split("/"))
    validate_request(r)
async def test_get(jp_fetch):
    r = await jp_fetch("lab", "api", "translations/")
    validate_request(r)
    data = json.loads(r.body.decode())["data"]
    assert "en" in data