|
| 1 | +from http import HTTPStatus |
| 2 | + |
| 3 | +import pytest |
| 4 | +from django.urls import reverse |
| 5 | + |
| 6 | +from pretix.base.models import Order |
| 7 | + |
| 8 | + |
| 9 | +def test_webhook_url(client, db): |
| 10 | + webhook_url = reverse("plugins:pretix_pix_openpix:webhook") |
| 11 | + |
| 12 | + response = client.post(webhook_url) |
| 13 | + |
| 14 | + assert response.status_code == HTTPStatus.OK |
| 15 | + |
| 16 | + |
| 17 | +def test_webhook_url_valid_payload(client, db, create_payload): |
| 18 | + payload = create_payload() |
| 19 | + |
| 20 | + webhook_url = reverse("plugins:pretix_pix_openpix:webhook") |
| 21 | + |
| 22 | + response = client.post( |
| 23 | + webhook_url, |
| 24 | + data=payload, |
| 25 | + headers={"Content-Type": "application/json"}, |
| 26 | + ) |
| 27 | + |
| 28 | + assert response.status_code == HTTPStatus.OK |
| 29 | + |
| 30 | + |
| 31 | +def test_mark_order_as_paid(client, db, order, create_payload): |
| 32 | + assert order.status == Order.STATUS_PENDING |
| 33 | + |
| 34 | + payload = create_payload(order_code=order.code, order_total=order.total) |
| 35 | + |
| 36 | + webhook_url = reverse("plugins:pretix_pix_openpix:webhook") |
| 37 | + |
| 38 | + response = client.post( |
| 39 | + webhook_url, |
| 40 | + data=payload, |
| 41 | + content_type="application/json", |
| 42 | + ) |
| 43 | + |
| 44 | + order.refresh_from_db() |
| 45 | + assert order.status == Order.STATUS_PAID |
| 46 | + assert response.status_code == HTTPStatus.OK |
| 47 | + |
| 48 | + |
| 49 | +def test_do_not_change_order_if_webhook_event_not_valid( |
| 50 | + client, db, order, create_payload |
| 51 | +): |
| 52 | + assert order.status == Order.STATUS_PENDING |
| 53 | + |
| 54 | + payload = create_payload(order_code=order.code, order_total=order.total) |
| 55 | + payload["event"] = "OPENPIX:TRANSACTION_REFUND_RECEIVED" |
| 56 | + |
| 57 | + webhook_url = reverse("plugins:pretix_pix_openpix:webhook") |
| 58 | + |
| 59 | + response = client.post( |
| 60 | + webhook_url, |
| 61 | + data=payload, |
| 62 | + content_type="application/json", |
| 63 | + ) |
| 64 | + |
| 65 | + order.refresh_from_db() |
| 66 | + assert order.status == Order.STATUS_PENDING |
| 67 | + assert response.status_code == HTTPStatus.OK |
| 68 | + |
| 69 | + |
| 70 | +def test_invalid_json_payload_do_nothing(client, db, order): |
| 71 | + assert order.status == Order.STATUS_PENDING |
| 72 | + |
| 73 | + payload = {"content": "not the expected JSON payload"} |
| 74 | + |
| 75 | + webhook_url = reverse("plugins:pretix_pix_openpix:webhook") |
| 76 | + |
| 77 | + response = client.post( |
| 78 | + webhook_url, |
| 79 | + data=payload, |
| 80 | + content_type="application/json", |
| 81 | + ) |
| 82 | + |
| 83 | + order.refresh_from_db() |
| 84 | + assert order.status == Order.STATUS_PENDING |
| 85 | + assert response.status_code == HTTPStatus.OK |
| 86 | + |
| 87 | + |
| 88 | +def test_missing_transaction_id_in_payload_do_nothing(client, db, order): |
| 89 | + assert order.status == Order.STATUS_PENDING |
| 90 | + |
| 91 | + payload = { |
| 92 | + "event": "OPENPIX:TRANSACTION_RECEIVED", |
| 93 | + "pix": { |
| 94 | + "value": 10000, |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + webhook_url = reverse("plugins:pretix_pix_openpix:webhook") |
| 99 | + response = client.post( |
| 100 | + webhook_url, |
| 101 | + data=payload, |
| 102 | + content_type="application/json", |
| 103 | + ) |
| 104 | + |
| 105 | + order.refresh_from_db() |
| 106 | + assert order.status == Order.STATUS_PENDING |
| 107 | + assert response.status_code == HTTPStatus.OK |
| 108 | + |
| 109 | + |
| 110 | +def test_missing_value_in_payload_do_nothing(client, db, order): |
| 111 | + assert order.status == Order.STATUS_PENDING |
| 112 | + |
| 113 | + payload = { |
| 114 | + "event": "OPENPIX:TRANSACTION_RECEIVED", |
| 115 | + "pix": { |
| 116 | + "transactionID": order.code, |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + webhook_url = reverse("plugins:pretix_pix_openpix:webhook") |
| 121 | + |
| 122 | + response = client.post( |
| 123 | + webhook_url, |
| 124 | + data=payload, |
| 125 | + content_type="application/json", |
| 126 | + ) |
| 127 | + |
| 128 | + order.refresh_from_db() |
| 129 | + assert order.status == Order.STATUS_PENDING |
| 130 | + assert response.status_code == HTTPStatus.OK |
| 131 | + |
| 132 | + |
| 133 | +def test_identifier_does_not_refer_to_existing_order(client, db, create_payload): |
| 134 | + payload = create_payload(order_code="NOT-VALID-ORDER") |
| 135 | + |
| 136 | + webhook_url = reverse("plugins:pretix_pix_openpix:webhook") |
| 137 | + |
| 138 | + response = client.post( |
| 139 | + webhook_url, |
| 140 | + data=payload, |
| 141 | + content_type="application/json", |
| 142 | + ) |
| 143 | + |
| 144 | + assert response.status_code == HTTPStatus.OK |
| 145 | + |
| 146 | + |
| 147 | +@pytest.mark.parametrize( |
| 148 | + "order_status", [Order.STATUS_PAID, Order.STATUS_EXPIRED, Order.STATUS_CANCELED] |
| 149 | +) |
| 150 | +def test_when_order_is_not_pending_do_nothing( |
| 151 | + order_status, client, db, order, create_payload |
| 152 | +): |
| 153 | + order.status = order_status |
| 154 | + order.save() |
| 155 | + |
| 156 | + payload = create_payload(order_code=order.code, order_total=order.total) |
| 157 | + |
| 158 | + webhook_url = reverse("plugins:pretix_pix_openpix:webhook") |
| 159 | + |
| 160 | + response = client.post( |
| 161 | + webhook_url, |
| 162 | + data=payload, |
| 163 | + content_type="application/json", |
| 164 | + ) |
| 165 | + |
| 166 | + order.refresh_from_db() |
| 167 | + assert order.status == order_status |
| 168 | + assert response.status_code == HTTPStatus.OK |
0 commit comments