Ejemplo n.º 1
0
        public async Task DeleteTestsAsync()
        {
            var id         = Guid.NewGuid();
            var newWebHook = new WebHookDTO
            {
                Id       = id,
                Callback = new Uri("http://www.example2.org?noecho="),
                Secret   = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_",
                Filters  = new List <WebHookFilterDTO>
                {
                    new WebHookFilterDTO
                    {
                        TriggerId = "noun.verb"
                    }
                }
            };

            await _fixture.Client.PostAsync("/api/webhooks", new StringContent(JsonConvert.SerializeObject(newWebHook), Encoding.UTF8, "application/json"));

            var badResponse = await _fixture.Client.DeleteAsync($"/api/webhooks/{Guid.NewGuid()}");

            Assert.Equal(HttpStatusCode.NotFound, badResponse.StatusCode);

            var response = await _fixture.Client.DeleteAsync($"/api/webhooks/{id}");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            response = await _fixture.Client.DeleteAsync($"/api/webhooks");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PutAsync(Guid id, [FromBody] WebHookDTO webHook)
        {
            if (webHook == null)
            {
                return(BadRequest("Body is missing"));
            }

            if (webHook.Id != id)
            {
                return(BadRequest("Id mismatch"));
            }

            try
            {
                await _webHookValidator.ValidateAsync(webHook, HttpContext.RequestAborted);
            }
            catch (ArgumentException ex)
            {
                _logger.LogInformation($"Webhook {id} update validation failed: " + ex.Message);
                return(BadRequest(ex));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Webhook {id} update validation unexpected failure: " + ex.Message);
                return(StatusCode(500));
            }

            try
            {
                var result = await _webHookRegistrationStore.UpdateWebHookAsync(User, webHook, HttpContext.RequestAborted);

                return(GetActionFromResult(result));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Webhook {id} modification unexpected failure: " + ex.Message);
                return(StatusCode(500));
            }
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> PostAsync([FromBody] WebHookDTO webHook)
        {
            if (webHook == null)
            {
                return(BadRequest("Body is missing"));
            }

            try
            {
                await _webHookValidator.ValidateAsync(webHook, HttpContext.RequestAborted);
            }
            catch (ArgumentException ex)
            {
                _logger.LogInformation("New webhook validation failed: " + ex.Message);
                return(BadRequest(ex));
            }
            catch (Exception ex)
            {
                _logger.LogError("New webhook validation unexpected failure: " + ex.Message);
                return(StatusCode(500));
            }

            try
            {
                var result = await _webHookRegistrationStore.InsertWebHookAsync(User, webHook, HttpContext.RequestAborted);

                if (result == WebHookRegistrationStoreResult.Success)
                {
                    return(CreatedAtRoute(GetByIdAsyncActionName, new { id = webHook.Id }, webHook));
                }
                return(GetActionFromResult(result));
            }
            catch (Exception ex)
            {
                _logger.LogError("Registration insertion unexpected failure: " + ex.Message);
                return(StatusCode(500));
            }
        }
Ejemplo n.º 4
0
        public async Task CreateAndGetByIdTestsAsync()
        {
            var response = await _fixture.Client.GetAsync($"/api/webhooks/{Guid.NewGuid()}");

            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);

            var badCreationResponse = await _fixture.Client.PostAsync("/api/webhooks", new StringContent("bug", Encoding.UTF8, "application/json"));

            Assert.Equal(HttpStatusCode.BadRequest, badCreationResponse.StatusCode);

            //invalid as no url
            var newWebHook = new WebHookDTO
            {
                Id      = Guid.NewGuid(),
                Secret  = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_",
                Filters = new List <WebHookFilterDTO>
                {
                    new WebHookFilterDTO
                    {
                        TriggerId = "noun.verb"
                    }
                }
            };

            badCreationResponse = await _fixture.Client.PostAsync("/api/webhooks", new StringContent(JsonConvert.SerializeObject(newWebHook), Encoding.UTF8, "application/json"));

            Assert.Equal(HttpStatusCode.BadRequest, badCreationResponse.StatusCode);

            newWebHook.Callback = new Uri("http://www.example.org?noecho=");
            var creationResponse = await _fixture.Client.PostAsync("/api/webhooks", new StringContent(JsonConvert.SerializeObject(newWebHook), Encoding.UTF8, "application/json"));

            Assert.Equal(HttpStatusCode.Created, creationResponse.StatusCode);

            response = await _fixture.Client.GetAsync($"/api/webhooks/{newWebHook.Id}");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }