DocSpace-buildtools/common/Tests/ASC.Webhooks.Tests/CommonWebhooksTests.cs

228 lines
9.9 KiB
C#
Raw Normal View History

2022-05-13 15:08:30 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
2022-05-13 11:48:51 +00:00
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2021-08-19 09:11:26 +00:00
using System;
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.Core;
using ASC.Core.Tenants;
using ASC.Web.Webhooks;
2021-08-26 21:15:47 +00:00
using ASC.Webhooks.Core;
2022-07-24 13:02:00 +00:00
using ASC.Webhooks.Core.EF.Model;
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;
2022-06-30 09:07:30 +00:00
using Microsoft.Extensions.Logging;
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
{
2022-05-13 11:48:51 +00:00
private readonly string EventName = "testEvent";
private readonly string secretKey = "testSecretKey";
private readonly string content = JsonSerializer.Serialize("testContent");
private readonly string headers = JsonSerializer.Serialize(new { Host = new StringValues("localhost") });
private readonly string URI = $"http://localhost:{port}/api/2.0/Test/";
private readonly DateTime creationTime = DateTime.Now;
private readonly CacheNotifyAction testCacheNotifyAction = CacheNotifyAction.Update;
2021-08-19 09:11:26 +00:00
[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");
2022-03-01 10:36:34 +00:00
var testWebhookConfig = new WebhooksConfig()
{
SecretKey = secretKey,
TenantId = testTenant.Id,
Uri = URI
2021-08-19 09:11:26 +00:00
};
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 mockedKafkaCaches = new Mock<ICacheNotify<WebhookRequest>>();
mockedKafkaCaches.Setup(a => a.Publish(testWebhookRequest, testCacheNotifyAction)).Verifiable();
2022-06-30 09:07:30 +00:00
var publisher = new WebhookPublisher(dbWorker, tenantManager, mockedKafkaCaches.Object);
2021-09-03 14:02:52 +00:00
publisher.Publish(EventName, content);
2021-08-19 09:11:26 +00:00
2022-03-01 10:36:34 +00:00
mockedKafkaCaches.Verify(a => a.Publish(testWebhookRequest, testCacheNotifyAction), Times.Once);
2021-08-19 09:11:26 +00:00
}
2022-03-01 10:36:34 +00:00
catch (Exception ex)
2021-08-19 09:11:26 +00:00
{
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
2022-06-30 09:07:30 +00:00
var mockedLog = new Mock<ILoggerProvider>();
//mockedLog.Setup(a => a.Error(It.IsAny<string>())).Verifiable();
2021-08-19 09:11:26 +00:00
2022-06-30 09:07:30 +00:00
//var mockedLogOptions = new Mock<IOptionsMonitor<ILog>>();
//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
2022-06-30 09:07:30 +00:00
var sender = new WebhookSender(mockedLog.Object, serviceProvider.GetRequiredService<IServiceScopeFactory>(), settings, httpClientFactory);
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");
2022-05-13 15:08:30 +00:00
Assert.IsTrue(requestHistory.СorrectSignature, "Problem with signature");
2021-08-19 09:11:26 +00:00
}
[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();
2022-03-01 10:36:34 +00:00
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
}
2022-03-01 10:36:34 +00:00
catch (Exception ex)
2021-08-19 09:11:26 +00:00
{
Assert.Fail(ex.ToString());
}
Assert.Pass();
}
}
}