|
1 | 1 | import aiohttp
|
| 2 | +from . import dfly_args |
| 3 | +from .instance import DflyInstance |
2 | 4 |
|
3 | 5 |
|
4 |
| -async def test_password(df_factory): |
5 |
| - with df_factory.create(port=1112, requirepass="XXX") as server: |
6 |
| - async with aiohttp.ClientSession() as session: |
7 |
| - resp = await session.get(f"http://localhost:{server.port}/") |
8 |
| - assert resp.status == 401 |
9 |
| - async with aiohttp.ClientSession( |
10 |
| - auth=aiohttp.BasicAuth("default", "wrongpassword") |
11 |
| - ) as session: |
12 |
| - resp = await session.get(f"http://localhost:{server.port}/") |
13 |
| - assert resp.status == 401 |
14 |
| - async with aiohttp.ClientSession(auth=aiohttp.BasicAuth("default", "XXX")) as session: |
15 |
| - resp = await session.get(f"http://localhost:{server.port}/") |
16 |
| - assert resp.status == 200 |
| 6 | +def get_http_session(*args): |
| 7 | + if args: |
| 8 | + return aiohttp.ClientSession(auth=aiohttp.BasicAuth(*args)) |
| 9 | + return aiohttp.ClientSession() |
17 | 10 |
|
18 | 11 |
|
19 |
| -async def test_skip_metrics(df_factory): |
20 |
| - with df_factory.create(port=1112, admin_port=1113, requirepass="XXX") as server: |
21 |
| - async with aiohttp.ClientSession(auth=aiohttp.BasicAuth("whoops", "whoops")) as session: |
22 |
| - resp = await session.get(f"http://localhost:{server.port}/metrics") |
23 |
| - assert resp.status == 200 |
24 |
| - async with aiohttp.ClientSession(auth=aiohttp.BasicAuth("whoops", "whoops")) as session: |
25 |
| - resp = await session.get(f"http://localhost:{server.admin_port}/metrics") |
26 |
| - assert resp.status == 200 |
| 12 | +@dfly_args({"proactor_threads": "1", "requirepass": "XXX"}) |
| 13 | +async def test_password(df_server: DflyInstance): |
| 14 | + async with get_http_session() as session: |
| 15 | + resp = await session.get(f"http://localhost:{df_server.port}/") |
| 16 | + assert resp.status == 401 |
| 17 | + async with get_http_session("default", "wrongpassword") as session: |
| 18 | + resp = await session.get(f"http://localhost:{df_server.port}/") |
| 19 | + assert resp.status == 401 |
| 20 | + async with get_http_session("default", "XXX") as session: |
| 21 | + resp = await session.get(f"http://localhost:{df_server.port}/") |
| 22 | + assert resp.status == 200 |
27 | 23 |
|
28 | 24 |
|
29 |
| -async def test_no_password_main_port(df_factory): |
30 |
| - with df_factory.create( |
31 |
| - port=1112, |
32 |
| - ) as server: |
33 |
| - async with aiohttp.ClientSession(auth=aiohttp.BasicAuth("default", "XXX")) as session: |
34 |
| - resp = await session.get(f"http://localhost:{server.port}/") |
35 |
| - assert resp.status == 200 |
36 |
| - async with aiohttp.ClientSession(auth=aiohttp.BasicAuth("random")) as session: |
37 |
| - resp = await session.get(f"http://localhost:{server.port}/") |
38 |
| - assert resp.status == 200 |
39 |
| - async with aiohttp.ClientSession() as session: |
40 |
| - resp = await session.get(f"http://localhost:{server.port}/") |
41 |
| - assert resp.status == 200 |
| 25 | +@dfly_args({"proactor_threads": "1", "requirepass": "XXX", "admin_port": 1113}) |
| 26 | +async def test_skip_metrics(df_server: DflyInstance): |
| 27 | + async with get_http_session("whoops", "whoops") as session: |
| 28 | + resp = await session.get(f"http://localhost:{df_server.port}/metrics") |
| 29 | + assert resp.status == 200 |
| 30 | + async with get_http_session("whoops", "whoops") as session: |
| 31 | + resp = await session.get(f"http://localhost:{df_server.admin_port}/metrics") |
| 32 | + assert resp.status == 200 |
| 33 | + |
| 34 | + |
| 35 | +async def test_no_password_main_port(df_server: DflyInstance): |
| 36 | + async with get_http_session("default", "XXX") as session: |
| 37 | + resp = await session.get(f"http://localhost:{df_server.port}/") |
| 38 | + assert resp.status == 200 |
| 39 | + async with get_http_session("random") as session: |
| 40 | + resp = await session.get(f"http://localhost:{df_server.port}/") |
| 41 | + assert resp.status == 200 |
| 42 | + async with get_http_session() as session: |
| 43 | + resp = await session.get(f"http://localhost:{df_server.port}/") |
| 44 | + assert resp.status == 200 |
| 45 | + |
42 | 46 |
|
| 47 | +@dfly_args( |
| 48 | + { |
| 49 | + "proactor_threads": "1", |
| 50 | + "requirepass": "XXX", |
| 51 | + "admin_port": 1113, |
| 52 | + "primary_port_http_enabled": True, |
| 53 | + "admin_nopass": True, |
| 54 | + } |
| 55 | +) |
| 56 | +async def test_no_password_on_admin(df_server: DflyInstance): |
| 57 | + async with get_http_session("default", "XXX") as session: |
| 58 | + resp = await session.get(f"http://localhost:{df_server.admin_port}/") |
| 59 | + assert resp.status == 200 |
| 60 | + async with get_http_session("random") as session: |
| 61 | + resp = await session.get(f"http://localhost:{df_server.admin_port}/") |
| 62 | + assert resp.status == 200 |
| 63 | + async with get_http_session() as session: |
| 64 | + resp = await session.get(f"http://localhost:{df_server.admin_port}/") |
| 65 | + assert resp.status == 200 |
43 | 66 |
|
44 |
| -async def test_no_password_on_admin(df_factory): |
45 |
| - with df_factory.create( |
46 |
| - port=1112, |
47 |
| - admin_port=1113, |
48 |
| - requirepass="XXX", |
49 |
| - primary_port_http_enabled=True, |
50 |
| - admin_nopass=True, |
51 |
| - ) as server: |
52 |
| - async with aiohttp.ClientSession(auth=aiohttp.BasicAuth("default", "XXX")) as session: |
53 |
| - resp = await session.get(f"http://localhost:{server.admin_port}/") |
| 67 | + |
| 68 | +@dfly_args({"proactor_threads": "1", "requirepass": "XXX", "admin_port": 1113}) |
| 69 | +async def test_password_on_admin(df_server: DflyInstance): |
| 70 | + async with get_http_session("default", "badpass") as session: |
| 71 | + resp = await session.get(f"http://localhost:{df_server.admin_port}/") |
| 72 | + assert resp.status == 401 |
| 73 | + async with get_http_session() as session: |
| 74 | + resp = await session.get(f"http://localhost:{df_server.admin_port}/") |
| 75 | + assert resp.status == 401 |
| 76 | + async with get_http_session("default", "XXX") as session: |
| 77 | + resp = await session.get(f"http://localhost:{df_server.admin_port}/") |
| 78 | + assert resp.status == 200 |
| 79 | + |
| 80 | + |
| 81 | +@dfly_args({"proactor_threads": "1", "expose_http_api": "true"}) |
| 82 | +async def test_no_password_on_http_api(df_server: DflyInstance): |
| 83 | + async with get_http_session("default", "XXX") as session: |
| 84 | + resp = await session.post(f"http://localhost:{df_server.port}/api", json=["ping"]) |
| 85 | + assert resp.status == 200 |
| 86 | + async with get_http_session("random") as session: |
| 87 | + resp = await session.post(f"http://localhost:{df_server.port}/api", json=["ping"]) |
| 88 | + assert resp.status == 200 |
| 89 | + async with get_http_session() as session: |
| 90 | + resp = await session.post(f"http://localhost:{df_server.port}/api", json=["ping"]) |
| 91 | + assert resp.status == 200 |
| 92 | + |
| 93 | + |
| 94 | +@dfly_args({"proactor_threads": "1", "expose_http_api": "true"}) |
| 95 | +async def test_http_api(df_server: DflyInstance): |
| 96 | + client = df_server.client() |
| 97 | + async with get_http_session() as session: |
| 98 | + body = '["set", "foo", "МайяХилли", "ex", "100"]' |
| 99 | + async with session.post(f"http://localhost:{df_server.port}/api", data=body) as resp: |
54 | 100 | assert resp.status == 200
|
55 |
| - async with aiohttp.ClientSession(auth=aiohttp.BasicAuth("random")) as session: |
56 |
| - resp = await session.get(f"http://localhost:{server.admin_port}/") |
| 101 | + text = await resp.text() |
| 102 | + assert text.strip() == '{"result":"OK"}' |
| 103 | + |
| 104 | + body = '["get", "foo"]' |
| 105 | + async with session.post(f"http://localhost:{df_server.port}/api", data=body) as resp: |
57 | 106 | assert resp.status == 200
|
58 |
| - async with aiohttp.ClientSession() as session: |
59 |
| - resp = await session.get(f"http://localhost:{server.admin_port}/") |
| 107 | + text = await resp.text() |
| 108 | + assert text.strip() == '{"result":"МайяХилли"}' |
| 109 | + |
| 110 | + body = '["foo", "bar"]' |
| 111 | + async with session.post(f"http://localhost:{df_server.port}/api", data=body) as resp: |
60 | 112 | assert resp.status == 200
|
| 113 | + text = await resp.text() |
| 114 | + assert text.strip() == '{"error": "unknown command `FOO`"}' |
61 | 115 |
|
| 116 | + assert await client.ttl("foo") > 0 |
62 | 117 |
|
63 |
| -async def test_password_on_admin(df_factory): |
64 |
| - with df_factory.create( |
65 |
| - port=1112, |
66 |
| - admin_port=1113, |
67 |
| - requirepass="XXX", |
68 |
| - ) as server: |
69 |
| - async with aiohttp.ClientSession(auth=aiohttp.BasicAuth("default", "badpass")) as session: |
70 |
| - resp = await session.get(f"http://localhost:{server.admin_port}/") |
71 |
| - assert resp.status == 401 |
72 |
| - async with aiohttp.ClientSession() as session: |
73 |
| - resp = await session.get(f"http://localhost:{server.admin_port}/") |
74 |
| - assert resp.status == 401 |
75 |
| - async with aiohttp.ClientSession(auth=aiohttp.BasicAuth("default", "XXX")) as session: |
76 |
| - resp = await session.get(f"http://localhost:{server.admin_port}/") |
77 |
| - assert resp.status == 200 |
| 118 | + |
| 119 | +@dfly_args({"proactor_threads": "1", "expose_http_api": "true", "requirepass": "XXX"}) |
| 120 | +async def test_password_on_http_api(df_server: DflyInstance): |
| 121 | + async with get_http_session("default", "badpass") as session: |
| 122 | + resp = await session.post(f"http://localhost:{df_server.port}/api", json=["ping"]) |
| 123 | + assert resp.status == 401 |
| 124 | + async with get_http_session() as session: |
| 125 | + resp = await session.post(f"http://localhost:{df_server.port}/api", json=["ping"]) |
| 126 | + assert resp.status == 401 |
| 127 | + async with get_http_session("default", "XXX") as session: |
| 128 | + resp = await session.post(f"http://localhost:{df_server.port}/api", json=["ping"]) |
| 129 | + assert resp.status == 200 |
0 commit comments