112 lines
2.9 KiB
C++
112 lines
2.9 KiB
C++
#define DROGON_TEST_MAIN
|
|
#include <drogon/HttpClient.h>
|
|
#include <drogon/drogon.h>
|
|
#include <drogon/drogon_test.h>
|
|
|
|
#include "common/GeoJson.h"
|
|
#include "common/Validation.h"
|
|
|
|
#include <future>
|
|
|
|
using namespace drogon;
|
|
|
|
namespace
|
|
{
|
|
std::pair<ReqResult, HttpResponsePtr> sendRequest(const HttpRequestPtr &request)
|
|
{
|
|
auto client = HttpClient::newHttpClient("http://127.0.0.1:15555",
|
|
app().getLoop());
|
|
auto promise =
|
|
std::make_shared<std::promise<std::pair<ReqResult, HttpResponsePtr>>>();
|
|
auto future = promise->get_future();
|
|
|
|
client->sendRequest(
|
|
request,
|
|
[promise](ReqResult result, const HttpResponsePtr &response) {
|
|
promise->set_value({result, response});
|
|
});
|
|
|
|
return future.get();
|
|
}
|
|
} // namespace
|
|
|
|
DROGON_TEST(HealthEndpoint)
|
|
{
|
|
auto request = HttpRequest::newHttpRequest();
|
|
request->setMethod(Get);
|
|
request->setPath("/api/v1/health");
|
|
|
|
auto [result, response] = sendRequest(request);
|
|
|
|
REQUIRE(result == ReqResult::Ok);
|
|
REQUIRE(response != nullptr);
|
|
CHECK(response->getStatusCode() == k200OK);
|
|
REQUIRE(response->getJsonObject() != nullptr);
|
|
CHECK((*response->getJsonObject())["status"].asString() == "ok");
|
|
CHECK((*response->getJsonObject())["service"].asString() ==
|
|
"visione-backend");
|
|
}
|
|
|
|
DROGON_TEST(ValidationRejectsMalformedIdentity)
|
|
{
|
|
CHECK(visione::validation::isValidEmail("[email protected]"));
|
|
CHECK(!visione::validation::isValidEmail("not-an-email"));
|
|
CHECK(visione::validation::isValidFiscalCode("RSSMRA80A01H501U"));
|
|
CHECK(!visione::validation::isValidFiscalCode("short"));
|
|
}
|
|
|
|
DROGON_TEST(GeoJsonMatchesPointInsidePolygon)
|
|
{
|
|
Json::Value polygon;
|
|
polygon["type"] = "Polygon";
|
|
Json::Value ring(Json::arrayValue);
|
|
|
|
Json::Value a(Json::arrayValue);
|
|
a.append(12.0);
|
|
a.append(41.0);
|
|
Json::Value b(Json::arrayValue);
|
|
b.append(13.0);
|
|
b.append(41.0);
|
|
Json::Value c(Json::arrayValue);
|
|
c.append(13.0);
|
|
c.append(42.0);
|
|
Json::Value d(Json::arrayValue);
|
|
d.append(12.0);
|
|
d.append(42.0);
|
|
|
|
ring.append(a);
|
|
ring.append(b);
|
|
ring.append(c);
|
|
ring.append(d);
|
|
ring.append(a);
|
|
polygon["coordinates"].append(ring);
|
|
|
|
CHECK(visione::geojson::pointInPolygon(41.5, 12.5, polygon));
|
|
CHECK(!visione::geojson::pointInPolygon(43.0, 12.5, polygon));
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
std::promise<void> startedPromise;
|
|
auto startedFuture = startedPromise.get_future();
|
|
|
|
app().addListener("127.0.0.1", 15555);
|
|
|
|
std::thread serverThread([&startedPromise]() {
|
|
app().getLoop()->queueInLoop([&startedPromise]() {
|
|
startedPromise.set_value();
|
|
});
|
|
app().run();
|
|
});
|
|
|
|
startedFuture.get();
|
|
const int status = test::run(argc, argv);
|
|
|
|
app().getLoop()->queueInLoop([]() {
|
|
app().quit();
|
|
});
|
|
serverThread.join();
|
|
|
|
return status;
|
|
}
|