code quality fixes and cleanup
This commit is contained in:
parent
29b1b17967
commit
2bbbcd4d65
32 changed files with 4943 additions and 1368 deletions
File diff suppressed because it is too large
Load diff
|
|
@ -1,35 +1,41 @@
|
|||
import json
|
||||
from loguru import logger
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.abspath(
|
||||
os.path.join(os.path.dirname(__file__), '..', '..')))
|
||||
import sys
|
||||
|
||||
from loguru import logger
|
||||
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
|
||||
####
|
||||
|
||||
import string
|
||||
import random
|
||||
from NWCServiceProvider import NWCServiceProvider
|
||||
from unittest.mock import AsyncMock, patch
|
||||
import asyncio
|
||||
import string
|
||||
|
||||
import pytest
|
||||
|
||||
from nwcp import NWCServiceProvider
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def nwc_service_provider():
|
||||
return NWCServiceProvider("d7b5232fba0e02e32cfe26f20cdf2c803b27ecd81052c2dd5d17e5e1a333fe58", "")
|
||||
return NWCServiceProvider(
|
||||
"d7b5232fba0e02e32cfe26f20cdf2c803b27ecd81052c2dd5d17e5e1a333fe58", ""
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def nwc_service_provider2():
|
||||
return NWCServiceProvider("ce40821040275f72f3074a89770db3e2744b189f204807c867840eb58565de51", "")
|
||||
return NWCServiceProvider(
|
||||
"ce40821040275f72f3074a89770db3e2744b189f204807c867840eb58565de51", ""
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_supported_methods(nwc_service_provider):
|
||||
def make_invoice(provider, pubkey, content):
|
||||
return "invoice"
|
||||
nwc_service_provider.addRequestListener("make_invoice", make_invoice)
|
||||
s = nwc_service_provider.getSupportedMethods()
|
||||
|
||||
nwc_service_provider.add_request_listener("make_invoice", make_invoice)
|
||||
s = nwc_service_provider.get_supported_methods()
|
||||
assert s == ["make_invoice"]
|
||||
|
||||
|
||||
|
|
@ -38,14 +44,18 @@ async def test_encrytdecrypt(nwc_service_provider, nwc_service_provider2):
|
|||
content = "Hello World"
|
||||
expected_enc = "qVurNVISSl/9CfREIhk5Lg==?iv=QpCo5dI9gUcoLsSMLA7o7Q=="
|
||||
enc_a = nwc_service_provider._encrypt_content(
|
||||
content, nwc_service_provider2.public_key_hex, 21)
|
||||
content, nwc_service_provider2.public_key_hex, 21
|
||||
)
|
||||
enc_b = nwc_service_provider2._encrypt_content(
|
||||
content, nwc_service_provider.public_key_hex, 21)
|
||||
content, nwc_service_provider.public_key_hex, 21
|
||||
)
|
||||
|
||||
dec_a = nwc_service_provider2._decrypt_content(
|
||||
enc_a, nwc_service_provider.public_key_hex)
|
||||
enc_a, nwc_service_provider.public_key_hex
|
||||
)
|
||||
dec_b = nwc_service_provider._decrypt_content(
|
||||
enc_b, nwc_service_provider2.public_key_hex)
|
||||
enc_b, nwc_service_provider2.public_key_hex
|
||||
)
|
||||
|
||||
assert dec_a == content
|
||||
assert dec_b == content
|
||||
|
|
@ -62,15 +72,16 @@ async def test_signverify(nwc_service_provider, nwc_service_provider2):
|
|||
|
||||
tags = []
|
||||
for _ in range(random.choice([1, 2, 3, 4])):
|
||||
tags.append([random.choice(string.ascii_letters)+"_" +
|
||||
random.choice(string.ascii_letters), random.choice(string.ascii_letters)])
|
||||
tags.append(
|
||||
[
|
||||
random.choice(string.ascii_letters)
|
||||
+ "_"
|
||||
+ random.choice(string.ascii_letters),
|
||||
random.choice(string.ascii_letters),
|
||||
]
|
||||
)
|
||||
|
||||
event = {
|
||||
"kind": 1,
|
||||
"content": content,
|
||||
"tags": tags,
|
||||
"created_at": 1234567890
|
||||
}
|
||||
event = {"kind": 1, "content": content, "tags": tags, "created_at": 1234567890}
|
||||
|
||||
signed = nwc_service_provider._sign_event(event)
|
||||
assert nwc_service_provider2._verify_event(signed)
|
||||
|
|
@ -78,22 +89,17 @@ async def test_signverify(nwc_service_provider, nwc_service_provider2):
|
|||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle(nwc_service_provider, nwc_service_provider2):
|
||||
content = nwc_service_provider._json_dumps({
|
||||
"method": "pay_invoice",
|
||||
"params": {
|
||||
"invoice": "abc"
|
||||
}
|
||||
})
|
||||
content = nwc_service_provider._json_dumps(
|
||||
{"method": "pay_invoice", "params": {"invoice": "abc"}}
|
||||
)
|
||||
content = nwc_service_provider._encrypt_content(
|
||||
content, nwc_service_provider2.public_key_hex, 21)
|
||||
content, nwc_service_provider2.public_key_hex, 21
|
||||
)
|
||||
event = {
|
||||
"kind": 23194,
|
||||
"content": content,
|
||||
"tags": [
|
||||
["p", nwc_service_provider2.public_key_hex]
|
||||
],
|
||||
"created_at": 1234567890
|
||||
|
||||
"tags": [["p", nwc_service_provider2.public_key_hex]],
|
||||
"created_at": 1234567890,
|
||||
}
|
||||
signed = nwc_service_provider._sign_event(event)
|
||||
|
||||
|
|
@ -101,30 +107,20 @@ async def test_handle(nwc_service_provider, nwc_service_provider2):
|
|||
assert pubkey == nwc_service_provider.public_key_hex
|
||||
assert content["method"] == "pay_invoice"
|
||||
assert content["params"]["invoice"] == "abc"
|
||||
return [
|
||||
(
|
||||
{
|
||||
"preimage": "00000"
|
||||
return [({"preimage": "00000"}, None, [["r1", "v1"]])]
|
||||
|
||||
},
|
||||
None,
|
||||
[
|
||||
["r1", "v1"]
|
||||
]
|
||||
)
|
||||
]
|
||||
|
||||
async def _send_pass(obj):
|
||||
pass
|
||||
nwc_service_provider2._send=_send_pass
|
||||
nwc_service_provider2.addRequestListener(
|
||||
"pay_invoice", _handle_pay_invoice)
|
||||
|
||||
nwc_service_provider2._send = _send_pass
|
||||
nwc_service_provider2.add_request_listener("pay_invoice", _handle_pay_invoice)
|
||||
sent_events = await nwc_service_provider2._handle_request(signed)
|
||||
assert len(sent_events) == 1
|
||||
for revent in sent_events:
|
||||
assert nwc_service_provider2._verify_event(revent)
|
||||
content = nwc_service_provider2._decrypt_content(
|
||||
revent["content"], nwc_service_provider.public_key_hex)
|
||||
revent["content"], nwc_service_provider.public_key_hex
|
||||
)
|
||||
logger.debug(event)
|
||||
logger.debug(revent)
|
||||
content = json.loads(content)
|
||||
|
|
@ -138,7 +134,7 @@ async def test_handle(nwc_service_provider, nwc_service_provider2):
|
|||
e_tag = [tag for tag in tags if tag[0] == "e"]
|
||||
assert len(e_tag) == 1
|
||||
assert e_tag[0][1] == event["id"]
|
||||
|
||||
|
||||
p_tag = [tag for tag in tags if tag[0] == "p"]
|
||||
assert len(p_tag) == 1
|
||||
assert p_tag[0][1] == nwc_service_provider.public_key_hex
|
||||
assert p_tag[0][1] == nwc_service_provider.public_key_hex
|
||||
Loading…
Add table
Add a link
Reference in a new issue