diff --git a/frappe/tests/test_api.py b/frappe/tests/test_api.py index 94bc3e7d01..d1c9588916 100644 --- a/frappe/tests/test_api.py +++ b/frappe/tests/test_api.py @@ -2,20 +2,23 @@ import sys import unittest from contextlib import contextmanager from random import choice -from typing import Dict, Optional, Tuple from threading import Thread +from typing import Dict, Optional, Tuple +from unittest.mock import patch import requests from semantic_version import Version +from werkzeug.test import TestResponse import frappe -from frappe.utils import get_site_url +from frappe.utils import get_site_url, get_test_client try: _site = frappe.local.site except Exception: _site = None + @contextmanager def suppress_stdout(): """Supress stdout for tests which expectedly make noise @@ -26,17 +29,22 @@ def suppress_stdout(): finally: sys.stdout = sys.__stdout__ + +def make_request(target: str, args: Optional[Tuple] = None, kwargs: Optional[Dict] = None) -> TestResponse: + t = ThreadWithReturnValue(target=target, args=args, kwargs=kwargs) + t.start() + t.join() + return t._return + + class ThreadWithReturnValue(Thread): - def __init__(self, group=None, target=None, name=None, - args=(), kwargs={}, Verbose=None): + def __init__(self, group=None, target=None, name=None, args=(), kwargs={}): Thread.__init__(self, group, target, name, args, kwargs) self._return = None def run(self): - from unittest.mock import patch if self._target is not None: with patch("frappe.app.get_site_name", return_value=_site): - # print(self._args, self._kwargs) self._return = self._target(*self._args, **self._kwargs) def join(self, *args): @@ -44,19 +52,6 @@ class ThreadWithReturnValue(Thread): return self._return -def get_test_client(): - from frappe.app import application - from werkzeug.test import Client - - return Client(application) - - -def make_request(target: str, args: Optional[Tuple] = None, kwargs: Optional[Dict] = None): - t = ThreadWithReturnValue(target=target, args=args, kwargs=kwargs) - t.start() - t.join() - return t._return - class FrappeAPITestCase(unittest.TestCase): SITE = frappe.local.site SITE_URL = get_site_url(SITE) @@ -64,7 +59,7 @@ class FrappeAPITestCase(unittest.TestCase): TEST_CLIENT = get_test_client() @property - def sid(self): + def sid(self) -> str: if not getattr(self, "_sid", None): r = self.post("/api/method/login", { "usr": "Administrator", @@ -74,16 +69,16 @@ class FrappeAPITestCase(unittest.TestCase): return self._sid - def get(self, path: str, params: Optional[Dict] = None): + def get(self, path: str, params: Optional[Dict] = None) -> TestResponse: return make_request(target=self.TEST_CLIENT.get, args=(path, ), kwargs={"data": params}) - def post(self, path, data): + def post(self, path, data) -> TestResponse: return make_request(target=self.TEST_CLIENT.post, args=(path, ), kwargs={"data": data}) - def put(self, path, data): + def put(self, path, data) -> TestResponse: return make_request(target=self.TEST_CLIENT.put, args=(path, ), kwargs={"data": data}) - def delete(self, path): + def delete(self, path) -> TestResponse: return make_request(target=self.TEST_CLIENT.delete, args=(path, )) diff --git a/frappe/tests/test_frappe_client.py b/frappe/tests/test_frappe_client.py index 28d33d956a..2d815d0731 100644 --- a/frappe/tests/test_frappe_client.py +++ b/frappe/tests/test_frappe_client.py @@ -170,7 +170,6 @@ class TestFrappeClient(unittest.TestCase): res = requests.post(get_url() + "/api/method/frappe.auth.get_logged_user", headers=header) self.assertEqual(res.status_code, 403) - # random api key and api secret api_key = "@3djdk3kld" api_secret = "ksk&93nxoe3os" diff --git a/frappe/utils/__init__.py b/frappe/utils/__init__.py index 92174ac129..1233bcd30f 100644 --- a/frappe/utils/__init__.py +++ b/frappe/utils/__init__.py @@ -438,17 +438,10 @@ def touch_file(path): os.utime(path, None) return path -def get_test_client(): +def get_test_client() -> Client: + """Returns an test instance of the Frappe WSGI""" from frappe.app import application - - class TestClient(Client): - def open(self, *args, **kwargs): - site = frappe.local.site - ret = super().open(*args, **kwargs) - frappe.connect(site=site) - return ret - - return TestClient(application) + return Client(application) def get_hook_method(hook_name, fallback=None): method = frappe.get_hooks().get(hook_name)