Skip to content

Commit

Permalink
Merge pull request #7 from biscuit-auth/more-api-doc
Browse files Browse the repository at this point in the history
add params annotations for all public functions and methods
  • Loading branch information
divarvel authored Jun 21, 2023
2 parents 2b8e9d5 + 38eed6c commit 83dc0e5
Show file tree
Hide file tree
Showing 2 changed files with 275 additions and 21 deletions.
27 changes: 21 additions & 6 deletions biscuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def test_fact():

def test_biscuit_builder():
kp = KeyPair()
pubkey = PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189")

builder = BiscuitBuilder(
"""
Expand All @@ -31,7 +32,7 @@ def test_biscuit_builder():
'datetime': datetime(2023, 4, 3, 10, 0, 0, tzinfo = timezone.utc),
'set': {2, True, "Test", datetime(2023, 4, 29, 1, 0, 0, tzinfo = timezone.utc) },
},
{ 'pubkey': PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189") }
{ 'pubkey': pubkey }
)

builder.add_fact(Fact("fact(false)"));
Expand All @@ -40,9 +41,10 @@ def test_biscuit_builder():
builder.add_rule(Rule(
"head($var) <- fact($var, {f}) trusting {pubkey}",
{ 'f': True },
{ 'pubkey': PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189") }
{ 'pubkey': pubkey }
));
builder.add_check(Check("check if fact($var, {f})", { 'f': True }));
builder.add_check(Check("check if fact($var, {f}) trusting {pubkey}", { 'f': True }, { 'pubkey': pubkey }));
builder.merge(BlockBuilder('builder(true);'))

assert repr(builder) == """// no root key id set
Expand All @@ -59,12 +61,14 @@ def test_biscuit_builder():
head($var) <- fact($var, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
check if true trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
check if fact($var, true);
check if fact($var, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
"""

builder.build(kp.private_key)
assert True

def test_block_builder():
pubkey = PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189")
builder = BlockBuilder(
"""
string({str});
Expand All @@ -80,7 +84,7 @@ def test_block_builder():
'bytes': [0xaa, 0xbb],
'datetime': datetime(2023, 4, 3, 10, 0, 0, tzinfo = timezone.utc),
},
{ 'pubkey': PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189") }
{ 'pubkey': pubkey }
)

builder.add_fact(Fact("fact(false)"));
Expand All @@ -89,9 +93,10 @@ def test_block_builder():
builder.add_rule(Rule(
"head($var) <- fact($var, {f}) trusting {pubkey}",
{ 'f': True },
{ 'pubkey': PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189") }
{ 'pubkey': pubkey }
));
builder.add_check(Check("check if fact($var, {f})", { 'f': True }));
builder.add_check(Check("check if fact($var, {f}) trusting {pubkey}", { 'f': True }, { 'pubkey': pubkey }));
builder.merge(BlockBuilder('builder(true);'))

assert repr(builder) == """string("1234");
Expand All @@ -106,9 +111,11 @@ def test_block_builder():
head($var) <- fact($var, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
check if true trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
check if fact($var, true);
check if fact($var, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
"""

def test_authorizer_builder():
pubkey = PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189")
builder = Authorizer(
"""
string({str});
Expand All @@ -118,14 +125,15 @@ def test_authorizer_builder():
datetime({datetime});
check if true trusting {pubkey};
allow if true;
allow if true trusting {pubkey};
""",
{ 'str': "1234",
'int': 1,
'bool': True,
'bytes': [0xaa, 0xbb],
'datetime': datetime(2023, 4, 3, 10, 0, 0, tzinfo = timezone.utc),
},
{ 'pubkey': PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189") }
{ 'pubkey': pubkey }
)

builder.add_fact(Fact("fact(false)"));
Expand All @@ -134,9 +142,12 @@ def test_authorizer_builder():
builder.add_rule(Rule(
"head($var) <- fact($var, {f}) trusting {pubkey}",
{ 'f': True },
{ 'pubkey': PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189") }
{ 'pubkey': pubkey }
));
builder.add_check(Check("check if fact($var, {f})", { 'f': True }));
builder.add_check(Check("check if fact($var, {f}) trusting {pubkey}", { 'f': True }, { 'pubkey': pubkey }));
builder.add_policy(Policy("allow if fact($var, {f})", { 'f': True}))
builder.add_policy(Policy("allow if fact($var, {f}) trusting {pubkey}", { 'f': True}, { 'pubkey': pubkey }))
builder.merge_block(BlockBuilder('builder(true);'))
builder.merge(Authorizer('builder(false);'))

Expand Down Expand Up @@ -166,9 +177,13 @@ def test_authorizer_builder():
// origin: authorizer
check if true trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
check if fact($var, true);
check if fact($var, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
// Policies:
allow if true;
allow if true trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
allow if fact($var, true);
allow if fact($var, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
"""

def test_key_selection():
Expand Down
Loading

0 comments on commit 83dc0e5

Please sign in to comment.