Skip to content

Commit

Permalink
Add default relation to assign/remove methods (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyphu authored Nov 16, 2023
1 parent 55303bf commit b4e1fca
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 56 deletions.
40 changes: 20 additions & 20 deletions test/test_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def test_multitenancy_example(self):
self.assertEqual(len(tenant1_users_list.results), 0)

# Assign user1 -> tenant1
tenant1.assign_user(user1.id, "member")
tenant1.assign_user(user1.id)

user1_tenants_list = warrant.Tenant.list_for_user(user1.id, {"limit": 100}, {"Warrant-Token": "latest"})
self.assertEqual(len(user1_tenants_list.results), 1)
Expand All @@ -306,7 +306,7 @@ def test_multitenancy_example(self):
self.assertEqual(tenant1_users_list.results[0].meta, {})

# Remove user1 -> tenant1
tenant1.remove_user(user1.id, "member")
tenant1.remove_user(user1.id)

user1_tenants_list = warrant.Tenant.list_for_user(user1.id, {"limit": 100}, {"Warrant-Token": "latest"})
self.assertEqual(len(user1_tenants_list.results), 0)
Expand Down Expand Up @@ -346,8 +346,8 @@ def test_rbac_example(self):
self.assertEqual(admin_user_has_permission, False)

# Assign 'create-report' -> admin role -> admin user
admin_role.assign_permission(create_permission.id, "member")
admin_user.assign_role(admin_role.id, "member")
admin_role.assign_permission(create_permission.id)
admin_user.assign_role(admin_role.id)

admin_user_has_permission = admin_user.has_permission(create_permission.id, {"Warrant-Token": "latest"})
self.assertEqual(admin_user_has_permission, True)
Expand All @@ -363,8 +363,8 @@ def test_rbac_example(self):
self.assertEqual(admin_role_permissions_list.results[0].meta, {"name": "Create Report", "description": "Permission to create reports"})

# Remove 'create-report' -> admin role -> admin user
admin_role.remove_permission(create_permission.id, "member")
admin_user.remove_role(admin_role.id, "member")
admin_role.remove_permission(create_permission.id)
admin_user.remove_role(admin_role.id)

admin_user_has_permission = admin_user.has_permission(create_permission.id, {"Warrant-Token": "latest"})
self.assertEqual(admin_user_has_permission, False)
Expand All @@ -382,7 +382,7 @@ def test_rbac_example(self):
viewer_user_permissions_list = warrant.Permission.list_for_user(viewer_user.id, {"limit": 100}, {"Warrant-Token": "latest"})
self.assertEqual(len(viewer_user_permissions_list.results), 0)

viewer_user.assign_permission(view_permission.id, "member")
viewer_user.assign_permission(view_permission.id)

viewer_user_has_permission = viewer_user.has_permission(view_permission.id, {"Warrant-Token": "latest"})
self.assertEqual(viewer_user_has_permission, True)
Expand All @@ -392,7 +392,7 @@ def test_rbac_example(self):
self.assertEqual(viewer_user_permissions_list.results[0].object_id, view_permission.id)
self.assertEqual(viewer_user_permissions_list.results[0].meta, {"name": "View Report", "description": "Permission to view reports"})

viewer_user.remove_permission(view_permission.id, "member")
viewer_user.remove_permission(view_permission.id)

viewer_user_has_permission = viewer_user.has_permission(view_permission.id, {"Warrant-Token": "latest"})
self.assertEqual(viewer_user_has_permission, False)
Expand Down Expand Up @@ -435,7 +435,7 @@ def test_pricing_tiers_features_and_users_example(self):
paid_user_features_list = warrant.Feature.list_for_user(paid_user.id, {"limit": 100}, {"Warrant-Token": "latest"})
self.assertEqual(len(paid_user_features_list.results), 0)

paid_user.assign_feature(custom_feature.id, "member")
paid_user.assign_feature(custom_feature.id)

paid_user_has_feature = paid_user.has_feature(custom_feature.id, {"Warrant-Token": "latest"})
self.assertEqual(paid_user_has_feature, True)
Expand All @@ -445,7 +445,7 @@ def test_pricing_tiers_features_and_users_example(self):
self.assertEqual(paid_user_features_list.results[0].object_id, "custom-feature")
self.assertEqual(paid_user_features_list.results[0].meta, {"name": "Custom Feature"})

paid_user.remove_feature(custom_feature.id, "member")
paid_user.remove_feature(custom_feature.id)

paid_user_has_feature = paid_user.has_feature(custom_feature.id, {"Warrant-Token": "latest"})
self.assertEqual(paid_user_has_feature, False)
Expand All @@ -463,8 +463,8 @@ def test_pricing_tiers_features_and_users_example(self):
free_user_tiers_list = warrant.PricingTier.list_for_user(free_user.id, {"limit": 100}, {"Warrant-Token": "latest"})
self.assertEqual(len(free_user_tiers_list.results), 0)

free_tier.assign_feature(feature1.id, "member")
free_user.assign_pricing_tier(free_tier.id, "member")
free_tier.assign_feature(feature1.id)
free_user.assign_pricing_tier(free_tier.id)

free_user_has_feature = free_user.has_feature(feature1.id, {"Warrant-Token": "latest"})
self.assertEqual(free_user_has_feature, True)
Expand All @@ -479,8 +479,8 @@ def test_pricing_tiers_features_and_users_example(self):
self.assertEqual(free_user_tiers_list.results[0].object_id, "free")
self.assertEqual(free_user_tiers_list.results[0].meta, {"name": "Free Tier"})

free_tier.remove_feature(feature1.id, "member")
free_user.remove_pricing_tier(free_tier.id, "member")
free_tier.remove_feature(feature1.id)
free_user.remove_pricing_tier(free_tier.id)

free_user_has_feature = free_user.has_feature(feature1.id, {"Warrant-Token": "latest"})
self.assertEqual(free_user_has_feature, False)
Expand Down Expand Up @@ -528,7 +528,7 @@ def test_pricing_tiers_features_and_tenants_example(self):
paid_tenant_features_list = paid_tenant.list_features({"limit": 100}, {"Warrant-Token": "latest"})
self.assertEqual(len(paid_tenant_features_list.results), 0)

paid_tenant.assign_feature(custom_feature.id, "member")
paid_tenant.assign_feature(custom_feature.id)

paid_tenant_has_feature = paid_tenant.has_feature(custom_feature.id, {"Warrant-Token": "latest"})
self.assertEqual(paid_tenant_has_feature, True)
Expand All @@ -538,7 +538,7 @@ def test_pricing_tiers_features_and_tenants_example(self):
self.assertEqual(paid_tenant_features_list.results[0].object_id, "custom-feature")
self.assertEqual(paid_tenant_features_list.results[0].meta, {"name": "Custom Feature"})

paid_tenant.remove_feature(custom_feature.id, "member")
paid_tenant.remove_feature(custom_feature.id)

paid_tenant_has_feature = paid_tenant.has_feature(custom_feature.id, {"Warrant-Token": "latest"})
self.assertEqual(paid_tenant_has_feature, False)
Expand All @@ -556,8 +556,8 @@ def test_pricing_tiers_features_and_tenants_example(self):
free_tenant_tiers_list = free_tenant.list_pricing_tiers({"limit": 100}, {"Warrant-Token": "latest"})
self.assertEqual(len(free_tenant_tiers_list.results), 0)

free_tier.assign_feature(feature1.id, "member")
free_tenant.assign_pricing_tier(free_tier.id, "member")
free_tier.assign_feature(feature1.id)
free_tenant.assign_pricing_tier(free_tier.id)

free_tenant_has_feature = free_tenant.has_feature(feature1.id, {"Warrant-Token": "latest"})
self.assertEqual(free_tenant_has_feature, True)
Expand All @@ -572,8 +572,8 @@ def test_pricing_tiers_features_and_tenants_example(self):
self.assertEqual(free_tenant_tiers_list.results[0].object_id, "free")
self.assertEqual(free_tenant_tiers_list.results[0].meta, {"name": "Free Tier"})

free_tier.remove_feature(feature1.id, "member")
free_tenant.remove_pricing_tier(free_tier.id, "member")
free_tier.remove_feature(feature1.id)
free_tenant.remove_pricing_tier(free_tier.id)

free_tenant_has_feature = free_tenant.has_feature(feature1.id, {"Warrant-Token": "latest"})
self.assertEqual(free_tenant_has_feature, False)
Expand Down
12 changes: 6 additions & 6 deletions warrant/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def list_for_pricing_tier(cls, pricing_tier_id: str, list_params: Dict[str, Any]
return ListResult[Feature](list(features))

@classmethod
def assign_to_pricing_tier(cls, pricing_tier_id: str, feature_id: str, relation, opts: Dict[str, Any] = {}):
def assign_to_pricing_tier(cls, pricing_tier_id: str, feature_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
pricing_tier_subject = Subject(constants.PRICING_TIER_OBJECT_TYPE, pricing_tier_id)
return Warrant.create(constants.FEATURE_OBJECT_TYPE, feature_id, relation, pricing_tier_subject, opts=opts)

@classmethod
def remove_from_pricing_tier(cls, pricing_tier_id: str, feature_id: str, relation: str, opts: Dict[str, Any] = {}):
def remove_from_pricing_tier(cls, pricing_tier_id: str, feature_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
pricing_tier_subject = Subject(constants.PRICING_TIER_OBJECT_TYPE, pricing_tier_id)
return Warrant.delete(constants.FEATURE_OBJECT_TYPE, feature_id, relation, pricing_tier_subject, opts=opts)

Expand All @@ -78,12 +78,12 @@ def list_for_tenant(cls, tenant_id: str, list_params: Dict[str, Any] = {}, opts:
return ListResult[Feature](list(features))

@classmethod
def assign_to_tenant(cls, tenant_id: str, feature_id: str, relation: str, opts: Dict[str, Any] = {}):
def assign_to_tenant(cls, tenant_id: str, feature_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
tenant_subject = Subject(constants.TENANT_OBJECT_TYPE, tenant_id)
return Warrant.create(constants.FEATURE_OBJECT_TYPE, feature_id, relation, tenant_subject, opts=opts)

@classmethod
def remove_from_tenant(cls, tenant_id: str, feature_id: str, relation: str, opts: Dict[str, Any] = {}):
def remove_from_tenant(cls, tenant_id: str, feature_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
tenant_subject = Subject(constants.TENANT_OBJECT_TYPE, tenant_id)
return Warrant.delete(constants.FEATURE_OBJECT_TYPE, feature_id, relation, tenant_subject, opts=opts)

Expand All @@ -104,12 +104,12 @@ def list_for_user(cls, user_id: str, list_params: Dict[str, Any] = {}, opts: Dic
return ListResult[Feature](list(features))

@classmethod
def assign_to_user(cls, user_id: str, feature_id: str, relation: str, opts: Dict[str, Any] = {}):
def assign_to_user(cls, user_id: str, feature_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
user_subject = Subject(constants.USER_OBJECT_TYPE, user_id)
return Warrant.create(constants.FEATURE_OBJECT_TYPE, feature_id, relation, user_subject, opts=opts)

@classmethod
def remove_from_user(cls, user_id: str, feature_id: str, relation: str, opts: Dict[str, Any] = {}):
def remove_from_user(cls, user_id: str, feature_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
user_subject = Subject(constants.USER_OBJECT_TYPE, user_id)
return Warrant.delete(constants.FEATURE_OBJECT_TYPE, feature_id, relation, user_subject, opts=opts)

Expand Down
8 changes: 4 additions & 4 deletions warrant/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def list_for_user(cls, user_id: str, list_params: Dict[str, Any] = {}, opts: Dic
return ListResult[Permission](list(permissions))

@classmethod
def assign_to_user(cls, user_id: str, permission_id: str, relation: str, opts: Dict[str, Any] = {}):
def assign_to_user(cls, user_id: str, permission_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
user_subject = Subject(constants.USER_OBJECT_TYPE, user_id)
return Warrant.create(constants.PERMISSION_OBJECT_TYPE, permission_id, relation, user_subject, opts=opts)

@classmethod
def remove_from_user(cls, user_id: str, permission_id: str, relation: str, opts: Dict[str, Any] = {}):
def remove_from_user(cls, user_id: str, permission_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
user_subject = Subject(constants.USER_OBJECT_TYPE, user_id)
return Warrant.delete(constants.PERMISSION_OBJECT_TYPE, permission_id, relation, user_subject, opts=opts)

Expand All @@ -78,12 +78,12 @@ def list_for_role(cls, role_id: str, list_params: Dict[str, Any] = {}, opts: Dic
return ListResult[Permission](list(permissions))

@classmethod
def assign_to_role(cls, role_id: str, permission_id: str, relation: str, opts: Dict[str, Any] = {}):
def assign_to_role(cls, role_id: str, permission_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
role_subject = Subject(constants.ROLE_OBJECT_TYPE, role_id)
return Warrant.create(constants.PERMISSION_OBJECT_TYPE, permission_id, relation, role_subject, opts=opts)

@classmethod
def remove_from_role(cls, role_id: str, permission_id: str, relation: str, opts: Dict[str, Any] = {}):
def remove_from_role(cls, role_id: str, permission_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
role_subject = Subject(constants.ROLE_OBJECT_TYPE, role_id)
return Warrant.delete(constants.PERMISSION_OBJECT_TYPE, permission_id, relation, role_subject, opts=opts)

Expand Down
12 changes: 6 additions & 6 deletions warrant/pricing_tier.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ def delete(cls, id: str, opts: Dict[str, Any] = {}):
def list_features(self, list_params: Dict[str, Any] = {}, opts: Dict[str, Any] = {}) -> ListResult[Feature]:
return Feature.list_for_pricing_tier(pricing_tier_id=self.id, list_params=list_params, opts=opts)

def assign_feature(self, feature_id: str, relation: str, opts: Dict[str, Any] = {}):
def assign_feature(self, feature_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
Feature.assign_to_pricing_tier(pricing_tier_id=self.id, feature_id=feature_id, relation=relation, opts=opts)

def remove_feature(self, feature_id: str, relation: str, opts: Dict[str, Any] = {}):
def remove_feature(self, feature_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
Feature.remove_from_pricing_tier(pricing_tier_id=self.id, feature_id=feature_id, relation=relation, opts=opts)

"""
Expand All @@ -64,12 +64,12 @@ def list_for_tenant(cls, tenant_id: str, list_params: Dict[str, Any] = {}, opts:
return ListResult[PricingTier](list(pricing_tiers))

@classmethod
def assign_to_tenant(cls, tenant_id: str, pricing_tier_id: str, relation: str, opts: Dict[str, Any] = {}):
def assign_to_tenant(cls, tenant_id: str, pricing_tier_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
tenant_subject = Subject(constants.TENANT_OBJECT_TYPE, tenant_id)
return Warrant.create(constants.PRICING_TIER_OBJECT_TYPE, pricing_tier_id, relation, tenant_subject, opts=opts)

@classmethod
def remove_from_tenant(cls, tenant_id: str, pricing_tier_id: str, relation: str, opts: Dict[str, Any] = {}):
def remove_from_tenant(cls, tenant_id: str, pricing_tier_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
tenant_subject = Subject(constants.TENANT_OBJECT_TYPE, tenant_id)
return Warrant.delete(constants.PRICING_TIER_OBJECT_TYPE, pricing_tier_id, relation, tenant_subject, opts=opts)

Expand All @@ -90,12 +90,12 @@ def list_for_user(cls, user_id: str, list_params: Dict[str, Any] = {}, opts: Dic
return ListResult[PricingTier](list(pricing_tiers))

@classmethod
def assign_to_user(cls, user_id: str, pricing_tier_id: str, relation: str, opts: Dict[str, Any] = {}):
def assign_to_user(cls, user_id: str, pricing_tier_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
user_subject = Subject(constants.USER_OBJECT_TYPE, user_id)
return Warrant.create(constants.PRICING_TIER_OBJECT_TYPE, pricing_tier_id, relation, user_subject, opts=opts)

@classmethod
def remove_from_user(cls, user_id: str, pricing_tier_id: str, relation: str, opts: Dict[str, Any] = {}):
def remove_from_user(cls, user_id: str, pricing_tier_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
user_subject = Subject(constants.USER_OBJECT_TYPE, user_id)
return Warrant.delete(constants.PRICING_TIER_OBJECT_TYPE, pricing_tier_id, relation, user_subject, opts=opts)

Expand Down
8 changes: 4 additions & 4 deletions warrant/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def list_for_user(cls, user_id: str, list_params: Dict[str, Any] = {}, opts: Dic
return ListResult[Role](list(roles))

@classmethod
def assign_to_user(cls, user_id: str, role_id: str, relation: str, opts: Dict[str, Any] = {}):
def assign_to_user(cls, user_id: str, role_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
user_subject = Subject(constants.USER_OBJECT_TYPE, user_id)
return Warrant.create(constants.ROLE_OBJECT_TYPE, role_id, relation, user_subject, opts=opts)

@classmethod
def remove_from_user(cls, user_id: str, role_id: str, relation: str, opts: Dict[str, Any] = {}):
def remove_from_user(cls, user_id: str, role_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
user_subject = Subject(constants.USER_OBJECT_TYPE, user_id)
return Warrant.delete(constants.ROLE_OBJECT_TYPE, role_id, relation, user_subject, opts=opts)

Expand All @@ -67,10 +67,10 @@ def remove_from_user(cls, user_id: str, role_id: str, relation: str, opts: Dict[
def list_permissions(self, list_params: Dict[str, Any] = {}, opts: Dict[str, Any] = {}) -> ListResult["Permission"]:
return Permission.list_for_role(self.id, list_params, opts=opts)

def assign_permission(self, permission_id: str, relation: str, opts: Dict[str, Any] = {}):
def assign_permission(self, permission_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
Permission.assign_to_role(self.id, permission_id, relation, opts=opts)

def remove_permission(self, permission_id: str, relation: str, opts: Dict[str, Any] = {}):
def remove_permission(self, permission_id: str, relation: str = "member", opts: Dict[str, Any] = {}):
Permission.remove_from_role(self.id, permission_id, relation, opts=opts)

"""
Expand Down
Loading

0 comments on commit b4e1fca

Please sign in to comment.