test_principal.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <gtest/gtest.h>
  2. #include "apikey.hpp"
  3. #include "principal.hpp"
  4. using namespace svapi;
  5. namespace {
  6. ApiKey plainKey(std::vector<std::string> projects, bool admin = false) {
  7. ApiKey k;
  8. k.id = "key-1";
  9. k.projects = std::move(projects);
  10. k.admin = admin;
  11. return k;
  12. }
  13. ApiKey scopedKey(std::vector<std::string> projects, KeyScope scope) {
  14. ApiKey k = plainKey(std::move(projects));
  15. k.scope = std::move(scope);
  16. return k;
  17. }
  18. KeyScope readOnly(const std::string& collection) {
  19. KeyScope s;
  20. KeyScopeRule r;
  21. r.collection = collection;
  22. r.ops = {KeyOp::Read, KeyOp::List};
  23. s.rules.push_back(r);
  24. return s;
  25. }
  26. } // namespace
  27. TEST(Principal, MirrorsTheKeyItWasBuiltFrom) {
  28. const ApiKey k = plainKey({"acme"});
  29. const Principal p = Principal::of(k);
  30. EXPECT_EQ(p.id, "key-1");
  31. EXPECT_FALSE(p.admin);
  32. EXPECT_EQ(p.scope, nullptr) << "an unscoped key must present as unscoped";
  33. EXPECT_TRUE(p.canAccess("acme"));
  34. EXPECT_FALSE(p.canAccess("other"));
  35. }
  36. TEST(Principal, AdminAndWildcardReachEveryProject) {
  37. EXPECT_TRUE(Principal::of(plainKey({}, /*admin*/ true)).canAccess("anything"));
  38. EXPECT_TRUE(Principal::of(plainKey({"*"})).canAccess("anything"));
  39. }
  40. TEST(Principal, CarriesTheScopeByReference) {
  41. const ApiKey k = scopedKey({"acme"}, readOnly("docs"));
  42. const Principal p = Principal::of(k);
  43. ASSERT_NE(p.scope, nullptr);
  44. EXPECT_NE(p.scope->findRule("docs", KeyOp::Read), nullptr);
  45. EXPECT_EQ(p.scope->findRule("docs", KeyOp::Delete), nullptr);
  46. }
  47. // A function is a principal with no key behind it: no secret, no origin, no
  48. // captcha - just a project and a scope. Constructing one directly is what lets
  49. // the runner reuse the same authorization code as an HTTP request.
  50. TEST(Principal, CanBeBuiltWithoutAnApiKey) {
  51. const std::vector<std::string> projects{"acme"};
  52. const KeyScope scope = readOnly("kb_chunks");
  53. const Principal p{"fn:kb-search", &projects, false, &scope};
  54. EXPECT_TRUE(p.canAccess("acme"));
  55. EXPECT_FALSE(p.canAccess("other"));
  56. ASSERT_NE(p.scope, nullptr);
  57. EXPECT_NE(p.scope->findRule("kb_chunks", KeyOp::List), nullptr);
  58. }
  59. TEST(Principal, AccessChecksAgreeWithTheKeysOwnAnswer) {
  60. // The two must never diverge: Principal is meant to be the single notion of
  61. // "who is asking", not a second one that drifts.
  62. for (const ApiKey& k : {plainKey({"acme"}), plainKey({"*"}), plainKey({}, true),
  63. scopedKey({"acme"}, readOnly("docs"))}) {
  64. const Principal p = Principal::of(k);
  65. for (const char* project : {"acme", "other", "default"})
  66. EXPECT_EQ(p.canAccess(project), k.canAccess(project))
  67. << "disagreement on project " << project;
  68. }
  69. }