public void TestPostImageReturns200_WhenTheImageIsInTheDatabase() { Guid guid = Guid.NewGuid(); this.businessValidation.Setup(validation => validation.IsValidGetNews(this.mockNewsRepository.Object, It.IsAny <string>(), It.IsAny <DateTime>(), It.IsAny <Guid>())).Returns(Task.FromResult(true)); this.businessValidation.Setup(validation => validation.IsValidModifyNews(this.mockNewsRepository.Object, It.IsAny <string>(), It.IsAny <DateTime>(), It.IsAny <Guid>(), It.IsAny <string>())).Returns(Task.FromResult(true)); this.businessValidation.Setup(validation => validation.IsValidGetImageNews(this.mockImageRepository.Object, It.IsAny <string>(), It.IsAny <DateTime>(), It.IsAny <Guid>())).Returns(Task.FromResult(false)); this.mockImageRepository.Setup(storage => storage.GetNewsImage(It.IsAny <string>(), It.IsAny <DateTime>(), It.IsAny <Guid>())).Returns(Task.FromResult(Image.FromFile("goten.png"))); var newsImageController = new NewsImageController(this.mockNewsRepository.Object, this.mockImageRepository.Object, this.inputValidation.Object, this.businessValidation.Object); Image image = Image.FromFile("goten.png"); MemoryStream stream = new MemoryStream(); image.Save(stream, image.RawFormat); stream.Position = 0; var request = new HttpRequestMessage(HttpMethod.Post, GOUriBuilder.BuildAbsoluteNewsImageUri(Scheme, Host, Port, City, Date, guid.ToString())); request.Headers.Referrer = new Uri(GOUriBuilder.BuildAbsoluteNewsImageUri(Scheme, Host, Port, City, Date, guid.ToString())); request.Content = new ByteArrayContent(stream.GetBuffer()); newsImageController.ConfigureForTesting(request, "PostImage", new HttpRoute(GOUriBuilder.NewsImageTemplate)); newsImageController.User = new GenericPrincipal(new GenericIdentity(DefaultNickname), null); HttpResponseMessage response = newsImageController.Post(City, Date, guid.ToString()).Result; Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); }
private void AssertPostFails(HttpStatusCode resultCode) { var newsImageController = new NewsImageController(this.mockNewsRepository.Object, this.mockImageRepository.Object, this.inputValidation.Object, this.businessValidation.Object); var request = new HttpRequestMessage(HttpMethod.Post, GOUriBuilder.BuildAbsoluteNewsImageUri(Scheme, Host, Port, City, Date, Id.ToString())); request.Headers.Referrer = new Uri(GOUriBuilder.BuildAbsoluteNewsImageUri(Scheme, Host, Port, City, Date, Id.ToString())); request.Content = new ByteArrayContent(new byte[0]); newsImageController.ConfigureForTesting(request, "PostImage", new HttpRoute(GOUriBuilder.NewsImageTemplate)); newsImageController.User = new GenericPrincipal(new GenericIdentity(DefaultNickname), null); HttpResponseMessage response = newsImageController.Post(City, Date, Id.ToString()).Result; Assert.AreEqual(resultCode, response.StatusCode); this.mockImageRepository.Verify(storage => storage.CreateNewsImage(It.IsAny <string>(), It.IsAny <DateTime>(), It.IsAny <Guid>(), It.IsAny <Image>()), Times.Never()); }