Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Core/Services/WebhookServiceTests.cs
Nikolaj Geisle 7bde16b4ef V13: Add eventype to webhookevents (#15157)
* Refactor IWebhookEvent to contain event type.

* refactor frontend to filter on eventType.

* Display event names

* refactor to use eventNames

* remove npm from overview

* implement alias for WebhookEvents

* Implement [WebhookEvent] attribute

* Refactor IWebhookService to get by event alias and not name

* Rename parameter to fit method name

* to lower event type to avoid casing issues

* Apply suggestions from code review

Co-authored-by: Ronald Barendse <ronald@barend.se>

* Change event names from constants to hard coded. And give more friendly names

* Refactor to not use event names, where it was not intended

* Add renaming column migration

* display event alias in logs

* Update migration to check if old column is there

* Apply suggestions from code review

Co-authored-by: Kenn Jacobsen <kja@umbraco.dk>

* add determineResource function to avoid duplicate code

---------

Co-authored-by: Zeegaan <nge@umbraco.dk>
Co-authored-by: Ronald Barendse <ronald@barend.se>
Co-authored-by: Kenn Jacobsen <kja@umbraco.dk>
2023-11-09 14:18:34 +01:00

107 lines
5.7 KiB
C#

using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class WebhookServiceTests : UmbracoIntegrationTest
{
private IWebHookService WebhookService => GetRequiredService<IWebHookService>();
[Test]
[TestCase("https://example.com", Constants.WebhookEvents.Aliases.ContentPublish, "00000000-0000-0000-0000-010000000000")]
[TestCase("https://example.com", Constants.WebhookEvents.Aliases.ContentDelete, "00000000-0000-0000-0000-000200000000")]
[TestCase("https://example.com", Constants.WebhookEvents.Aliases.ContentUnpublish, "00000000-0000-0000-0000-300000000000")]
[TestCase("https://example.com", Constants.WebhookEvents.Aliases.MediaDelete, "00000000-0000-0000-0000-000004000000")]
[TestCase("https://example.com", Constants.WebhookEvents.Aliases.MediaSave, "00000000-0000-0000-0000-000000500000")]
public async Task Can_Create_And_Get(string url, string webhookEvent, Guid key)
{
var createdWebhook = await WebhookService.CreateAsync(new Webhook(url, true, new[] { key }, new[] { webhookEvent }));
var webhook = await WebhookService.GetAsync(createdWebhook.Key);
Assert.Multiple(() =>
{
Assert.IsNotNull(webhook);
Assert.AreEqual(1, webhook.Events.Length);
Assert.IsTrue(webhook.Events.Contains(webhookEvent));
Assert.AreEqual(url, webhook.Url);
Assert.IsTrue(webhook.ContentTypeKeys.Contains(key));
});
}
[Test]
public async Task Can_Get_All()
{
var createdWebhookOne = await WebhookService.CreateAsync(new Webhook("https://example.com", true, new[] { Guid.NewGuid() }, new[] { Constants.WebhookEvents.Aliases.ContentPublish }));
var createdWebhookTwo = await WebhookService.CreateAsync(new Webhook("https://example.com", true, new[] { Guid.NewGuid() }, new[] { Constants.WebhookEvents.Aliases.ContentDelete }));
var createdWebhookThree = await WebhookService.CreateAsync(new Webhook("https://example.com", true, new[] { Guid.NewGuid() }, new[] { Constants.WebhookEvents.Aliases.ContentUnpublish }));
var webhooks = await WebhookService.GetAllAsync(0, int.MaxValue);
Assert.Multiple(() =>
{
Assert.IsNotEmpty(webhooks.Items);
Assert.IsNotNull(webhooks.Items.FirstOrDefault(x => x.Key == createdWebhookOne.Key));
Assert.IsNotNull(webhooks.Items.FirstOrDefault(x => x.Key == createdWebhookTwo.Key));
Assert.IsNotNull(webhooks.Items.FirstOrDefault(x => x.Key == createdWebhookThree.Key));
});
}
[Test]
[TestCase("https://example.com", Constants.WebhookEvents.Aliases.ContentPublish, "00000000-0000-0000-0000-010000000000")]
[TestCase("https://example.com", Constants.WebhookEvents.Aliases.ContentDelete, "00000000-0000-0000-0000-000200000000")]
[TestCase("https://example.com", Constants.WebhookEvents.Aliases.ContentUnpublish, "00000000-0000-0000-0000-300000000000")]
[TestCase("https://example.com", Constants.WebhookEvents.Aliases.MediaDelete, "00000000-0000-0000-0000-000004000000")]
[TestCase("https://example.com", Constants.WebhookEvents.Aliases.MediaSave, "00000000-0000-0000-0000-000000500000")]
public async Task Can_Delete(string url, string webhookEvent, Guid key)
{
var createdWebhook = await WebhookService.CreateAsync(new Webhook(url, true, new[] { key }, new[] { webhookEvent }));
var webhook = await WebhookService.GetAsync(createdWebhook.Key);
Assert.IsNotNull(webhook);
await WebhookService.DeleteAsync(webhook.Key);
var deletedWebhook = await WebhookService.GetAsync(createdWebhook.Key);
Assert.IsNull(deletedWebhook);
}
[Test]
public async Task Can_Create_With_No_EntityKeys()
{
var createdWebhook = await WebhookService.CreateAsync(new Webhook("https://example.com", events: new[] { Constants.WebhookEvents.Aliases.ContentPublish }));
var webhook = await WebhookService.GetAsync(createdWebhook.Key);
Assert.IsNotNull(webhook);
Assert.IsEmpty(webhook.ContentTypeKeys);
}
[Test]
public async Task Can_Update()
{
var createdWebhook = await WebhookService.CreateAsync(new Webhook("https://example.com", events: new[] { Constants.WebhookEvents.Aliases.ContentPublish }));
createdWebhook.Events = new[] { Constants.WebhookEvents.Aliases.ContentDelete };
await WebhookService.UpdateAsync(createdWebhook);
var updatedWebhook = await WebhookService.GetAsync(createdWebhook.Key);
Assert.IsNotNull(updatedWebhook);
Assert.AreEqual(1, updatedWebhook.Events.Length);
Assert.IsTrue(updatedWebhook.Events.Contains(Constants.WebhookEvents.Aliases.ContentDelete));
}
[Test]
public async Task Can_Get_By_EventName()
{
var webhook1 = await WebhookService.CreateAsync(new Webhook("https://example.com", events: new[] { Constants.WebhookEvents.Aliases.ContentPublish }));
var webhook2 = await WebhookService.CreateAsync(new Webhook("https://example.com", events: new[] { Constants.WebhookEvents.Aliases.ContentUnpublish }));
var webhook3 = await WebhookService.CreateAsync(new Webhook("https://example.com", events: new[] { Constants.WebhookEvents.Aliases.ContentUnpublish }));
var result = await WebhookService.GetByAliasAsync(Constants.WebhookEvents.Aliases.ContentUnpublish);
Assert.IsNotEmpty(result);
Assert.AreEqual(2, result.Count());
}
}