Beispiel #1
0
        public async void Patch_No_Errors()
        {
            VoteControllerMockFacade mock = new VoteControllerMockFacade();
            var mockResult = new Mock <UpdateResponse <ApiVoteResponseModel> >();

            mockResult.SetupGet(x => x.Success).Returns(true);
            mock.ServiceMock.Setup(x => x.Update(It.IsAny <int>(), It.IsAny <ApiVoteRequestModel>()))
            .Callback <int, ApiVoteRequestModel>(
                (id, model) => model.BountyAmount.Should().Be(1)
                )
            .Returns(Task.FromResult <UpdateResponse <ApiVoteResponseModel> >(mockResult.Object));
            mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <ApiVoteResponseModel>(new ApiVoteResponseModel()));
            VoteController controller = new VoteController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, new ApiVoteModelMapper());

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            var patch = new JsonPatchDocument <ApiVoteRequestModel>();

            patch.Replace(x => x.BountyAmount, 1);

            IActionResult response = await controller.Patch(default(int), patch);

            response.Should().BeOfType <OkObjectResult>();
            (response as OkObjectResult).StatusCode.Should().Be((int)HttpStatusCode.OK);
            mock.ServiceMock.Verify(x => x.Update(It.IsAny <int>(), It.IsAny <ApiVoteRequestModel>()));
        }
        private void AssertPatchFails(HttpStatusCode resultCode)
        {
            var voteController = new VoteController(this.mockNewsRepository.Object, this.mockVoteRepository.Object, this.inputValidation.Object, this.businessValidation.Object);

            var request = new HttpRequestMessage(HttpMethod.Post, GOUriBuilder.BuildAbsoluteVoteUri(Scheme, Host, Port, City, Date, Id.ToString()));

            request.Headers.Referrer = new Uri(GOUriBuilder.BuildAbsoluteVoteUri(Scheme, Host, Port, City, Date, Id.ToString()));

            voteController.ConfigureForTesting(request, "PostVote", new HttpRoute(GOUriBuilder.NewsVoteTemplate));
            voteController.User = new GenericPrincipal(new GenericIdentity(DefaultNickname), null);

            HttpResponseMessage response = voteController.Patch(City, Date, Id.ToString(), DefaultVote).Result;

            Assert.AreEqual(resultCode, response.StatusCode);
            this.mockVoteRepository.Verify(storage => storage.AddVote(It.IsAny <string>(), It.IsAny <DateTime>(), It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <VoteBll>()), Times.Never());
        }
Beispiel #3
0
        public async void Patch_Record_Not_Found()
        {
            VoteControllerMockFacade mock = new VoteControllerMockFacade();
            var mockResult = new Mock <ActionResponse>();

            mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <ApiVoteResponseModel>(null));
            VoteController controller = new VoteController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object);

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            var patch = new JsonPatchDocument <ApiVoteRequestModel>();

            patch.Replace(x => x.BountyAmount, 1);

            IActionResult response = await controller.Patch(default(int), patch);

            response.Should().BeOfType <StatusCodeResult>();
            (response as StatusCodeResult).StatusCode.Should().Be((int)HttpStatusCode.NotFound);
            mock.ServiceMock.Verify(x => x.Get(It.IsAny <int>()));
        }
        public void TestPatchReturns200_IfEverythingIsOk()
        {
            this.inputValidation.Setup(validation => validation.IsValidVote(It.IsAny <Vote>())).Returns(true);
            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.IsValidGetVote(this.mockVoteRepository.Object, It.IsAny <string>(), It.IsAny <DateTime>(), It.IsAny <Guid>(), It.IsAny <string>())).Returns(Task.FromResult(true));

            var voteController = new VoteController(this.mockNewsRepository.Object, this.mockVoteRepository.Object, this.inputValidation.Object, this.businessValidation.Object);

            var request = new HttpRequestMessage(new HttpMethod("PATCH"), GOUriBuilder.BuildAbsoluteVoteUri(Scheme, Host, Port, City, Date, Id.ToString()));

            request.Headers.Referrer = new Uri(GOUriBuilder.BuildAbsoluteVoteUri(Scheme, Host, Port, City, Date, Id.ToString()));

            voteController.ConfigureForTesting(request, "PatchVote", new HttpRoute(GOUriBuilder.NewsVoteTemplate));
            voteController.User = new GenericPrincipal(new GenericIdentity(DefaultNickname), null);

            HttpResponseMessage response = voteController.Patch(City, Date, Id.ToString(), new Vote {
                Value = 5
            }).Result;

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            this.mockVoteRepository.Verify(repository => repository.UpdateVote(It.IsAny <string>(), It.IsAny <DateTime>(), It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <VoteBll>()), Times.Once);
        }