public async Task Register_ReturnsCorrectJson_WhenTaskIsClosed() { const string taskSignUpResultStatus = TaskSignupResult.FAILURE_CLOSEDTASK; var model = new TaskSignupViewModel(); var mediator = new Mock <IMediator>(); mediator.Setup(x => x.SendAsync(It.Is <TaskSignupCommandAsync>(y => y.TaskSignupModel == model))) .Returns(Task.FromResult(new TaskSignupResult { Status = taskSignUpResultStatus })); var sut = new TaskApiController(mediator.Object, null, null); var jsonResult = await sut.RegisterTask(model) as JsonResult; var successStatus = jsonResult.GetValueForProperty <bool>("isSuccess"); var errors = jsonResult.GetValueForProperty <string[]>("errors"); Assert.False(successStatus); Assert.NotNull(errors); Assert.Equal(1, errors.Count()); Assert.Equal("Signup failed - Task is closed", errors[0]); }
public async Task Register_ReturnsCorrectJson_WhenApiResult_IsSuccess() { const string taskSignUpResultStatus = TaskSignupResult.SUCCESS; var model = new TaskSignupViewModel(); var mediator = new Mock <IMediator>(); mediator.Setup(x => x.SendAsync(It.Is <TaskSignupCommandAsync>(y => y.TaskSignupModel == model))) .Returns(Task.FromResult(new TaskSignupResult { Status = taskSignUpResultStatus, Task = new AllReadyTask { Id = 1, Name = "Task" } })); var sut = new TaskApiController(mediator.Object, null, null); var jsonResult = await sut.RegisterTask(model) as JsonResult; var successStatus = jsonResult.GetValueForProperty <bool>("isSuccess"); var taskModel = jsonResult.GetValueForProperty <TaskViewModel>("task"); Assert.True(successStatus); Assert.NotNull(taskModel); }
public async Task Register_ReturnsCorrectJson_WhenEventNotFound() { const string taskSignUpResultStatus = TaskSignupResult.FAILURE_EVENTNOTFOUND; var model = new TaskSignupViewModel(); var mediator = new Mock <IMediator>(); mediator.Setup(x => x.SendAsync(It.Is <TaskSignupCommand>(y => y.TaskSignupModel == model))) .ReturnsAsync(new TaskSignupResult { Status = taskSignUpResultStatus }); var sut = new TaskApiController(mediator.Object, null, null); var jsonResult = await sut.RegisterTask(model) as JsonResult; var successStatus = jsonResult.GetValueForProperty <bool>("isSuccess"); var errors = jsonResult.GetValueForProperty <string[]>("errors"); Assert.False(successStatus); Assert.NotNull(errors); Assert.Equal(1, errors.Count()); Assert.Equal("Signup failed - The event could not be found", errors[0]); }
public async Task <ActionResult> RegisterTask(TaskSignupViewModel signupModel) { if (signupModel == null) { return(BadRequest()); } if (!ModelState.IsValid) { // this condition should never be hit because client side validation is being performed // but just to cover the bases, if this does happen send the erros to the client return(Json(new { errors = ModelState.GetErrorMessages() })); } var result = await _mediator.SendAsync(new TaskSignupCommand { TaskSignupModel = signupModel }); switch (result.Status) { case TaskSignupResult.SUCCESS: return(Json(new { isSuccess = true, task = result.Task == null ? null : new TaskViewModel(result.Task, signupModel.UserId) })); case TaskSignupResult.FAILURE_CLOSEDTASK: return(Json(new { isSuccess = false, errors = new[] { "Signup failed - Task is closed" }, })); case TaskSignupResult.FAILURE_EVENTNOTFOUND: return(Json(new { isSuccess = false, errors = new[] { "Signup failed - The event could not be found" }, })); case TaskSignupResult.FAILURE_TASKNOTFOUND: return(Json(new { isSuccess = false, errors = new[] { "Signup failed - The task could not be found" }, })); default: return(Json(new { isSuccess = false, errors = new[] { "Unkown error" }, })); } }
public void SetTaskIdAndTaskName_WhenTaskSignupsTaskIsNotNull() { var taskSignup = new TaskSignup { Task = new AllReadyTask { Id = 1, Name = "TaskName" } }; var sut = new TaskSignupViewModel(taskSignup); Assert.Equal(sut.TaskId, taskSignup.Task.Id); Assert.Equal(sut.TaskName, taskSignup.Task.Name); }
public void SetUserIdAndUserName_WhenTaskSignupsUserIsNotNull() { var taskSignup = new TaskSignup { User = new ApplicationUser { Id = "1", UserName = "******" } }; var sut = new TaskSignupViewModel(taskSignup); Assert.Equal(sut.UserId, taskSignup.User.Id); Assert.Equal(sut.UserName, taskSignup.User.UserName); }
public async Task RegisterTaskSendsTaskSignupCommandWithCorrectTaskSignupModel() { var model = new TaskSignupViewModel(); var mediator = new Mock <IMediator>(); mediator.Setup(x => x.SendAsync(It.Is <TaskSignupCommandAsync>(y => y.TaskSignupModel == model))).Returns(Task.FromResult(new TaskSignupResult())); var sut = new TaskApiController(mediator.Object, null, null); await sut.RegisterTask(model); mediator.Verify(x => x.SendAsync(It.Is <TaskSignupCommandAsync>(command => command.TaskSignupModel.Equals(model)))); }