Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KoalaSat committed Jun 21, 2024
1 parent a9e411d commit 88467b2
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 0 deletions.
37 changes: 37 additions & 0 deletions docs/assets/schemas/api-latest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,30 @@ paths:
type: string
description: Reason for the failure
description: ''
/api/notifications/:
get:
operationId: notifications_list
description: Get a list of notifications sent to the robot.
summary: Get robot notifications
parameters:
- in: query
name: created_at
schema:
type: string
description: Shows notifications created AFTER this date.
tags:
- notifications
security:
- tokenAuth: []
responses:
'200':
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Notification'
description: ''
/api/order/:
get:
operationId: order_retrieve
Expand Down Expand Up @@ -1262,6 +1286,19 @@ components:
* `success` - success
* `error` - error
* `info` - info
Notification:
type: object
properties:
title:
type: string
description:
type: string
order_id:
type: integer
readOnly: true
required:
- order_id
- title
NullEnum:
enum:
- null
Expand Down
175 changes: 175 additions & 0 deletions tests/test_trade_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,16 @@ def test_make_order(self):
self.assertIsNone(data["taker"], "New order's taker is not null")
self.assert_order_logs(data["id"])

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
0,
"There is only one notification",
)

def test_make_order_on_blocked_country(self):
"""
Test the creation of an F2F order on a geoblocked location
Expand Down Expand Up @@ -347,6 +357,17 @@ def test_publish_order(self):

self.assert_order_logs(data["id"])

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
1,
"There is only one notification",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

def test_pause_unpause_order(self):
"""
Tests pausing and unpausing a public order
Expand All @@ -369,6 +390,17 @@ def test_pause_unpause_order(self):
self.assertResponse(trade.response)
self.assertEqual(data["status_message"], Order.Status(Order.Status.PUB).label)

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
2,
"There is only one notification",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

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

Expand Down Expand Up @@ -415,6 +447,17 @@ def test_make_and_take_order(self):

self.assert_order_logs(data["id"])

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
2,
"There is only one notification",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

def test_make_and_lock_contract(self):
"""
Tests a trade from order creation to taker bond locked.
Expand All @@ -437,6 +480,17 @@ def test_make_and_lock_contract(self):
self.assertTrue(data["taker_locked"])
self.assertFalse(data["escrow_locked"])

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
2,
"There is only one notification",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

# Maker GET
trade.get_order(trade.maker_index)
data = trade.response.json()
Expand All @@ -457,6 +511,17 @@ def test_make_and_lock_contract(self):
self.assertTrue(data["taker_locked"])
self.assertFalse(data["escrow_locked"])

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
2,
"There is only one notification",
)
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 All @@ -483,6 +548,17 @@ def test_trade_to_locked_escrow(self):
self.assertTrue(data["taker_locked"])
self.assertTrue(data["escrow_locked"])

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
3,
"There is only one notification",
)
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 All @@ -506,6 +582,17 @@ def test_trade_to_submitted_address(self):
self.assertEqual(data["status_message"], Order.Status(Order.Status.CHA).label)
self.assertFalse(data["is_fiat_sent"])

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
4,
"There is only one notification",
)
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)
trade.cancel_order(trade.taker_index)
Expand All @@ -532,6 +619,17 @@ def test_trade_to_submitted_invoice(self):
self.assertEqual(data["status_message"], Order.Status(Order.Status.CHA).label)
self.assertFalse(data["is_fiat_sent"])

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
5,
"There is only one notification",
)
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)
trade.cancel_order(trade.taker_index)
Expand All @@ -556,6 +654,17 @@ 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"])

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
6,
"There is only one notification",
)
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)
data = trade.response.json()
Expand Down Expand Up @@ -595,6 +704,17 @@ def test_trade_to_confirm_fiat_received_LN(self):

self.assert_order_logs(data["id"])

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
7,
"There is only one notification",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

def test_successful_LN(self):
"""
Tests a trade from order creation until Sats sent to buyer
Expand Down Expand Up @@ -702,6 +822,17 @@ def test_collaborative_cancel_order_in_chat(self):
"This order has been cancelled collaborativelly",
)

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
6,
"There is only one notification",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

def test_created_order_expires(self):
"""
Tests the expiration of a public order
Expand Down Expand Up @@ -734,6 +865,17 @@ def test_created_order_expires(self):

self.assert_order_logs(data["id"])

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
6,
"There is only one notification",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

def test_public_order_expires(self):
"""
Tests the expiration of a public order
Expand Down Expand Up @@ -767,6 +909,17 @@ def test_public_order_expires(self):

self.assert_order_logs(data["id"])

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
6,
"There is only one notification",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

def test_taken_order_expires(self):
"""
Tests the expiration of a public order
Expand Down Expand Up @@ -802,6 +955,17 @@ def test_taken_order_expires(self):

self.assert_order_logs(data["id"])

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
6,
"There is only one notification",
)
self.assertEqual(notifications_data[0]["order_id"], trade.order_id)

def test_escrow_locked_expires(self):
"""
Tests the expiration of a public order
Expand Down Expand Up @@ -890,6 +1054,17 @@ def test_chat(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {}) # Nothing in the response

trade.get_notifications()
# Checks
self.assertResponse(trade.response)
notifications_data = trade.response.json()
self.assertEqual(
notifications_data.count,
8,
"There is only one 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 Down
9 changes: 9 additions & 0 deletions tests/utils/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ def get_order(self, robot_index=1, first_encounter=False):
headers = self.get_robot_auth(robot_index, first_encounter)
self.response = self.client.get(path + params, **headers)

def get_notifications(self, created_at=0, robot_index=1, first_encounter=False):
"""
Fetch the latest state of the order
"""
path = reverse("notifications")
params = f"?created_at={created_at}"
headers = self.get_robot_auth(robot_index, first_encounter)
self.response = self.client.get(path + params, **headers)

@patch("api.tasks.send_notification.delay", send_notification)
def cancel_order(self, robot_index=1):
path = reverse("order")
Expand Down

0 comments on commit 88467b2

Please sign in to comment.