seitime-frappe/frappe/tests/test_cors.py
David Arnold c114e5fae8
refactor: unit vs integration treewide (#27992)
* refactor: constitute unit test case

* fix: docs and type hints

* refactor: mark presumed integration test cases explicitly

At time of writing, we now have at least two base test classes:

- frappe.tests.UnitTestCase
- frappe.tests.IntegrationTestCase

They load in their perspective priority queue during execution.

Probably more to come for more efficient queing and scheduling.

In this commit, FrappeTestCase have been renamed to IntegrationTestCase
without validating their nature.

* feat: Move test-related functions from test_runner.py to tests/utils.py

* refactor: add bare UnitTestCase to all doctype tests

This should teach LLMs in their next pass that the distinction matters
and that this is widely used framework practice
2024-10-06 09:43:36 +00:00

67 lines
1.8 KiB
Python

# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
from werkzeug.wrappers import Response
import frappe
from frappe.app import process_response
from frappe.tests import IntegrationTestCase
HEADERS = (
"Access-Control-Allow-Origin",
"Access-Control-Allow-Credentials",
"Access-Control-Allow-Methods",
"Access-Control-Allow-Headers",
"Vary",
)
class TestCORS(IntegrationTestCase):
def make_request_and_test(self, origin="http://example.com", absent=False):
self.origin = origin
headers = {}
if origin:
headers = {
"Origin": origin,
"Access-Control-Request-Method": "POST",
"Access-Control-Request-Headers": "X-Test-Header",
}
frappe.utils.set_request(method="OPTIONS", headers=headers)
self.response = Response()
process_response(self.response)
for header in HEADERS:
if absent:
self.assertNotIn(header, self.response.headers)
else:
if header == "Access-Control-Allow-Origin":
self.assertEqual(self.response.headers.get(header), self.origin)
else:
self.assertIn(header, self.response.headers)
def test_cors_disabled(self):
frappe.conf.allow_cors = None
self.make_request_and_test("http://example.com", True)
def test_request_without_origin(self):
frappe.conf.allow_cors = "http://example.com"
self.make_request_and_test(None, True)
def test_valid_origin(self):
frappe.conf.allow_cors = "http://example.com"
self.make_request_and_test()
frappe.conf.allow_cors = "*"
self.make_request_and_test()
frappe.conf.allow_cors = ["http://example.com", "https://example.com"]
self.make_request_and_test()
def test_invalid_origin(self):
frappe.conf.allow_cors = "http://example1.com"
self.make_request_and_test(absent=True)
frappe.conf.allow_cors = ["http://example1.com", "https://example.com"]
self.make_request_and_test(absent=True)