public async Task DeleteAttemptAsync_GivenAnotherUsersGoal_ShouldReturnBadRequest() { // Arrange. var attemptStore = Substitute.For <IGoalAttemptStore>(); var goalStore = Substitute.For <IGoalStore>(); var tokenStore = Substitute.For <ITokenStore>(); var userStore = Substitute.For <IUserStore>(); var log = Substitute.For <ILogger>(); var testObject = new GoalAttemptController(attemptStore, goalStore, tokenStore, userStore, log); var currentUser = new User { Id = 1 }; var otherUsersGoal = new Goal { UserId = 100 }; testObject.ControllerContext = new HttpControllerContext { Request = new HttpRequestMessage() }; testObject.ControllerContext.Request.Headers.Add("auth", Guid.NewGuid().ToString()); tokenStore.GetExistingValidTokenByGuidAsync(Arg.Any <Guid>()).Returns(new Token()); userStore.GetUserAsync(Arg.Any <Token>()).Returns(currentUser); goalStore.GetGoalAsync(123).Returns(otherUsersGoal); // Act. var result = await testObject.DeleteAttemptAsync(123); // Assert. var notFoundResult = result as NotFoundResult; Assert.NotNull(notFoundResult); }
public async Task DeleteAttemptAsync_GivenValidTokenButNoUser_ShouldReturnInternalServerError() { // Arrange. var attemptStore = Substitute.For <IGoalAttemptStore>(); var goalStore = Substitute.For <IGoalStore>(); var tokenStore = Substitute.For <ITokenStore>(); var userStore = Substitute.For <IUserStore>(); var log = Substitute.For <ILogger>(); var testObject = new GoalAttemptController(attemptStore, goalStore, tokenStore, userStore, log); testObject.ControllerContext = new HttpControllerContext { Request = new HttpRequestMessage() }; testObject.ControllerContext.Request.Headers.Add("auth", Guid.NewGuid().ToString()); tokenStore.GetExistingValidTokenByGuidAsync(Arg.Any <Guid>()).Returns(new Token()); // Act. var result = await testObject.DeleteAttemptAsync(123); // Assert. var serverErrorResult = result as InternalServerErrorResult; Assert.NotNull(serverErrorResult); }
public async Task DeleteAttemptAsync_GivenValidAttempt_ShouldRemoveFromDb() { // Arrange. var attemptStore = Substitute.For <IGoalAttemptStore>(); var goalStore = Substitute.For <IGoalStore>(); var tokenStore = Substitute.For <ITokenStore>(); var userStore = Substitute.For <IUserStore>(); var log = Substitute.For <ILogger>(); var testObject = new GoalAttemptController(attemptStore, goalStore, tokenStore, userStore, log); var user = new User { Id = 1 }; var goal = new Goal { UserId = 1 }; testObject.ControllerContext = new HttpControllerContext { Request = new HttpRequestMessage() }; testObject.ControllerContext.Request.Headers.Add("auth", Guid.NewGuid().ToString()); tokenStore.GetExistingValidTokenByGuidAsync(Arg.Any <Guid>()).Returns(new Token()); userStore.GetUserAsync(Arg.Any <Token>()).Returns(user); goalStore.GetGoalAsync(Arg.Any <int>()).Returns(goal); attemptStore.GetAttemptAsync(101).Returns(new GoalAttempt()); // Act. var result = await testObject.DeleteAttemptAsync(101); // Assert. var okResult = result as OkResult; Assert.NotNull(okResult); await attemptStore.Received(1).RemoveAttemptAsync(101); }
public async Task DeleteAttemptAsync_GivenNoAuthToken_ShouldReturnBadRequest() { // Arrange. var attemptStore = Substitute.For <IGoalAttemptStore>(); var goalStore = Substitute.For <IGoalStore>(); var tokenStore = Substitute.For <ITokenStore>(); var userStore = Substitute.For <IUserStore>(); var log = Substitute.For <ILogger>(); var testObject = new GoalAttemptController(attemptStore, goalStore, tokenStore, userStore, log); testObject.ControllerContext = new HttpControllerContext { Request = new HttpRequestMessage() }; // Act. var result = await testObject.DeleteAttemptAsync(123); // Assert. var badRequestResult = result as BadRequestErrorMessageResult; Assert.NotNull(badRequestResult); Assert.AreEqual("No token provided.", badRequestResult.Message); }