public RunTestsResponse RunTests(RunTestsRequest request) { RunTestsResponse response = new RunTestsResponse(); AuthToken authToken = null; try { Common.Helpers.ValidationHelper.ValidateRequiredField(request.AuthToken, "Auth Token"); Common.Helpers.ValidationHelper.ValidateRequiredField(request.AntiForgeryToken, "Auth Token"); Common.Helpers.ValidationHelper.ValidateRequiredField(request.Output, "Output"); Common.Helpers.ValidationHelper.ValidateStringLength(request.Output, "Output", Constants.MaxBlobLength); if (string.IsNullOrEmpty(request.AuthToken) || !UserController.ValidateSession(request.AuthToken, out authToken)) { throw new AuthenticationException("Authentication failed."); } UserController.ValidateAntiForgeryToken(request.AntiForgeryToken, authToken); DbContext context = DataController.CreateDbContext(); E::Question question = context.Questions .Where(q => q.ID == request.QuestionID) .FirstOrDefault(); if (question.LastTestRunOn.HasValue && DateTime.UtcNow - question.LastTestRunOn.Value < TimeSpan.FromSeconds(5)) { throw new Common.Exceptions.ValidationException("Please wait at least 5 seconds between test runs."); } question.LastTestRunOn = DateTime.UtcNow; if (question.QuestionTypeID == (short)QuestionType.Standard) { var testsJson = DataController.DownloadBlob(question.Tests.Value.ToString()); var tests = TestsController.FromJson(testsJson).OrderBy(t => t.ID); int numberOfTests = Settings.Default.TestsExecutedPerQuestion; var randomTests = tests.Shuffle(0).Take(numberOfTests).ToList(); var parsedOutput = request.Output.Lines(); if (randomTests.Count > parsedOutput.Length) { throw new Common.Exceptions.ValidationException(string.Format("Invalid output. Expected {0} lines, found {1}.", randomTests.Count, parsedOutput.Length)); } else { response.Tests = (from pair in randomTests.OrderBy(t => t.Name).Zip(parsedOutput, Tuple.Create) select new RunTestsResponseItem { TestName = pair.Item1.Name, TestID = pair.Item1.ID, Success = pair.Item1.ExpectedOutput.EqualsLenient(pair.Item2), Input = pair.Item1.Input.Truncate(100), ExpectedOutput = pair.Item1.ExpectedOutput.Truncate(100), Output = pair.Item2.Truncate(100) }) .ToArray(); } } else { response.Tests = CodedQuestionController.RunTests(question.CodedTestID.Value, request.Output, 0) .Select(test => new RunTestsResponseItem { TestName = test.TestName, Success = test.Success, Input = test.Input.Truncate(100), ExpectedOutput = test.ExpectedOutput.Truncate(100), Output = test.Output.Truncate(100) }) .OrderBy(test => test.TestName) .ToArray(); } } catch (AuthenticationException ex) { throw new WebFaultException <string>(ex.Message, System.Net.HttpStatusCode.BadRequest); } catch (Common.Exceptions.ValidationException ex) { throw new WebFaultException <string>(ex.Message, System.Net.HttpStatusCode.BadRequest); } catch (Exception ex) { ExceptionHelper.Log(ex, authToken == null ? null : authToken.Username); throw new WebFaultException <string>("An unknown error has occurred.", System.Net.HttpStatusCode.InternalServerError); } return(response); }
public DownloadInputResponse DownloadInput(DownloadInputRequest request) { DownloadInputResponse response = new DownloadInputResponse(); AuthToken authToken = null; try { Common.Helpers.ValidationHelper.ValidateRequiredField(request.AuthToken, "Auth Token"); if (string.IsNullOrEmpty(request.AuthToken) || !UserController.ValidateSession(request.AuthToken, out authToken)) { throw new AuthenticationException("Authentication failed."); } DbContext context = DataController.CreateDbContext(); var question = context.Questions .Where(q => q.ID == request.QuestionID) .Select(q => new { QuestionTypeID = q.QuestionTypeID, CodedTestID = q.CodedTestID, TestsID = q.Tests }) .FirstOrDefault(); if (question.QuestionTypeID == (short)QuestionType.Standard) { var testsJson = DataController.DownloadBlob(question.TestsID.Value.ToString()); var tests = TestsController.FromJson(testsJson).OrderBy(t => t.ID); int numberOfTests = Settings.Default.TestsExecutedPerQuestion; var randomTests = tests.Shuffle(0).Take(numberOfTests).ToList(); var selectedTests = from test in randomTests orderby test.Name select test.Input; response.Input = string.Join("\n", selectedTests); } else { response.Input = CodedQuestionController.GetInput(question.CodedTestID.Value, 0); } } catch (AuthenticationException ex) { throw new WebFaultException <string>(ex.Message, System.Net.HttpStatusCode.BadRequest); } catch (Common.Exceptions.ValidationException ex) { throw new WebFaultException <string>(ex.Message, System.Net.HttpStatusCode.BadRequest); } catch (Exception ex) { ExceptionHelper.Log(ex, authToken == null ? null : authToken.Username); throw new WebFaultException <string>("An unknown error has occurred.", System.Net.HttpStatusCode.InternalServerError); } return(response); }