Skip to content

Commit

Permalink
feat: make rule function (#15)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ericmiguel committed Jan 30, 2024
1 parent 19c60f8 commit 2cf1f3b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
9 changes: 0 additions & 9 deletions docs/rule.md

This file was deleted.

4 changes: 4 additions & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions missil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -27,6 +28,7 @@
"HTTPTokenBearer",
"FlexibleTokenBearer",
"Rule",
"make_rule",
"make_rules",
"QualifiedRouter",
"READ",
Expand Down
19 changes: 19 additions & 0 deletions missil/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
32 changes: 32 additions & 0 deletions tests/test_rules.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 2cf1f3b

Please sign in to comment.