test: check invalid events

This commit is contained in:
Vlad Stan 2023-02-01 13:32:09 +02:00
parent e1ed5fb2d0
commit ca3fcf6cd1
3 changed files with 76 additions and 19 deletions

View file

@ -1,17 +1,47 @@
import json
from typing import List, Optional
import pytest
from loguru import logger
from pydantic import BaseModel
from lnbits.extensions.nostrrelay.models import NostrEvent
FIXTURES_PATH = "tests/extensions/nostrrelay/fixture"
class EventFixture(BaseModel):
name: str
exception: Optional[str]
data: NostrEvent
def test_function_with_scenario_one():
print("Testing function with scenario one")
assert 1 + 1 == 2, f"Check addition value {1 + 1} does not match {2}"
data = get_fixture(f"{FIXTURES_PATH}/events.json")
print("### data", data)
@pytest.fixture
def valid_events() -> List[EventFixture]:
data = get_fixture("events")
return [EventFixture.parse_obj(e) for e in data["valid"]]
@pytest.fixture
def invalid_events() -> List[EventFixture]:
data = get_fixture("events")
return [EventFixture.parse_obj(e) for e in data["invalid"]]
def test_event_id_and_signature_ok(valid_events: List[EventFixture]):
for f in valid_events:
try:
f.data.check_signature()
except Exception as e:
logger.error(f"Failed for fixture: '{f.name}'")
raise e
def test_event_id_and_signature_invalid(invalid_events: List[EventFixture]):
for f in invalid_events:
print("### test_event_id_and_signature_invalid", f.name)
with pytest.raises(ValueError, match=f.exception) as e_info:
print("### e.info", e_info)
f.data.check_signature()
def get_fixture(file):
@ -19,6 +49,6 @@ def get_fixture(file):
Read the content of the JSON file.
"""
with open(file) as f:
with open(f"{FIXTURES_PATH}/{file}.json") as f:
raw_data = json.load(f)
return raw_data