public void CreateTodoViewModel_AllFieldsValid_NoErrors()
        {
            // Arrange
            var model = MockCreateTodoViewModel.Get(1);

            // Act
            var validation = ModelValidator.ValidateModel(model);

            // Assert
            Assert.Equal(0, validation.Count);
        }
        public void CreateTodoViewModel_Setter_Modifies()
        {
            // Arrange
            var viewModel = MockCreateTodoViewModel.Get(0);

            // Act
            viewModel.Description = "Drink a gallon of habanero tabasco";
            viewModel.Due         = new DateTime(2025, 12, 22, 0, 0, 0);

            // Assert
            Assert.Equal("Drink a gallon of habanero tabasco", viewModel.Description);
            Assert.Equal(new DateTime(2025, 12, 22, 0, 0, 0), viewModel.Due);
        }
Пример #3
0
        public async Task Create_NoToken_Unauthorized()
        {
            // Arrange
            var          model   = MockCreateTodoViewModel.Get(0);
            var          body    = JsonStringBuilder.CreateTodoJsonBody(model.Description, model.Due.ToString());
            var          content = new StringContent(body);
            const string path    = Routes.TodoRoute;

            // Act
            var response = await _endSystems.Post(path, content);

            // Assert
            Assert.Equal(HttpStatusCode.Unauthorized, response.Code);

            // Tear down
            _endSystems.Dispose();
        }
        public async Task CreateTodo_InvalidUser_UserNotFoundException()
        {
            // Arrange
            var service = new MockTodoService
            {
                MCreateTodoAsync = (model, userId) => throw new UserNotFoundException()
            };
            var controller = new TodoController(service);

            MockClaims.AddUserIdClaim(controller, MockApplicationUsers.Get(0).Id);

            // Act
            var result = await controller.CreateTodo(MockCreateTodoViewModel.Get(0)) as UnauthorizedResult;

            // Assert
            Assert.NotNull(result);
            Assert.Equal(401, result.StatusCode);
        }
Пример #5
0
        public void TodoUserAndViewModelConstructor_Getter_Matches()
        {
            // Arrange
            var viewModel = MockCreateTodoViewModel.Get(1);
            var user      = MockApplicationUsers.Get(3);

            // Act
            var todo = new Todo(viewModel, user);

            // Assert
            Assert.Equal(0 /* unset */, todo.Id);
            Assert.Equal(viewModel.Due, todo.Due);
            Assert.Equal(viewModel.Description, todo.Description);
            Assert.Equal(user.Id, todo.Owner.Id);
            Assert.Equal(user.Name, todo.Owner.Name);
            Assert.Equal(user.Email, todo.Owner.Email);
            Assert.Equal(user.UserName, todo.Owner.UserName);
            Assert.Equal(user.PasswordHash, todo.Owner.PasswordHash);
        }
        public async Task CreateTodo_ValidUserAndTodo_CreatedAtRoute()
        {
            // Arrange
            var id      = 15;
            var service = new MockTodoService
            {
                MCreateTodoAsync = (model, userId) => id
            };
            var controller = new TodoController(service);

            MockClaims.AddUserIdClaim(controller, MockApplicationUsers.Get(0).Id);

            // Act
            var result = await controller.CreateTodo(MockCreateTodoViewModel.Get(0)) as CreatedAtRouteResult;

            // Assert
            Assert.NotNull(result);
            Assert.Equal(201, result.StatusCode);
            Assert.Equal(MethodNames.GetSingleTodoMethodName, result.RouteName);
            Assert.Single(result.RouteValues.Keys);
            Assert.True(result.RouteValues.ContainsKey("todoId"));
            Assert.Equal(id, result.RouteValues.GetValueOrDefault("todoId"));
        }
        public async Task CreateTodo_InvalidModel_BadRequest()
        {
            // Arrange
            const string field        = "required";
            const string errorMessage = "something is required";
            var          service      = new MockTodoService();
            var          controller   = new TodoController(service);

            controller.ModelState.AddModelError(field, errorMessage);
            var model = MockCreateTodoViewModel.Get(0);

            // Act
            var result = await controller.CreateTodo(model) as BadRequestObjectResult;

            var error = result?.Value as SerializableError;

            // Assert
            Assert.NotNull(error);
            Assert.Equal(400, result.StatusCode);
            Assert.Single(error.Keys);
            Assert.True(error.ContainsKey(field));
            Assert.Equal(new[] { errorMessage }, error.GetValueOrDefault(field));
        }