CI: Test core/views/generic.py (#772)

* Adds tests for GET /wallet

* Update `httpx` to `0.23.0` and `http-core` to `0.15.0` in `venv` installation path

* Fix `secp256k1 = "==0.14.0"` and `cffi = "==1.15.0"`
This commit is contained in:
calle 2022-07-23 10:39:58 +02:00 committed by GitHub
parent fd0d6bffce
commit 96af5fc3a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 258 additions and 156 deletions

View file

@ -51,32 +51,42 @@ async def db():
@pytest_asyncio.fixture(scope="session")
async def from_user_wallet():
async def from_user():
user = await create_account()
yield user
@pytest_asyncio.fixture(scope="session")
async def from_wallet(from_user):
user = from_user
wallet = await create_wallet(user_id=user.id, wallet_name="test_wallet_from")
await credit_wallet(
wallet_id=wallet.id,
amount=99999999,
)
# print("new from_user_wallet:", wallet)
yield user, wallet
yield wallet
@pytest_asyncio.fixture(scope="session")
async def to_user_wallet():
async def to_user():
user = await create_account()
yield user
@pytest_asyncio.fixture(scope="session")
async def to_wallet(to_user):
user = to_user
wallet = await create_wallet(user_id=user.id, wallet_name="test_wallet_to")
await credit_wallet(
wallet_id=wallet.id,
amount=99999999,
)
# print("new to_user_wallet:", wallet)
yield user, wallet
yield wallet
@pytest_asyncio.fixture(scope="session")
async def inkey_headers_from(from_user_wallet):
_, wallet = from_user_wallet
async def inkey_headers_from(from_wallet):
wallet = from_wallet
yield {
"X-Api-Key": wallet.inkey,
"Content-type": "application/json",
@ -84,8 +94,8 @@ async def inkey_headers_from(from_user_wallet):
@pytest_asyncio.fixture(scope="session")
async def adminkey_headers_from(from_user_wallet):
_, wallet = from_user_wallet
async def adminkey_headers_from(from_wallet):
wallet = from_wallet
yield {
"X-Api-Key": wallet.adminkey,
"Content-type": "application/json",
@ -93,8 +103,8 @@ async def adminkey_headers_from(from_user_wallet):
@pytest_asyncio.fixture(scope="session")
async def inkey_headers_to(to_user_wallet):
_, wallet = to_user_wallet
async def inkey_headers_to(to_wallet):
wallet = to_wallet
yield {
"X-Api-Key": wallet.inkey,
"Content-type": "application/json",
@ -102,8 +112,8 @@ async def inkey_headers_to(to_user_wallet):
@pytest_asyncio.fixture(scope="session")
async def adminkey_headers_to(to_user_wallet):
_, wallet = to_user_wallet
async def adminkey_headers_to(to_wallet):
wallet = to_wallet
yield {
"X-Api-Key": wallet.adminkey,
"Content-type": "application/json",
@ -111,18 +121,13 @@ async def adminkey_headers_to(to_user_wallet):
@pytest_asyncio.fixture(scope="session")
async def invoice(to_user_wallet):
_, wallet = to_user_wallet
async def invoice(to_wallet):
wallet = to_wallet
data = await get_random_invoice_data()
invoiceData = CreateInvoiceData(**data)
# print("--------- New invoice!")
# print("wallet:")
# print(wallet)
stuff_lock = asyncio.Lock()
async with stuff_lock:
invoice = await api_payments_create_invoice(invoiceData, wallet)
await asyncio.sleep(1)
# print("invoice")
# print(invoice)
yield invoice
del invoice

View file

@ -11,11 +11,26 @@ async def test_core_views_generic(client):
assert response.status_code == 200
# check GET /api/v1/wallet: wallet info
# check GET /api/v1/wallet with inkey: wallet info, no balance
@pytest.mark.asyncio
async def test_get_wallet(client, inkey_headers_to):
async def test_get_wallet_inkey(client, inkey_headers_to):
response = await client.get("/api/v1/wallet", headers=inkey_headers_to)
assert response.status_code < 300
assert response.status_code == 200
result = response.json()
assert "name" in result
assert "balance" in result
assert "id" not in result
# check GET /api/v1/wallet with adminkey: wallet info with balance
@pytest.mark.asyncio
async def test_get_wallet_adminkey(client, adminkey_headers_to):
response = await client.get("/api/v1/wallet", headers=adminkey_headers_to)
assert response.status_code == 200
result = response.json()
assert "name" in result
assert "balance" in result
assert "id" in result
# check POST /api/v1/payments: invoice creation

View file

@ -6,4 +6,94 @@ from tests.conftest import client
@pytest.mark.asyncio
async def test_core_views_generic(client):
response = await client.get("/")
assert response.status_code == 200
assert response.status_code == 200, (
str(response.url) + " " + str(response.status_code)
)
# check GET /wallet: wallet info
@pytest.mark.asyncio
async def test_get_wallet(client):
response = await client.get("wallet")
assert response.status_code == 307, ( # redirect not modified
str(response.url) + " " + str(response.status_code)
)
# check GET /wallet: do not allow redirects, expect code 307
@pytest.mark.asyncio
async def test_get_wallet_no_redirect(client):
response = await client.get("wallet", follow_redirects=False)
assert response.status_code == 307, (
str(response.url) + " " + str(response.status_code)
)
# determine the next redirect location
request = client.build_request("GET", "wallet")
i = 0
while request is not None:
response = await client.send(request)
request = response.next_request
if i == 0:
assert response.status_code == 307, ( # first redirect
str(response.url) + " " + str(response.status_code)
)
elif i == 1:
assert response.status_code == 200, ( # then get the actual page
str(response.url) + " " + str(response.status_code)
)
i += 1
# check GET /wallet: wrong user, expect 204
@pytest.mark.asyncio
async def test_get_wallet_with_nonexistent_user(client):
response = await client.get("wallet", params={"usr": "1"})
assert response.status_code == 204, (
str(response.url) + " " + str(response.status_code)
)
# check GET /wallet: with user
@pytest.mark.asyncio
async def test_get_wallet_with_user(client, to_user):
response = await client.get("wallet", params={"usr": to_user.id})
assert response.status_code == 307, (
str(response.url) + " " + str(response.status_code)
)
# determine the next redirect location
request = client.build_request("GET", "wallet", params={"usr": to_user.id})
i = 0
while request is not None:
response = await client.send(request)
request = response.next_request
if i == 0:
assert response.status_code == 307, ( # first redirect
str(response.url) + " " + str(response.status_code)
)
elif i == 1:
assert response.status_code == 200, ( # then get the actual page
str(response.url) + " " + str(response.status_code)
)
i += 1
# check GET /wallet: wallet and user
@pytest.mark.asyncio
async def test_get_wallet_with_user_and_wallet(client, to_user, to_wallet):
response = await client.get(
"wallet", params={"usr": to_user.id, "wal": to_wallet.id}
)
assert response.status_code == 200, (
str(response.url) + " " + str(response.status_code)
)
# check GET /wallet: wrong wallet and user, expect 204
@pytest.mark.asyncio
async def test_get_wallet_with_user_and_wrong_wallet(client, to_user, to_wallet):
response = await client.get("wallet", params={"usr": to_user.id, "wal": "1"})
assert response.status_code == 204, (
str(response.url) + " " + str(response.status_code)
)