public async Task CreateAttemptAsync_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); var attempt = new GoalAttemptData { GoalId = 123 }; testObject.ControllerContext = new HttpControllerContext { Request = new HttpRequestMessage() }; // Act. var result = await testObject.CreateAttemptAsync(attempt); // Assert. var badRequestResult = result as BadRequestErrorMessageResult; Assert.NotNull(badRequestResult); Assert.AreEqual("No token provided.", badRequestResult.Message); }
public async Task CreateAttemptAsync_GivenUnknownGoalId_ShouldReturnNotFound() { // 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()); userStore.GetUserAsync(Arg.Any <Token>()).Returns(new User()); var attempt = new GoalAttemptData { GoalId = 123 }; // Act. var result = await testObject.CreateAttemptAsync(attempt); // Assert. var notFoundResult = result as NotFoundResult; Assert.NotNull(notFoundResult); }
public async Task CreateAttemptAsync_GivenValidAttempt_ShouldAddToDb() { // 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(123).Returns(goal); var attempt = new GoalAttemptData { GoalId = 123 }; // Act. var result = await testObject.CreateAttemptAsync(attempt); // Assert. var statusResult = result as StatusCodeResult; Assert.NotNull(statusResult); Assert.AreEqual(HttpStatusCode.Created, statusResult.StatusCode); await attemptStore.Received(1).CreateAttemptAsync(123); }