|
10 | 10 | TEST_ASSET_DIR = Path(__file__).parent / "assets" |
11 | 11 |
|
12 | 12 | GET_XML = TEST_ASSET_DIR / "webhook_get.xml" |
| 13 | +GET_NEW_EVENT_XML = TEST_ASSET_DIR / "webhook_get_new_event.xml" |
13 | 14 | CREATE_XML = TEST_ASSET_DIR / "webhook_create.xml" |
14 | 15 | CREATE_REQUEST_XML = TEST_ASSET_DIR / "webhook_create_request.xml" |
15 | 16 |
|
@@ -87,3 +88,82 @@ def test_request_factory(): |
87 | 88 | webhook_request_actual = "{}\n".format(RequestFactory.Webhook.create_req(webhook_item).decode("utf-8")) |
88 | 89 | # windows does /r/n for linebreaks, remove the extra char if it is there |
89 | 90 | assert webhook_request_expected.replace("\r", "") == webhook_request_actual |
| 91 | + |
| 92 | + |
| 93 | +def test_event_setter_none(): |
| 94 | + """Setting event to None should store None without crashing.""" |
| 95 | + item = WebhookItem() |
| 96 | + item.event = "datasource-updated" |
| 97 | + assert item.event == "datasource-updated" |
| 98 | + item.event = None |
| 99 | + assert item._event is None |
| 100 | + assert item.event is None |
| 101 | + |
| 102 | + |
| 103 | +def test_event_setter_short_name(): |
| 104 | + """Short event names should be stored with the webhook-source-event- prefix.""" |
| 105 | + item = WebhookItem() |
| 106 | + item.event = "datasource-updated" |
| 107 | + assert item._event == "webhook-source-event-datasource-updated" |
| 108 | + assert item.event == "datasource-updated" |
| 109 | + |
| 110 | + |
| 111 | +def test_event_setter_full_source_name(): |
| 112 | + """Full webhook-source-event- names should be accepted and stored as-is.""" |
| 113 | + item = WebhookItem() |
| 114 | + item.event = "webhook-source-event-datasource-updated" |
| 115 | + assert item._event == "webhook-source-event-datasource-updated" |
| 116 | + assert item.event == "datasource-updated" |
| 117 | + |
| 118 | + |
| 119 | +def test_event_setter_new_style_event_name(): |
| 120 | + """New-style event names (webhook-event-*) should be stored as-is and not mangled.""" |
| 121 | + item = WebhookItem() |
| 122 | + item.event = "webhook-event-user-promoted-admin" |
| 123 | + assert item._event == "webhook-event-user-promoted-admin" |
| 124 | + assert item.event == "webhook-event-user-promoted-admin" |
| 125 | + |
| 126 | + |
| 127 | +def test_get_new_style_event(server: TSC.Server) -> None: |
| 128 | + """Webhooks with new-style event names (webhook-event-*) should parse correctly.""" |
| 129 | + response_xml = GET_NEW_EVENT_XML.read_text() |
| 130 | + with requests_mock.mock() as m: |
| 131 | + m.get(server.webhooks.baseurl, text=response_xml) |
| 132 | + webhooks, _ = server.webhooks.get() |
| 133 | + assert len(webhooks) == 1 |
| 134 | + webhook = webhooks[0] |
| 135 | + |
| 136 | + assert webhook.id == "webhook-id-2" |
| 137 | + assert webhook.name == "webhook-name-2" |
| 138 | + assert webhook.url == "https://example.com/hook" |
| 139 | + # New-style event name should not have the webhook-source-event- prefix stripped |
| 140 | + assert webhook.event == "webhook-event-user-promoted-admin" |
| 141 | + assert webhook.owner_id == "webhook_owner_luid" |
| 142 | + |
| 143 | + |
| 144 | +def test_create_with_short_event_name(server: TSC.Server) -> None: |
| 145 | + """Creating a webhook with a short event name (e.g. datasource-created) should work.""" |
| 146 | + response_xml = CREATE_XML.read_text() |
| 147 | + with requests_mock.mock() as m: |
| 148 | + m.post(server.webhooks.baseurl, text=response_xml) |
| 149 | + webhook_model = TSC.WebhookItem() |
| 150 | + webhook_model.name = "Test Webhook" |
| 151 | + webhook_model.url = "https://ifttt.com/maker-url" |
| 152 | + webhook_model.event = "datasource-created" |
| 153 | + |
| 154 | + new_webhook = server.webhooks.create(webhook_model) |
| 155 | + assert new_webhook.id is not None |
| 156 | + |
| 157 | + |
| 158 | +def test_create_with_source_event_name(server: TSC.Server) -> None: |
| 159 | + """Creating a webhook with a full webhook-source-event-* name should work.""" |
| 160 | + response_xml = CREATE_XML.read_text() |
| 161 | + with requests_mock.mock() as m: |
| 162 | + m.post(server.webhooks.baseurl, text=response_xml) |
| 163 | + webhook_model = TSC.WebhookItem() |
| 164 | + webhook_model.name = "Test Webhook" |
| 165 | + webhook_model.url = "https://ifttt.com/maker-url" |
| 166 | + webhook_model.event = "webhook-source-event-datasource-created" |
| 167 | + |
| 168 | + new_webhook = server.webhooks.create(webhook_model) |
| 169 | + assert new_webhook.id is not None |
0 commit comments