Beispiel #1
0
        private void MethodDeleteVideoDeletesVideoFromRepo()
        {
            _videoServiceMock.Setup(repo => repo.GetById(3)).Returns(_video3);
            _videoServiceMock.Setup(repo => repo.Delete(_video3));

            var actionResult = _controller.DeleteVideo(3);

            Assert.IsType <OkResult>(actionResult);

            _videoServiceMock.Verify(repo => repo.Delete(_video3), Times.Once);
        }
        public async void DeleteVideo_CalledWithInvalidId_ReturnsNotFoundWithId()
        {
            IUnitOfWork    unitOfWork    = Substitute.For <IUnitOfWork>();
            IVideoManager  videoManager  = Substitute.For <IVideoManager>();
            ICookieManager cookieManager = Substitute.For <ICookieManager>();
            var            context       = Substitute.For <HttpContext>();

            long  notValidId = 1;
            Video nullVideo  = null;

            unitOfWork.Videos.GetById(notValidId).Returns(nullVideo);

            cookieManager.GetRoleFromToken(Arg.Any <string>()).Returns(Role.Admin.ToString());

            var videosController = new VideosController(unitOfWork, videoManager, cookieManager);

            videosController.ControllerContext = new ControllerContext()
            {
                HttpContext = context
            };

            var result = await videosController.DeleteVideo(notValidId);

            var notFoundObjectResult = Assert.IsType <NotFoundObjectResult>(result);
            var returnValue          = Assert.IsType <long>(notFoundObjectResult.Value);

            Assert.Equal(notValidId, returnValue);
        }
        public async void DeleteVideo_CalledAndRoleIsNotAdmin_ReturnsBadRequestWithError()
        {
            IUnitOfWork    unitOfWork    = Substitute.For <IUnitOfWork>();
            IVideoManager  videoManager  = Substitute.For <IVideoManager>();
            ICookieManager cookieManager = Substitute.For <ICookieManager>();
            var            context       = Substitute.For <HttpContext>();

            long   testId    = 1;
            var    testVideo = new Video();
            string error     = "Only Admins can delete videos.";

            unitOfWork.Videos.GetById(testId).Returns(testVideo);

            cookieManager.GetRoleFromToken(Arg.Any <string>()).Returns(Role.Student.ToString());

            var videosController = new VideosController(unitOfWork, videoManager, cookieManager);

            videosController.ControllerContext = new ControllerContext()
            {
                HttpContext = context
            };

            var result = await videosController.DeleteVideo(testId);

            var badRequestObjectResult = Assert.IsType <BadRequestObjectResult>(result);
            var retrunValue            = Assert.IsType <string>(badRequestObjectResult.Value);

            Assert.Equal(error, retrunValue);
        }
        public async void DeleteVideo_CalledWithValidId_ReturnsOk()
        {
            IUnitOfWork    unitOfWork    = Substitute.For <IUnitOfWork>();
            IVideoManager  videoManager  = Substitute.For <IVideoManager>();
            ICookieManager cookieManager = Substitute.For <ICookieManager>();
            var            context       = Substitute.For <HttpContext>();

            long testId    = 1;
            var  testVideo = new Video();

            unitOfWork.Videos.GetById(testId).Returns(testVideo);

            cookieManager.GetRoleFromToken(Arg.Any <string>()).Returns(Role.Admin.ToString());

            var videosController = new VideosController(unitOfWork, videoManager, cookieManager);

            videosController.ControllerContext = new ControllerContext()
            {
                HttpContext = context
            };

            var result = await videosController.DeleteVideo(testId);

            var okResult = Assert.IsType <OkResult>(result);

            Assert.Equal(200, okResult.StatusCode);
        }