clortho_test.py (2191B)
1 # 2 # Copyright 2018 by Brian C. Lane <bcl@brianlane.com> 3 # All Rights Reserved 4 # 5 # This program is free software; you can redistribute it and/or modify 6 # it under the terms of the GNU General Public License as published by 7 # the Free Software Foundation; either version 2 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU General Public License for more details. 14 # 15 # You should have received a copy of the GNU General Public License 16 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 # 18 import pytest 19 from aiohttp.test_utils import make_mocked_request 20 21 from clortho import get_client, setup_app, VERSION 22 23 @pytest.fixture 24 def cli(loop, test_client): 25 app = setup_app(loop) 26 app["keystore"] = {} 27 return loop.run_until_complete(test_client(app)) 28 29 async def test_version(cli): 30 resp = await cli.get("/keystore/version") 31 assert resp.status == 200 32 assert await resp.text() == "version: %s" % VERSION 33 34 async def test_post(cli): 35 resp = await cli.post("/keystore/test", data={"value": "test value"}) 36 assert resp.status == 200 37 assert await resp.text() == "OK" 38 39 async def test_get(cli): 40 resp = await cli.post("/keystore/test-get", data={"value": "test-get value"}) 41 assert resp.status == 200 42 assert await resp.text() == "OK" 43 44 resp = await cli.get("/keystore/test-get") 45 assert resp.status == 200 46 assert await resp.text() == "test-get value" 47 48 async def test_info(cli): 49 resp = await cli.get("/keystore/info") 50 assert resp.status == 200 51 52 def test_proxy_client(): 53 req = make_mocked_request("GET", "/keystore/version", headers={"X-Forwarded-For": "1.2.3.4, 5.6.7.8"}) 54 client = get_client(req) 55 assert client == "1.2.3.4" 56 57 def test_client_peer(): 58 class MockTransport(object): 59 def get_extra_info(self, ignore): 60 return ("1.2.3.4", 65535) 61 62 req = make_mocked_request("GET", "/keystore/version", transport=MockTransport()) 63 client = get_client(req) 64 assert client == "1.2.3.4"