VerifyRequest(int userId, int attemptId, int timeSpent) { var attempt = await Context.QuizAttempts.FindAsync(attemptId); if (attempt == null || attempt.UserId != userId) { var response = new UpdateQuizAttemptResponse { Result = UpdateQuizAttemptStatusResult.NotAuthorized }; return(attempt, null, response); } var quiz = await Context.Quizes.FindAsync(attempt.QuizId); // no need to check for null, because otherwise the attempt cannot be there. if (quiz.TimeConstraint && timeSpent >= quiz.TimeLimitInSeconds) { await FinishQuizAsync(attempt, quiz.PassScore, timeSpent); var response = new UpdateQuizAttemptResponse { Result = UpdateQuizAttemptStatusResult.TimeUp, Attempt = attempt }; return(attempt, quiz, response); } if (quiz.AvailableTo != null && quiz.AvailableTo < DateTime.Now) { await FinishQuizAsync(attempt, quiz.PassScore, timeSpent); var response = new UpdateQuizAttemptResponse { Result = UpdateQuizAttemptStatusResult.DateError, Attempt = attempt }; return(attempt, quiz, response); } if (attempt.Status != QuizAttemptStatus.Incomplete) { var response = new UpdateQuizAttemptResponse { Result = UpdateQuizAttemptStatusResult.StatusError }; return(attempt, quiz, response); } return(attempt, quiz, null); }
private async Task <JsonResult> ExecutePost(int userId, int attemptId, UpdateQuizAttemptStatus data, UpdateQuizAttemptResponse response) { var quizAttemptsManager = new Mock <IQuizAttemptManager>(); quizAttemptsManager.Setup(c => c.UpdateStatus(userId, attemptId, It.IsAny <UpdateQuizAttemptStatus>())) .Returns <int, int, UpdateQuizAttemptStatus>((u, q, s) => Task.FromResult(response)); var controller = new QuizAttemptsController(quizAttemptsManager.Object); controller.Token = new AuthToken { Token = "token", UserId = userId, ValidUntil = DateTime.Now.AddDays(1), IsVerified = true }; var result = await controller.Post(attemptId, data); return(result); }