Beispiel #1
0
        public async Task Test_hooks_crud_sequence()
        {
            // Get all hooks
            var hooksBefore = await _apiClient.Hooks.GetAllAsync(new GetHooksRequest(), new PaginationInfo());

            // Add a new hook
            var newHookRequest = new HookCreateRequest()
            {
                Name         = $"integration-test-hook-{Guid.NewGuid():N}",
                Script       = @"module.exports = function(client, scope, audience, context, callback) { {
                              // TODO: implement your hook
                              callback(null, context);
                            }",
                Dependencies = JObject.Parse("{ \"auth0\": \"2.32.0\"}"),
                TriggerId    = "credentials-exchange"
            };
            var newHookResponse = await _apiClient.Hooks.CreateAsync(newHookRequest);

            newHookResponse.Should().NotBeNull();
            Assert.True(JObject.DeepEquals(newHookRequest.Dependencies, newHookResponse.Dependencies));

            // Get all the hooks again, and check that we now have one more
            var hooksAfter = await _apiClient.Hooks.GetAllAsync(new GetHooksRequest(), new PaginationInfo());

            hooksAfter.Count.Should().Be(hooksBefore.Count + 1);

            // Update the Hook
            var updateHookRequest = new HookUpdateRequest
            {
                Name    = $"integration-test-hook-2-{Guid.NewGuid():N}",
                Enabled = true
            };
            var updateHookResponse = await _apiClient.Hooks.UpdateAsync(newHookResponse.Id, updateHookRequest);

            updateHookResponse.Should().NotBeNull();
            // Because the Hooks endpoint changes the name of a Hook when using a Guid in the name,
            // we can only verify the name starts with the part without the Guid.
            updateHookResponse.Name.StartsWith("integration-test-hook-2-").Should().BeTrue();
            updateHookResponse.Enabled.Should().BeTrue();

            // Get a single hook
            var hook = await _apiClient.Hooks.GetAsync(newHookResponse.Id);

            hook.Should().NotBeNull();
            hook.Name.StartsWith("integration-test-hook-2").Should().BeTrue();
            hook.Enabled.Should().BeTrue();

            // Delete the hook, and ensure we get exception when trying to fetch it again
            await _apiClient.Hooks.DeleteAsync(hook.Id);

            Func <Task> getFunc = async() => await _apiClient.Hooks.GetAsync(hook.Id);

            getFunc.Should().Throw <ErrorApiException>().And.ApiError.ErrorCode.Should().Be("HookDoesNotExist");
        }
Beispiel #2
0
        public async Task <HookCreateResponse> CreateHookAsync(HookCreateRequest request)
        {
            await ValidateRequestAsync(request);

            var sender = _senderRepository.FindOne(x => x.Token == request.SenderToken);

            if (request.EventId.HasValue)
            {
                var entity = _hookRepository.Insert(new HookEntity()
                {
                    EventId  = request.EventId.Value,
                    SenderId = sender.Id,
                    Data     = request.Data,
                    StatusId = (int)StatusEnum.Created,
                    Message  = StatusEnum.Created.ToString()
                });

                _requesterQueue.Add(entity.Id);

                return(new HookCreateResponse()
                {
                    Status = true, Message = "Created 1 hook(s)"
                });
            }
            else if (request.ActionId.HasValue)
            {
                var events = _eventRepository.FindMany(x => x.ActionId == request.ActionId.Value && (x.SenderId == null || x.SenderId == sender.Id));

                foreach (var item in events)
                {
                    var entity = _hookRepository.Insert(new HookEntity()
                    {
                        EventId  = item.Id,
                        SenderId = sender.Id,
                        Data     = request.Data,
                        StatusId = (int)StatusEnum.Created,
                        Message  = StatusEnum.Created.ToString()
                    });

                    _requesterQueue.Add(entity.Id);
                }

                return(new HookCreateResponse()
                {
                    Status = true, Message = $"Created {events.Count()} hook(s)"
                });
            }
            else if (!string.IsNullOrEmpty(request.ActionName))
            {
                var events = _eventRepository.FindMany(x => x.Action.Name == request.ActionName && (x.SenderId == null || x.SenderId == sender.Id));

                foreach (var item in events)
                {
                    var entity = _hookRepository.Insert(new HookEntity()
                    {
                        EventId  = item.Id,
                        SenderId = sender.Id,
                        Data     = request.Data,
                        StatusId = (int)StatusEnum.Created,
                        Message  = StatusEnum.Created.ToString()
                    });

                    _requesterQueue.Add(entity.Id);
                }

                return(new HookCreateResponse()
                {
                    Status = true, Message = $"Created {events.Count()} hook(s)"
                });
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
Beispiel #3
0
 /// <summary>
 /// Creates a new hook according to the request.
 /// </summary>
 /// <param name="request">The <see cref="HookCreateRequest" /> containing the details of the hook to create.</param>
 /// <returns>The newly created <see cref="Hook" />.</returns>
 public Task <Hook> CreateAsync(HookCreateRequest request)
 {
     return(Connection.SendAsync <Hook>(HttpMethod.Post, BuildUri("hooks"), request, DefaultHeaders));
 }
Beispiel #4
0
 void CreateHook(string serviceId, HookCreateRequest request);
Beispiel #5
0
 void RegisterOrderHook(string serviceId, HookCreateRequest request);
Beispiel #6
0
 void RegisterCheckoutHook(string serviceId, HookCreateRequest request);
Beispiel #7
0
 void RegisterProductHook(string serviceId, HookCreateRequest request);