Check tests

This commit is contained in:
KoalaSat
2024-06-25 12:49:23 +02:00
parent b8ceaf08c2
commit 4a7a28b1ff
2 changed files with 182 additions and 92 deletions

View File

@ -1,5 +1,3 @@
import time
from datetime import datetime
from decimal import Decimal
@ -391,16 +389,6 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
"""
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
"✅ 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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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 @@ class TradeTest(BaseAPITestCase):
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

View File

@ -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,6 +119,14 @@ class Trade:
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}"
@ -126,11 +134,13 @@ class Trade:
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,6 +170,7 @@ class Trade:
# 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}"
@ -167,6 +178,7 @@ class Trade:
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 @@ class Trade:
# 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 @@ class Trade:
# 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 @@ class Trade:
}
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,6 +249,7 @@ class Trade:
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}"
@ -241,9 +257,17 @@ class Trade:
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()