From 6655f0852935efdc1715aa90d11a086d9439d844 Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Sat, 5 Aug 2017 19:29:20 +0200 Subject: [PATCH] Tests improved. --- .../Schemas/WebhookEnqueuerTests.cs | 118 ++++++++++++++++++ .../Squidex.Domain.Apps.Read.Tests.csproj | 1 + .../DefaultDomainObjectFactoryTests.cs | 2 - 3 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 tests/Squidex.Domain.Apps.Read.Tests/Schemas/WebhookEnqueuerTests.cs diff --git a/tests/Squidex.Domain.Apps.Read.Tests/Schemas/WebhookEnqueuerTests.cs b/tests/Squidex.Domain.Apps.Read.Tests/Schemas/WebhookEnqueuerTests.cs new file mode 100644 index 000000000..41aad56cb --- /dev/null +++ b/tests/Squidex.Domain.Apps.Read.Tests/Schemas/WebhookEnqueuerTests.cs @@ -0,0 +1,118 @@ +// ========================================================================== +// WebhookEnqueuerTests.cs +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex Group +// All rights reserved. +// ========================================================================== + +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using FakeItEasy; +using Newtonsoft.Json; +using NodaTime; +using Squidex.Domain.Apps.Events.Contents; +using Squidex.Domain.Apps.Read.Schemas.Repositories; +using Squidex.Infrastructure; +using Squidex.Infrastructure.CQRS.Events; +using Xunit; + +// ReSharper disable MethodSupportsCancellation +// ReSharper disable ImplicitlyCapturedClosure +// ReSharper disable ConvertToConstant.Local + +namespace Squidex.Domain.Apps.Read.Schemas +{ + public class WebhookEnqueuerTests + { + private readonly IClock clock = A.Fake(); + private readonly ISchemaWebhookRepository webhookRepository = A.Fake(); + private readonly IWebhookEventRepository webhookEventRepository = A.Fake(); + private readonly TypeNameRegistry typeNameRegisty = new TypeNameRegistry(); + private readonly Instant now = SystemClock.Instance.GetCurrentInstant(); + private readonly WebhookEnqueuer sut; + + public WebhookEnqueuerTests() + { + A.CallTo(() => clock.GetCurrentInstant()).Returns(now); + + typeNameRegisty.Map(typeof(ContentCreated)); + + sut = new WebhookEnqueuer( + typeNameRegisty, + webhookEventRepository, + webhookRepository, + clock, new JsonSerializer()); + } + + [Fact] + public void Should_return_contents_filter_for_events_filter() + { + Assert.Equal("^content-", sut.EventsFilter); + } + + [Fact] + public void Should_return_type_name_for_name() + { + Assert.Equal(typeof(WebhookEnqueuer).Name, sut.Name); + } + + [Fact] + public Task Should_do_nothing_on_clear() + { + return sut.ClearAsync(); + } + + [Fact] + public async Task Should_update_repositories_on_successful_requests() + { + var appId = new NamedId(Guid.NewGuid(), "my-app"); + + var schemaId = new NamedId(Guid.NewGuid(), "my-schema"); + + var @event = Envelope.Create(new ContentCreated { AppId = appId, SchemaId = schemaId }); + + var webhook1 = CreateWebhook(1); + var webhook2 = CreateWebhook(2); + + A.CallTo(() => webhookRepository.QueryUrlsBySchemaAsync(appId.Id, schemaId.Id)) + .Returns(Task.FromResult>(new List { webhook1, webhook2 })); + + await sut.On(@event); + + A.CallTo(() => webhookEventRepository.EnqueueAsync( + A.That.Matches(webhookJob => + !string.IsNullOrWhiteSpace(webhookJob.RequestSignature) + && !string.IsNullOrWhiteSpace(webhookJob.RequestBody) + && webhookJob.Id != Guid.Empty + && webhookJob.Expires == now.Plus(Duration.FromDays(2)) + && webhookJob.AppId == appId.Id + && webhookJob.EventName == "MySchemaCreatedEvent" + && webhookJob.RequestUrl == webhook1.Url + && webhookJob.WebhookId == webhook1.Id), now)).MustHaveHappened(); + + A.CallTo(() => webhookEventRepository.EnqueueAsync( + A.That.Matches(webhookJob => + !string.IsNullOrWhiteSpace(webhookJob.RequestSignature) + && !string.IsNullOrWhiteSpace(webhookJob.RequestBody) + && webhookJob.Id != Guid.Empty + && webhookJob.Expires == now.Plus(Duration.FromDays(2)) + && webhookJob.AppId == appId.Id + && webhookJob.EventName == "MySchemaCreatedEvent" + && webhookJob.RequestUrl == webhook2.Url + && webhookJob.WebhookId == webhook2.Id), now)).MustHaveHappened(); + } + + private static ISchemaWebhookUrlEntity CreateWebhook(int offset) + { + var webhook = A.Dummy(); + + A.CallTo(() => webhook.Id).Returns(Guid.NewGuid()); + A.CallTo(() => webhook.Url).Returns(new Uri($"http://domain{offset}.com")); + A.CallTo(() => webhook.SharedSecret).Returns($"secret{offset}"); + + return webhook; + } +} +} diff --git a/tests/Squidex.Domain.Apps.Read.Tests/Squidex.Domain.Apps.Read.Tests.csproj b/tests/Squidex.Domain.Apps.Read.Tests/Squidex.Domain.Apps.Read.Tests.csproj index b694924eb..2efcd320b 100644 --- a/tests/Squidex.Domain.Apps.Read.Tests/Squidex.Domain.Apps.Read.Tests.csproj +++ b/tests/Squidex.Domain.Apps.Read.Tests/Squidex.Domain.Apps.Read.Tests.csproj @@ -12,6 +12,7 @@ + diff --git a/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectFactoryTests.cs b/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectFactoryTests.cs index ed014f12d..b6c6914bd 100644 --- a/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectFactoryTests.cs +++ b/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectFactoryTests.cs @@ -55,8 +55,6 @@ namespace Squidex.Infrastructure.CQRS.Commands [Fact] public void Should_throw_exception_if_new_entity_has_invalid_version() { - var serviceProvider = A.Fake(); - var factoryFunction = new DomainObjectFactoryFunction(passedId => { return new DO(passedId, 0);