From 88467b2b93fb7fe544ea0bbd4cd2c3881013727e Mon Sep 17 00:00:00 2001 From: KoalaSat Date: Fri, 21 Jun 2024 11:09:18 +0200 Subject: [PATCH] Tests --- docs/assets/schemas/api-latest.yaml | 37 ++++++ tests/test_trade_pipeline.py | 175 ++++++++++++++++++++++++++++ tests/utils/trade.py | 9 ++ 3 files changed, 221 insertions(+) diff --git a/docs/assets/schemas/api-latest.yaml b/docs/assets/schemas/api-latest.yaml index 677350290..173ef5e3c 100644 --- a/docs/assets/schemas/api-latest.yaml +++ b/docs/assets/schemas/api-latest.yaml @@ -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 @@ -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 diff --git a/tests/test_trade_pipeline.py b/tests/test_trade_pipeline.py index 32a39c9e8..be91346a0 100644 --- a/tests/test_trade_pipeline.py +++ b/tests/test_trade_pipeline.py @@ -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 @@ -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 @@ -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() @@ -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. @@ -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() @@ -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() @@ -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) @@ -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) @@ -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) @@ -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() @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/tests/utils/trade.py b/tests/utils/trade.py index 16b9b89df..8d21660a9 100644 --- a/tests/utils/trade.py +++ b/tests/utils/trade.py @@ -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")