Skip to content

Commit

Permalink
feat(auth): add some common auth resource helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed Oct 19, 2023
1 parent b424f14 commit 0b1da18
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
22 changes: 22 additions & 0 deletions bento_lib/auth/resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
__all__ = [
"RESOURCE_EVERYTHING",
"build_resource",
]

# TODO: port Pydantic models from authz instead (when we get pydantic 2)


RESOURCE_EVERYTHING = {"everything": True}


def build_resource(project: str | None = None, dataset: str | None = None, data_type: str | None = None) -> dict:
if project is None:
return RESOURCE_EVERYTHING

res = {"project": project}
if dataset:
res["dataset"] = dataset
if data_type:
res["data_type"] = data_type

return res
20 changes: 17 additions & 3 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

import pytest
from bento_lib.auth.permissions import (
Permission,
Expand All @@ -7,24 +9,36 @@
P_QUERY_DATA,
P_DELETE_DATA,
)
from bento_lib.auth.resources import RESOURCE_EVERYTHING, build_resource


def test_recreate_permission_error():
with pytest.raises(PermissionDefinitionError):
Permission(QUERY_VERB, DATA) # already exists


def test_equality():
def test_permissions_equality():
assert P_QUERY_DATA == P_QUERY_DATA
assert P_QUERY_DATA == "query:data"
assert P_QUERY_DATA != P_DELETE_DATA
assert P_QUERY_DATA != "a"
assert P_QUERY_DATA != 5


def test_repr():
def test_permissions_repr():
assert repr(P_QUERY_DATA) == "Permission(query:data)"


def test_hash():
def test_permissions_hash():
assert len({P_QUERY_DATA, P_QUERY_DATA, P_DELETE_DATA}) == 2


def test_build_resource():
assert build_resource() == RESOURCE_EVERYTHING
assert json.dumps(build_resource(project="a")) == json.dumps({"project": "a"})
assert json.dumps(build_resource(project="a", data_type="z"), sort_keys=True) == json.dumps(
{"data_type": "z", "project": "a"}, sort_keys=True)
assert json.dumps(build_resource(project="a", dataset="z"), sort_keys=True) == json.dumps(
{"dataset": "z", "project": "a"}, sort_keys=True)
assert json.dumps(build_resource(project="a", dataset="z", data_type="t"), sort_keys=True) == json.dumps(
{"data_type": "t", "dataset": "z", "project": "a"}, sort_keys=True)

0 comments on commit 0b1da18

Please sign in to comment.