mirror of
https://github.com/yzernik/squeaknode.git
synced 2026-08-13 12:33:25 +02:00
Fixed itests (#2014)
This commit is contained in:
parent
93d12ec6c9
commit
a8ba458fad
6 changed files with 28 additions and 388 deletions
|
|
@ -35,7 +35,7 @@ from tests.util import create_signing_profile
|
|||
from tests.util import delete_profile
|
||||
from tests.util import generate_private_key
|
||||
from tests.util import get_public_key
|
||||
from tests.util import open_peer_connection
|
||||
from tests.util import saved_peer
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
|
|
@ -56,6 +56,12 @@ def other_admin_stub():
|
|||
yield squeak_admin_pb2_grpc.SqueakAdminStub(admin_channel)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def admin_peer(other_admin_stub):
|
||||
with saved_peer(other_admin_stub, 'squeaknode', 'squeaknode', 18777):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def private_key():
|
||||
# Create a private key
|
||||
|
|
@ -201,14 +207,3 @@ def random_image_base64_string(random_image):
|
|||
# peer_id=peer_id,
|
||||
# )
|
||||
# )
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def connected_tcp_peer_id(other_admin_stub):
|
||||
with open_peer_connection(
|
||||
other_admin_stub,
|
||||
"test_peer",
|
||||
"squeaknode",
|
||||
18777,
|
||||
):
|
||||
yield
|
||||
|
|
|
|||
|
|
@ -32,19 +32,13 @@ from proto import lnd_pb2 as ln
|
|||
from proto import squeak_admin_pb2
|
||||
from tests.util import channel
|
||||
from tests.util import clear_sell_price
|
||||
from tests.util import connect_squeak_peer
|
||||
from tests.util import create_contact_profile
|
||||
from tests.util import create_saved_peer
|
||||
from tests.util import create_signing_profile
|
||||
from tests.util import delete_profile
|
||||
from tests.util import delete_squeak
|
||||
from tests.util import download_offers
|
||||
from tests.util import download_squeak
|
||||
from tests.util import download_squeaks
|
||||
from tests.util import download_squeaks_for_pubkey
|
||||
from tests.util import free_price
|
||||
from tests.util import get_connected_peer
|
||||
from tests.util import get_connected_peers
|
||||
from tests.util import get_default_peer_port
|
||||
from tests.util import get_external_address
|
||||
from tests.util import get_hash
|
||||
|
|
@ -58,15 +52,11 @@ from tests.util import get_squeak_display
|
|||
from tests.util import get_squeak_profile
|
||||
from tests.util import import_signing_profile
|
||||
from tests.util import make_squeak
|
||||
from tests.util import open_peer_connection
|
||||
from tests.util import peer_connection
|
||||
from tests.util import public_key_from_hex
|
||||
from tests.util import send_coins
|
||||
from tests.util import set_sell_price
|
||||
from tests.util import subscribe_connected_peers
|
||||
from tests.util import subscribe_squeak_ancestor_entries
|
||||
from tests.util import subscribe_squeak_entry
|
||||
from tests.util import subscribe_squeaks_for_address
|
||||
|
||||
|
||||
def test_get_network(admin_stub):
|
||||
|
|
@ -650,17 +640,13 @@ def test_send_coins(admin_stub, other_admin_stub):
|
|||
def test_buy_squeak(
|
||||
admin_stub,
|
||||
other_admin_stub,
|
||||
connected_tcp_peer_id,
|
||||
signing_profile_id,
|
||||
saved_squeak_hash,
|
||||
admin_peer,
|
||||
):
|
||||
# Download squeak
|
||||
download_squeak(other_admin_stub, saved_squeak_hash)
|
||||
|
||||
# Download offer
|
||||
download_offers(other_admin_stub, saved_squeak_hash)
|
||||
time.sleep(5)
|
||||
|
||||
# Get the sent offers from the seller node
|
||||
get_sent_offers_response = admin_stub.GetSentOffers(
|
||||
squeak_admin_pb2.GetSentOffersRequest(),
|
||||
|
|
@ -804,8 +790,8 @@ def test_buy_squeak(
|
|||
def test_download_free_squeak(
|
||||
admin_stub,
|
||||
other_admin_stub,
|
||||
connected_tcp_peer_id,
|
||||
saved_squeak_hash,
|
||||
admin_peer,
|
||||
):
|
||||
with free_price(admin_stub):
|
||||
# Download squeak
|
||||
|
|
@ -815,10 +801,6 @@ def test_download_free_squeak(
|
|||
assert download_result.number_downloaded == 1
|
||||
assert download_result.number_requested == 1
|
||||
|
||||
# Download offer
|
||||
download_offers(other_admin_stub, saved_squeak_hash)
|
||||
time.sleep(5)
|
||||
|
||||
# Get the squeak display item
|
||||
get_squeak_display_entry = get_squeak_display(
|
||||
other_admin_stub, saved_squeak_hash)
|
||||
|
|
@ -830,9 +812,9 @@ def test_download_free_squeak(
|
|||
def test_download_single_squeak(
|
||||
admin_stub,
|
||||
other_admin_stub,
|
||||
connected_tcp_peer_id,
|
||||
signing_profile_id,
|
||||
saved_squeak_hash,
|
||||
admin_peer,
|
||||
):
|
||||
|
||||
with subscribe_squeak_entry(other_admin_stub, saved_squeak_hash) as subscription_queue, \
|
||||
|
|
@ -862,10 +844,6 @@ def test_download_single_squeak(
|
|||
other_admin_stub, saved_squeak_hash)
|
||||
assert squeak_display_entry is not None
|
||||
|
||||
# Download offer
|
||||
download_offers(other_admin_stub, saved_squeak_hash)
|
||||
time.sleep(5)
|
||||
|
||||
# Get the buy offer
|
||||
get_buy_offers_response = other_admin_stub.GetBuyOffers(
|
||||
squeak_admin_pb2.GetBuyOffersRequest(
|
||||
|
|
@ -886,49 +864,6 @@ def test_download_single_squeak(
|
|||
assert item[0].squeak_hash == saved_squeak_hash
|
||||
|
||||
|
||||
def test_download_squeaks_for_pubkey(
|
||||
admin_stub,
|
||||
other_admin_stub,
|
||||
connected_tcp_peer_id,
|
||||
signing_profile_id,
|
||||
saved_squeak_hash,
|
||||
):
|
||||
squeak_profile = get_squeak_profile(admin_stub, signing_profile_id)
|
||||
squeak_profile_pubkey = squeak_profile.pubkey
|
||||
|
||||
with subscribe_squeaks_for_address(other_admin_stub, squeak_profile_pubkey) as subscription_queue:
|
||||
|
||||
# Get the squeak display item (should be empty)
|
||||
squeak_display_entry = get_squeak_display(
|
||||
other_admin_stub, saved_squeak_hash)
|
||||
assert squeak_display_entry is None
|
||||
|
||||
# Get buy offers for the squeak hash (should be empty)
|
||||
get_buy_offers_response = other_admin_stub.GetBuyOffers(
|
||||
squeak_admin_pb2.GetBuyOffersRequest(
|
||||
squeak_hash=saved_squeak_hash,
|
||||
)
|
||||
)
|
||||
# print(get_buy_offers_response)
|
||||
assert len(get_buy_offers_response.offers) == 0
|
||||
|
||||
# Download squeaks for address
|
||||
download_result = download_squeaks_for_pubkey(
|
||||
other_admin_stub, squeak_profile_pubkey)
|
||||
assert download_result.number_downloaded == 1
|
||||
assert download_result.number_requested == 10
|
||||
|
||||
# Get the squeak display item
|
||||
squeak_display_entry = get_squeak_display(
|
||||
other_admin_stub, saved_squeak_hash)
|
||||
assert squeak_display_entry is not None
|
||||
|
||||
item = subscription_queue.get()
|
||||
print("item:")
|
||||
print(item)
|
||||
assert item.squeak_hash == saved_squeak_hash
|
||||
|
||||
|
||||
def test_like_squeak(admin_stub, saved_squeak_hash):
|
||||
# Get the squeak display item
|
||||
get_squeak_display_entry = get_squeak_display(
|
||||
|
|
@ -966,154 +901,6 @@ def test_like_squeak(admin_stub, saved_squeak_hash):
|
|||
)
|
||||
|
||||
|
||||
def test_connect_peer(admin_stub, other_admin_stub):
|
||||
# connected_peers = get_connected_peers(admin_stub)
|
||||
# assert len(connected_peers) == 0
|
||||
# other_connected_peers = get_connected_peers(other_admin_stub)
|
||||
# assert len(other_connected_peers) == 0
|
||||
|
||||
with subscribe_connected_peers(other_admin_stub) as subscription_queue:
|
||||
with open_peer_connection(
|
||||
other_admin_stub,
|
||||
"test_peer",
|
||||
"squeaknode",
|
||||
18777,
|
||||
):
|
||||
connected_peers = get_connected_peers(admin_stub)
|
||||
print(connected_peers)
|
||||
assert len(connected_peers) == 1
|
||||
print("connected_peers: {}".format(connected_peers))
|
||||
other_connected_peers = get_connected_peers(other_admin_stub)
|
||||
assert len(other_connected_peers) == 1
|
||||
print("other_connected_peers: {}".format(other_connected_peers))
|
||||
assert other_connected_peers[0].peer_address.host == "squeaknode"
|
||||
assert other_connected_peers[0].peer_address.port == 18777
|
||||
connected_peer = get_connected_peer(
|
||||
other_admin_stub, "squeaknode", 18777)
|
||||
assert connected_peer is not None
|
||||
|
||||
# Get item from queue
|
||||
item = subscription_queue.get()
|
||||
print("item:")
|
||||
print(item)
|
||||
assert len(item) == 1
|
||||
|
||||
time.sleep(2)
|
||||
connected_peers = get_connected_peers(admin_stub)
|
||||
assert len(connected_peers) == 0
|
||||
other_connected_peers = get_connected_peers(other_admin_stub)
|
||||
assert len(other_connected_peers) == 0
|
||||
|
||||
# Get item from queue
|
||||
item = subscription_queue.get()
|
||||
print("item:")
|
||||
print(item)
|
||||
assert len(item) == 0
|
||||
|
||||
|
||||
def test_connect_invalid_peer_address(admin_stub):
|
||||
with pytest.raises(Exception) as excinfo:
|
||||
# Try to connect the peer and fail
|
||||
connect_squeak_peer(
|
||||
admin_stub,
|
||||
host="fake_peer_host_56789",
|
||||
port=12345,
|
||||
)
|
||||
assert "Name or service not known" in str(excinfo.value)
|
||||
|
||||
|
||||
def test_get_squeak_by_lookup(
|
||||
admin_stub,
|
||||
other_admin_stub,
|
||||
connected_tcp_peer_id,
|
||||
signing_profile_id,
|
||||
saved_squeak_hash,
|
||||
):
|
||||
# Get the squeak profile
|
||||
squeak_profile = get_squeak_profile(admin_stub, signing_profile_id)
|
||||
squeak_profile_pubkey = squeak_profile.pubkey
|
||||
squeak_profile.profile_name
|
||||
|
||||
# Get the squeak display item
|
||||
squeak_display_entry = get_squeak_display(
|
||||
other_admin_stub, saved_squeak_hash)
|
||||
assert squeak_display_entry is None
|
||||
|
||||
# Sync squeaks
|
||||
download_result = download_squeaks(
|
||||
other_admin_stub,
|
||||
[squeak_profile_pubkey],
|
||||
-1,
|
||||
-1,
|
||||
None,
|
||||
)
|
||||
print(download_result)
|
||||
assert download_result.number_downloaded == 1
|
||||
assert download_result.number_requested == 10
|
||||
|
||||
# Get the squeak display item
|
||||
squeak_display_entry = get_squeak_display(
|
||||
other_admin_stub, saved_squeak_hash)
|
||||
assert squeak_display_entry.squeak_hash == saved_squeak_hash
|
||||
|
||||
|
||||
def test_subscribe_squeaks(
|
||||
admin_stub,
|
||||
other_admin_stub,
|
||||
signing_profile_id,
|
||||
):
|
||||
|
||||
# Get the squeak profile
|
||||
squeak_profile = get_squeak_profile(
|
||||
admin_stub, signing_profile_id)
|
||||
squeak_profile_pubkey = squeak_profile.pubkey
|
||||
squeak_profile_name = squeak_profile.profile_name
|
||||
|
||||
# Add the contact profile to the other server and set the profile to be following
|
||||
public_key = public_key_from_hex(squeak_profile_pubkey)
|
||||
contact_profile_id = create_contact_profile(
|
||||
other_admin_stub, squeak_profile_name, public_key)
|
||||
other_admin_stub.SetSqueakProfileFollowing(
|
||||
squeak_admin_pb2.SetSqueakProfileFollowingRequest(
|
||||
profile_id=contact_profile_id,
|
||||
following=True,
|
||||
)
|
||||
)
|
||||
|
||||
with free_price(admin_stub), \
|
||||
open_peer_connection(
|
||||
other_admin_stub,
|
||||
"test_peer",
|
||||
"squeaknode",
|
||||
18777,
|
||||
):
|
||||
# Create a new squeak using the new profile
|
||||
make_squeak_content = "Hello this message should be subscribed!"
|
||||
make_squeak_hash = make_squeak(
|
||||
admin_stub,
|
||||
signing_profile_id,
|
||||
make_squeak_content,
|
||||
)
|
||||
|
||||
time.sleep(2)
|
||||
|
||||
# Get the squeak display item
|
||||
squeak_display_entry = get_squeak_display(
|
||||
other_admin_stub,
|
||||
make_squeak_hash,
|
||||
)
|
||||
assert squeak_display_entry is not None
|
||||
|
||||
# Get the squeak display item
|
||||
get_squeak_display_entry = get_squeak_display(
|
||||
other_admin_stub,
|
||||
make_squeak_hash,
|
||||
)
|
||||
assert (
|
||||
get_squeak_display_entry.content_str == make_squeak_content
|
||||
)
|
||||
|
||||
|
||||
def test_search(admin_stub, signing_profile_id):
|
||||
make_squeak_content = "Just some random weird text."
|
||||
make_squeak_hash = make_squeak(
|
||||
|
|
|
|||
|
|
@ -84,6 +84,24 @@ def peer_connection(node_stub, lightning_host, remote_pubkey):
|
|||
time.sleep(2)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def saved_peer(node_stub, peer_name, host, port):
|
||||
# Create a new peer
|
||||
peer_id = create_saved_peer(
|
||||
node_stub,
|
||||
peer_name,
|
||||
host,
|
||||
port,
|
||||
)
|
||||
yield peer_id
|
||||
# Delete the peer
|
||||
node_stub.DeletePeer(
|
||||
squeak_admin_pb2.DeletePeerRequest(
|
||||
peer_id=peer_id,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def free_price(node_stub):
|
||||
# Set the price to zero
|
||||
|
|
@ -146,59 +164,6 @@ def channel(node_stub, remote_pubkey, amount):
|
|||
close_channel(node_stub, channel_point)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def open_peer_connection(node_stub, peer_name, peer_host, peer_port):
|
||||
try:
|
||||
# Connect the peer
|
||||
connect_squeak_peer(node_stub, peer_host, peer_port)
|
||||
# node_stub.ConnectPeer(
|
||||
# squeak_admin_pb2.ConnectPeerRequest(
|
||||
# peer_address=squeak_admin_pb2.PeerAddress(
|
||||
# network="IPV4",
|
||||
# host=peer_host,
|
||||
# port=peer_port,
|
||||
# )
|
||||
# )
|
||||
# )
|
||||
yield
|
||||
except Exception as e:
|
||||
print("Failed to connect to peer: {}:{}.".format(peer_host, peer_port))
|
||||
print(e)
|
||||
raise
|
||||
finally:
|
||||
# Disconnect the peer
|
||||
disconnect_squeak_peer(node_stub, peer_host, peer_port)
|
||||
# node_stub.DisconnectPeer(
|
||||
# squeak_admin_pb2.DisconnectPeerRequest(
|
||||
# peer_address=squeak_admin_pb2.PeerAddress(
|
||||
# network="IPV4",
|
||||
# host=peer_host,
|
||||
# port=peer_port,
|
||||
# )
|
||||
# )
|
||||
# )
|
||||
|
||||
|
||||
def get_connected_peers(node_stub):
|
||||
get_connected_peers_response = node_stub.GetConnectedPeers(
|
||||
squeak_admin_pb2.GetConnectedPeersRequest()
|
||||
)
|
||||
return get_connected_peers_response.connected_peers
|
||||
|
||||
|
||||
def get_connected_peer(node_stub, host, port):
|
||||
get_connected_peer_response = node_stub.GetConnectedPeer(
|
||||
squeak_admin_pb2.GetConnectedPeerRequest(
|
||||
peer_address=squeak_admin_pb2.PeerAddress(
|
||||
network="IPV4",
|
||||
host=host,
|
||||
port=port,
|
||||
)
|
||||
)
|
||||
)
|
||||
return get_connected_peer_response.connected_peer
|
||||
|
||||
|
||||
def create_saved_peer(node_stub, name, host, port):
|
||||
create_peer_response = node_stub.CreatePeer(
|
||||
squeak_admin_pb2.CreatePeerRequest(
|
||||
|
|
@ -236,24 +201,6 @@ def get_search_squeaks(node_stub, search_text):
|
|||
return get_search_squeak_display_response.squeak_display_entries
|
||||
|
||||
|
||||
@contextmanager
|
||||
def subscribe_connected_peers(node_stub):
|
||||
q = queue.Queue()
|
||||
subscribe_connected_peers_response = node_stub.SubscribeConnectedPeers(
|
||||
squeak_admin_pb2.SubscribeConnectedPeersRequest()
|
||||
)
|
||||
|
||||
def enqueue_results():
|
||||
for result in subscribe_connected_peers_response:
|
||||
q.put(result.connected_peers)
|
||||
|
||||
threading.Thread(
|
||||
target=enqueue_results,
|
||||
).start()
|
||||
yield q
|
||||
subscribe_connected_peers_response.cancel()
|
||||
|
||||
|
||||
def get_squeak_display(node_stub, squeak_hash):
|
||||
get_squeak_display_response = node_stub.GetSqueakDisplay(
|
||||
squeak_admin_pb2.GetSqueakDisplayRequest(
|
||||
|
|
@ -468,32 +415,6 @@ def pending_channels(node_stub):
|
|||
)
|
||||
|
||||
|
||||
def connect_squeak_peer(node_stub, host, port):
|
||||
connect_peer_response = node_stub.ConnectPeer(
|
||||
squeak_admin_pb2.ConnectPeerRequest(
|
||||
peer_address=squeak_admin_pb2.PeerAddress(
|
||||
network="IPV4",
|
||||
host=host,
|
||||
port=port,
|
||||
)
|
||||
)
|
||||
)
|
||||
return connect_peer_response
|
||||
|
||||
|
||||
def disconnect_squeak_peer(node_stub, host, port):
|
||||
disconnect_peer_response = node_stub.DisconnectPeer(
|
||||
squeak_admin_pb2.DisconnectPeerRequest(
|
||||
peer_address=squeak_admin_pb2.PeerAddress(
|
||||
network="IPV4",
|
||||
host=host,
|
||||
port=port,
|
||||
)
|
||||
)
|
||||
)
|
||||
return disconnect_peer_response
|
||||
|
||||
|
||||
def connect_peer(node_stub, pubkey, host):
|
||||
lightning_address = lnd_pb2.LightningAddress(
|
||||
pubkey=pubkey,
|
||||
|
|
|
|||
|
|
@ -874,21 +874,6 @@ class SqueakAdminServerHandler(object):
|
|||
squeak_display_entries=squeak_display_msgs
|
||||
)
|
||||
|
||||
def handle_connect_peer(self, request):
|
||||
logger.info("peer address msg: {}".format(request.peer_address))
|
||||
peer_address = message_to_peer_address(request.peer_address)
|
||||
logger.info(
|
||||
"Handle connect peer with peer address: {}".format(peer_address))
|
||||
self.squeak_controller.connect_peer(peer_address)
|
||||
return squeak_admin_pb2.ConnectPeerReply()
|
||||
|
||||
def handle_disconnect_peer(self, request):
|
||||
peer_address = message_to_peer_address(request.peer_address)
|
||||
logger.info(
|
||||
"Handle disconnect peer with peer address: {}".format(peer_address))
|
||||
self.squeak_controller.disconnect_peer(peer_address)
|
||||
return squeak_admin_pb2.DisconnectPeerReply()
|
||||
|
||||
def handle_subscribe_buy_offers(self, request, stopped):
|
||||
squeak_hash_str = request.squeak_hash
|
||||
squeak_hash = bytes.fromhex(squeak_hash_str)
|
||||
|
|
|
|||
|
|
@ -254,42 +254,6 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer):
|
|||
def GetLikedSqueakDisplays(self, request, context):
|
||||
return self.handler.handle_get_liked_squeak_display_entries(request)
|
||||
|
||||
def ConnectPeer(self, request, context):
|
||||
return self.handler.handle_connect_peer(request)
|
||||
|
||||
def GetConnectedPeers(self, request, context):
|
||||
return self.handler.handle_get_connected_peers(request)
|
||||
|
||||
def GetConnectedPeer(self, request, context):
|
||||
return self.handler.handle_get_connected_peer(request)
|
||||
|
||||
def DisconnectPeer(self, request, context):
|
||||
return self.handler.handle_disconnect_peer(request)
|
||||
|
||||
def SubscribeConnectedPeers(self, request, context):
|
||||
stopped = threading.Event()
|
||||
|
||||
def on_rpc_done():
|
||||
logger.info("Stopping SubscribeConnectedPeers.")
|
||||
stopped.set()
|
||||
context.add_callback(on_rpc_done)
|
||||
return self.handler.handle_subscribe_connected_peers(
|
||||
request,
|
||||
stopped,
|
||||
)
|
||||
|
||||
def SubscribeConnectedPeer(self, request, context):
|
||||
stopped = threading.Event()
|
||||
|
||||
def on_rpc_done():
|
||||
logger.info("Stopping SubscribeConnectedPeer.")
|
||||
stopped.set()
|
||||
context.add_callback(on_rpc_done)
|
||||
return self.handler.handle_subscribe_connected_peer(
|
||||
request,
|
||||
stopped,
|
||||
)
|
||||
|
||||
def SubscribeBuyOffers(self, request, context):
|
||||
stopped = threading.Event()
|
||||
|
||||
|
|
|
|||
|
|
@ -481,18 +481,6 @@ def create_app(handler, username, password):
|
|||
def getlikedsqueakdisplays(msg):
|
||||
return handler.handle_get_liked_squeak_display_entries(msg)
|
||||
|
||||
@app.route("/connectpeer", methods=["POST"])
|
||||
@login_required
|
||||
@protobuf_serialized(squeak_admin_pb2.ConnectPeerRequest())
|
||||
def connectpeer(msg):
|
||||
return handler.handle_connect_peer(msg)
|
||||
|
||||
@app.route("/disconnectpeer", methods=["POST"])
|
||||
@login_required
|
||||
@protobuf_serialized(squeak_admin_pb2.DisconnectPeerRequest())
|
||||
def disconnectpeer(msg):
|
||||
return handler.handle_disconnect_peer(msg)
|
||||
|
||||
@app.route("/getexternaladdress", methods=["POST"])
|
||||
@login_required
|
||||
@protobuf_serialized(squeak_admin_pb2.GetExternalAddressRequest())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue