public async Task HandleAsync_WhenCommandIsCorrent_ShouldReturnSuccessResult() { // Arrange var handler = new CreateWebsiteHandler(_repositoryMock.Object, _unitOfWorkMock.Object, _cypherMock.Object); // Act var request = new Application.Websites.Commands.CreateWebsite.CreateWebsite("mySite", "www.mysite.com", new List <string> { "cat1", "cat2" }, new ImageManipulation("myImage.png", "image/png", new byte[1]), "*****@*****.**", "123456"); OperationResult <WebsiteOutputModel> createWebsiteOperation = await handler.Handle(request, CancellationToken.None); // Assert _unitOfWorkMock.Verify(x => x.CommitAsync(CancellationToken.None), Times.Once); _repositoryMock.Verify(x => x.Add(It.IsAny <Website>()), Times.Once); createWebsiteOperation.Should().BeOfType(typeof(OperationResult <WebsiteOutputModel>)); createWebsiteOperation.IsSuccessful.Should().BeTrue(); createWebsiteOperation.Errors.Should().BeNull(); createWebsiteOperation.Result.Name.Should().Be("mySite"); createWebsiteOperation.Result.Url.Should().Be("www.mysite.com"); createWebsiteOperation.Result.Categories.Count.Should().Be(2); createWebsiteOperation.Result.Categories[0].Should().Be("cat1"); createWebsiteOperation.Result.Categories[1].Should().Be("cat2"); createWebsiteOperation.Result.Image.Name.Should().Be("myImage.png"); createWebsiteOperation.Result.Image.DownloadLink.Should().BeNull(); createWebsiteOperation.Result.Login.Email.Should().Be("*****@*****.**"); createWebsiteOperation.Result.Login.Password.Should().Be("123456"); }
public async Task IsValid_WhenRequestIsValid_ReturnSuccessResult() { // Act var command = new Application.Websites.Commands.CreateWebsite.CreateWebsite("aaaa", "www.mysite.com", new List <string> { "cat1,cat2" }, new ImageManipulation("$myImage.png", "image/png", new byte[1]), "*****@*****.**", "123456"); RequestHandlerDelegate <OperationResult <WebsiteOutputModel> > requestHandlerDelegate = () => Task.FromResult(OperationResult <WebsiteOutputModel> .Success(null)); var validationResult = await _validator.Handle(command, CancellationToken.None, requestHandlerDelegate); // Assert validationResult.IsSuccessful.Should().BeTrue(); }
public async Task IsValid_WhenFileExpensionNotSupported_ReturnFailureResult(string extenstion) { // Act var command = new Application.Websites.Commands.CreateWebsite.CreateWebsite("aaaa", "www.mysite.com", new List <string> { "cat1,cat2" }, new ImageManipulation($"$myImage{extenstion}", "image/png", new byte[1]), "*****@*****.**", "123456"); var validationResult = await _validator.Handle(command, CancellationToken.None, null); // Assert validationResult.IsSuccessful.Should().BeFalse(); validationResult.Errors.Count.Should().Be(1); validationResult.Errors.First().Key.Should().Be("Image"); validationResult.Errors.First().Value.Should().Be("File extension is not supported. Supported types are: .jpg, .png, .jpeg"); }
public async Task IsValid_WhenNameIsLessThan3Characters_ReturnFailureResult(string name) { // Act var command = new Application.Websites.Commands.CreateWebsite.CreateWebsite(name, "www.mysite.com", new List <string> { "cat1,cat2" }, new ImageManipulation("myImage.png", "image/png", new byte[1]), "*****@*****.**", "123456"); var validationResult = await _validator.Handle(command, CancellationToken.None, null); // Assert validationResult.IsSuccessful.Should().BeFalse(); validationResult.Errors.Count.Should().Be(1); validationResult.Errors.First().Key.Should().Be("Name"); validationResult.Errors.First().Value.Should().Be("Name should be more than 3 and less than 100 characters."); }
public async Task IsValid_WhenFileIsGreaterThan5Mb_ReturnFailureResult() { long fileLength = 6 * 1024 * 1024; // Act var command = new Application.Websites.Commands.CreateWebsite.CreateWebsite("aaaa", "www.mysite.com", new List <string> { "cat1,cat2" }, new ImageManipulation("$myImage.png", "image/png", new byte[fileLength]), "*****@*****.**", "123456"); var validationResult = await _validator.Handle(command, CancellationToken.None, null); // Assert validationResult.IsSuccessful.Should().BeFalse(); validationResult.Errors.Count.Should().Be(1); validationResult.Errors.First().Key.Should().Be("Image"); validationResult.Errors.First().Value.Should().Be("File size is greater than 5 MB."); }
public async Task HandleAsync_WhenCommitAsyncThrowsUrlExistsException_ShouldReturnFailureResult() { // Arrange _unitOfWorkMock.Setup(x => x.CommitAsync(CancellationToken.None)).Throws <UrlExistsException>(); var handler = new CreateWebsiteHandler(_repositoryMock.Object, _unitOfWorkMock.Object, _cypherMock.Object); // Act var request = new Application.Websites.Commands.CreateWebsite.CreateWebsite("mySite", "www.mysite.com", new List <string> { "cat1,cat2" }, new ImageManipulation("myImage.png", "image/png", new byte[1]), "*****@*****.**", "123456"); OperationResult <WebsiteOutputModel> createWebsiteOperation = await handler.Handle(request, CancellationToken.None); // Assert _unitOfWorkMock.Verify(x => x.CommitAsync(CancellationToken.None), Times.Once); createWebsiteOperation.Should().BeOfType(typeof(OperationResult <WebsiteOutputModel>)); createWebsiteOperation.IsSuccessful.Should().BeFalse(); createWebsiteOperation.Errors.Count.Should().Be(1); createWebsiteOperation.Errors.First().Key.Should().Be("Url"); createWebsiteOperation.Errors.First().Value.Should().Be("Url already exists."); }
public void ToWebsite_WithCorrectCreateWebsite_ShouldReturnWebsite() { // Arrange var createWebsite = new Application.Websites.Commands.CreateWebsite.CreateWebsite("mySite", "www.ank.com", new List <string> { "cat" }, new ImageManipulation("myimg", "png", new byte[4]), "*****@*****.**", "123456"); // Act var website = createWebsite.ToWebsite("encryptedPassword"); // Assert website.Name.Should().Be("mySite"); website.Url.Should().Be("www.ank.com"); website.Categories.Count.Should().Be(1); website.Categories.First().Value.Should().Be("cat"); website.Image.Name.Should().Be("myimg"); website.Image.MimeType.Should().Be("png"); website.Image.Blob.Length.Should().Be(4); website.Email.Should().Be("*****@*****.**"); website.Password.Should().Be("encryptedPassword"); website.IsDeleted.Should().Be(false); }