test_captcha.cpp 1.1 KB

12345678910111213141516171819202122232425
  1. #include <gtest/gtest.h>
  2. #include "captcha.hpp"
  3. #include "settings.hpp"
  4. #include <httplib.h>
  5. #include <thread>
  6. #include <chrono>
  7. using namespace svapi;
  8. TEST(Captcha, VerifiesAgainstProvider) {
  9. httplib::Server mock;
  10. mock.Post("/verify", [](const httplib::Request& req, httplib::Response& res){
  11. bool ok = req.body.find("good") != std::string::npos;
  12. res.set_content(ok ? R"({"success":true})" : R"({"success":false})", "application/json");
  13. });
  14. int port = mock.bind_to_any_port("127.0.0.1");
  15. std::thread t([&]{ mock.listen_after_bind(); });
  16. while (!mock.is_running()) std::this_thread::sleep_for(std::chrono::milliseconds(5));
  17. Settings s; s.captchaProvider = "turnstile";
  18. s.captchaVerifyUrl = "http://127.0.0.1:" + std::to_string(port) + "/verify";
  19. s.captchaSecret = "sek";
  20. EXPECT_TRUE(verifyCaptcha(s, "good-token", "1.2.3.4"));
  21. EXPECT_FALSE(verifyCaptcha(s, "bad-token", "1.2.3.4"));
  22. EXPECT_FALSE(verifyCaptcha(s, "", "1.2.3.4"));
  23. Settings off; EXPECT_FALSE(verifyCaptcha(off, "good", ""));
  24. mock.stop(); t.join();
  25. }