| 1 |
import asyncio |
| 2 |
from unittest import mock |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from aiohttp import web |
| 7 |
from aiohttp_cors import ( |
| 8 |
APP_CONFIG_KEY, |
| 9 |
CorsConfig, |
| 10 |
CorsViewMixin, |
| 11 |
ResourceOptions, |
| 12 |
custom_cors, |
| 13 |
) |
| 14 |
|
| 15 |
DEFAULT_CONFIG = {"*": ResourceOptions()} |
| 16 |
|
| 17 |
CLASS_CONFIG = {"*": ResourceOptions()} |
| 18 |
|
| 19 |
CUSTOM_CONFIG = {"www.client1.com": ResourceOptions(allow_headers=["X-Host"])} |
| 20 |
|
| 21 |
|
| 22 |
class SimpleView(web.View, CorsViewMixin): |
| 23 |
async def get(self): |
| 24 |
return web.Response(text="Done") |
| 25 |
|
| 26 |
|
| 27 |
class SimpleViewWithConfig(web.View, CorsViewMixin): |
| 28 |
|
| 29 |
cors_config = CLASS_CONFIG |
| 30 |
|
| 31 |
async def get(self): |
| 32 |
return web.Response(text="Done") |
| 33 |
|
| 34 |
|
| 35 |
class CustomMethodView(web.View, CorsViewMixin): |
| 36 |
|
| 37 |
cors_config = CLASS_CONFIG |
| 38 |
|
| 39 |
async def get(self): |
| 40 |
return web.Response(text="Done") |
| 41 |
|
| 42 |
@custom_cors(CUSTOM_CONFIG) |
| 43 |
async def post(self): |
| 44 |
return web.Response(text="Done") |
| 45 |
|
| 46 |
|
| 47 |
@pytest.fixture |
| 48 |
def _app(): |
| 49 |
return web.Application() |
| 50 |
|
| 51 |
|
| 52 |
@pytest.fixture |
| 53 |
def cors(_app): |
| 54 |
ret = CorsConfig(_app, defaults=DEFAULT_CONFIG) |
| 55 |
_app[APP_CONFIG_KEY] = ret |
| 56 |
return ret |
| 57 |
|
| 58 |
|
| 59 |
@pytest.fixture |
| 60 |
def app(_app, cors): |
| 61 |
|
| 62 |
return _app |
| 63 |
|
| 64 |
|
| 65 |
def test_raise_exception_when_cors_not_configure(): |
| 66 |
request = mock.Mock() |
| 67 |
request.app = {} |
| 68 |
view = CustomMethodView(request) |
| 69 |
|
| 70 |
with pytest.raises(ValueError): |
| 71 |
view.get_request_config(request, "post") |
| 72 |
|
| 73 |
|
| 74 |
async def test_raises_forbidden_when_config_not_found(app): |
| 75 |
app[APP_CONFIG_KEY].defaults = {} |
| 76 |
request = mock.Mock() |
| 77 |
request.app = app |
| 78 |
request.headers = {"Origin": "*", "Access-Control-Request-Method": "GET"} |
| 79 |
view = SimpleView(request) |
| 80 |
|
| 81 |
with pytest.raises(web.HTTPForbidden): |
| 82 |
await view.options() |
| 83 |
|
| 84 |
|
| 85 |
def test_method_with_custom_cors(app): |
| 86 |
"""Test adding resource with web.View as handler""" |
| 87 |
request = mock.Mock() |
| 88 |
request.app = app |
| 89 |
view = CustomMethodView(request) |
| 90 |
|
| 91 |
assert hasattr(view.post, "post_cors_config") |
| 92 |
assert asyncio.iscoroutinefunction(view.post) |
| 93 |
config = view.get_request_config(request, "post") |
| 94 |
|
| 95 |
assert config.get("www.client1.com") == CUSTOM_CONFIG["www.client1.com"] |
| 96 |
|
| 97 |
|
| 98 |
def test_method_with_class_config(app): |
| 99 |
"""Test adding resource with web.View as handler""" |
| 100 |
request = mock.Mock() |
| 101 |
request.app = app |
| 102 |
view = SimpleViewWithConfig(request) |
| 103 |
|
| 104 |
assert not hasattr(view.get, "get_cors_config") |
| 105 |
config = view.get_request_config(request, "get") |
| 106 |
|
| 107 |
assert config.get("*") == CLASS_CONFIG["*"] |
| 108 |
|
| 109 |
|
| 110 |
def test_method_with_default_config(app): |
| 111 |
"""Test adding resource with web.View as handler""" |
| 112 |
request = mock.Mock() |
| 113 |
request.app = app |
| 114 |
view = SimpleView(request) |
| 115 |
|
| 116 |
assert not hasattr(view.get, "get_cors_config") |
| 117 |
config = view.get_request_config(request, "get") |
| 118 |
|
| 119 |
assert config.get("*") == DEFAULT_CONFIG["*"] |