Webhooks administration from Backoffice (#15050)

* Create webhook models

* Define interfaces for service and repository

* Create Webhook dto and corresponding factory

* implement WebhookRepository.cs

* Remove entity name from models, as that should be resolved in mapping instead

* Add new table to schema creator

* Register repo for DI

* Remove more mentions of entityname

* Refactor repository to guids

* Implement WebhookService

* Use scopes in service

* Start creating tests for service

* Refactor delete to use Id and not entire entity

* Rework Webhooks to be able to have multiple entity keys

* Implement GetAll functionality

* Implement webhook controller

* Imeplement get all events action

* Add equalityComparer deletegate to Webhook

* Add datacontract attirbutes to properties

* Implement backoffice webhooks tree

* Implement first webhooks menu

* Make WebHookController authorized

* Update to have tabs with webhooks and logs

* Enable create overlay

* Push to entityKeys array

* Fix up pagination

* Implement delete functionality

* remove pagination

* add log view

* Fix create to be able to select more than one content type

* implement type name resolving for content

* Refactor to use less duplication

* Implement update functionality in frontend

* Rename database table

* Make multiple events possible

* create new event picker

* Refactor to actually add new database table with proper name

* Make it possible to select multiple events

* Fix updating current items

* Fix up update functionality after db rework

* Add webhook icon

* Switch to match heartcore icons

* Refactor to use bases instead of Enum

* Refactor to make IWebhookEvent to Collection, so it can be injected instead of using reflection

* Fix up frontend to match new models

* Fix integration tests

* Remove obsolete entity key from webhookdto

* Introduce constants instead of hard coded strings

* Start implementation of firing mechanism

* Add new GetByEventName method

* Add 1 to many list on WebhookDto

* Implement new repository pattern

* Implement GetByEventName

* Fix up repository to use all async

* Refactor events to fire

* Refactor WebhookEvents to be more DRY

* Add custom header

* Start implementing log repository

* Implement GetPaged

* Implement WebhookLogService

* Implement GetLogs

* Add url to webhook log

* Implement log overview

* Formatting

* Implement details view

* Refactor to get actual retry count

* Refactor firing to fire only when Enabled

* Add Status code to detailed view

* Add configuration to disable webhooks entirely

* Implement custom headers frontend

* Implement persistence of custom headers

* Refactor retry service to also retry on non success status codes.

* Refactor registration of Webhooks, to also register as NotificationHandler

* Add webhooks migration

* Add key for adding webhook headers

* Fix up test

* Change event icon to flag

* Remember event, when editing what events you have chosen

* Refactor reflection to check if INotificationAsyncHandler instead

* Formatting

* Refactor webhook model to no longer derive from EntityBase

* Rename entityKeys to content keys

* Rename controller to lowercase H

* Add null check before trying to access selectedEvents

* Add configuration for maximum number of retries

* Add index to date

* Add webhook Key to logs

* Check for SchedulingPublisher before sending webhooks

* rename requestObject to payload

* Refactor event to send appropriate payloads

* Refactor logging to happen for every try.

* Order date by descending

* Add todo

* Change firing service to use String not ByteContent

* Update Headers to Interface instead of concrete implementation

* Dont return if a table exists already

* Rename updateModel to webhook

* Annotate WebhookController.cs with PluginController attribute

* Add danish translations

* Do not check if fail

* Dont filter when selecting custom items

* Remove delay from WebhookFiringService

---------

Co-authored-by: Zeegaan <nge@umbraco.dk>
This commit is contained in:
Nikolaj Geisle
2023-10-31 10:06:14 +01:00
committed by GitHub
parent 5bfd7d405f
commit 1b34d33eb7
76 changed files with 2818 additions and 8 deletions

View File

@@ -0,0 +1,49 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Webhooks;
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 WebhookLogServiceTests : UmbracoIntegrationTest
{
private IWebhookLogService WebhookLogService => GetRequiredService<IWebhookLogService>();
[Test]
public async Task Can_Create_And_Get()
{
var createdWebhookLog = await WebhookLogService.CreateAsync(new WebhookLog
{
Date = DateTime.UtcNow,
EventName = Constants.WebhookEvents.ContentPublish,
RequestBody = "Test Request Body",
ResponseBody = "Test response body",
StatusCode = "200",
RetryCount = 0,
Key = Guid.NewGuid(),
});
var webhookLogsPaged = await WebhookLogService.Get();
Assert.Multiple(() =>
{
Assert.IsNotNull(webhookLogsPaged);
Assert.IsNotEmpty(webhookLogsPaged.Items);
Assert.AreEqual(1, webhookLogsPaged.Items.Count());
var webHookLog = webhookLogsPaged.Items.First();
Assert.AreEqual(createdWebhookLog.Date, webHookLog.Date);
Assert.AreEqual(createdWebhookLog.EventName, webHookLog.EventName);
Assert.AreEqual(createdWebhookLog.RequestBody, webHookLog.RequestBody);
Assert.AreEqual(createdWebhookLog.ResponseBody, webHookLog.ResponseBody);
Assert.AreEqual(createdWebhookLog.StatusCode, webHookLog.StatusCode);
Assert.AreEqual(createdWebhookLog.RetryCount, webHookLog.RetryCount);
Assert.AreEqual(createdWebhookLog.Key, webHookLog.Key);
});
}
}

View File

@@ -0,0 +1,106 @@
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.ContentPublish, "00000000-0000-0000-0000-010000000000")]
[TestCase("https://example.com", Constants.WebhookEvents.ContentDelete, "00000000-0000-0000-0000-000200000000")]
[TestCase("https://example.com", Constants.WebhookEvents.ContentUnpublish, "00000000-0000-0000-0000-300000000000")]
[TestCase("https://example.com", Constants.WebhookEvents.MediaDelete, "00000000-0000-0000-0000-000004000000")]
[TestCase("https://example.com", Constants.WebhookEvents.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.ContentPublish }));
var createdWebhookTwo = await WebhookService.CreateAsync(new Webhook("https://example.com", true, new[] { Guid.NewGuid() }, new[] { Constants.WebhookEvents.ContentDelete }));
var createdWebhookThree = await WebhookService.CreateAsync(new Webhook("https://example.com", true, new[] { Guid.NewGuid() }, new[] { Constants.WebhookEvents.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.ContentPublish, "00000000-0000-0000-0000-010000000000")]
[TestCase("https://example.com", Constants.WebhookEvents.ContentDelete, "00000000-0000-0000-0000-000200000000")]
[TestCase("https://example.com", Constants.WebhookEvents.ContentUnpublish, "00000000-0000-0000-0000-300000000000")]
[TestCase("https://example.com", Constants.WebhookEvents.MediaDelete, "00000000-0000-0000-0000-000004000000")]
[TestCase("https://example.com", Constants.WebhookEvents.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.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.ContentPublish }));
createdWebhook.Events = new[] { Constants.WebhookEvents.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.ContentDelete));
}
[Test]
public async Task Can_Get_By_EventName()
{
var webhook1 = await WebhookService.CreateAsync(new Webhook("https://example.com", events: new[] { Constants.WebhookEvents.ContentPublish }));
var webhook2 = await WebhookService.CreateAsync(new Webhook("https://example.com", events: new[] { Constants.WebhookEvents.ContentUnpublish }));
var webhook3 = await WebhookService.CreateAsync(new Webhook("https://example.com", events: new[] { Constants.WebhookEvents.ContentUnpublish }));
var result = await WebhookService.GetByEventNameAsync(Constants.WebhookEvents.ContentUnpublish);
Assert.IsNotEmpty(result);
Assert.AreEqual(2, result.Count());
}
}