diff --git a/api/notifications.py b/api/notifications.py index 847ce0c7..480c6d47 100644 --- a/api/notifications.py +++ b/api/notifications.py @@ -210,8 +210,8 @@ class Notifications: notification_reason = f"(You receive this notification because this was the first in-chat message. You will only be notified again if there is a gap bigger than {TIMEGAP} minutes between messages)" user = chat_message.receiver - title = f"💬 Hey {user.username}, a new chat message in-app was sent to you by {chat_message.sender.username} for order ID {str(order.id)}. {notification_reason}" - self.send_message(order, user.robot, title) + title = f"💬 Hey {user.username}, a new chat message in-app was sent to you by {chat_message.sender.username} for order ID {str(order.id)}." + self.send_message(order, user.robot, title, notification_reason) return diff --git a/api/views.py b/api/views.py index 2120c635..0caa7407 100644 --- a/api/views.py +++ b/api/views.py @@ -5,6 +5,8 @@ from django.conf import settings from django.contrib.auth.models import User from django.db.models import Q, Sum from django.utils import timezone +from django.utils.dateparse import parse_datetime +from django.http import HttpResponseBadRequest from drf_spectacular.utils import extend_schema from rest_framework import status, viewsets from rest_framework.authentication import TokenAuthentication @@ -746,15 +748,15 @@ class NotificationsView(ListAPIView): @extend_schema(**NotificationSchema.get) def get(self, request, format=None): - # robot = request.user.robot - queryset = Notification.objects.all().order_by("created_at") - # created_at = request.GET.get("created_at") + robot = request.user.robot + queryset = Notification.objects.filter(robot=robot).order_by("-created_at") + created_at = request.GET.get("created_at") - # if created_at: - # created_at = parse_datetime(created_at) - # if not created_at: - # return HttpResponseBadRequest("Invalid date format") - # queryset = queryset.filter(created_at__gte=created_at) + if created_at: + created_at = parse_datetime(created_at) + if not created_at: + return HttpResponseBadRequest("Invalid date format") + queryset = queryset.filter(created_at__gte=created_at) notification_data = [] for notification in queryset: diff --git a/tests/test_trade_pipeline.py b/tests/test_trade_pipeline.py index 4570773a..95bea1aa 100644 --- a/tests/test_trade_pipeline.py +++ b/tests/test_trade_pipeline.py @@ -1,3 +1,5 @@ +import time + from datetime import datetime from decimal import Decimal @@ -352,21 +354,20 @@ class TradeTest(BaseAPITestCase): self.assertIsInstance(public_data["price_now"], float) self.assertIsInstance(data["satoshis_now"], int) - # Cancel order to avoid leaving pending HTLCs after a successful test - trade.cancel_order() - - 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( - len(notifications_data), - 1, - "User has a new order notification", - ) 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() + + self.assert_order_logs(data["id"]) def test_pause_unpause_order(self): """ @@ -395,6 +396,10 @@ class TradeTest(BaseAPITestCase): 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() @@ -437,16 +442,29 @@ class TradeTest(BaseAPITestCase): self.assertFalse(data["taker_locked"]) self.assertFalse(data["escrow_locked"]) - # Cancel order to avoid leaving pending HTLCs after a successful test - trade.cancel_order() - - 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 {str(data['maker_nick'])}, your order was taken by {str(data['taker_nick'])}!🥳", + ) + 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 {str(data['taker_nick'])}, you just took the order with ID {str(trade.order_id)}.", + ) + + # Cancel order to avoid leaving pending HTLCs after a successful test + trade.cancel_order() + + self.assert_order_logs(data["id"]) def test_make_and_lock_contract(self): """ @@ -475,12 +493,11 @@ class TradeTest(BaseAPITestCase): self.assertResponse(response) notifications_data = list(response.json()) notifications_data = list(trade.response.json()) - self.assertEqual( - len(notifications_data), - 3, - "User has a bond locked notification", - ) 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) @@ -506,12 +523,11 @@ class TradeTest(BaseAPITestCase): response = self.client.get(reverse("notifications"), **maker_headers) self.assertResponse(response) notifications_data = list(response.json()) - self.assertEqual( - len(notifications_data), - 2, - "User has a bond locked notification", - ) 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() @@ -543,12 +559,11 @@ class TradeTest(BaseAPITestCase): response = self.client.get(reverse("notifications"), **maker_headers) self.assertResponse(response) notifications_data = list(response.json()) - self.assertEqual( - len(notifications_data), - 3, - "User has a scrow locked notification", - ) 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) @@ -577,12 +592,11 @@ class TradeTest(BaseAPITestCase): response = self.client.get(reverse("notifications"), **maker_headers) self.assertResponse(response) notifications_data = list(response.json()) - self.assertEqual( - len(notifications_data), - 4, - "User has a new order ready notification", - ) self.assertEqual(notifications_data[0]["order_id"], trade.order_id) + self.assertEqual( + notifications_data[0]["title"], + f"✅ Hey {data['maker_nick']}, your order wi", + ) # Cancel order to avoid leaving pending HTLCs after a successful test trade.cancel_order(trade.maker_index) @@ -614,11 +628,6 @@ class TradeTest(BaseAPITestCase): response = self.client.get(reverse("notifications"), **maker_headers) self.assertResponse(response) notifications_data = list(response.json()) - self.assertEqual( - len(notifications_data), - 4, - "User has a new order ready notification", - ) self.assertEqual(notifications_data[0]["order_id"], trade.order_id) # Cancel order to avoid leaving pending HTLCs after a successful test @@ -649,12 +658,11 @@ class TradeTest(BaseAPITestCase): response = self.client.get(reverse("notifications"), **maker_headers) self.assertResponse(response) notifications_data = list(response.json()) - self.assertEqual( - len(notifications_data), - 6, - "User has a new fiat sent notification", - ) 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) @@ -699,12 +707,11 @@ class TradeTest(BaseAPITestCase): response = self.client.get(reverse("notifications"), **maker_headers) self.assertResponse(response) notifications_data = list(response.json()) - self.assertEqual( - len(notifications_data), - 7, - "User has a new fiat received notification", - ) self.assertEqual(notifications_data[0]["order_id"], trade.order_id) + self.assertEqual( + notifications_data[0]["title"], + f"✅ Hey {data['maker_nick']}, your o", + ) def test_successful_LN(self): """ @@ -781,6 +788,17 @@ class TradeTest(BaseAPITestCase): data["bad_request"], "This order has been cancelled by the maker" ) + 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}, you have cancelled your public order with ID {trade.order_id}.", + ) + def test_collaborative_cancel_order_in_chat(self): """ Tests the collaborative cancellation of an order in the chat state @@ -814,15 +832,15 @@ class TradeTest(BaseAPITestCase): ) 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( - len(notifications_data), - 6, - "User has a new order cancelled notification", - ) self.assertEqual(notifications_data[0]["order_id"], trade.order_id) + self.assertEqual( + notifications_data[0]["title"], + f"❌ Hey {maker_nick}, your order with ID {trade.order_id} has been collaboratively cancelled.", + ) def test_created_order_expires(self): """ @@ -893,12 +911,11 @@ class TradeTest(BaseAPITestCase): response = self.client.get(reverse("notifications"), **maker_headers) self.assertResponse(response) notifications_data = list(response.json()) - self.assertEqual( - len(notifications_data), - 6, - "User has a new order expired notification", - ) self.assertEqual(notifications_data[0]["order_id"], trade.order_id) + self.assertEqual( + notifications_data[0]["title"], + f"✅ Hey {data['maker_nick']}, your order wit", + ) def test_taken_order_expires(self): """ @@ -939,12 +956,11 @@ class TradeTest(BaseAPITestCase): response = self.client.get(reverse("notifications"), **maker_headers) self.assertResponse(response) notifications_data = list(response.json()) - self.assertEqual( - len(notifications_data), - 6, - "User has a new order expired notification", - ) 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): """ @@ -1034,17 +1050,6 @@ class TradeTest(BaseAPITestCase): self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {}) # Nothing in the response - 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( - len(notifications_data), - 8, - "User has a new chat notification", - ) - self.assertEqual(notifications_data[0]["order_id"], trade.order_id) - # Get the two chatroom messages as maker response = self.client.get(path + params, **maker_headers) self.assertResponse(response) @@ -1055,6 +1060,27 @@ class TradeTest(BaseAPITestCase): self.assertEqual(response.json()["messages"][0]["nick"], maker_nick) self.assertEqual(response.json()["messages"][1]["nick"], taker_nick) + time.sleep(10) + + 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"], + "✅ Hey your order wit", + ) + 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"], + "✅ Hey your order wit", + ) + # Cancel order to avoid leaving pending HTLCs after a successful test trade.cancel_order(trade.maker_index) trade.cancel_order(trade.taker_index)