Skip to content

Commit 9f61264

Browse files
committed
chore: Add support for testing tenant model to fixture model_instance
ref: #833
1 parent 4ad40f8 commit 9f61264

2 files changed

Lines changed: 51 additions & 15 deletions

File tree

app/access/middleware/request.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from django.utils.deprecation import MiddlewareMixin
55

66

7-
from access.models.tenant import Tenant as Organization
7+
from access.models.tenant import Tenant
88
from access.models.team import Team
99

1010

@@ -40,8 +40,8 @@ class Tenancy:
4040
_app_settings: AppSettings = None
4141

4242

43-
_user_organizations: list([Organization]) = None
44-
"""Cached User Organizations"""
43+
_user_organizations: list([Tenant]) = None
44+
"""Cached User Tenants"""
4545

4646
_user_teams: list([Team]) = None
4747
"""Cached User Teams"""
@@ -90,7 +90,7 @@ def __init__(self, user: User, app_settings: AppSettings):
9090

9191

9292

93-
def is_member(self, organization: Organization) -> bool:
93+
def is_member(self, organization: Tenant) -> bool:
9494
"""Returns true if the current user is a member of the organization
9595
9696
iterates over the user_organizations list and returns true if the user is a member
@@ -113,11 +113,11 @@ def is_member(self, organization: Organization) -> bool:
113113

114114

115115

116-
def has_organization_permission(self, organization: Organization, permissions_required: str) -> bool:
116+
def has_organization_permission(self, organization: Tenant, permissions_required: str) -> bool:
117117
""" Check if user has permission within organization.
118118
119119
Args:
120-
organization (int): Organization to check.
120+
organization (int): Tenant to check.
121121
permissions_required (list): if doing object level permissions, pass in required permission.
122122
123123
Returns:
@@ -126,9 +126,9 @@ def has_organization_permission(self, organization: Organization, permissions_re
126126

127127
has_permission: bool = False
128128

129-
if type(organization) is not Organization:
129+
if type(organization) is not Tenant:
130130

131-
raise TypeError('Organization must be of type Organization')
131+
raise TypeError('Tenant must be of type Tenant')
132132

133133

134134
if type(permissions_required) is not str:

app/tests/fixtures/model_instance.py

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from django.apps import apps
55
from django.db import models
66

7+
from access.models.tenant import Tenant
8+
79

810

911
model_objs: list = []
@@ -31,6 +33,32 @@ def instance( random_field:str = '', kwargs_create: dict = {} ):
3133
global model_objs
3234

3335
obj = None
36+
org = None
37+
38+
kwargs = model_kwargs
39+
40+
if kwargs_create:
41+
42+
if(
43+
'organization' in kwargs_create
44+
and type(model) is not Tenant
45+
):
46+
47+
org = kwargs_create['organization']
48+
49+
elif(
50+
'organization' in kwargs_create
51+
and type(model) is Tenant
52+
):
53+
54+
# org = kwargs_create['organization']
55+
56+
del kwargs_create['organization']
57+
58+
kwargs.update(
59+
**kwargs_create
60+
)
61+
3462

3563

3664
if model._meta.abstract:
@@ -52,17 +80,25 @@ class Meta:
5280
else:
5381

5482

55-
obj = model_kwarg_data(
56-
model = model,
57-
model_kwargs = model_kwargs,
58-
create_instance = True,
59-
)
83+
if(
84+
model is not Tenant
85+
and org is not None
86+
):
87+
88+
obj = model_kwarg_data(
89+
model = model,
90+
model_kwargs = kwargs,
91+
create_instance = True,
92+
)
93+
94+
obj = obj['instance']
6095

61-
obj = obj['instance']
96+
model_objs += [ obj ]
6297

98+
else:
6399

100+
obj = org
64101

65-
model_objs += [ obj ]
66102

67103
return obj
68104

0 commit comments

Comments
 (0)