test_settings.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <gtest/gtest.h>
  2. #include "settings.hpp"
  3. #include "apikey.hpp"
  4. #include <cctype>
  5. using namespace svapi;
  6. TEST(Settings, DefaultsAndRoundTrip) {
  7. Settings s = Settings::fromJson(nlohmann::json::object());
  8. EXPECT_EQ(s.openaiApiBase, "https://api.openai.com");
  9. EXPECT_EQ(s.defaultEmbeddingModel, "text-embedding-3-small");
  10. EXPECT_EQ(s.sessionTtlMinutes, 720u);
  11. EXPECT_TRUE(s.webuiEnabled);
  12. EXPECT_EQ(s.defaultProject, "default");
  13. EXPECT_EQ(Settings::fromJson(s.toJson()).toJson(), s.toJson());
  14. }
  15. TEST(Settings, EmbeddingTimeoutDefaults) {
  16. Settings s = Settings::fromJson(nlohmann::json::object());
  17. EXPECT_EQ(s.embeddingConnectTimeoutSec, 10u);
  18. EXPECT_EQ(s.embeddingReadTimeoutSec, 60u);
  19. }
  20. TEST(Settings, EmbeddingTimeoutRoundTrip) {
  21. nlohmann::json j{{"embedding_connect_timeout_sec", 30}, {"embedding_read_timeout_sec", 120}};
  22. Settings s = Settings::fromJson(j);
  23. EXPECT_EQ(s.embeddingConnectTimeoutSec, 30u);
  24. EXPECT_EQ(s.embeddingReadTimeoutSec, 120u);
  25. // round-trip: toJson → fromJson preserves values
  26. Settings s2 = Settings::fromJson(s.toJson());
  27. EXPECT_EQ(s2.embeddingConnectTimeoutSec, 30u);
  28. EXPECT_EQ(s2.embeddingReadTimeoutSec, 120u);
  29. }
  30. TEST(Settings, CaptchaDefaults) {
  31. Settings s = Settings::fromJson(nlohmann::json::object());
  32. EXPECT_EQ(s.captchaProvider, "");
  33. EXPECT_EQ(s.captchaSecret, "");
  34. EXPECT_EQ(s.captchaVerifyUrl, "");
  35. }
  36. TEST(Settings, CaptchaProviderAndUrlRoundTrip) {
  37. nlohmann::json j{{"captcha_provider", "turnstile"},
  38. {"captcha_verify_url", "https://challenges.cloudflare.com/turnstile/v0/siteverify"}};
  39. Settings s = Settings::fromJson(j);
  40. EXPECT_EQ(s.captchaProvider, "turnstile");
  41. EXPECT_EQ(s.captchaVerifyUrl, "https://challenges.cloudflare.com/turnstile/v0/siteverify");
  42. Settings s2 = Settings::fromJson(s.toJson());
  43. EXPECT_EQ(s2.captchaProvider, "turnstile");
  44. EXPECT_EQ(s2.captchaVerifyUrl, "https://challenges.cloudflare.com/turnstile/v0/siteverify");
  45. }
  46. TEST(Settings, CaptchaSecretRoundTrip) {
  47. nlohmann::json j{{"captcha_secret", "mysecret"}};
  48. Settings s = Settings::fromJson(j);
  49. EXPECT_EQ(s.captchaSecret, "mysecret");
  50. Settings s2 = Settings::fromJson(s.toJson());
  51. EXPECT_EQ(s2.captchaSecret, "mysecret");
  52. }
  53. TEST(ApiKey, JsonRoundTripAndPublicMasks) {
  54. ApiKey k = ApiKey::fromJson(nlohmann::json::parse(
  55. R"({"key":"abcdef0123456789","label":"n8n","projects":["p1","p2"],"admin":false,"created_at":7})"));
  56. EXPECT_EQ(k.key, "abcdef0123456789");
  57. EXPECT_EQ(k.projects.size(), 2u);
  58. EXPECT_FALSE(k.admin);
  59. EXPECT_EQ(k.createdAt, 7u);
  60. EXPECT_EQ(ApiKey::fromJson(k.toJson()).toJson(), k.toJson());
  61. auto pub = k.toPublicJson();
  62. EXPECT_FALSE(pub.contains("key")); // secret not exposed
  63. EXPECT_EQ(pub["label"], "n8n");
  64. EXPECT_EQ(pub["projects"].size(), 2u);
  65. EXPECT_TRUE(pub.contains("key_prefix")); // short hint only
  66. }
  67. TEST(ApiKey, CanAccess) {
  68. ApiKey adminK; adminK.admin = true;
  69. EXPECT_TRUE(adminK.canAccess("anything"));
  70. ApiKey star; star.projects = {"*"};
  71. EXPECT_TRUE(star.canAccess("anything"));
  72. ApiKey scoped; scoped.projects = {"p1", "p2"};
  73. EXPECT_TRUE(scoped.canAccess("p1"));
  74. EXPECT_FALSE(scoped.canAccess("p3"));
  75. }
  76. TEST(ApiKey, GenerateIsHexAndUnique) {
  77. std::string a = generateApiKey(), b = generateApiKey();
  78. EXPECT_GE(a.size(), 32u);
  79. EXPECT_NE(a, b);
  80. for (char c : a) EXPECT_TRUE(std::isxdigit(static_cast<unsigned char>(c)));
  81. }
  82. // ---------------------------------------------------------------------------
  83. // 0.18.1: values that used to be compiled in. Each default is pinned, because
  84. // the point of making them settings is that an operator can move them - and a
  85. // default that drifts silently changes behaviour for every deployment that
  86. // never set it.
  87. // ---------------------------------------------------------------------------
  88. TEST(Settings, OperationalLimitDefaults) {
  89. Settings s = Settings::fromJson(nlohmann::json::object());
  90. // 2, not 0 and not the configured connect timeout. This is the one default
  91. // in this group that CHANGES behaviour relative to 0.18.0, deliberately:
  92. // the half-of-budget rule alone gave a 6 s embedding ask connect 3 s and
  93. // read 3 s, so a caller's six seconds bought a three-second read.
  94. EXPECT_EQ(s.embeddingConnectCapSec, 2u);
  95. // Unchanged from the constants they replace, so no deployment that never
  96. // sets them behaves differently.
  97. EXPECT_EQ(s.vectorFilterScanCap, 10000u);
  98. EXPECT_EQ(s.captchaConnectTimeoutSec, 5u);
  99. EXPECT_EQ(s.captchaReadTimeoutSec, 10u);
  100. }
  101. TEST(Settings, OperationalLimitsRoundTrip) {
  102. auto j = nlohmann::json::parse(R"({
  103. "embedding_connect_cap_sec": 1,
  104. "vector_filter_scan_cap": 50000,
  105. "captcha_connect_timeout_sec": 3,
  106. "captcha_read_timeout_sec": 7
  107. })");
  108. Settings s = Settings::fromJson(j);
  109. EXPECT_EQ(s.embeddingConnectCapSec, 1u);
  110. EXPECT_EQ(s.vectorFilterScanCap, 50000u);
  111. EXPECT_EQ(s.captchaConnectTimeoutSec, 3u);
  112. EXPECT_EQ(s.captchaReadTimeoutSec, 7u);
  113. // Through the document and back, because these are stored in the settings
  114. // collection and a field that parses but is not emitted is lost on the
  115. // next write - which would look like the operator's change being reverted.
  116. EXPECT_EQ(Settings::fromJson(s.toJson()).toJson(), s.toJson());
  117. Settings again = Settings::fromJson(s.toJson());
  118. EXPECT_EQ(again.embeddingConnectCapSec, 1u);
  119. EXPECT_EQ(again.vectorFilterScanCap, 50000u);
  120. }
  121. TEST(Settings, AZeroConnectCapIsUnsetRatherThanNoConnectTime) {
  122. // 0 means "no absolute cap, fall back to the half rule" - it must never
  123. // reach the HTTP client as a zero connect timeout, which to cpp-httplib
  124. // means NO timeout and is the unbounded call the whole split prevents.
  125. // resolveEmbedBudget enforces that; this pins that 0 survives the config
  126. // round trip as 0 rather than being coerced to the default.
  127. Settings s = Settings::fromJson(nlohmann::json::parse(R"({"embedding_connect_cap_sec":0})"));
  128. EXPECT_EQ(s.embeddingConnectCapSec, 0u);
  129. EXPECT_EQ(Settings::fromJson(s.toJson()).embeddingConnectCapSec, 0u);
  130. }