Example #1
0
        public void CreateWebhookCommand_Validation_Failures_2()
        {
            // Arrange
            var mediator = GetService <IMediator>();
            var ct       = new CancellationToken();
            var command  = new CreateWebhookCommand()
            {
                ApiVersion    = "1.0.0",
                EnabledEvents = new HashSet <string>()
                {
                    "Test"
                },
                Secret = "1234",
                Url    = "http://localhost/webhook"
            };

            // Act & Assert
            var ex = Assert.ThrowsAsync <ValidationException>(() => mediator.Send(command, ct));

            Assert.AreEqual("One or more validation failures have occurred.", ex.Message);
            Assert.IsNotNull(ex.Failures);
            Assert.AreEqual(1, ex.Failures.Count);
            var firstEntry = ex.Failures.FirstOrDefault().Value;

            Assert.AreEqual(1, firstEntry.Length);
            Assert.IsTrue(firstEntry[0].StartsWith("'Enabled Events' must be one of these values: Unknown, TaskCr"));
        }
        public void DeleteWebhookCommand()
        {
            // Arrange
            var mediator = GetService <IMediator>();
            var uow      = GetService <IAmiUnitOfWork>();
            var ct       = new CancellationToken();
            var command1 = new CreateWebhookCommand()
            {
                ApiVersion    = "1.0.0",
                EnabledEvents = new HashSet <string>()
                {
                    "TaskUpdated",
                    "TaskCreated"
                },
                Secret = "1234",
                Url    = "http://localhost/webhook"
            };
            var result1  = mediator.Send(command1, ct).Result;
            var entity1  = uow.WebhookRepository.GetFirstOrDefault(e => e.Id == new Guid(result1.Id));
            var command2 = new DeleteWebhookCommand()
            {
                Id = result1.Id
            };

            // Act
            var result2 = mediator.Send(command2, ct).Result;
            var entity2 = uow.WebhookRepository.GetFirstOrDefault(e => e.Id == new Guid(result1.Id));

            // Assert
            Assert.IsNotNull(entity1);
            Assert.IsTrue(result2);
            Assert.IsNull(entity2);
        }
Example #3
0
        public void CreateWebhookCommand_Validation_Failures_3()
        {
            // Arrange
            var mediator = GetService <IMediator>();
            var ct       = new CancellationToken();
            var command  = new CreateWebhookCommand()
            {
                ApiVersion    = "1.0.0",
                EnabledEvents = new HashSet <string>()
                {
                    "Unknown"
                },
                Secret = "1234",
                Url    = "localhost/webhook"
            };

            // Act & Assert
            var ex = Assert.ThrowsAsync <ValidationException>(() => mediator.Send(command, ct));

            Assert.AreEqual("One or more validation failures have occurred.", ex.Message);
            Assert.IsNotNull(ex.Failures);
            Assert.AreEqual(1, ex.Failures.Count);
            var firstEntry = ex.Failures[nameof(command.Url)];

            Assert.AreEqual(1, firstEntry.Length);
            Assert.AreEqual("The specified 'Url' is not valid.", firstEntry[0]);
        }
Example #4
0
        public async Task <IActionResult> PostWebhooks(string id, [FromBody] CreateWebhookCommand command)
        {
            command.AppId = id;
            var result = await _mediator.Send(command);

            var resource = await _mediator.Send(new GetWebhookQuery { Id = result.Id });

            return(CreatedAtAction("Get", _webhookControllerName, new { id = resource.Id }, resource));
        }
Example #5
0
        private WebhookModel Create(IMediator mediator, CancellationToken ct)
        {
            var command1 = new CreateWebhookCommand()
            {
                ApiVersion    = "1.0.0",
                EnabledEvents = new HashSet <string>()
                {
                    "TaskUpdated",
                    "TaskCreated"
                },
                Secret = "1234",
                Url    = "http://localhost/webhook"
            };

            return(mediator.Send(command1, ct).Result);
        }
Example #6
0
        public void CreateWebhookCommand_Validation_Failures_1()
        {
            // Arrange
            var mediator = GetService <IMediator>();
            var ct       = new CancellationToken();
            var command  = new CreateWebhookCommand();

            // Act & Assert
            var ex = Assert.ThrowsAsync <ValidationException>(() => mediator.Send(command, ct));

            Assert.AreEqual("One or more validation failures have occurred.", ex.Message);
            Assert.IsNotNull(ex.Failures);
            Assert.AreEqual(3, ex.Failures.Count);
            var firstEntry = ex.Failures[nameof(command.Url)];

            Assert.AreEqual(2, firstEntry.Length);
            Assert.AreEqual("'Url' must not be empty.", firstEntry[0]);
            Assert.AreEqual("The specified 'Url' is not valid.", firstEntry[1]);
        }
Example #7
0
        public void CreateWebhookCommand()
        {
            // Arrange
            var mediator = GetService <IMediator>();
            var uow      = GetService <IAmiUnitOfWork>();
            var ct       = new CancellationToken();
            var command  = new CreateWebhookCommand()
            {
                ApiVersion    = "1.0.0",
                EnabledEvents = new HashSet <string>()
                {
                    "TaskUpdated",
                    "TaskCreated"
                },
                Secret = "1234",
                Url    = "http://localhost/webhook"
            };

            // Act
            var result1 = mediator.Send(command, ct).Result;
            var entity  = uow.WebhookRepository.GetFirstOrDefault(e => e.Id == new Guid(result1.Id));

            // Assert
            Assert.IsNotNull(result1);
            Assert.AreEqual(command.ApiVersion, result1.ApiVersion);
            Assert.AreEqual(2, result1.EnabledEvents.Length);
            Assert.AreEqual("TaskUpdated", result1.EnabledEvents[0]);
            Assert.AreEqual("TaskCreated", result1.EnabledEvents[1]);
            Assert.AreEqual(command.Url, result1.Url);
            Assert.AreEqual(SHARED_GUID_1, result1.UserId);

            Assert.IsNotNull(entity);
            Assert.AreEqual("#TaskUpdated#,#TaskCreated#", entity.EnabledEvents);

            uow.WebhookRepository.Remove(entity);
            uow.SaveChanges();
        }
        private IList <WebhookEntity> CreateWebhooks(IMediator mediator, IAmiUnitOfWork uow, CancellationToken ct)
        {
            IList <WebhookEntity> entities = new List <WebhookEntity>();

            // Webhook 1
            var command1 = new CreateWebhookCommand()
            {
                ApiVersion    = "1.0.0",
                EnabledEvents = new HashSet <string>()
                {
                    EventType.TaskUpdated.ToString(),
                EventType.TaskCreated.ToString()
                },
                Secret = "1234",
                Url    = "http://localhost/webhook1"
            };
            var result1 = mediator.Send(command1, ct).Result;
            var entity1 = uow.WebhookRepository.GetFirstOrDefault(e => e.Id == new Guid(result1.Id));

            entities.Add(entity1);

            // Webhook 2
            var command2 = new CreateWebhookCommand()
            {
                ApiVersion    = "1.0.0",
                EnabledEvents = new HashSet <string>()
                {
                    "*"
                },
                Secret = "1234",
                Url    = "http://localhost/webhook2"
            };
            var result2 = mediator.Send(command2, ct).Result;
            var entity2 = uow.WebhookRepository.GetFirstOrDefault(e => e.Id == new Guid(result2.Id));

            entities.Add(entity2);

            // Change user
            var principal1 = TestExecutionContext.CurrentContext.CurrentPrincipal;

            TestExecutionContext.CurrentContext.CurrentPrincipal = new MockPrincipal(
                SHARED_GUID_2, new RoleType[] { RoleType.Administrator });

            // Webhook 3
            var command3 = new CreateWebhookCommand()
            {
                ApiVersion    = "1.0.0",
                EnabledEvents = new HashSet <string>()
                {
                    EventType.TaskUpdated.ToString(),
                EventType.TaskCreated.ToString()
                },
                Secret = "1234",
                Url    = "http://localhost/webhook3"
            };
            var result3 = mediator.Send(command3, ct).Result;
            var entity3 = uow.WebhookRepository.GetFirstOrDefault(e => e.Id == new Guid(result3.Id));

            entities.Add(entity3);

            // Change user
            TestExecutionContext.CurrentContext.CurrentPrincipal = principal1;

            return(entities);
        }
        public async Task <IActionResult> Create([FromBody] CreateWebhookCommand command)
        {
            var result = await Mediator.Send(command, CancellationToken);

            return(CreatedAtAction(nameof(GetById), new { id = result.Id }, result));
        }