2021-08-19 09:11:26 +00:00
|
|
|
|
using System;
|
2021-09-03 14:02:52 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-08-19 09:11:26 +00:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
using ASC.Api.Core.Core;
|
|
|
|
|
using ASC.Api.Core.Middleware;
|
|
|
|
|
using ASC.Common;
|
|
|
|
|
using ASC.Common.Caching;
|
|
|
|
|
using ASC.Common.Logging;
|
|
|
|
|
using ASC.Core;
|
|
|
|
|
using ASC.Core.Tenants;
|
|
|
|
|
using ASC.Web.Webhooks;
|
2021-08-26 21:15:47 +00:00
|
|
|
|
using ASC.Webhooks.Core;
|
|
|
|
|
using ASC.Webhooks.Core.Dao.Models;
|
2021-08-27 14:50:49 +00:00
|
|
|
|
using ASC.Webhooks.Service;
|
2021-08-19 09:11:26 +00:00
|
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2021-09-03 14:02:52 +00:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-08-19 09:11:26 +00:00
|
|
|
|
using Microsoft.AspNetCore.TestHost;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
2021-09-03 14:02:52 +00:00
|
|
|
|
using Microsoft.Extensions.Primitives;
|
2021-08-19 09:11:26 +00:00
|
|
|
|
|
|
|
|
|
using Moq;
|
|
|
|
|
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
|
|
namespace ASC.Webhooks.Tests
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class CommonWebhooksTests : BaseSetUp
|
|
|
|
|
{
|
|
|
|
|
private string EventName = "testEvent";
|
|
|
|
|
private string secretKey = "testSecretKey";
|
2021-09-03 14:02:52 +00:00
|
|
|
|
private string content = JsonSerializer.Serialize("testContent");
|
|
|
|
|
private string headers = JsonSerializer.Serialize(new { Host = new StringValues("localhost")});
|
2021-08-19 09:11:26 +00:00
|
|
|
|
private string URI = $"http://localhost:{port}/api/2.0/Test/";
|
|
|
|
|
private DateTime creationTime = DateTime.Now;
|
|
|
|
|
private CacheNotifyAction testCacheNotifyAction = CacheNotifyAction.Update;
|
|
|
|
|
|
|
|
|
|
[Order(1)]
|
|
|
|
|
[Test]
|
|
|
|
|
public void Publisher()
|
|
|
|
|
{
|
|
|
|
|
var scope = host.Services.CreateScope();
|
|
|
|
|
var dbWorker = scope.ServiceProvider.GetService<DbWorker>();
|
2021-08-26 21:15:47 +00:00
|
|
|
|
var tenantManager = scope.ServiceProvider.GetService<TenantManager>();
|
2021-08-19 09:11:26 +00:00
|
|
|
|
|
|
|
|
|
var id = 1;
|
|
|
|
|
var testWebhookRequest = new WebhookRequest { Id = id };
|
|
|
|
|
var testTenant = new Tenant(1, "testWebhooksPublisher");
|
|
|
|
|
var testWebhookConfig = new WebhooksConfig()
|
|
|
|
|
{
|
|
|
|
|
SecretKey = secretKey,
|
|
|
|
|
TenantId = testTenant.TenantId,
|
|
|
|
|
Uri = URI
|
|
|
|
|
};
|
|
|
|
|
var testWebhooksEntry = new WebhookEntry()
|
|
|
|
|
{
|
|
|
|
|
Id = id,
|
2021-09-03 14:02:52 +00:00
|
|
|
|
Payload = content,
|
2021-08-19 09:11:26 +00:00
|
|
|
|
SecretKey = secretKey,
|
|
|
|
|
Uri = URI
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
tenantManager.SetCurrentTenant(testTenant);
|
|
|
|
|
|
2021-08-26 21:15:47 +00:00
|
|
|
|
dbWorker.AddWebhookConfig(testWebhookConfig);
|
|
|
|
|
|
2021-08-19 09:11:26 +00:00
|
|
|
|
var mockedLog = new Mock<IOptionsMonitor<ILog>>();
|
|
|
|
|
mockedLog.Setup(a => a.Get("ASC.Webhooks")).Verifiable();
|
|
|
|
|
|
|
|
|
|
var mockedKafkaCaches = new Mock<ICacheNotify<WebhookRequest>>();
|
|
|
|
|
mockedKafkaCaches.Setup(a => a.Publish(testWebhookRequest, testCacheNotifyAction)).Verifiable();
|
|
|
|
|
|
|
|
|
|
var publisher = new WebhookPublisher(dbWorker, tenantManager, mockedLog.Object, mockedKafkaCaches.Object);
|
2021-09-03 14:02:52 +00:00
|
|
|
|
publisher.Publish(EventName, content);
|
2021-08-19 09:11:26 +00:00
|
|
|
|
|
|
|
|
|
mockedKafkaCaches.Verify(a => a.Publish(testWebhookRequest, testCacheNotifyAction), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Assert.Fail(ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
Assert.AreEqual(dbWorker.ReadFromJournal(id), testWebhooksEntry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Order(2)]
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Sender()
|
|
|
|
|
{
|
|
|
|
|
var scope = host.Services.CreateScope();
|
|
|
|
|
var serviceProvider = scope.ServiceProvider;
|
|
|
|
|
var dbWorker = serviceProvider.GetService<DbWorker>();
|
2021-08-26 21:15:47 +00:00
|
|
|
|
var tenantManager = serviceProvider.GetService<TenantManager>();
|
2021-08-19 09:11:26 +00:00
|
|
|
|
|
|
|
|
|
var successedId = dbWorker.ConfigsNumber() + 1;
|
|
|
|
|
var failedId = successedId + 1;
|
|
|
|
|
var testTenant = new Tenant(2, "testWebhooksSender");
|
|
|
|
|
|
2021-08-26 21:15:47 +00:00
|
|
|
|
tenantManager.SetCurrentTenant(testTenant);
|
|
|
|
|
|
|
|
|
|
var successWebhookConfig = new WebhooksConfig { ConfigId = successedId, SecretKey = secretKey, Uri = $"{URI}SuccessRequest/" };
|
|
|
|
|
var failedWebhookConfig = new WebhooksConfig { ConfigId = failedId, SecretKey = secretKey, Uri = $"{URI}FailedRequest/" };
|
2021-08-19 09:11:26 +00:00
|
|
|
|
dbWorker.AddWebhookConfig(successWebhookConfig);
|
|
|
|
|
dbWorker.AddWebhookConfig(failedWebhookConfig);
|
|
|
|
|
|
2021-09-03 14:02:52 +00:00
|
|
|
|
var successWebhookPayload = new WebhooksLog { ConfigId = successedId, Status = ProcessStatus.InProcess, CreationTime = creationTime, RequestPayload = content };
|
|
|
|
|
var failedWebhookPayload = new WebhooksLog { ConfigId = failedId, Status = ProcessStatus.InProcess, CreationTime = creationTime, RequestPayload = content };
|
2021-08-19 10:17:54 +00:00
|
|
|
|
var successWebhookPayloadId = dbWorker.WriteToJournal(successWebhookPayload);
|
|
|
|
|
var failedWebhookPayloadId = dbWorker.WriteToJournal(failedWebhookPayload);
|
2021-08-19 09:11:26 +00:00
|
|
|
|
|
|
|
|
|
var mockedLog = new Mock<ILog>();
|
|
|
|
|
mockedLog.Setup(a => a.Error(It.IsAny<string>())).Verifiable();
|
|
|
|
|
|
|
|
|
|
var mockedLogOptions = new Mock<IOptionsMonitor<ILog>>();
|
2021-08-26 21:15:47 +00:00
|
|
|
|
mockedLogOptions.Setup(a => a.Get("ASC.Webhooks.Core")).Returns(mockedLog.Object).Verifiable();
|
2021-08-19 09:11:26 +00:00
|
|
|
|
|
|
|
|
|
var source = new CancellationTokenSource();
|
|
|
|
|
var token = source.Token;
|
|
|
|
|
|
2021-08-19 10:17:54 +00:00
|
|
|
|
var SuccessedWebhookRequest = new WebhookRequest { Id = successWebhookPayloadId };
|
|
|
|
|
var FailedWebhookRequest = new WebhookRequest { Id = failedWebhookPayloadId };
|
2021-08-19 09:11:26 +00:00
|
|
|
|
|
2021-08-24 08:08:58 +00:00
|
|
|
|
var sender = new WebhookSender(mockedLogOptions.Object, serviceProvider, settings);
|
2021-08-19 09:11:26 +00:00
|
|
|
|
await sender.Send(SuccessedWebhookRequest, token);
|
|
|
|
|
await sender.Send(FailedWebhookRequest, token);
|
|
|
|
|
|
|
|
|
|
var asd = requestHistory.SuccessCounter;
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(requestHistory.SuccessCounter == 1, "Problem with successed request");
|
|
|
|
|
Assert.IsTrue(requestHistory.FailedCounter == webhookSender.RepeatCount, "Problem with failed request");
|
|
|
|
|
Assert.IsTrue(requestHistory.<EFBFBD>orrectSignature, "Problem with signature");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2021-09-03 14:02:52 +00:00
|
|
|
|
public async Task GlobalFilter()
|
2021-08-19 09:11:26 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var controllerAddress = "api/2.0/Test/testMethod";
|
|
|
|
|
var getEventName = $"method: GET, route: {controllerAddress}";
|
|
|
|
|
var postEventName = $"method: POST, route: {controllerAddress}";
|
|
|
|
|
|
|
|
|
|
var mockedWebhookPubslisher = new Mock<IWebhookPublisher>();
|
2021-09-03 14:02:52 +00:00
|
|
|
|
mockedWebhookPubslisher.Setup(a => a.Publish(getEventName, content)).Verifiable();
|
|
|
|
|
mockedWebhookPubslisher.Setup(a => a.Publish(postEventName, content)).Verifiable();
|
2021-08-19 09:11:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using var host = await new HostBuilder()
|
|
|
|
|
.ConfigureWebHost(webBuilder =>
|
|
|
|
|
{
|
|
|
|
|
webBuilder
|
|
|
|
|
.UseTestServer()
|
|
|
|
|
.ConfigureServices(services =>
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton(mockedWebhookPubslisher.Object);
|
|
|
|
|
services.AddControllers();
|
|
|
|
|
|
2021-09-03 14:02:52 +00:00
|
|
|
|
services.AddSingleton(new Action<JsonOptions>(opt => opt.JsonSerializerOptions.IgnoreNullValues = false));
|
|
|
|
|
|
2021-08-19 09:11:26 +00:00
|
|
|
|
var dIHelper = new DIHelper();
|
|
|
|
|
dIHelper.Configure(services);
|
|
|
|
|
dIHelper.TryAdd<TestController>();
|
2021-09-03 14:02:52 +00:00
|
|
|
|
dIHelper.TryAdd<WebhooksGlobalFilterAttribute>();
|
|
|
|
|
|
|
|
|
|
var builder = services.AddMvcCore(config =>
|
|
|
|
|
{
|
|
|
|
|
config.Filters.Add(new TypeFilterAttribute(typeof(WebhooksGlobalFilterAttribute)));
|
|
|
|
|
});
|
2021-08-19 09:11:26 +00:00
|
|
|
|
})
|
|
|
|
|
.Configure(app =>
|
|
|
|
|
{
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
|
{
|
|
|
|
|
endpoints.MapControllers();
|
|
|
|
|
endpoints.MapCustom();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.StartAsync();
|
|
|
|
|
|
|
|
|
|
var getResponse = await host.GetTestClient().GetAsync(controllerAddress);
|
2021-09-03 14:02:52 +00:00
|
|
|
|
mockedWebhookPubslisher.Verify(a => a.Publish(getEventName, content), Times.Never);
|
2021-08-19 09:11:26 +00:00
|
|
|
|
|
2021-09-03 14:02:52 +00:00
|
|
|
|
StringContent stringContent = new StringContent(content);
|
2021-08-19 09:11:26 +00:00
|
|
|
|
|
|
|
|
|
var postResponse = await host.GetTestClient().PostAsync(controllerAddress, stringContent);
|
2021-09-03 14:02:52 +00:00
|
|
|
|
mockedWebhookPubslisher.Verify(a => a.Publish(postEventName, content), Times.Once);
|
2021-08-19 09:11:26 +00:00
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Assert.Fail(ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
Assert.Pass();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|