planetmint/tests/web/test_assets.py
Jürgen Eckel 3afb2e6f3d
blackified with newest verion to satisfy CI
Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
2023-02-14 22:43:33 +01:00

65 lines
2.3 KiB
Python

# Copyright © 2020 Interplanetary Database Association e.V.,
# Planetmint and IPDB software contributors.
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
# Code is Apache-2.0 and docs are CC-BY-4.0
import pytest
from transactions.types.assets.create import Create
from ipld import marshal, multihash
ASSETS_ENDPOINT = "/api/v1/assets/"
def test_get_assets_with_empty_text_search(client):
res = client.get(ASSETS_ENDPOINT + "?search=")
assert res.json == {"status": 400, "message": "text_search cannot be empty"}
assert res.status_code == 400
def test_get_assets_with_missing_text_search(client):
res = client.get(ASSETS_ENDPOINT)
assert res.status_code == 400
@pytest.mark.bdb
def test_get_assets_tendermint(client, b, alice):
# test returns empty list when no assets are found
res = client.get(ASSETS_ENDPOINT + "?search=abc")
assert res.json == []
assert res.status_code == 200
# create asset
assets = [{"data": multihash(marshal({"msg": "abc"}))}]
tx = Create.generate([alice.public_key], [([alice.public_key], 1)], assets=assets).sign([alice.private_key])
b.store_bulk_transactions([tx])
# test that asset is returned
res = client.get(ASSETS_ENDPOINT + "?search=" + assets[0]["data"])
assert res.status_code == 200
assert len(res.json) == 1
assert res.json[0] == {"data": assets[0]["data"], "id": tx.id}
@pytest.mark.bdb
def test_get_assets_limit_tendermint(client, b, alice):
# create two assets
assets1 = [{"data": multihash(marshal({"msg": "abc 1"}))}]
assets2 = [{"data": multihash(marshal({"msg": "abc 2"}))}]
tx1 = Create.generate([alice.public_key], [([alice.public_key], 1)], assets=assets1).sign([alice.private_key])
tx2 = Create.generate([alice.public_key], [([alice.public_key], 1)], assets=assets2).sign([alice.private_key])
b.store_bulk_transactions([tx1])
b.store_bulk_transactions([tx2])
# test that both assets are returned without limit
res = client.get(ASSETS_ENDPOINT + "?search=" + assets1[0]["data"])
assert res.status_code == 200
assert len(res.json) == 1
# test that only one asset is returned when using limit=1
res = client.get(ASSETS_ENDPOINT + "?search=" + assets1[0]["data"] + "&limit=1")
assert res.status_code == 200
assert len(res.json) == 1