Skip to content

Commit

Permalink
CR
Browse files Browse the repository at this point in the history
  • Loading branch information
KoalaSat committed Jun 25, 2024
1 parent cb927f2 commit b8ceaf0
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 77 deletions.
4 changes: 2 additions & 2 deletions api/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ def new_chat_message(self, order, chat_message):
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

Expand Down
20 changes: 11 additions & 9 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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
Expand Down Expand Up @@ -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")

# 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)
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)

notification_data = []
for notification in queryset:
Expand Down
158 changes: 92 additions & 66 deletions tests/test_trade_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

from datetime import datetime
from decimal import Decimal

Expand Down Expand Up @@ -352,21 +354,20 @@ def test_publish_order(self):
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(notifications_data[0]["order_id"], trade.order_id)
self.assertEqual(
len(notifications_data),
1,
"User has a new order notification",
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your order with ID {trade.order_id} is public in the order book.",
)
self.assertEqual(notifications_data[0]["order_id"], 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_pause_unpause_order(self):
"""
Expand Down Expand Up @@ -395,6 +396,10 @@ def test_pause_unpause_order(self):
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()
Expand Down Expand Up @@ -437,16 +442,29 @@ def test_make_and_take_order(self):
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):
"""
Expand Down Expand Up @@ -475,12 +493,11 @@ def test_make_and_lock_contract(self):
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(
len(notifications_data),
3,
"User has a bond locked notification",
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your order wi",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

# Maker GET
trade.get_order(trade.maker_index)
Expand All @@ -506,12 +523,11 @@ def test_make_and_lock_contract(self):
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(
len(notifications_data),
2,
"User has a bond locked notification",
notifications_data[0]["title"],
f"✅ Hey {str(data['taker_nick'])}, you just took the order with ID {str(trade.order_id)}.",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

# Maker cancels order to avoid leaving pending HTLCs after a successful test
trade.cancel_order()
Expand Down Expand Up @@ -543,12 +559,11 @@ def test_trade_to_locked_escrow(self):
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(
len(notifications_data),
3,
"User has a scrow locked notification",
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your order wit.",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

# Cancel order to avoid leaving pending HTLCs after a successful test
trade.cancel_order(trade.taker_index)
Expand Down Expand Up @@ -577,12 +592,11 @@ def test_trade_to_submitted_address(self):
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(
len(notifications_data),
4,
"User has a new order ready notification",
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your order wi",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

# Cancel order to avoid leaving pending HTLCs after a successful test
trade.cancel_order(trade.maker_index)
Expand Down Expand Up @@ -614,11 +628,6 @@ def test_trade_to_submitted_invoice(self):
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
Expand Down Expand Up @@ -649,12 +658,11 @@ def test_trade_to_confirm_fiat_sent_LN(self):
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(
len(notifications_data),
6,
"User has a new fiat sent notification",
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your order w",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

# Cancel order to avoid leaving pending HTLCs after a successful test
trade.undo_confirm_sent(trade.maker_index)
Expand Down Expand Up @@ -699,12 +707,11 @@ def test_trade_to_confirm_fiat_received_LN(self):
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(
len(notifications_data),
7,
"User has a new fiat received notification",
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your o",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

def test_successful_LN(self):
"""
Expand Down Expand Up @@ -781,6 +788,17 @@ def test_cancel_public_order(self):
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
Expand Down Expand Up @@ -814,15 +832,15 @@ def test_collaborative_cancel_order_in_chat(self):
)

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(
len(notifications_data),
6,
"User has a new order cancelled notification",
notifications_data[0]["title"],
f"❌ Hey {maker_nick}, your order with ID {trade.order_id} has been collaboratively cancelled.",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

def test_created_order_expires(self):
"""
Expand Down Expand Up @@ -893,12 +911,11 @@ def test_public_order_expires(self):
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(
len(notifications_data),
6,
"User has a new order expired notification",
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your order wit",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

def test_taken_order_expires(self):
"""
Expand Down Expand Up @@ -939,12 +956,11 @@ def test_taken_order_expires(self):
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(
len(notifications_data),
6,
"User has a new order expired notification",
notifications_data[0]["title"],
f"✅ Hey {data['maker_nick']}, your or",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

def test_escrow_locked_expires(self):
"""
Expand Down Expand Up @@ -1034,17 +1050,6 @@ def test_chat(self):
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)
Expand All @@ -1055,6 +1060,27 @@ def test_chat(self):
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)
Expand Down

0 comments on commit b8ceaf0

Please sign in to comment.