[test] webpush_api endpoints (#2534)

* test: webpush_api endpoints

* fix: SQL quote for `user`
This commit is contained in:
Vlad Stan 2024-05-24 00:23:32 +03:00 committed by GitHub
parent ae60b4517c
commit 93965bc5b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 98 additions and 6 deletions

View File

@ -1287,7 +1287,7 @@ async def create_webpush_subscription(
) -> WebPushSubscription: ) -> WebPushSubscription:
await db.execute( await db.execute(
""" """
INSERT INTO webpush_subscriptions (endpoint, user, data, host) INSERT INTO webpush_subscriptions (endpoint, "user", data, host)
VALUES (?, ?, ?, ?) VALUES (?, ?, ?, ?)
""", """,
( (
@ -1302,17 +1302,19 @@ async def create_webpush_subscription(
return subscription return subscription
async def delete_webpush_subscription(endpoint: str, user: str) -> None: async def delete_webpush_subscription(endpoint: str, user: str) -> int:
await db.execute( resp = await db.execute(
"""DELETE FROM webpush_subscriptions WHERE endpoint = ? AND "user" = ?""", """DELETE FROM webpush_subscriptions WHERE endpoint = ? AND "user" = ?""",
( (
endpoint, endpoint,
user, user,
), ),
) )
return resp.rowcount
async def delete_webpush_subscriptions(endpoint: str) -> None: async def delete_webpush_subscriptions(endpoint: str) -> int:
await db.execute( resp = await db.execute(
"DELETE FROM webpush_subscriptions WHERE endpoint = ?", (endpoint,) "DELETE FROM webpush_subscriptions WHERE endpoint = ?", (endpoint,)
) )
return resp.rowcount

View File

@ -67,7 +67,8 @@ async def api_delete_webpush_subscription(
endpoint = unquote( endpoint = unquote(
base64.b64decode(str(request.query_params.get("endpoint"))).decode("utf-8") base64.b64decode(str(request.query_params.get("endpoint"))).decode("utf-8")
) )
await delete_webpush_subscription(endpoint, wallet.wallet.user) count = await delete_webpush_subscription(endpoint, wallet.wallet.user)
return {"count": count}
except Exception as exc: except Exception as exc:
logger.debug(exc) logger.debug(exc)
raise HTTPException( raise HTTPException(

View File

@ -0,0 +1,89 @@
from http import HTTPStatus
import pytest
@pytest.mark.asyncio
async def test_create___bad_body(client, adminkey_headers_from):
response = await client.post(
"/api/v1/webpush",
headers=adminkey_headers_from,
json={"subscription": "bad_json"},
)
assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
@pytest.mark.asyncio
async def test_create___missing_fields(client, adminkey_headers_from):
response = await client.post(
"/api/v1/webpush",
headers=adminkey_headers_from,
json={"subscription": """{"a": "x"}"""},
)
assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
@pytest.mark.asyncio
async def test_create___bad_access_key(client, inkey_headers_from):
response = await client.post(
"/api/v1/webpush",
headers=inkey_headers_from,
json={"subscription": """{"a": "x"}"""},
)
assert response.status_code == HTTPStatus.UNAUTHORIZED
@pytest.mark.asyncio
async def test_delete__bad_endpoint_format(client, adminkey_headers_from):
response = await client.delete(
"/api/v1/webpush",
params={"endpoint": "https://this.should.be.base64.com"},
headers=adminkey_headers_from,
)
assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
@pytest.mark.asyncio
async def test_delete__no_endpoint_param(client, adminkey_headers_from):
response = await client.delete(
"/api/v1/webpush",
headers=adminkey_headers_from,
)
assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
@pytest.mark.asyncio
async def test_delete__no_endpoint_found(client, adminkey_headers_from):
response = await client.delete(
"/api/v1/webpush",
params={"endpoint": "aHR0cHM6Ly9kZW1vLmxuYml0cy5jb20="},
headers=adminkey_headers_from,
)
assert response.status_code == HTTPStatus.OK
assert response.json()["count"] == 0
@pytest.mark.asyncio
async def test_delete__bad_access_key(client, inkey_headers_from):
response = await client.delete(
"/api/v1/webpush",
headers=inkey_headers_from,
)
assert response.status_code == HTTPStatus.UNAUTHORIZED
@pytest.mark.asyncio
async def test_create_and_delete(client, adminkey_headers_from):
response = await client.post(
"/api/v1/webpush",
headers=adminkey_headers_from,
json={"subscription": """{"endpoint": "https://demo.lnbits.com"}"""},
)
assert response.status_code == HTTPStatus.CREATED
response = await client.delete(
"/api/v1/webpush",
params={"endpoint": "aHR0cHM6Ly9kZW1vLmxuYml0cy5jb20="},
headers=adminkey_headers_from,
)
assert response.status_code == HTTPStatus.OK
assert response.json()["count"] == 1