mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.0 KiB
96 lines
3.0 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using FakeItEasy;
|
|
using Squidex.Domain.Apps.Core.HandleRules;
|
|
using Squidex.Domain.Apps.Core.Rules;
|
|
using Squidex.Domain.Apps.Core.Rules.EnrichedEvents;
|
|
using Squidex.Domain.Apps.Core.Rules.Triggers;
|
|
using Squidex.Domain.Apps.Events;
|
|
using Squidex.Domain.Apps.Events.Contents;
|
|
using Squidex.Infrastructure;
|
|
using Squidex.Infrastructure.EventSourcing;
|
|
using Xunit;
|
|
|
|
namespace Squidex.Domain.Apps.Entities.Rules.UsageTracking
|
|
{
|
|
public class UsageTriggerHandlerTests
|
|
{
|
|
private readonly IRuleTriggerHandler sut = new UsageTriggerHandler();
|
|
|
|
[Fact]
|
|
public void Should_return_false_if_asking_for_snapshot_support()
|
|
{
|
|
Assert.False(sut.CanCreateSnapshotEvents);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_handle_usage_event()
|
|
{
|
|
Assert.True(sut.Handles(new AppUsageExceeded()));
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_not_handle_other_event()
|
|
{
|
|
Assert.False(sut.Handles(new ContentCreated()));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Should_create_enriched_event()
|
|
{
|
|
var ctx = Context();
|
|
|
|
var @event = new AppUsageExceeded { CallsCurrent = 80, CallsLimit = 120 };
|
|
|
|
var result = await sut.CreateEnrichedEventsAsync(Envelope.Create<AppEvent>(@event), ctx, default).ToListAsync();
|
|
|
|
var enrichedEvent = result.Single() as EnrichedUsageExceededEvent;
|
|
|
|
Assert.Equal(@event.CallsCurrent, enrichedEvent!.CallsCurrent);
|
|
Assert.Equal(@event.CallsLimit, enrichedEvent!.CallsLimit);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_not_trigger_precheck_if_rule_id_not_matchs()
|
|
{
|
|
var ctx = Context();
|
|
|
|
var @event = new AppUsageExceeded();
|
|
|
|
var result = sut.Trigger(Envelope.Create<AppEvent>(@event), ctx);
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_trigger_precheck_if_event_type_correct_and_rule_id_matchs()
|
|
{
|
|
var ctx = Context();
|
|
|
|
var @event = new AppUsageExceeded { RuleId = ctx.RuleId };
|
|
|
|
var result = sut.Trigger(Envelope.Create<AppEvent>(@event), ctx);
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
private static RuleContext Context(RuleTrigger? trigger = null)
|
|
{
|
|
trigger ??= new UsageTrigger();
|
|
|
|
return new RuleContext
|
|
{
|
|
AppId = NamedId.Of(DomainId.NewGuid(), "my-app"),
|
|
Rule = new Rule(trigger, A.Fake<RuleAction>()),
|
|
RuleId = DomainId.NewGuid()
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|