From 2cf1f3ba388faf31232d840077793e4c66c29b39 Mon Sep 17 00:00:00 2001 From: Eric Miguel Date: Tue, 30 Jan 2024 14:01:55 -0300 Subject: [PATCH] feat: make rule function (#15) * feat: make_rule function * test: make_rule function test * test: added make_rules function tests * docs: added make_rule function into online docs * chore: remove orphan md file --- docs/rule.md | 9 --------- docs/rules.md | 4 ++++ missil/__init__.py | 2 ++ missil/rules.py | 19 +++++++++++++++++++ tests/test_rules.py | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 9 deletions(-) delete mode 100644 docs/rule.md create mode 100644 tests/test_rules.py diff --git a/docs/rule.md b/docs/rule.md deleted file mode 100644 index 28f318b..0000000 --- a/docs/rule.md +++ /dev/null @@ -1,9 +0,0 @@ -# Rule - -Missil main class. Works as a FastAPI [dependency](https://fastapi.tiangolo.com/reference/dependencies/?h=depen), so it can be injected in API routes. - -::: missil.Rule - -## Make Rules - -::: missil.make_rules diff --git a/docs/rules.md b/docs/rules.md index 28f318b..14f0579 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -4,6 +4,10 @@ Missil main class. Works as a FastAPI [dependency](https://fastapi.tiangolo.com/ ::: missil.Rule +## Make Rule + +:: missil.make_rule + ## Make Rules ::: missil.make_rules diff --git a/missil/__init__.py b/missil/__init__.py index 6379fdf..29980ec 100644 --- a/missil/__init__.py +++ b/missil/__init__.py @@ -14,6 +14,7 @@ from missil.rules import READ from missil.rules import WRITE from missil.rules import Rule +from missil.rules import make_rule from missil.rules import make_rules @@ -27,6 +28,7 @@ "HTTPTokenBearer", "FlexibleTokenBearer", "Rule", + "make_rule", "make_rules", "QualifiedRouter", "READ", diff --git a/missil/rules.py b/missil/rules.py index 5710877..4c4ee6b 100644 --- a/missil/rules.py +++ b/missil/rules.py @@ -191,3 +191,22 @@ def read_item(item_id: int, q: Union[str, None] = None): Dict containing endpoint-appliable rules. """ return {area: Area(area, bearer) for area in areas} + + +def make_rule(bearer: TokenBearer, area: str) -> Area: + """ + Create a single Missil rule. + + Parameters + ---------- + bearer : TokenBearer + JWT token source source. See Bearers module. + area : str + A business area name. + + Returns + ------- + Area + Business area object, containing READ and WRITE rules. + """ + return Area(area, bearer) diff --git a/tests/test_rules.py b/tests/test_rules.py new file mode 100644 index 0000000..6586ba5 --- /dev/null +++ b/tests/test_rules.py @@ -0,0 +1,32 @@ +from missil import make_rule +from missil import make_rules +from missil.rules import Area +from missil.rules import Rule + + +def test_make_rule(bearer_token): + test_area = make_rule(bearer_token, "test") + assert test_area.name == "test" + assert isinstance(test_area, Area) + assert isinstance(test_area.READ, Rule) + assert isinstance(test_area.WRITE, Rule) + + +def test_make_rules_single_ba(bearer_token): + test_area = make_rules(bearer_token, "test_1") + assert "test_1" in test_area + assert isinstance(test_area["test_1"], Area) + assert isinstance(test_area["test_1"].READ, Rule) + assert isinstance(test_area["test_1"].WRITE, Rule) + + +def test_make_rules_multiple_bas(bearer_token): + test_areas = make_rules(bearer_token, "test_1", "test_2") + assert "test_1" in test_areas + assert "test_2" in test_areas + assert isinstance(test_areas["test_1"], Area) + assert isinstance(test_areas["test_2"], Area) + assert isinstance(test_areas["test_1"].READ, Rule) + assert isinstance(test_areas["test_2"].READ, Rule) + assert isinstance(test_areas["test_1"].WRITE, Rule) + assert isinstance(test_areas["test_2"].WRITE, Rule)