public void Put_WhenCorrectTestViewModelUserIsNotPermittedToDeleteButIsAdmin_ShouldReturnOkResult()
        {
            var testId = 2;
            var test   = new Test
            {
                Id     = testId,
                Title  = "title1",
                UserId = "user1"
            };

            _mockRepo.Setup(x => x.UpdateTest(It.Is <Test>(t => t.Id == testId))).Returns(Task.FromResult(test));
            _mockRepo.Setup(x => x.GetTest(testId)).Returns(Task.FromResult(test));

            var user = new ClaimsPrincipal(new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.NameIdentifier, _username),
                new Claim(ClaimTypes.Role, "Admin"),
            }, "mock"));

            var controller = new TestController(_mockRepo.Object, _mockHub.Object)
            {
                ControllerContext = new ControllerContext
                {
                    HttpContext = new DefaultHttpContext {
                        User = user
                    }
                }
            };

            var result = controller.Put(test.Adapt <TestViewModel>()).Result as JsonResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.GetValueFromJsonResult <string>("Title"), test.Title);
            Assert.AreEqual(result.GetValueFromJsonResult <int>("Id"), test.Id);
        }
        public void Put_WhenCorrectTestViewModelButUserIsNotPermittedToEdit_ShouldReturnUnauthorized()
        {
            var test = new Test
            {
                Id     = 1,
                Title  = "title1",
                UserId = "user1"
            };

            _mockRepo.Setup(x => x.GetTest(test.Id)).Returns(Task.FromResult(test));

            var result = _sut.Put(test.Adapt <TestViewModel>()).Result;

            Assert.IsNotNull(result);
            Assert.IsInstanceOf <UnauthorizedResult>(result);
        }
        public void Post_WhenInvalidViewModelGiven_ShouldReturnStatusCode500()
        {
            var testId = 1;
            var test   = new Test
            {
                Id    = testId,
                Title = "title1"
            };

            _mockRepo.Setup(x => x.CreateTest(It.Is <Test>(t => t.Id == testId))).Throws(new Exception());

            var result = _sut.Post(test.Adapt <TestViewModel>()).Result as StatusCodeResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.StatusCode, 500);
        }
        public void Put_WhenCorrectTestViewModelErrorDuringProcessing_ShouldReturnsNotFound()
        {
            var test = new Test
            {
                Id     = 1,
                Title  = "title1",
                UserId = _username
            };

            _mockRepo.Setup(x => x.GetTest(test.Id)).Returns(Task.FromResult(test));
            _mockRepo.Setup(x => x.UpdateTest(test)).Returns <TestViewModel>(null);

            var result = _sut.Put(test.Adapt <TestViewModel>()).Result;

            Assert.IsNotNull(result);
            Assert.IsInstanceOf <NotFoundObjectResult>(result);
        }
        public void Put_WhenErrorDuringProcessing_ShouldReturnStatusCode500()
        {
            var testId = 1;
            var test   = new Test
            {
                Id     = 1,
                Title  = "title1",
                UserId = _username
            };

            _mockRepo.Setup(x => x.GetTest(test.Id)).Returns(Task.FromResult(test));
            _mockRepo.Setup(x => x.UpdateTest(It.Is <Test>(t => t.Id == testId))).Throws(new Exception());

            var result = _sut.Put(test.Adapt <TestViewModel>()).Result as StatusCodeResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.StatusCode, 500);
        }
        public void Post_WhenCorrectViewModelGiven_ShouldReturnJsonViewModel()
        {
            var testId = 1;
            var test   = new Test
            {
                Id    = testId,
                Title = "title1"
            };

            _mockRepo.Setup(x => x.CreateTest(It.Is <Test>(t => t.Id == testId))).Returns(Task.FromResult(test));
            _mockHub.Setup(x => x.Clients.All.TestCreated()).Returns(Task.CompletedTask);

            var result = _sut.Post(test.Adapt <TestViewModel>()).Result as JsonResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.GetValueFromJsonResult <string>("Title"), test.Title);
            Assert.AreEqual(result.GetValueFromJsonResult <int>("Id"), test.Id);
        }
        public void Put_WhenCorrectViewModelGiven_ShouldReturnsJsonViewModel()
        {
            var testId = 1;
            var test   = new Test
            {
                Id     = 1,
                Title  = "title1",
                UserId = _username
            };

            _mockRepo.Setup(x => x.GetTest(test.Id)).Returns(Task.FromResult(test));
            _mockRepo.Setup(x => x.UpdateTest(It.Is <Test>(t => t.Id == testId))).Returns(Task.FromResult(test));

            var result = _sut.Put(test.Adapt <TestViewModel>()).Result as JsonResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.GetValueFromJsonResult <string>("Title"), test.Title);
            Assert.AreEqual(result.GetValueFromJsonResult <int>("Id"), test.Id);
        }