Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lnbits/core/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ async def create_webpush_subscription(
) -> WebPushSubscription:
await db.execute(
"""
INSERT INTO webpush_subscriptions (endpoint, user, data, host)
INSERT INTO webpush_subscriptions (endpoint, "user", data, host)
VALUES (?, ?, ?, ?)
""",
(
Expand All @@ -1302,17 +1302,19 @@ async def create_webpush_subscription(
return subscription


async def delete_webpush_subscription(endpoint: str, user: str) -> None:
await db.execute(
async def delete_webpush_subscription(endpoint: str, user: str) -> int:
resp = await db.execute(
"""DELETE FROM webpush_subscriptions WHERE endpoint = ? AND "user" = ?""",
(
endpoint,
user,
),
)
return resp.rowcount


async def delete_webpush_subscriptions(endpoint: str) -> None:
await db.execute(
async def delete_webpush_subscriptions(endpoint: str) -> int:
resp = await db.execute(
"DELETE FROM webpush_subscriptions WHERE endpoint = ?", (endpoint,)
)
return resp.rowcount
3 changes: 2 additions & 1 deletion lnbits/core/views/webpush_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ async def api_delete_webpush_subscription(
endpoint = unquote(
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:
logger.debug(exc)
raise HTTPException(
Expand Down
89 changes: 89 additions & 0 deletions tests/api/test_webpush_api.py
Original file line number Diff line number Diff line change
@@ -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