Ejemplo n.º 1
0
        public async Task Should_Deactivate_Subscription_If_Reached_To_Max_Fail_Count_Async()
        {
            var(subscription, data, predicate) = await InitializeTestCase(AppWebhookDefinitionNames.Test);

            var webhooksConfiguration = Resolve <IWebhooksConfiguration>();

            webhooksConfiguration.IsAutomaticSubscriptionDeactivationEnabled          = true;
            webhooksConfiguration.MaxConsecutiveFailCountBeforeDeactivateSubscription = 1;

            var webhookSenderSubstitute = RegisterFake <IWebhookSender>();

            webhookSenderSubstitute.When(w => w.SendWebhook(Arg.Any <WebhookSenderArgs>()))
            .Do(x =>
                throw new Exception()
                );

            var webhookSendAttemptStore = RegisterFake <IWebhookSendAttemptStore>();

            webhookSendAttemptStore
            .HasXConsecutiveFail(Arg.Any <int?>(), Arg.Any <Guid>(), Arg.Any <int>())
            .Returns(true);

            var webhookSubscriptionManager = Resolve <IWebhookSubscriptionManager>();

            _backgroundJobManagerSubstitute.When(m => m.EnqueueAsync <WebhookSenderJob, WebhookSenderArgs>(Arg.Any <WebhookSenderArgs>()))
            .Do((callback) =>
            {
                var args = callback.Args();
                args[0].ShouldBeAssignableTo <WebhookSenderArgs>("Argument is not WebhookSenderArgs");

                var webhookSenderJob = Resolve <WebhookSenderJob>();
                webhookSenderJob.Execute(args[0] as WebhookSenderArgs);

                var sub = webhookSubscriptionManager.Get(subscription.Id);
                sub.IsActive.ShouldBeFalse();    //after it get error MaxConsecutiveFailCountBeforeDeactivateSubscription times(in our case it is 1) subscription becomes deactivate
            });

            await webhookSubscriptionManager.AddOrUpdateSubscriptionAsync(subscription);

            var storedSubscription = webhookSubscriptionManager.Get(subscription.Id);

            storedSubscription.IsActive.ShouldBeTrue();//subscription is active

            await _webhookPublisher.PublishAsync(AppWebhookDefinitionNames.Test, data, subscription.TenantId);

            await _backgroundJobManagerSubstitute.Received()
            .EnqueueAsync <WebhookSenderJob, WebhookSenderArgs>(Arg.Is <WebhookSenderArgs>(w => predicate(w)));

            webhookSenderSubstitute.Received().SendWebhook(Arg.Any <WebhookSenderArgs>());
        }