Skip to content
Snippets Groups Projects
Commit 528b37ca authored by Tanner Prestegard's avatar Tanner Prestegard Committed by GraceDB
Browse files

Moving test utility from API base to generic base

Moved request_as_user from GraceDbApiTestBase to GraceDbTestBase
so that it can be used in tests for other apps.
parent 1f092e63
No related branches found
No related tags found
No related merge requests found
......@@ -5,28 +5,3 @@ from core.tests.utils import GraceDbTestBase
class GraceDbApiTestBase(GraceDbTestBase):
client_class = APIClient
def request_as_user(self, url, method, user=None, data=None, **kwargs):
"""Shortcut function for making a request to the API"""
# Get client method for HTTP method requested
try:
method_func = getattr(self.client, method.lower())
except Exception as e:
raise ValueError('{method} is not a valid HTTP method'.format(
method=method))
if user is not None:
# Set up user dict
user_dict = {
'HTTP_REMOTE_USER': user.username,
'HTTP_ISMEMBEROF': ';'.join([g.name for g in
user.groups.all()]),
}
if kwargs:
user_dict.update(**kwargs)
# Make request and return response
return method_func(url, data, **user_dict)
else:
# Anonymous user
return method_func(url, data)
......@@ -272,5 +272,33 @@ class PublicGroupSetup(TestCase):
class GraceDbTestBase(DefineTestSettings, InternalGroupAndUserSetup,
LvemGroupAndUserSetup, PublicGroupSetup):
"""Combines all test base classes"""
pass
"""
Combines all test base classes and defines method for easily making
requests with a specfic user account.
"""
def request_as_user(self, url, method, user=None, data=None, **kwargs):
"""Shortcut function for making a request"""
# Get client method for HTTP method requested
try:
method_func = getattr(self.client, method.lower())
except Exception as e:
raise ValueError('{method} is not a valid HTTP method'.format(
method=method))
if user is not None:
# Set up user dict
user_dict = {
'HTTP_REMOTE_USER': user.username,
'HTTP_ISMEMBEROF': ';'.join([g.name for g in
user.groups.all()]),
}
if kwargs:
user_dict.update(**kwargs)
# Make request and return response
return method_func(url, data, **user_dict)
else:
# Anonymous user
return method_func(url, data)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment