You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/tests
veypi b0322047cd feat: Restrict user APIs to admins and add public user search
- Add /api/auth/users endpoint for authenticated users to search other users
    - Only return public info (id, username, nickname, avatar) in search results
    - Change /api/user routes to require user:admin permission instead of user:read
    - Update auth tests to use /api/auth/me for self updates
    - Add tests for new user search endpoint
1 week ago
..
README.md test: Refactor test infrastructure to use in-memory SQLite database 1 week ago
auth_test.go feat: Restrict user APIs to admins and add public user search 1 week ago
helpers_test.go refactor(test): restructure integration tests for auth and permissions 1 week ago
main_test.go test: Refactor test infrastructure to use in-memory SQLite database 1 week ago
none_auth_test.go refactor(test): restructure integration tests for auth and permissions 1 week ago
org_load_middleware_test.go refactor(test): restructure integration tests for auth and permissions 1 week ago
org_permission_test.go refactor(test): restructure integration tests for auth and permissions 1 week ago
resource_perm_test.go feat: Restrict user APIs to admins and add public user search 1 week ago
search_users_test.go feat: Restrict user APIs to admins and add public user search 1 week ago

README.md

VBase Integration Tests

本目录包含 VBase 后端 API 的集成测试。这些测试验证 API 端点的功能,包括身份验证、权限检查和资源管理。

运行测试

在项目根目录下运行以下命令以执行所有测试:

go test -v ./tests/...

或者运行单个测试文件:

go test -v ./tests/auth_test.go

测试结构

测试设计为在隔离环境中运行,使用临时 SQLite 数据库和内存 Redis 模拟。

1. 全局设置 (main_test.go)

  • TestMain: 测试套件的入口点。
    • 初始化 Vigo 应用程序。
    • 设置内存 SQLite 数据库 (:memory:)。
    • 模拟 Redis 客户端。
    • 运行所有测试。
    • 执行后无需清理资源。

2. 用户设置 (helpers_test.go)

  • ensureUsers: 一个辅助函数,由需要身份验证的测试调用。
    • 检查全局 Token (AdminToken, User1Token, User2Token) 是否已设置。
    • 如果未设置,它会注册并登录三个标准测试用户:
      • admin_test (管理员)
      • user1_test (普通用户)
      • user2_test (普通用户)
    • 将它们的 ID 和 Token 存储在全局变量中,供跨测试文件使用。

3. 测试场景

  • none_auth_test.go: 检查公开端点,并验证受保护端点拒绝未认证请求。
  • auth_test.go: 测试用户生命周期事件(注册 -> 登录 -> 获取/更新个人资料 -> 登出)。
  • resource_perm_test.go: 验证用户除非获得授权(例如管理员),否则无法修改其他用户的数据。
  • org_permission_test.go: 测试组织基于角色的访问控制 (RBAC)(创建组织 -> 添加成员 -> 验证角色)。
  • org_load_middleware_test.go: 专门测试 LoadOrg 中间件,该中间件强制执行组织特定端点的成员资格检查。

开发注意事项

  • 全局状态: 我们在 main_test.go 中使用全局变量(如 AdminToken, User1ID)在测试函数之间共享状态。这模拟了测试运行期间的持久环境。
  • 幂等性: ensureUsers 辅助函数设计为幂等的。它会优雅地处理"用户已存在"错误,允许测试在不手动清理数据库的情况下重新运行(尽管 TestMain 通常会重置数据库)。
  • 辅助函数: 使用 doRequest, assertStatus, 和 decodeResponse (在 helpers_test.go 中) 保持测试代码整洁一致。
  • 数据库隔离: 测试使用内存数据库 (:memory:) 以避免干扰开发数据库,并提供更快的执行速度。

添加新测试

  1. 在此目录下创建一个新的 _test.go 文件。
  2. 使用 package tests
  3. 如果需要已认证的用户,请在测试函数开头调用 ensureUsers(t)
  4. 使用 doRequest 与 API 交互。
func TestNewFeature(t *testing.T) {
    ensureUsers(t) // 确保 User1Token 可用

    t.Run("Scenario Description", func(t *testing.T) {
        resp := doRequest(t, "GET", "/api/new-feature", nil, User1Token)
        assertStatus(t, resp, 200)
    })
}