Skip to content

Commit

Permalink
replaced _create_mock_request with get_mock_request
Browse files Browse the repository at this point in the history
  • Loading branch information
DevilsAutumn committed Sep 6, 2024
1 parent 5a81bca commit 998a8cb
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,14 @@ def add_arguments(self, parser):
"workspace",
help="The workspace name to be used.",
)
parser.add_argument(
"--test", action="store_true", help="Run the migration for test database"
)
parser.add_argument(
"--is_package_migration",
action="store_true",
help="Run makemigration on package models",
)

def handle(self, *args, **options):
is_test_mode = options["test"]
tenant = options["workspace"]
if is_test_mode:
connection.settings_dict["NAME"] = connection.settings_dict["NAME"]
while True:
try:
tenant_obj = TenantModel.objects.get(name=tenant)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django_tenants.management.commands.migrate_schemas import MigrateSchemasCommand

from django.conf import settings
from django.db import connection

from zango.apps.shared.tenancy.models import TenantModel

Expand All @@ -15,16 +14,10 @@ def add_arguments(self, parser):
"workspace",
help="The workspace name to be used.",
)
parser.add_argument(
"--test", action="store_true", help="Run the migration for test database"
)
parser.add_argument("--package", help="Run the migrations for the package")

def handle(self, *args, **options):
is_test_mode = options["test"]
tenant = options["workspace"]
if is_test_mode:
connection.settings_dict["NAME"] = connection.settings_dict["NAME"]
while True:
try:
tenant_obj = TenantModel.objects.get(name=tenant)
Expand Down
10 changes: 10 additions & 0 deletions backend/src/zango/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json

from importlib import import_module

import pytz

from django.conf import settings
Expand Down Expand Up @@ -51,6 +53,14 @@ def get_mock_request(**kwargs):
request.META = kwargs.get("META", {})
request.header = kwargs.get("header", {})

if kwargs.get("session", False):
session = kwargs.get("session")
if session:
request.session = session
else:
engine = import_module(settings.SESSION_ENGINE)
request.session = engine.SessionStore()

return request


Expand Down
19 changes: 4 additions & 15 deletions backend/src/zango/test/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.http import HttpRequest, SimpleCookie
from django.test import Client, RequestFactory

from zango.core.utils import get_mock_request
from zango.middleware.tenant import ZangoTenantMainMiddleware


Expand All @@ -31,19 +32,7 @@ class ZangoRequestFactory(BaseZangoRequestFactory, RequestFactory):
pass


class ZangoClient(BaseZangoRequestFactory, Client):
def _create_mock_request(self):
request = HttpRequest()
request.tenant = self.tenant

if self.session:
request.session = self.session
else:
engine = import_module(settings.SESSION_ENGINE)
request.session = engine.SessionStore()

return request

class ZangoClient(ZangoRequestFactory, Client):
def logout(self):
"""Log out the user by removing the cookies and session object."""
from django.contrib.auth import logout
Expand All @@ -62,7 +51,7 @@ def logout(self):
def _login(self, user, backend=None):
from django.contrib.auth import login

request = self._create_mock_request()
request = get_mock_request(session=self.session)
login(request, user, backend)
request.session.save()

Expand All @@ -80,7 +69,7 @@ def _login(self, user, backend=None):
)

def login(self, **credentials):
request = self._create_mock_request()
request = get_mock_request(session=self.session)
user = authenticate(request, **credentials)

if user:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def setUpClass(self):
def syn_db(self):
call_command(
'ws_migrate',
'testapp',
'--test'
'testapp'
)

def test_callable_default(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def test_ws_makemigration(self):
call_command(
'ws_makemigration',
'testapp',
'--test',
stdout=out
)
self.assertIn("0001_initial", out.getvalue())

0 comments on commit 998a8cb

Please sign in to comment.