Skip to content

Commit

Permalink
Work on not auto-filling username (#792)
Browse files Browse the repository at this point in the history
* Work on not auto-filling username

* Update changelog

* Use clear_default_identity correctly
  • Loading branch information
jwlodek authored Oct 2, 2024
1 parent 06a8eec commit 3fda02e
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 86 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ tiled/_version.py

.env
.asv/
venv/
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Write the date in place of the "Unreleased" in the case a new version is release

# Changelog

## Unreleased

- Add kwarg to client logout to auto-clear default identity.
- Do not automatically enter username if default identity is used.

## v0.1.0b9 (2024-09-19)

### Added
Expand Down
6 changes: 3 additions & 3 deletions tiled/_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ..catalog import from_uri, in_memory
from ..client.base import BaseClient
from ..server.settings import get_settings
from .utils import enter_password as utils_enter_password
from .utils import enter_username_password as utils_enter_uname_passwd
from .utils import temp_postgres


Expand Down Expand Up @@ -75,11 +75,11 @@ def tmp_profiles_dir():


@pytest.fixture
def enter_password():
def enter_username_password():
"""
DEPRECATED: Use the normal (non-fixture) context manager in .utils.
"""
return utils_enter_password
return utils_enter_uname_passwd


@pytest.fixture(scope="module")
Expand Down
36 changes: 19 additions & 17 deletions tiled/_tests/test_access_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..adapters.mapping import MapAdapter
from ..client import Context, from_context
from ..server.app import build_app_from_config
from .utils import enter_password, fail_with_status_code
from .utils import enter_username_password, fail_with_status_code

arr = numpy.ones((5, 5))
arr_ad = ArrayAdapter.from_array(arr)
Expand Down Expand Up @@ -132,7 +132,7 @@ def context(tmpdir_module):
}
app = build_app_from_config(config)
with Context.from_app(app) as context:
with enter_password("admin"):
with enter_username_password("admin", "admin"):
admin_client = from_context(context, username="admin")
for k in ["c", "d", "e"]:
admin_client[k].write_array(arr, key="A1")
Expand All @@ -141,8 +141,8 @@ def context(tmpdir_module):
yield context


def test_top_level_access_control(context, enter_password):
with enter_password("secret1"):
def test_top_level_access_control(context, enter_username_password):
with enter_username_password("alice", "secret1"):
alice_client = from_context(context, username="alice")
assert "a" in alice_client
assert "A2" in alice_client["a"]
Expand All @@ -152,20 +152,22 @@ def test_top_level_access_control(context, enter_password):
with pytest.raises(KeyError):
alice_client["b"]

with enter_password("secret2"):
with enter_username_password("bob", "secret2"):
bob_client = from_context(context, username="bob")
assert not list(bob_client)
with pytest.raises(KeyError):
bob_client["a"]
with pytest.raises(KeyError):
bob_client["b"]
alice_client.logout()
bob_client.logout()

# Make sure clearing default identity works without raising an error.
bob_client.logout(clear_default=True)

def test_access_control_with_api_key_auth(context, enter_password):

def test_access_control_with_api_key_auth(context, enter_username_password):
# Log in, create an API key, log out.
with enter_password("secret1"):
with enter_username_password("alice", "secret1"):
context.authenticate(username="alice")
key_info = context.create_api_key()
context.logout()
Expand All @@ -180,9 +182,9 @@ def test_access_control_with_api_key_auth(context, enter_password):
context.api_key = None


def test_node_export(enter_password, context, buffer):
def test_node_export(enter_username_password, context, buffer):
"Exporting a node should include only the children we can see."
with enter_password("secret1"):
with enter_username_password("alice", "secret1"):
alice_client = from_context(context, username="alice")
alice_client.export(buffer, format="application/json")
alice_client.logout()
Expand All @@ -195,8 +197,8 @@ def test_node_export(enter_password, context, buffer):
exported_dict["contents"]["a"]["contents"]["A2"]


def test_create_and_update_allowed(enter_password, context):
with enter_password("secret1"):
def test_create_and_update_allowed(enter_username_password, context):
with enter_username_password("alice", "secret1"):
alice_client = from_context(context, username="alice")

# Update
Expand All @@ -209,17 +211,17 @@ def test_create_and_update_allowed(enter_password, context):
alice_client.logout()


def test_writing_blocked_by_access_policy(enter_password, context):
with enter_password("secret1"):
def test_writing_blocked_by_access_policy(enter_username_password, context):
with enter_username_password("alice", "secret1"):
alice_client = from_context(context, username="alice")
alice_client["d"]["x"].metadata
with fail_with_status_code(HTTP_403_FORBIDDEN):
alice_client["d"]["x"].update_metadata(metadata={"added_key": 3})
alice_client.logout()


def test_create_blocked_by_access_policy(enter_password, context):
with enter_password("secret1"):
def test_create_blocked_by_access_policy(enter_username_password, context):
with enter_username_password("alice", "secret1"):
alice_client = from_context(context, username="alice")
with fail_with_status_code(HTTP_403_FORBIDDEN):
alice_client["e"].write_array([1, 2, 3])
Expand Down Expand Up @@ -278,7 +280,7 @@ def test_service_principal_access(tmpdir):
],
}
with Context.from_app(build_app_from_config(config)) as context:
with enter_password("admin"):
with enter_username_password("admin", "admin"):
admin_client = from_context(context, username="admin")
sp = admin_client.context.admin.create_service_principal("user")
key_info = admin_client.context.admin.create_api_key(sp["uuid"])
Expand Down
Loading

0 comments on commit 3fda02e

Please sign in to comment.