Ejemplo n.º 1
0
def test_write_bad_json(tmp_path):
    from sregistry.utils import write_json
    bad_json = {"Wakkawakkawakka'}": [{True}, "2", 3]}
    tmpfile = str(tmp_path / 'json_file.txt')
    assert not os.path.exists(tmpfile)
    with pytest.raises(TypeError):
        write_json(bad_json, tmpfile)
Ejemplo n.º 2
0
def test_write_json(tmp_path):
    import json
    from sregistry.utils import write_json, read_json
    good_json = {"Wakkawakkawakka": [True, "2", 3]}
    tmpfile = str(tmp_path / 'good_json_file.txt')
    assert not os.path.exists(tmpfile)
    write_json(good_json, tmpfile)
    with open(tmpfile, 'r') as f:
        content = json.loads(f.read())
    assert isinstance(content, dict)
    assert "Wakkawakkawakka" in content
    content = read_json(tmpfile)
    assert "Wakkawakkawakka" in content
Ejemplo n.º 3
0
def update_client_secrets(backend, updates, secrets=None, save=True):
    """update client secrets will update the data structure for a particular
       authentication. This should only be used for a (quasi permanent) token
       or similar. The secrets file, if found, is updated and saved by default.
    """
    if secrets is None:
        secrets = read_client_secrets()
    if backend not in secrets:
        secrets[backend] = {}
    secrets[backend].update(updates)

    # The update typically includes a save
    if save is True:
        secrets_file = get_secrets_file()
        if secrets_file is not None:
            write_json(secrets, secrets_file)

    return secrets
Ejemplo n.º 4
0
def read_client_secrets():
    '''for private or protected registries, a client secrets file is required
       to be located at .sregistry. If no secrets are found, we use default
       of Singularity Hub, and return a dummy secrets.
    '''
    client_secrets = _default_client_secrets()

    # If token file not provided, check environment
    secrets = _get_secrets_file()

    # If exists, load
    if secrets is not None:
        client_secrets = read_json(secrets)

    # Otherwise, initialize
    else:
        from sregistry.defaults import SREGISTRY_CLIENT_SECRETS
        write_json(client_secrets, SREGISTRY_CLIENT_SECRETS)

    return client_secrets
Ejemplo n.º 5
0
def update_secrets(secrets):
    secrets_file = get_secrets_file()
    write_json(secrets, secrets_file)