|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import lightspark |
| 6 | + |
| 7 | + |
| 8 | +class TestWebhooks: |
| 9 | + def test_valid_data_parses(self): |
| 10 | + data = b'{"event_type": "NODE_STATUS", "event_id": "1615c8be5aa44e429eba700db2ed8ca5", "timestamp": "2023-05-17T23:56:47.874449+00:00", "entity_id": "lightning_node:01882c25-157a-f96b-0000-362d42b64397"}' |
| 11 | + secret = "3gZ5oQQUASYmqQNuEk0KambNMVkOADDItIJjzUlAWjX" |
| 12 | + hex_digest = "62a8829aeb48b4142533520b1f7f86cdb1ee7d718bf3ea15bc1c662d4c453b74" |
| 13 | + |
| 14 | + webhook = lightspark.WebhookEvent.verify_and_parse(data, hex_digest, secret) |
| 15 | + |
| 16 | + assert ( |
| 17 | + webhook.entity_id == "lightning_node:01882c25-157a-f96b-0000-362d42b64397" |
| 18 | + ) |
| 19 | + assert webhook.event_id == "1615c8be5aa44e429eba700db2ed8ca5" |
| 20 | + assert webhook.event_type == lightspark.WebhookEventType.NODE_STATUS |
| 21 | + assert webhook.timestamp == datetime.fromisoformat( |
| 22 | + "2023-05-17T23:56:47.874449+00:00" |
| 23 | + ) |
| 24 | + |
| 25 | + @pytest.mark.parametrize( |
| 26 | + "hex_digest", |
| 27 | + [ |
| 28 | + pytest.param("deadbeef", id="wrong length"), |
| 29 | + pytest.param("a" * 64, id="incorrect"), |
| 30 | + pytest.param("NotAHexString", id="not hex"), |
| 31 | + pytest.param( |
| 32 | + "62a8829aeb48b4142533520b1f7f86cdb1ee7d718bf3ea15bc1c662d4c453b74" |
| 33 | + + "qq", |
| 34 | + id="extra bytes", |
| 35 | + ), |
| 36 | + ], |
| 37 | + ) |
| 38 | + def test_invalid_data_raises_value_error(self, hex_digest: str): |
| 39 | + data = b'{"event_type": "NODE_STATUS", "event_id": "1615c8be5aa44e429eba700db2ed8ca5", "timestamp": "2023-05-17T23:56:47.874449+00:00", "entity_id": "lightning_node:01882c25-157a-f96b-0000-362d42b64397"}' |
| 40 | + secret = "3gZ5oQQUASYmqQNuEk0KambNMVkOADDItIJjzUlAWjX" |
| 41 | + |
| 42 | + with pytest.raises(ValueError): |
| 43 | + lightspark.WebhookEvent.verify_and_parse(data, hex_digest, secret) |
| 44 | + |
| 45 | + def test_invalid_data_type_raises_type_error(self): |
| 46 | + data = 1 |
| 47 | + hex_digest = "deadbeef" |
| 48 | + secret = "3gZ5oQQUASYmqQNuEk0KambNMVkOADDItIJjzUlAWjX" |
| 49 | + |
| 50 | + with pytest.raises(TypeError): |
| 51 | + lightspark.WebhookEvent.verify_and_parse(data, hex_digest, secret) # type: ignore |
0 commit comments