M app/insert_to_db.py => app/insert_to_db.py +1 -2
@@ 3,7 3,6 @@ from dotenv import load_dotenv
from pathlib import Path
import asyncio
import asyncpg
-import hashlib
import os
@@ 54,7 53,7 @@ async def insert_all_passwords():
if count % 200000 == 0:
# add batch of 10000 to db
await conn.executemany(
- '''INSERT INTO passwords (hash, occurences) VALUES ($1, $2);''',
+ '''INSERT INTO passwords (hash, occurences) VALUES ($1, $2);''', # noqa
tasks)
print(f'Approximately {100 / 555278657 * count}% done.')
tasks = []
M app/routes.py => app/routes.py +4 -1
@@ 33,6 33,9 @@ async def fetch_pw(password: str) -> Dict[str, Any]:
results = await stmt.fetch(password_hash)
await conn.close()
+ if not results:
+ return {}
+
return dict(results[0])
@@ 64,4 67,4 @@ async def api_get_post(request: web.Request) -> web.Response:
password = request.match_info["password"]
db_ret = await fetch_pw(password)
return web.json_response(
- { 'status': 'ok', 'password': password, 'data': db_ret})
+ {'status': 'ok', 'password': password, 'data': db_ret})
M test/conftest.py => test/conftest.py +13 -0
@@ 0,0 1,13 @@
+from app import init_app
+import pytest
+
+
+@pytest.fixture
+async def app():
+ app = await init_app()
+ yield app
+
+
+@pytest.fixture
+def client(loop, aiohttp_client, app):
+ return loop.run_until_complete(aiohttp_client(app))
M test/test_routes.py => test/test_routes.py +12 -19
@@ 1,28 1,21 @@
-from app import app_setup
+from unittest import mock
-async def test_index(aiohttp_client):
- app = app_setup()
- client = await aiohttp_client(app)
+async def test_index(client):
resp = await client.get('/')
assert resp.status == 200
text = await resp.text()
- assert '{"status": "success"}' in text
+ assert '{"status": 200}' in text
-async def test_does_pw_exist_without_argument(aiohttp_client):
- app = app_setup()
- client = await aiohttp_client(app)
- resp = await client.get('/check-pw')
- assert resp.status == 500
- text = await resp.text()
- assert '{"status": "failed", "reason": "\'password\'"}' in text
-
-
-async def test_does_pw_exist_with_argument(aiohttp_client):
- app = app_setup()
- client = await aiohttp_client(app)
- resp = await client.get('/check-pw?password=pwned')
+@mock.patch(
+ 'app.routes.fetch_pw',
+ return_value={
+ "hash": "5C1D8EAF2F254732680E8AC339B84F3266ECCBB5",
+ "occurences": 666})
+async def test_does_pw_exist_with_argument(hugo, client):
+ resp = await client.get('/api/pwned')
assert resp.status == 200
text = await resp.text()
- assert '{"status": "success", "password": "pwned"}' in text
+ assert '{"status": "ok", "password": "pwned"' in text
+ assert '"data": {"hash": "5C1D8EAF2' and '"occurences": 666}}' in text