Skip to content

Commit

Permalink
fix(share): let newer account wildcard policy take precedence over ol…
Browse files Browse the repository at this point in the history
…der account policy.
  • Loading branch information
SEIAROTg committed May 9, 2024
1 parent 7417cb8 commit 9f7fcc8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 5 additions & 1 deletion autobean/share/policy_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ def add_policy(self, name: str, policy_def: PolicyDefinition) -> None:
raise error_lib.PluginException(
f'Policy with share_enforced must define ownership')
if name.endswith(':*'):
self._wildcard_account_policies[name.removesuffix(':*')] = policy_def
prefix = name.removesuffix(':*')
self._wildcard_account_policies[prefix] = policy_def
if (account_policy_def := self._account_policies.get(prefix)) is not None:
self._account_policies[prefix] = _override_policy_def(
policy_def, account_policy_def, is_ephemeral=False)
elif ':' in name:
self._account_policies[name] = policy_def
else:
Expand Down
20 changes: 18 additions & 2 deletions autobean/share/policy_lib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,32 @@ def test_priority_account(self, txn: Transaction) -> None:
share-Alice: 1
Assets:Account 100.00 USD
share_prorated_included: FALSE
Assets:Account:Account 100.00 USD
share_prorated_included: FALSE
"""
self._policy_db.add_policy('Assets:Account', self._create_policy(42))
self._policy_db.add_policy('Assets:Account:*', self._create_policy(2))
self._policy_db.add_policy('Assets:Account:*', self._create_policy(42))
self._policy_db.add_policy('Assets:Account', self._create_policy(43))
self._policy_db.add_policy('Assets:*', self._create_policy(3))
self._policy_db.add_policy('default', self._create_policy(4))

policy_def = policy_lib.try_parse_policy_definition(txn.meta)

policy = self._policy_db.get_posting_policy(txn.postings[0], policy_def)
assert isinstance(policy.ownership, policy_lib.WeightedOwnership)
assert policy.ownership.weights['Alice'] == 43
policy = self._policy_db.get_posting_policy(txn.postings[1], policy_def)
assert isinstance(policy.ownership, policy_lib.WeightedOwnership)
assert policy.ownership.weights['Alice'] == 42

self._policy_db.add_policy('Assets:Account:*', self._create_policy(42))

policy = self._policy_db.get_posting_policy(txn.postings[0], policy_def)
assert isinstance(policy.ownership, policy_lib.WeightedOwnership)
assert policy.ownership.weights['Alice'] == 42
policy = self._policy_db.get_posting_policy(txn.postings[1], policy_def)
assert isinstance(policy.ownership, policy_lib.WeightedOwnership)
assert policy.ownership.weights['Alice'] == 42


@_parse_doc(Transaction)
def test_priority_wildcard_account_same(self, txn: Transaction) -> None:
Expand Down

0 comments on commit 9f7fcc8

Please sign in to comment.