feat: rebuild CSP practice workflow, UX and automation
这个提交包含在:
@@ -0,0 +1,80 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "csp/app_state.h"
|
||||
#include "csp/controllers/submission_controller.h"
|
||||
#include "csp/services/auth_service.h"
|
||||
#include "csp/services/problem_service.h"
|
||||
|
||||
#include <drogon/HttpRequest.h>
|
||||
|
||||
#include <future>
|
||||
|
||||
namespace {
|
||||
|
||||
drogon::HttpResponsePtr CallRun(csp::controllers::SubmissionController& ctl,
|
||||
const std::string& code,
|
||||
const std::string& input) {
|
||||
Json::Value body;
|
||||
body["code"] = code;
|
||||
body["input"] = input;
|
||||
auto req = drogon::HttpRequest::newHttpJsonRequest(body);
|
||||
req->setMethod(drogon::Post);
|
||||
|
||||
std::promise<drogon::HttpResponsePtr> p;
|
||||
ctl.runCpp(req, [&p](const drogon::HttpResponsePtr& resp) { p.set_value(resp); });
|
||||
return p.get_future().get();
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr CallSubmit(csp::controllers::SubmissionController& ctl,
|
||||
int64_t problem_id,
|
||||
const std::string& token,
|
||||
const std::string& code) {
|
||||
Json::Value body;
|
||||
body["language"] = "cpp";
|
||||
body["code"] = code;
|
||||
auto req = drogon::HttpRequest::newHttpJsonRequest(body);
|
||||
req->setMethod(drogon::Post);
|
||||
req->addHeader("Authorization", "Bearer " + token);
|
||||
|
||||
std::promise<drogon::HttpResponsePtr> p;
|
||||
ctl.submitProblem(req,
|
||||
[&p](const drogon::HttpResponsePtr& resp) {
|
||||
p.set_value(resp);
|
||||
},
|
||||
problem_id);
|
||||
return p.get_future().get();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("submission controller run and submit") {
|
||||
csp::AppState::Instance().Init(":memory:");
|
||||
|
||||
csp::services::AuthService auth(csp::AppState::Instance().db());
|
||||
const auto user = auth.Register("submission_http_user", "password123");
|
||||
|
||||
csp::services::ProblemService problems(csp::AppState::Instance().db());
|
||||
const auto list = problems.List(csp::services::ProblemQuery{});
|
||||
REQUIRE_FALSE(list.items.empty());
|
||||
|
||||
csp::controllers::SubmissionController ctl;
|
||||
|
||||
auto run = CallRun(ctl,
|
||||
"#include <iostream>\nint main(){std::cout<<\"ok\\n\";}",
|
||||
"");
|
||||
REQUIRE(run->statusCode() == drogon::k200OK);
|
||||
|
||||
auto submit = CallSubmit(
|
||||
ctl,
|
||||
list.items.front().id,
|
||||
user.token,
|
||||
R"CPP(#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int main(){ long long a,b; if(!(cin>>a>>b)) return 0; cout << (a+b) << "\n"; }
|
||||
)CPP");
|
||||
|
||||
REQUIRE(submit->statusCode() == drogon::k200OK);
|
||||
auto submit_json = submit->jsonObject();
|
||||
REQUIRE(submit_json != nullptr);
|
||||
REQUIRE((*submit_json)["data"]["id"].asInt64() > 0);
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户