文件
csp/backend/tests/auth_service_test.cc
2026-02-23 20:02:46 +08:00

34 行
1015 B
C++

#include <catch2/catch_test_macros.hpp>
#include "csp/db/sqlite_db.h"
#include "csp/services/auth_service.h"
TEST_CASE("auth register/login/verify") {
auto db = csp::db::SqliteDb::OpenMemory();
csp::db::ApplyMigrations(db);
csp::services::AuthService auth(db);
const auto r = auth.Register("alice", "password123");
REQUIRE(r.user_id > 0);
REQUIRE(r.token.size() > 10);
const auto uid = auth.VerifyToken(r.token);
REQUIRE(uid.has_value());
REQUIRE(uid.value() == r.user_id);
const auto r2 = auth.Login("alice", "password123");
REQUIRE(r2.user_id == r.user_id);
REQUIRE(r2.token != r.token);
const auto verified = auth.VerifyCredentials("alice", "password123");
REQUIRE(verified.has_value());
REQUIRE(verified.value() == r.user_id);
const auto wrong_password = auth.VerifyCredentials("alice", "wrongpass");
REQUIRE_FALSE(wrong_password.has_value());
const auto wrong_user = auth.VerifyCredentials("missing_user", "password123");
REQUIRE_FALSE(wrong_user.has_value());
}