| 12345678910111213141516171819202122232425 |
- #include <gtest/gtest.h>
- #include "captcha.hpp"
- #include "settings.hpp"
- #include <httplib.h>
- #include <thread>
- #include <chrono>
- using namespace svapi;
- TEST(Captcha, VerifiesAgainstProvider) {
- httplib::Server mock;
- mock.Post("/verify", [](const httplib::Request& req, httplib::Response& res){
- bool ok = req.body.find("good") != std::string::npos;
- res.set_content(ok ? R"({"success":true})" : R"({"success":false})", "application/json");
- });
- int port = mock.bind_to_any_port("127.0.0.1");
- std::thread t([&]{ mock.listen_after_bind(); });
- while (!mock.is_running()) std::this_thread::sleep_for(std::chrono::milliseconds(5));
- Settings s; s.captchaProvider = "turnstile";
- s.captchaVerifyUrl = "http://127.0.0.1:" + std::to_string(port) + "/verify";
- s.captchaSecret = "sek";
- EXPECT_TRUE(verifyCaptcha(s, "good-token", "1.2.3.4"));
- EXPECT_FALSE(verifyCaptcha(s, "bad-token", "1.2.3.4"));
- EXPECT_FALSE(verifyCaptcha(s, "", "1.2.3.4"));
- Settings off; EXPECT_FALSE(verifyCaptcha(off, "good", ""));
- mock.stop(); t.join();
- }
|