-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhookManagementIntegrationTests.cs
More file actions
224 lines (180 loc) · 6.63 KB
/
WebhookManagementIntegrationTests.cs
File metadata and controls
224 lines (180 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
namespace Smtp2Go.NET.IntegrationTests.Webhooks;
using Fixtures;
using Helpers;
using Smtp2Go.NET.Models.Webhooks;
/// <summary>
/// Integration tests for webhook CRUD lifecycle operations using the live API key.
/// </summary>
/// <remarks>
/// <para>
/// These tests create, list, and delete real webhooks on the SMTP2GO account.
/// Each test cleans up after itself by deleting any webhooks it creates.
/// </para>
/// </remarks>
/// <summary>
/// Collection definition for webhook tests — ensures they run sequentially
/// because SMTP2GO free tier limits the account to 1 webhook at a time.
/// </summary>
[CollectionDefinition("Webhook")]
public class WebhookTestCollection;
[Collection("Webhook")]
[Trait("Category", "Integration.Live")]
public sealed class WebhookManagementIntegrationTests : IClassFixture<Smtp2GoLiveFixture>
{
#region Properties & Fields - Non-Public
/// <summary>The live-configured client fixture.</summary>
private readonly Smtp2GoLiveFixture _fixture;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="WebhookManagementIntegrationTests" /> class.
/// </summary>
public WebhookManagementIntegrationTests(Smtp2GoLiveFixture fixture)
{
_fixture = fixture;
}
#endregion
#region Methods - Helpers
/// <summary>
/// Deletes all existing webhooks on the SMTP2GO account.
/// SMTP2GO free tier limits accounts to 1 webhook — stale webhooks from
/// previous failed runs or E2E tests block creation of new ones.
/// </summary>
private async Task DeleteAllExistingWebhooksAsync(CancellationToken ct)
{
var listResponse = await _fixture.Client.Webhooks.ListAsync(ct);
if (listResponse.Data is not { Length: > 0 })
return;
foreach (var webhook in listResponse.Data)
{
if (webhook.WebhookId is { } id)
{
try
{
await _fixture.Client.Webhooks.DeleteAsync(id, ct);
}
catch
{
// Best-effort cleanup — continue with remaining webhooks.
}
}
}
}
#endregion
#region Webhook Lifecycle
[Fact]
public async Task WebhookLifecycle_CreateListDelete_Succeeds()
{
// Fail if live secrets are not configured.
TestSecretValidator.AssertLiveSecretsPresent();
var ct = TestContext.Current.CancellationToken;
int? webhookId = null;
// SMTP2GO free tier allows only 1 webhook — clear stale webhooks from previous runs.
await DeleteAllExistingWebhooksAsync(ct);
try
{
// Step 1: Create a webhook.
var createRequest = new WebhookCreateRequest
{
WebhookUrl = $"https://webhook-test.example.com/smtp2go/{Guid.NewGuid():N}",
Events = [WebhookCreateEvent.Delivered, WebhookCreateEvent.Bounce]
};
var createResponse = await _fixture.Client.Webhooks.CreateAsync(createRequest, ct);
// Assert — Create should return a valid response.
createResponse.Should().NotBeNull();
createResponse.RequestId.Should().NotBeNullOrWhiteSpace();
createResponse.Data.Should().NotBeNull();
createResponse.Data!.WebhookId.Should().NotBeNull()
.And.BeGreaterThan(0, "a new webhook should receive a positive ID");
webhookId = createResponse.Data.WebhookId!.Value;
// Step 2: List webhooks and verify the created one appears.
var listResponse = await _fixture.Client.Webhooks.ListAsync(ct);
listResponse.Should().NotBeNull();
listResponse.Data.Should().NotBeNull();
// The created webhook should appear in the list.
// WebhookListResponse.Data is WebhookInfo[] (extends ApiResponse<WebhookInfo[]>).
listResponse.Data!.Should().Contain(
w => w.WebhookId == webhookId,
"the newly created webhook should be in the list");
// Step 3: Delete the webhook.
var deleteResponse = await _fixture.Client.Webhooks.DeleteAsync(webhookId!.Value, ct);
deleteResponse.Should().NotBeNull();
deleteResponse.RequestId.Should().NotBeNullOrWhiteSpace();
// Mark as cleaned up so the finally block doesn't try again.
webhookId = null;
// Step 4: Verify the webhook is no longer in the list.
var listAfterDelete = await _fixture.Client.Webhooks.ListAsync(ct);
var webhookIds = listAfterDelete.Data?.Select(w => w.WebhookId) ?? [];
webhookIds.Should().NotContain(createResponse.Data.WebhookId,
"the deleted webhook should no longer appear in the list");
}
finally
{
// Cleanup: Delete the webhook if the test failed midway.
if (webhookId != null)
{
try
{
await _fixture.Client.Webhooks.DeleteAsync(webhookId.Value, ct);
}
catch
{
// Best-effort cleanup.
}
}
}
}
[Fact]
public async Task WebhookCreate_WithSpecificEvents_ConfiguresCorrectly()
{
// Fail if live secrets are not configured.
TestSecretValidator.AssertLiveSecretsPresent();
var ct = TestContext.Current.CancellationToken;
int? webhookId = null;
// SMTP2GO free tier allows only 1 webhook — clear stale webhooks from previous runs.
await DeleteAllExistingWebhooksAsync(ct);
try
{
// Arrange — Create a webhook with a specific set of event types.
// Subscribe to a representative set of subscription-level events.
// NOTE: WebhookCreateEvent values are subscription events (e.g., Bounce, Spam),
// NOT callback payload events (e.g., "hard_bounced", "spam_complaint").
var createRequest = new WebhookCreateRequest
{
WebhookUrl = $"https://webhook-test.example.com/smtp2go/{Guid.NewGuid():N}",
Events =
[
WebhookCreateEvent.Processed,
WebhookCreateEvent.Delivered,
WebhookCreateEvent.Bounce,
WebhookCreateEvent.Spam
]
};
// Act
var createResponse = await _fixture.Client.Webhooks.CreateAsync(createRequest, ct);
createResponse.Should().NotBeNull();
createResponse.Data.Should().NotBeNull();
webhookId = createResponse.Data!.WebhookId!.Value;
// Assert — Verify via the list endpoint that the webhook has the correct events.
var listResponse = await _fixture.Client.Webhooks.ListAsync(ct);
var webhook = listResponse.Data?.FirstOrDefault(w => w.WebhookId == webhookId);
webhook.Should().NotBeNull("the created webhook should be in the list");
}
finally
{
// Cleanup.
if (webhookId != null)
{
try
{
await _fixture.Client.Webhooks.DeleteAsync(webhookId.Value, ct);
}
catch
{
// Best-effort cleanup.
}
}
}
}
#endregion
}