Ejemplo n.º 1
0
        public async Task Given_InvalidHtml_When_Update_Then_BadRequest()
        {
            // Arrange
            var banner = GenerateRandomBanner();

            var updateModel = new UpdateBannerModel
            {
                Title = Guid.NewGuid().ToString(),
                Html  = $"<html><body><span>{Guid.NewGuid()}</span></body></html>"
            };

            HtmlValidatorMock.Setup(v => v.Validate(updateModel.Html))
            .Returns(Task.FromResult(new ValidationResult()
            {
                Errors = new[] { "Invalid HTML" }
            }));

            // Act
            var result = await Controller.Update(banner.Id, updateModel);

            // Assert
            Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult));

            var validationResult = ((BadRequestObjectResult)result).Value as ValidationResult;

            Assert.IsNotNull(validationResult);
            Assert.IsFalse(validationResult.IsValidHtml);
            Assert.AreEqual("Invalid HTML", validationResult.Errors.Single());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Update(int bannerId, [FromBody] UpdateBannerModel updateModel)
        {
            if (updateModel == null || !ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (IsTitleAlreadyRegistered(updateModel.Title, bannerId))
            {
                return(Conflict("Title is already registered"));
            }

            var bannerToUpdate = _context.Banners.FirstOrDefault(b => b.Id == bannerId);

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

            var validationResult = await _htmlValidator.Validate(updateModel.Html);

            if (!validationResult.IsValidHtml)
            {
                return(BadRequest(validationResult));
            }

            _mapper.Map(updateModel, bannerToUpdate);

            _context.Banners.Update(bannerToUpdate);
            await _context.SaveChangesAsync();

            return(Ok(_mapper.Map <BannerModel>(bannerToUpdate)));
        }
Ejemplo n.º 3
0
        public async Task Given_ValidModel_When_Update_Then_OkResult()
        {
            // Arrange
            var banner = GenerateRandomBanner();

            var updateModel = new UpdateBannerModel
            {
                Title = Guid.NewGuid().ToString(),
                Html  = $"<html><body><span>{Guid.NewGuid()}</span></body></html>"
            };

            HtmlValidatorMock.Setup(v => v.Validate(updateModel.Html))
            .Returns(Task.FromResult(new ValidationResult()));

            // Act
            var result = await Controller.Update(banner.Id, updateModel);

            // Assert
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));

            var updatedBanner = ((OkObjectResult)result).Value as BannerModel;

            Assert.IsNotNull(updatedBanner);
            Assert.AreEqual(banner.Id, updatedBanner.Id);
            Assert.AreEqual(banner.Created, updatedBanner.Created);
            Assert.AreEqual(updateModel.Title, updatedBanner.Title);
            Assert.AreEqual(updateModel.Html, updatedBanner.Html);
            Assert.IsNotNull(updatedBanner.Modified);
        }
Ejemplo n.º 4
0
        public async Task Given_NonexistentBanner_When_Update_Then_NotFound()
        {
            // Arrange
            var updateModel = new UpdateBannerModel
            {
                Title = Guid.NewGuid().ToString(),
                Html  = $"<html><body><span>{Guid.NewGuid()}</span></body></html>"
            };

            // Act
            var result = await Controller.Update(new Random().Next(), updateModel);

            // Assert
            Assert.IsInstanceOfType(result, typeof(NotFoundResult));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Put(int id, [FromBody] UpdateBannerModel banner)
        {
            if (banner == null)
            {
                return(BadRequest());
            }

            var result = await _service.Update(banner);

            if (!result.Sucessful)
            {
                return(BadRequest(result));
            }

            return(Ok(result));
        }
Ejemplo n.º 6
0
        public async Task Given_AlreadyExistingTitle_When_Update_Then_Conflict()
        {
            // Arrange
            var firstBanner  = GenerateRandomBanner();
            var secondBanner = GenerateRandomBanner();

            var updateModel = new UpdateBannerModel
            {
                Title = firstBanner.Title,
                Html  = $"<html><body><span>{Guid.NewGuid()}</span></body></html>"
            };

            // Act
            var result = await Controller.Update(secondBanner.Id, updateModel);

            // Assert
            Assert.IsInstanceOfType(result, typeof(ConflictObjectResult));
            Assert.AreEqual("Title is already registered", ((ConflictObjectResult)result).Value);
        }
Ejemplo n.º 7
0
        public async Task UpdateBanner_WithValidHtml_ShouldReturnOkWithNewId()
        {
            var newBanner = await CreateBannerTestData();

            var banner = new UpdateBannerModel();

            banner.Id   = newBanner.Id.Value;
            banner.Html = ValidHtml;

            var httpResponse = await _client.PutAsJsonAsync("/api/banners/" + newBanner.Id, banner);

            var stringResponse = await httpResponse.Content.ReadAsStringAsync();

            var result = JsonConvert.DeserializeObject <SaveBannerResult>(stringResponse);

            Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode);
            Assert.Empty(result.Errors);
            Assert.True(result.Sucessful);
        }
Ejemplo n.º 8
0
        public async Task UpdateBanner_WithInvalidHtml_ShouldReturnBadRequestsWithDetails()
        {
            var newBanner = await CreateBannerTestData();

            var banner = new UpdateBannerModel();

            banner.Id   = newBanner.Id.Value;
            banner.Html = "ABC123";

            var httpResponse = await _client.PutAsJsonAsync("/api/banners/" + newBanner.Id, banner);

            var stringResponse = await httpResponse.Content.ReadAsStringAsync();

            var result = JsonConvert.DeserializeObject <SaveBannerResult>(stringResponse);

            Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
            Assert.False(result.Sucessful);
            Assert.NotEmpty(result.Errors);
        }
Ejemplo n.º 9
0
        public async Task UpdateBanner_WithWrongId_ShouldReturnErrorWithDescription()
        {
            var validationSuccesful = new ValidationResult(true);

            _htmlValidator.Setup(x => x.Validate(It.IsAny <string>())).Returns(Task.FromResult(validationSuccesful));

            var bannerService  = new BannerService(_htmlValidator.Object, _repository.Object);
            var bannerToUpdate = new UpdateBannerModel
            {
                Id   = 1,
                Html = "ABC123"
            };

            var result = await bannerService.Update(bannerToUpdate);

            Assert.False(result.Sucessful);
            Assert.NotEmpty(result.Errors);
            Assert.Contains("Banner not found", result.Errors);
        }
Ejemplo n.º 10
0
        public async Task UpdateBanner_WithInvalidHtml_ShouldReturnErrorWithDescription()
        {
            var validationFailed = new ValidationResult(false, new List <string> {
                "Error"
            });

            _htmlValidator.Setup(x => x.Validate(It.IsAny <string>())).Returns(Task.FromResult(validationFailed));

            var bannerService  = new BannerService(_htmlValidator.Object, _repository.Object);
            var bannerToUpdate = new UpdateBannerModel
            {
                Id   = 1,
                Html = "ABC123"
            };

            var result = await bannerService.Update(bannerToUpdate);

            Assert.False(result.Sucessful);
            Assert.NotEmpty(result.Errors);
            Assert.Contains("Error", result.Errors);
        }
Ejemplo n.º 11
0
        public async Task <SaveBannerResult> Update(UpdateBannerModel banner)
        {
            var validationResult = await _validator.Validate(banner.Html);

            if (!validationResult.IsValid)
            {
                return(ValidationFailed(validationResult));
            }

            var bannerToUpdate = await _repository.Get(banner.Id);

            if (bannerToUpdate == null)
            {
                return(BannerNotFound());
            }

            bannerToUpdate.Html     = banner.Html;
            bannerToUpdate.Modified = DateTime.Now;

            await _repository.Update(bannerToUpdate);

            return(SaveSucessful(bannerToUpdate));
        }