public async Task Should_update_repositories_on_successful_requests()
        {
            var appId = new NamedId <Guid>(Guid.NewGuid(), "my-app");

            var schemaId = new NamedId <Guid>(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 <IReadOnlyList <ISchemaWebhookUrlEntity> >(new List <ISchemaWebhookUrlEntity> {
                webhook1, webhook2
            }));

            await sut.On(@event);

            A.CallTo(() => webhookEventRepository.EnqueueAsync(
                         A <WebhookJob> .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 <WebhookJob> .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();
        }
Esempio n. 2
0
        public async Task <IActionResult> PutEvent(string app, Guid id)
        {
            var entity = await webhookEventsRepository.FindAsync(id);

            if (entity == null)
            {
                return(NotFound());
            }

            await webhookEventsRepository.EnqueueAsync(id, SystemClock.Instance.GetCurrentInstant());

            return(Ok());
        }
Esempio n. 3
0
        private async Task EnqueueJobAsync(string payload, IWebhookEntity webhook, AppEvent contentEvent, string eventName, Instant now)
        {
            var signature = $"{payload}{webhook.SharedSecret}".Sha256Base64();

            var job = new WebhookJob
            {
                Id               = Guid.NewGuid(),
                AppId            = contentEvent.AppId.Id,
                RequestUrl       = webhook.Url,
                RequestBody      = payload,
                RequestSignature = signature,
                EventName        = eventName,
                Expires          = now.Plus(ExpirationTime),
                WebhookId        = webhook.Id
            };

            await webhookEventRepository.EnqueueAsync(job, now);
        }