예제 #1
0
def test_on_refresh_and_retrieve(mock_response):
    """just confirm that the aliasing of these functions does not change anything"""
    adapter = SQLiteAdapter(MEMORY_DBNAME)
    adapter.on_refresh(mock_response)

    data = adapter.get_by_resource_server()
    assert data == mock_response.by_resource_server
예제 #2
0
def test_store_and_retrieve_simple_config():
    adapter = SQLiteAdapter(MEMORY_DBNAME)
    store_val = {"val1": True, "val2": None, "val3": 1.4}
    adapter.store_config("myconf", store_val)
    read_val = adapter.read_config("myconf")
    assert read_val == store_val
    assert read_val is not store_val
예제 #3
0
def test_sqlite_reading_bad_token_data():
    adapter = SQLiteAdapter(":memory:")
    # inject bad data (array, needs to be dict)
    adapter._connection.execute(
        """\
INSERT INTO token_storage(namespace, resource_server, token_data_json)
VALUES (?, ?, ?)""",
        (adapter.namespace, "foo_rs", "[]"),
    )
    with pytest.raises(ValueError,
                       match="data error: token data was not saved as a dict"):
        adapter.get_token_data("foo_rs")
예제 #4
0
def test_constructor(success, use_file, kwargs, db_filename):
    if success:
        if use_file:
            assert SQLiteAdapter(db_filename, **kwargs)
        else:
            assert SQLiteAdapter(**kwargs)
    else:
        with pytest.raises(TypeError):
            if use_file:
                SQLiteAdapter(db_filename, **kwargs)
            else:
                SQLiteAdapter(**kwargs)
예제 #5
0
def test_remove_config():
    adapter = SQLiteAdapter(MEMORY_DBNAME)
    store_val = {"val1": True, "val2": None, "val3": 1.4}
    adapter.store_config("myconf", store_val)
    adapter.store_config("myconf2", store_val)
    removed = adapter.remove_config("myconf")
    assert removed
    read_val = adapter.read_config("myconf")
    assert read_val is None
    read_val = adapter.read_config("myconf2")
    assert read_val == store_val

    removed = adapter.remove_config("myconf")
    assert not removed
예제 #6
0
def test_store_and_refresh(mock_response, mock_refresh_response):
    adapter = SQLiteAdapter(MEMORY_DBNAME)
    adapter.store(mock_response)

    # rs1 and rs2 data was stored correctly
    data = adapter.get_token_data("resource_server_1")
    assert data["access_token"] == "access_token_1"
    data = adapter.get_token_data("resource_server_2")
    assert data["access_token"] == "access_token_2"

    # "refresh" happens, this should change rs2 but not rs1
    adapter.store(mock_refresh_response)
    data = adapter.get_token_data("resource_server_1")
    assert data["access_token"] == "access_token_1"
    data = adapter.get_token_data("resource_server_2")
    assert data["access_token"] == "access_token_2_refreshed"
예제 #7
0
def test_multiple_adapters_store_and_retrieve(mock_response, db_filename):
    adapter1 = SQLiteAdapter(db_filename)
    adapter2 = SQLiteAdapter(db_filename)
    adapter1.store(mock_response)

    data = adapter2.get_by_resource_server()
    assert data == mock_response.by_resource_server
예제 #8
0
def test_multiple_adapters_store_and_retrieve_different_namespaces(
        mock_response, db_filename):
    adapter1 = SQLiteAdapter(db_filename, namespace="foo")
    adapter2 = SQLiteAdapter(db_filename, namespace="bar")
    adapter1.store(mock_response)

    data = adapter2.get_by_resource_server()
    assert data == {}
예제 #9
0
def test_sqlite_reading_bad_config():
    adapter = SQLiteAdapter(":memory:")
    # inject bad data (array, needs to be dict)
    # store_config does not check the input type, just uses json.dumps()
    adapter.store_config("foo_conf", [])

    with pytest.raises(ValueError,
                       match="reading config data and got non-dict result"):
        adapter.read_config("foo_conf")
예제 #10
0
def test_remove_tokens(mock_response):
    adapter = SQLiteAdapter(MEMORY_DBNAME)
    adapter.store(mock_response)

    removed = adapter.remove_tokens_for_resource_server("resource_server_1")
    assert removed
    data = adapter.get_by_resource_server()
    assert data == {
        "resource_server_2":
        mock_response.by_resource_server["resource_server_2"]
    }

    removed = adapter.remove_tokens_for_resource_server("resource_server_1")
    assert not removed
예제 #11
0
def test_store_and_retrieve(mock_response):
    adapter = SQLiteAdapter(MEMORY_DBNAME)
    adapter.store(mock_response)

    data = adapter.get_by_resource_server()
    assert data == mock_response.by_resource_server
예제 #12
0
def test_iter_namespaces(mock_response, db_filename):
    foo_adapter = SQLiteAdapter(db_filename, namespace="foo")
    bar_adapter = SQLiteAdapter(db_filename, namespace="bar")
    baz_adapter = SQLiteAdapter(db_filename, namespace="baz")

    for adapter in [foo_adapter, bar_adapter, baz_adapter]:
        assert list(adapter.iter_namespaces()) == []
        assert list(
            adapter.iter_namespaces(include_config_namespaces=True)) == []

    foo_adapter.store(mock_response)

    for adapter in [foo_adapter, bar_adapter, baz_adapter]:
        assert list(adapter.iter_namespaces()) == ["foo"]
        assert list(adapter.iter_namespaces(
            include_config_namespaces=True)) == ["foo"]

    bar_adapter.store(mock_response)

    for adapter in [foo_adapter, bar_adapter, baz_adapter]:
        assert set(adapter.iter_namespaces()) == {"foo", "bar"}
        assert set(
            adapter.iter_namespaces(include_config_namespaces=True)) == {
                "foo",
                "bar",
            }

    baz_adapter.store_config("some_conf", {})

    for adapter in [foo_adapter, bar_adapter, baz_adapter]:
        assert set(adapter.iter_namespaces()) == {"foo", "bar"}
        assert set(
            adapter.iter_namespaces(include_config_namespaces=True)) == {
                "foo",
                "bar",
                "baz",
            }
예제 #13
0
def test_load_missing_token_data():
    adapter = SQLiteAdapter(MEMORY_DBNAME)
    assert adapter.get_by_resource_server() == {}
    assert adapter.get_token_data("resource_server_1") is None
예제 #14
0
def test_load_missing_config_data():
    adapter = SQLiteAdapter(MEMORY_DBNAME)
    assert adapter.read_config("foo") is None