Skip to content

Commit

Permalink
Check tests
Browse files Browse the repository at this point in the history
KoalaSat committed Jun 25, 2024
1 parent b8ceaf0 commit 4a7a28b
Showing 2 changed files with 182 additions and 92 deletions.
248 changes: 157 additions & 91 deletions tests/test_trade_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import time

from datetime import datetime
from decimal import Decimal

@@ -391,16 +389,6 @@ def test_pause_unpause_order(self):
self.assertResponse(trade.response)
self.assertEqual(data["status_message"], Order.Status(Order.Status.PUB).label)

maker_headers = trade.get_robot_auth(trade.maker_index)
response = self.client.get(reverse("notifications"), **maker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your order with ID {trade.order_id} is public in the order book.",
)

# Cancel order to avoid leaving pending HTLCs after a successful test
trade.cancel_order()

@@ -449,7 +437,7 @@ def test_make_and_take_order(self):
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {str(data['maker_nick'])}, your order was taken by {str(data['taker_nick'])}!🥳",
f"✅ Hey {str(data['maker_nick'])}, your order with ID {trade.order_id} is public in the order book.",
)
taker_headers = trade.get_robot_auth(trade.taker_index)
response = self.client.get(reverse("notifications"), **taker_headers)
@@ -458,7 +446,7 @@ def test_make_and_take_order(self):
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {str(data['taker_nick'])}, you just took the order with ID {str(trade.order_id)}.",
f"✅ Hey {str(data['taker_nick'])}, you just took the order with ID {trade.order_id}.",
)

# Cancel order to avoid leaving pending HTLCs after a successful test
@@ -488,17 +476,6 @@ def test_make_and_lock_contract(self):
self.assertTrue(data["taker_locked"])
self.assertFalse(data["escrow_locked"])

maker_headers = trade.get_robot_auth(trade.maker_index)
response = self.client.get(reverse("notifications"), **maker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
notifications_data = list(trade.response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your order wi",
)

# Maker GET
trade.get_order(trade.maker_index)
data = trade.response.json()
@@ -519,16 +496,6 @@ def test_make_and_lock_contract(self):
self.assertTrue(data["taker_locked"])
self.assertFalse(data["escrow_locked"])

maker_headers = trade.get_robot_auth(trade.maker_index)
response = self.client.get(reverse("notifications"), **maker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {str(data['taker_nick'])}, you just took the order with ID {str(trade.order_id)}.",
)

# Maker cancels order to avoid leaving pending HTLCs after a successful test
trade.cancel_order()

@@ -555,16 +522,6 @@ def test_trade_to_locked_escrow(self):
self.assertTrue(data["taker_locked"])
self.assertTrue(data["escrow_locked"])

maker_headers = trade.get_robot_auth(trade.maker_index)
response = self.client.get(reverse("notifications"), **maker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your order wit.",
)

# Cancel order to avoid leaving pending HTLCs after a successful test
trade.cancel_order(trade.taker_index)

@@ -595,7 +552,16 @@ def test_trade_to_submitted_address(self):
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your order wi",
f"✅ Hey {data['maker_nick']}, the escrow and invoice have been submitted. The fiat exchange starts now via the platform chat.",
)
taker_headers = trade.get_robot_auth(trade.taker_index)
response = self.client.get(reverse("notifications"), **taker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['taker_nick']}, the escrow and invoice have been submitted. The fiat exchange starts now via the platform chat.",
)

# Cancel order to avoid leaving pending HTLCs after a successful test
@@ -625,10 +591,25 @@ def test_trade_to_submitted_invoice(self):
self.assertFalse(data["is_fiat_sent"])

maker_headers = trade.get_robot_auth(trade.maker_index)
maker_nick = read_file(f"tests/robots/{trade.maker_index}/nickname")
response = self.client.get(reverse("notifications"), **maker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {maker_nick}, the escrow and invoice have been submitted. The fiat exchange starts now via the platform chat.",
)
taker_headers = trade.get_robot_auth(trade.taker_index)
taker_nick = read_file(f"tests/robots/{trade.taker_index}/nickname")
response = self.client.get(reverse("notifications"), **taker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {taker_nick}, the escrow and invoice have been submitted. The fiat exchange starts now via the platform chat.",
)

# Cancel order to avoid leaving pending HTLCs after a successful test
trade.cancel_order(trade.maker_index)
@@ -654,16 +635,6 @@ def test_trade_to_confirm_fiat_sent_LN(self):
self.assertEqual(data["status_message"], Order.Status(Order.Status.FSE).label)
self.assertTrue(data["is_fiat_sent"])

maker_headers = trade.get_robot_auth(trade.maker_index)
response = self.client.get(reverse("notifications"), **maker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your order w",
)

# Cancel order to avoid leaving pending HTLCs after a successful test
trade.undo_confirm_sent(trade.maker_index)
data = trade.response.json()
@@ -710,7 +681,16 @@ def test_trade_to_confirm_fiat_received_LN(self):
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your o",
f"🥳 Your order with ID {str(trade.order_id)} has finished successfully!",
)
taker_headers = trade.get_robot_auth(trade.taker_index)
response = self.client.get(reverse("notifications"), **taker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"🥳 Your order with ID {str(trade.order_id)} has finished successfully!",
)

def test_successful_LN(self):
@@ -841,6 +821,16 @@ def test_collaborative_cancel_order_in_chat(self):
notifications_data[0]["title"],
f"❌ Hey {maker_nick}, your order with ID {trade.order_id} has been collaboratively cancelled.",
)
taker_headers = trade.get_robot_auth(trade.taker_index)
taker_nick = read_file(f"tests/robots/{trade.taker_index}/nickname")
response = self.client.get(reverse("notifications"), **taker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"❌ Hey {taker_nick}, your order with ID {trade.order_id} has been collaboratively cancelled.",
)

def test_created_order_expires(self):
"""
@@ -880,11 +870,7 @@ def test_public_order_expires(self):
"""
trade = Trade(self.client)
trade.publish_order()

# Change order expiry to now
order = Order.objects.get(id=trade.response.json()["id"])
order.expires_at = datetime.now()
order.save()
trade.expire_order()

# Make orders expire
trade.clean_orders()
@@ -914,7 +900,7 @@ def test_public_order_expires(self):
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f" Hey {data['maker_nick']}, your order wit",
f"😪 Hey {data['maker_nick']}, your order with ID {str(trade.order_id)} has expired without a taker.",
)

def test_taken_order_expires(self):
@@ -927,9 +913,7 @@ def test_taken_order_expires(self):
trade.lock_taker_bond()

# Change order expiry to now
order = Order.objects.get(id=trade.response.json()["id"])
order.expires_at = datetime.now()
order.save()
trade.expire_order()

# Make orders expire
trade.clean_orders()
@@ -961,6 +945,15 @@ def test_taken_order_expires(self):
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your or",
)
taker_headers = trade.get_robot_auth(trade.taker_index)
response = self.client.get(reverse("notifications"), **taker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your or",
)

def test_escrow_locked_expires(self):
"""
@@ -998,6 +991,25 @@ def test_escrow_locked_expires(self):

self.assert_order_logs(data["id"])

maker_headers = trade.get_robot_auth(trade.maker_index)
response = self.client.get(reverse("notifications"), **maker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your or",
)
taker_headers = trade.get_robot_auth(trade.taker_index)
response = self.client.get(reverse("notifications"), **taker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your or",
)

def test_chat(self):
"""
Tests the chatting REST functionality
@@ -1036,31 +1048,11 @@ def test_chat(self):
self.assertTrue(response.json()["peer_connected"])

# Post new message as maker
body = {"PGP_message": message, "order_id": trade.order_id, "offset": 0}
response = self.client.post(path, data=body, **maker_headers)
self.assertResponse(response)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["messages"][0]["message"], message)
self.assertTrue(response.json()["peer_connected"])

# Post new message as taker without offset, so response should not have messages.
body = {"PGP_message": message + " 2", "order_id": trade.order_id}
response = self.client.post(path, data=body, **taker_headers)
self.assertResponse(response)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {}) # Nothing in the response

# Get the two chatroom messages as maker
response = self.client.get(path + params, **maker_headers)
self.assertResponse(response)
self.assertEqual(response.status_code, 200)
self.assertTrue(response.json()["peer_connected"])
self.assertEqual(response.json()["messages"][0]["message"], message)
self.assertEqual(response.json()["messages"][1]["message"], message + " 2")
self.assertEqual(response.json()["messages"][0]["nick"], maker_nick)
self.assertEqual(response.json()["messages"][1]["nick"], taker_nick)

time.sleep(10)
trade.send_chat_message(message, trade.maker_index)
self.assertResponse(trade.response)
self.assertEqual(trade.response.status_code, 200)
self.assertEqual(trade.response.json()["messages"][0]["message"], message)
self.assertTrue(trade.response.json()["peer_connected"])

maker_headers = trade.get_robot_auth(trade.maker_index)
response = self.client.get(reverse("notifications"), **maker_headers)
@@ -1071,6 +1063,13 @@ def test_chat(self):
notifications_data[0]["title"],
"✅ Hey your order wit",
)

# Post new message as taker without offset, so response should not have messages.
trade.send_chat_message(message, trade.taker_index)
self.assertResponse(trade.response)
self.assertEqual(trade.response.status_code, 200)
self.assertEqual(trade.response.json(), {}) # Nothing in the response

taker_headers = trade.get_robot_auth(trade.taker_index)
response = self.client.get(reverse("notifications"), **taker_headers)
self.assertResponse(response)
@@ -1081,6 +1080,16 @@ def test_chat(self):
"✅ Hey your order wit",
)

# Get the two chatroom messages as maker
response = self.client.get(path + params, **maker_headers)
self.assertResponse(response)
self.assertEqual(response.status_code, 200)
self.assertTrue(response.json()["peer_connected"])
self.assertEqual(response.json()["messages"][0]["message"], message)
self.assertEqual(response.json()["messages"][1]["message"], message + " 2")
self.assertEqual(response.json()["messages"][0]["nick"], maker_nick)
self.assertEqual(response.json()["messages"][1]["nick"], taker_nick)

# Cancel order to avoid leaving pending HTLCs after a successful test
trade.cancel_order(trade.maker_index)
trade.cancel_order(trade.taker_index)
@@ -1127,6 +1136,25 @@ def test_order_expires_after_only_taker_messaged(self):

self.assert_order_logs(data["id"])

maker_headers = trade.get_robot_auth(trade.maker_index)
response = self.client.get(reverse("notifications"), **maker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your or",
)
taker_headers = trade.get_robot_auth(trade.taker_index)
response = self.client.get(reverse("notifications"), **taker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your or",
)

def test_order_expires_after_only_maker_messaged(self):
"""
Tests the expiration of an order in chat where taker never messaged
@@ -1169,6 +1197,25 @@ def test_order_expires_after_only_maker_messaged(self):

self.assert_order_logs(data["id"])

maker_headers = trade.get_robot_auth(trade.maker_index)
response = self.client.get(reverse("notifications"), **maker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your or",
)
taker_headers = trade.get_robot_auth(trade.taker_index)
response = self.client.get(reverse("notifications"), **taker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your or",
)

def test_withdraw_reward_after_unilateral_cancel(self):
"""
Tests withdraw rewards as taker after maker cancels order unilaterally
@@ -1239,6 +1286,25 @@ def test_order_expires_after_fiat_sent(self):

self.assert_order_logs(data["id"])

maker_headers = trade.get_robot_auth(trade.maker_index)
response = self.client.get(reverse("notifications"), **maker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your or",
)
taker_headers = trade.get_robot_auth(trade.taker_index)
response = self.client.get(reverse("notifications"), **taker_headers)
self.assertResponse(response)
notifications_data = list(response.json())
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your or",
)

def test_ticks(self):
"""
Tests the historical ticks serving endpoint after creating a contract
26 changes: 25 additions & 1 deletion tests/utils/trade.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from unittest.mock import patch

from datetime import datetime
from django.urls import reverse

from api.management.commands.clean_orders import Command as CleanOrders
@@ -119,18 +119,28 @@ def cancel_order(self, robot_index=1):
body = {"action": "cancel"}
self.response = self.client.post(path + params, body, **headers)

@patch("api.tasks.send_notification.delay", send_notification)
def send_chat_message(self, message, robot_index=1):
path = reverse("chat")
headers = self.get_robot_auth(robot_index)
body = {"PGP_message": message, "order_id": self.order_id, "offset": 0}
self.response = self.client.post(path, data=body, **headers)

@patch("api.tasks.send_notification.delay", send_notification)
def pause_order(self, robot_index=1):
path = reverse("order")
params = f"?order_id={self.order_id}"
headers = self.get_robot_auth(robot_index)
body = {"action": "pause"}
self.response = self.client.post(path + params, body, **headers)

@patch("api.tasks.send_notification.delay", send_notification)
def follow_hold_invoices(self):
# A background thread checks every 5 second the status of invoices. We invoke directly during test.
follower = FollowInvoices()
follower.follow_hold_invoices()

@patch("api.tasks.send_notification.delay", send_notification)
def clean_orders(self):
# A background thread checks every 5 second order expirations. We invoke directly during test.
cleaner = CleanOrders()
@@ -160,13 +170,15 @@ def publish_order(self):
# Get order
self.get_order()

@patch("api.tasks.send_notification.delay", send_notification)
def take_order(self):
path = reverse("order")
params = f"?order_id={self.order_id}"
headers = self.get_robot_auth(self.taker_index, first_encounter=True)
body = {"action": "take", "amount": self.take_amount}
self.response = self.client.post(path + params, body, **headers)

@patch("api.tasks.send_notification.delay", send_notification)
def lock_taker_bond(self):
# Takers's first order fetch. Should trigger maker bond hold invoice generation.
self.get_order(self.taker_index)
@@ -181,6 +193,7 @@ def lock_taker_bond(self):
# Get order
self.get_order(self.taker_index)

@patch("api.tasks.send_notification.delay", send_notification)
def lock_escrow(self, robot_index):
# Takers's order fetch. Should trigger trade escrow bond hold invoice generation.
self.get_order(robot_index)
@@ -195,6 +208,7 @@ def lock_escrow(self, robot_index):
# Get order
self.get_order()

@patch("api.tasks.send_notification.delay", send_notification)
def submit_payout_address(self, robot_index=1):
path = reverse("order")
params = f"?order_id={self.order_id}"
@@ -213,6 +227,7 @@ def submit_payout_address(self, robot_index=1):
}
self.response = self.client.post(path + params, body, **headers)

@patch("api.tasks.send_notification.delay", send_notification)
def submit_payout_invoice(self, robot_index=1, routing_budget=0):
path = reverse("order")
params = f"?order_id={self.order_id}"
@@ -234,16 +249,25 @@ def submit_payout_invoice(self, robot_index=1, routing_budget=0):

self.response = self.client.post(path + params, body, **headers)

@patch("api.tasks.send_notification.delay", send_notification)
def confirm_fiat(self, robot_index=1):
path = reverse("order")
params = f"?order_id={self.order_id}"
headers = self.get_robot_auth(robot_index)
body = {"action": "confirm"}
self.response = self.client.post(path + params, body, **headers)

@patch("api.tasks.send_notification.delay", send_notification)
def undo_confirm_sent(self, robot_index=1):
path = reverse("order")
params = f"?order_id={self.order_id}"
headers = self.get_robot_auth(robot_index)
body = {"action": "undo_confirm"}
self.response = self.client.post(path + params, body, **headers)

@patch("api.tasks.send_notification.delay", send_notification)
def expire_order(self):
# Change order expiry to now
order = Order.objects.get(id=self.order_id)
order.expires_at = datetime.now()
order.save()

0 comments on commit 4a7a28b

Please sign in to comment.