Skip to content

Commit 92dbba3

Browse files
committed
test: preserve email and complete webhook channel coverage
1 parent 64a3f97 commit 92dbba3

2 files changed

Lines changed: 146 additions & 14 deletions

File tree

‎test/unit/groups/monitor_api_test.py‎

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ def test_clone_alert_definition_with_optional_fields(self):
266266
assert mock_post.call_data["channel_ids"] == [1, 2]
267267
assert mock_post.call_data["group_by"] == ["entity_id"]
268268

269-
def test_create_email_channel(self):
269+
def test_create_update_delete_alert_channel(self):
270270
"""
271-
Test creating an email alert channel.
272-
Verifies that channel_create() properly handles email channel details.
271+
E2E test for alert channel CRUD: create, update, and delete.
272+
Verifies the full lifecycle of an alert channel.
273273
"""
274274
create_url = "/monitor/alert-channels"
275275
channel_id = 789
@@ -314,6 +314,63 @@ def test_create_email_channel(self):
314314
assert channel.label == "Email Test Channel"
315315
assert channel.channel_type == "email"
316316

317+
updated_response = create_response.copy()
318+
updated_response["label"] = "Email Test Channel Updated"
319+
updated_response["updated"] = "2024-01-02T00:00:00"
320+
321+
with self.mock_put(updated_response) as mock_put:
322+
channel.label = "Email Test Channel Updated"
323+
result = channel.save()
324+
325+
assert mock_put.call_url == channel_url
326+
assert result is True
327+
assert channel.label == "Email Test Channel Updated"
328+
329+
with self.mock_delete() as mock_delete:
330+
result = channel.delete()
331+
332+
assert mock_delete.call_url == channel_url
333+
assert result is True
334+
335+
def test_alert_channel_alerts(self):
336+
"""Test retrieval of alerts associated with an alert channel."""
337+
channel_id = 123
338+
alerts_url = f"/monitor/alert-channels/{channel_id}/alerts"
339+
alerts_response = {
340+
"data": [
341+
{
342+
"id": 12345,
343+
"label": "DBAAS Alert 1",
344+
"service_type": "dbaas",
345+
"type": "alerts-definitions",
346+
"url": "/monitor/services/dbaas/alerts-definitions/12345",
347+
},
348+
{
349+
"id": 12346,
350+
"label": "DBAAS Alert 2",
351+
"service_type": "dbaas",
352+
"type": "alerts-definitions",
353+
"url": "/monitor/services/dbaas/alerts-definitions/12346",
354+
},
355+
],
356+
"page": 1,
357+
"pages": 1,
358+
"results": 2,
359+
}
360+
361+
with self.mock_get(alerts_response) as mock_get:
362+
alerts = self.client.monitor.alert_channel_alerts(channel_id)
363+
364+
assert mock_get.call_url == alerts_url
365+
assert isinstance(alerts, PaginatedList)
366+
assert len(alerts) == 2
367+
assert isinstance(alerts[0], AlertDefinition)
368+
assert alerts[0].id == 12345
369+
assert alerts[0].service_type == "dbaas"
370+
assert isinstance(alerts[1], AlertDefinition)
371+
assert alerts[1].id == 12346
372+
assert alerts[1].service_type == "dbaas"
373+
317374
def test_create_webhook_channel(self):
318375
"""
319376
Test creating a webhook alert channel.
@@ -422,17 +479,16 @@ def test_update_webhook_channel(self):
422479
channel_id = 123
423480
update_url = f"/monitor/alert-channels/{channel_id}"
424481

425-
channel = self.client.load(AlertChannel, channel_id)
426-
427482
update_response = {
428483
"id": channel_id,
429484
"label": "python-sdk-webhook-change-updated",
430485
"type": "user",
431-
"channel_type": "email",
486+
"channel_type": "webhook",
432487
"details": {
433-
"email": {
434-
"usernames": ["admin-user1", "admin-user2"],
435-
"recipient_type": "user",
488+
"webhook": {
489+
"endpoint_url": "https://example.com/webhook",
490+
"authentication": {"type": "none"},
491+
"data_compression": "none",
436492
}
437493
},
438494
"alerts": {
@@ -445,6 +501,9 @@ def test_update_webhook_channel(self):
445501
"created_by": "user1",
446502
"updated_by": "user1",
447503
}
504+
channel_data = update_response.copy()
505+
channel_data["label"] = "python-sdk-webhook-change"
506+
channel = AlertChannel(self.client, channel_id, channel_data)
448507

449508
with self.mock_put(update_response) as mock_put:
450509
channel.label = "python-sdk-webhook-change-updated"
@@ -455,6 +514,11 @@ def test_update_webhook_channel(self):
455514
mock_put.call_data["label"]
456515
== "python-sdk-webhook-change-updated"
457516
)
517+
assert channel.channel_type == "webhook"
518+
assert (
519+
channel.details.webhook.endpoint_url
520+
== "https://example.com/webhook"
521+
)
458522

459523
def test_delete_webhook_channel(self):
460524
"""

‎test/unit/objects/monitor_test.py‎

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
CustomHeader,
1919
CustomHTTPSLogsDestinationDetails,
2020
DestinationAuthentication,
21+
EmailDetails,
2122
LogsDestinationDetailsBase,
2223
LogsStreamDetails,
2324
LogsStreamType,
@@ -195,6 +196,61 @@ def test_alert_channels(self):
195196
)
196197
self.assertEqual(channels[0].alerts.alert_count, 0)
197198

199+
def test_create_update_delete_channel(self):
200+
"""Test the parent email channel create, update, and delete lifecycle."""
201+
create_url = "/monitor/alert-channels"
202+
channel_id = 999
203+
channel_url = f"{create_url}/{channel_id}"
204+
create_response = {
205+
"id": channel_id,
206+
"label": "CRUD Test Channel",
207+
"type": "user",
208+
"channel_type": "email",
209+
"details": {
210+
"email": {
211+
"usernames": ["crud_user1", "crud_user2"],
212+
"recipient_type": "user",
213+
}
214+
},
215+
"alerts": {
216+
"url": f"{channel_url}/alerts",
217+
"type": "alerts-definitions",
218+
"alert_count": 0,
219+
},
220+
"created": "2024-01-01T00:00:00",
221+
"updated": "2024-01-01T00:00:00",
222+
"created_by": "crud_user1",
223+
"updated_by": "crud_user1",
224+
}
225+
226+
with self.mock_post(create_response) as mock_post:
227+
channel = self.client.monitor.channel_create(
228+
label="CRUD Test Channel",
229+
channel_type="email",
230+
details=ChannelDetails(
231+
email=EmailDetails(
232+
recipient_type="user",
233+
usernames=["crud_user1", "crud_user2"],
234+
)
235+
),
236+
)
237+
self.assertEqual(mock_post.call_url, create_url)
238+
self.assertIsInstance(channel, AlertChannel)
239+
self.assertEqual(channel.channel_type, "email")
240+
241+
updated_response = create_response.copy()
242+
updated_response["label"] = "CRUD Test Channel Updated"
243+
with self.mock_put(updated_response) as mock_put:
244+
channel.label = "CRUD Test Channel Updated"
245+
self.assertTrue(channel.save())
246+
247+
self.assertEqual(mock_put.call_url, channel_url)
248+
self.assertEqual(channel.label, "CRUD Test Channel Updated")
249+
250+
with self.mock_delete() as mock_delete:
251+
self.assertTrue(channel.delete())
252+
self.assertEqual(mock_delete.call_url, channel_url)
253+
198254
def test_webhook_channel_validation(self):
199255
"""
200256
Test webhook channel validation constraints for create and verify operations.
@@ -288,14 +344,15 @@ def test_webhook_channel_validation(self):
288344
self.client.monitor._validate_webhook_details(webhook_details)
289345
self.assertIn("Content-Type", str(cm.exception))
290346

291-
def test_create_update_verify_delete_webhook_channel(self):
347+
def test_create_get_update_verify_delete_webhook_channel(self):
292348
"""
293-
Test webhook channel create, update, verify, and delete operations.
349+
Test webhook channel create, get, update, verify, and delete operations.
294350
Verifies the full lifecycle of a webhook alert channel object:
295351
1. POST /monitor/alert-channels - Create webhook channel
296-
2. PUT /monitor/alert-channels/{id} - Update channel label
297-
3. POST /monitor/alert-channels/verify - Verify webhook configuration
298-
4. DELETE /monitor/alert-channels/{id} - Delete the channel
352+
2. GET /monitor/alert-channels/{id} - Retrieve webhook channel
353+
3. PUT /monitor/alert-channels/{id} - Update channel label
354+
4. POST /monitor/alert-channels/verify - Verify webhook configuration
355+
5. DELETE /monitor/alert-channels/{id} - Delete the channel
299356
"""
300357
create_url = "/monitor/alert-channels"
301358
channel_id = 888
@@ -367,6 +424,17 @@ def test_create_update_verify_delete_webhook_channel(self):
367424
webhook_channel.details.webhook.authentication.type, "basic"
368425
)
369426

427+
# GET: Retrieve the webhook channel by ID
428+
with self.mock_get(create_response) as m_get:
429+
webhook_channel = self.client.load(AlertChannel, channel_id)
430+
431+
self.assertEqual(m_get.call_url, channel_url)
432+
self.assertEqual(webhook_channel.channel_type, "webhook")
433+
self.assertEqual(
434+
webhook_channel.details.webhook.endpoint_url,
435+
"https://example.com/webhook",
436+
)
437+
370438
# UPDATE: Update the webhook channel label
371439
update_response = create_response.copy()
372440
update_response["label"] = "python-sdk-webhook-change-updated"

0 commit comments

Comments
 (0)