-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from rockychen-dpaw/master
Add health check & fix session timeout issue
- Loading branch information
Showing
35 changed files
with
1,286 additions
and
494 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,76 +28,86 @@ def get(self,path, authorization=None,domain=None,url=None): | |
|
||
return super().get(path,headers=headers) | ||
|
||
class BaseAuthTestCase(TestCase): | ||
client_class = Auth2Client | ||
home_url = reverse('home') | ||
auth_url = reverse('auth') | ||
auth_optional_url = reverse('auth_optional') | ||
auth_basic_url = reverse('auth_basic') | ||
auth_basic_optional_url = reverse('auth_basic_optional') | ||
test_usergroups = None | ||
class BaseTestCase(TestCase): | ||
test_usergroupauthorization = None | ||
test_userauthorization = None | ||
test_users = None | ||
|
||
|
||
def create_client(self): | ||
return Auth2Client() | ||
|
||
def setUp(self): | ||
test_usergroups = None | ||
@classmethod | ||
def setUpClass(cls): | ||
super(BaseTestCase,cls).setUpClass() | ||
print("*********************************************************") | ||
if settings.RELEASE: | ||
print("Running in release mode") | ||
print("Running unitest({}) in release mode".format(cls.__name__)) | ||
else: | ||
print("Running in dev mode") | ||
settings.AUTH_CACHE_SIZE=2000 | ||
settings.BASIC_AUTH_CACHE_SIZE=1000 | ||
settings.AUTH_BASIC_CACHE_EXPIRETIME=timedelta(seconds=3600) | ||
settings.AUTH_CACHE_EXPIRETIME=timedelta(seconds=3600) | ||
settings.STAFF_AUTH_CACHE_EXPIRETIME=settings.AUTH_CACHE_EXPIRETIME | ||
settings.AUTH_CACHE_CLEAN_HOURS = [0] | ||
settings.AUTHORIZATION_CACHE_CHECK_HOURS = [0,12] | ||
print("Running unitest({}) in dev mode".format(cls.__name__)) | ||
|
||
settings.CHECK_AUTH_BASIC_PER_REQUEST = False | ||
User.objects.filter(email__endswith="@gunfire.com").delete() | ||
User.objects.filter(email__endswith="@gunfire.com.au").delete() | ||
User.objects.filter(email__endswith="@hacker.com").delete() | ||
UserToken.objects.all().delete() | ||
UserGroup.objects.all().exclude(users=["*"],excluded_users__isnull=True).delete() | ||
UserAuthorization.objects.all().delete() | ||
def delete_testdata(self): | ||
#delete user group authorization | ||
if self.test_usergroupauthorization: | ||
for groupname,domain,paths,excluded_paths in self.test_usergroupauthorization : | ||
UserGroupAuthorization.objects.filter(usergroup=UserGroup.objects.get(name=groupname),domain=domain,paths=paths,excluded_paths=excluded_paths).delete() | ||
|
||
#delete user authorization | ||
if self.test_userauthorization: | ||
for user,domain,paths,excluded_paths in self.test_userauthorization: | ||
UserAuthorization.objects.filter(user=user,domain=domain,paths=paths,excluded_paths=excluded_paths).delete() | ||
|
||
cache.refresh_authorization_cache(True) | ||
if not UserGroup.objects.filter(users=["*"], excluded_users__isnull=True).exists(): | ||
public_group = UserGroup(name="Public User",groupid="PUBLIC",users=["*"]) | ||
public_group.clean() | ||
public_group.save() | ||
if not CustomizableUserflow.objects.filter(domain="*").exists(): | ||
default_flow = CustomizableUserflow( | ||
domain='*', | ||
default='default', | ||
mfa_set="default_mfa_set", | ||
mfa_reset="default_mfa_reset", | ||
password_reset="default_password_reset", | ||
verifyemail_from="[email protected]", | ||
verifyemail_subject="test" | ||
) | ||
default_flow.clean() | ||
default_flow.save() | ||
#delete users | ||
if self.test_users: | ||
for user in self.test_users.values(): | ||
user.delete() | ||
|
||
cache.clean_auth_cache(True) | ||
cache.refresh_authorization_cache(True) | ||
|
||
#delete UserGroup objects | ||
if self.test_usergroups: | ||
del_usergroups = [] | ||
created_usergroups = [(UserGroup.public_group(),self.test_usergroups)] | ||
while created_usergroups: | ||
parent_obj,subgroup_datas = created_usergroups.pop() | ||
for subgroup in subgroup_datas: | ||
if len(subgroup) == 4: | ||
name,users,excluded_users,subgroups = subgroup | ||
session_timeout = None | ||
else: | ||
name,users,excluded_users,session_timeout,subgroups = subgroup | ||
|
||
obj = UserGroup.objects.filter(name=name,parent_group=parent_obj).first() | ||
if obj: | ||
del_usergroups.insert(0,obj) | ||
if subgroups: | ||
created_usergroups.append((obj,subgroups)) | ||
|
||
def basic_auth(self, username, password): | ||
return 'Basic {}'.format(base64.b64encode('{}:{}'.format(username, password).encode('utf-8')).decode('utf-8')) | ||
for usergroup in del_usergroups: | ||
usergroup.delete() | ||
|
||
def populate_testdata(self): | ||
#popuate UserGroup objects | ||
if self.test_usergroups: | ||
uncreated_usergroups = [(UserGroup.public_group(),self.test_usergroups)] | ||
while uncreated_usergroups: | ||
parent_obj,subgroup_datas = uncreated_usergroups.pop() | ||
for name,users,excluded_users,subgroups in subgroup_datas: | ||
obj = UserGroup(name=name,groupid=name,users=users,excluded_users=excluded_users,parent_group=parent_obj) | ||
for subgroup in subgroup_datas: | ||
if len(subgroup) == 4: | ||
name,users,excluded_users,subgroups = subgroup | ||
session_timeout = None | ||
else: | ||
name,users,excluded_users,session_timeout,subgroups = subgroup | ||
|
||
if users and isinstance(users,str): | ||
users = [users] | ||
if excluded_users and isinstance(excluded_users,str): | ||
excluded_users = [excluded_users] | ||
|
||
obj = UserGroup.objects.filter(name=name).first() | ||
if obj: | ||
obj.groupid=name | ||
obj.users=users | ||
obj.excluded_users=excluded_users | ||
obj.parent_group=parent_obj | ||
obj.session_timeout=session_timeout | ||
else: | ||
obj = UserGroup(name=name,groupid=name,users=users,excluded_users=excluded_users,parent_group=parent_obj,session_timeout=session_timeout) | ||
obj.clean() | ||
print("save usergroup={}".format(obj)) | ||
obj.save() | ||
|
@@ -107,7 +117,11 @@ def populate_testdata(self): | |
users = OrderedDict() | ||
if self.test_users: | ||
for test_user in self.test_users: | ||
obj = User(username=test_user[0],email=test_user[1]) | ||
obj = User.objects.filter(username=test_user[0]).first() | ||
if obj: | ||
obj.email = test_user[1] | ||
else: | ||
obj = User(username=test_user[0],email=test_user[1]) | ||
obj.clean() | ||
obj.save() | ||
users[test_user[0]] = obj | ||
|
@@ -135,6 +149,64 @@ def populate_testdata(self): | |
|
||
cache.refresh_authorization_cache(True) | ||
|
||
class BaseAuthTestCase(BaseTestCase): | ||
client_class = Auth2Client | ||
home_url = reverse('home') | ||
auth_url = reverse('auth') | ||
auth_optional_url = reverse('auth_optional') | ||
auth_basic_url = reverse('auth_basic') | ||
auth_basic_optional_url = reverse('auth_basic_optional') | ||
|
||
|
||
def create_client(self): | ||
return Auth2Client() | ||
|
||
def setUp(self): | ||
settings.AUTH_CACHE_SIZE=2000 | ||
settings.BASIC_AUTH_CACHE_SIZE=1000 | ||
settings.AUTH_BASIC_CACHE_EXPIRETIME=timedelta(seconds=3600) | ||
settings.AUTH_CACHE_EXPIRETIME=timedelta(seconds=3600) | ||
settings.STAFF_AUTH_CACHE_EXPIRETIME=settings.AUTH_CACHE_EXPIRETIME | ||
settings.AUTH_CACHE_CLEAN_HOURS = [0] | ||
settings.AUTHORIZATION_CACHE_CHECK_HOURS = [0,12] | ||
|
||
settings.CHECK_AUTH_BASIC_PER_REQUEST = False | ||
User.objects.filter(email__endswith="@gunfire.com").delete() | ||
User.objects.filter(email__endswith="@gunfire.com.au").delete() | ||
User.objects.filter(email__endswith="@hacker.com").delete() | ||
UserToken.objects.all().delete() | ||
UserGroup.objects.all().exclude(users=["*"],excluded_users__isnull=True).delete() | ||
UserAuthorization.objects.all().delete() | ||
|
||
cache.refresh_authorization_cache(True) | ||
public_group = UserGroup.objects.filter(users=["*"], excluded_users__isnull=True).first() | ||
if public_group: | ||
public_group.session_timeout = 900 | ||
public_group.save() | ||
else: | ||
public_group = UserGroup(name="Public User",groupid="PUBLIC",users=["*"],session_timeout=900) | ||
public_group.clean() | ||
public_group.save() | ||
if not CustomizableUserflow.objects.filter(domain="*").exists(): | ||
default_flow = CustomizableUserflow( | ||
domain='*', | ||
default='default', | ||
mfa_set="default_mfa_set", | ||
mfa_reset="default_mfa_reset", | ||
password_reset="default_password_reset", | ||
verifyemail_from="[email protected]", | ||
verifyemail_subject="test" | ||
) | ||
default_flow.clean() | ||
default_flow.save() | ||
|
||
cache.clean_auth_cache(True) | ||
cache.refresh_authorization_cache(True) | ||
|
||
|
||
def basic_auth(self, username, password): | ||
return 'Basic {}'.format(base64.b64encode('{}:{}'.format(username, password).encode('utf-8')).decode('utf-8')) | ||
|
||
class BaseAuthCacheTestCase(BaseAuthTestCase): | ||
def setUp(self): | ||
super().setUp() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.