280db0b862
CI/CD Pipeline / Check if frontend-only change (push) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (push) Successful in 3m19s
CI/CD Pipeline / Frontend Lint (push) Successful in 4m24s
CI/CD Pipeline / Validate Code Quality And Tests (push) Successful in 4m48s
CI/CD Pipeline / Unit Tests (push) Successful in 5m28s
CI/CD Pipeline / Integration Tests (push) Successful in 1m34s
CI Build & Deploy Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Successful in 53s
CI Build & Deploy Pipeline / Build Staging API Image (push) Successful in 6m43s
CI Build & Deploy Pipeline / Build Staging Web Image (push) Successful in 1m48s
CI Build & Deploy Pipeline / Build Staging Worker Image (push) Successful in 8m36s
CI Build & Deploy Pipeline / Build Production API Image (push) Has been skipped
CI Build & Deploy Pipeline / Build Production Web Image (push) Has been skipped
CI Build & Deploy Pipeline / Build Production Worker Image (push) Has been skipped
CI Build & Deploy Pipeline / Deploy Production (push) Has been skipped
CI Build & Deploy Pipeline / Production Browser E2E (push) Has been skipped
CI Build & Deploy Pipeline / Staging E2E Tests (push) Failing after 1m14s
CI Build & Deploy Pipeline / Staging API Integration Tests (push) Successful in 3m3s
Co-authored-by: xiaoxia <dev@xiaoxiajianji.com> Co-committed-by: xiaoxia <dev@xiaoxiajianji.com>
120 lines
3.0 KiB
TypeScript
120 lines
3.0 KiB
TypeScript
/**
|
|
* auth API 纯函数测试
|
|
* - normalizeUser
|
|
*/
|
|
import { describe, it, expect } from "vitest"
|
|
import { normalizeUser } from "@/api/auth"
|
|
|
|
describe("normalizeUser", () => {
|
|
it("应该正确映射标准用户数据", () => {
|
|
const input = {
|
|
id: "123",
|
|
user_id: "123",
|
|
email: "test@example.com",
|
|
username: "testuser",
|
|
display_name: "Test User",
|
|
is_email_verified: true,
|
|
email_verified: true,
|
|
created_at: "2024-01-01T00:00:00Z",
|
|
}
|
|
|
|
const result = normalizeUser(input)
|
|
|
|
expect(result.id).toBe("123")
|
|
expect(result.user_id).toBe("123")
|
|
expect(result.email).toBe("test@example.com")
|
|
expect(result.username).toBe("testuser")
|
|
expect(result.display_name).toBe("Test User")
|
|
expect(result.is_email_verified).toBe(true)
|
|
expect(result.email_verified).toBe(true)
|
|
expect(result.created_at).toBe("2024-01-01T00:00:00Z")
|
|
})
|
|
|
|
it("id 优先于 user_id", () => {
|
|
const input = {
|
|
id: "id-from-id",
|
|
user_id: "id-from-user-id",
|
|
email: "a@b.com",
|
|
username: "user",
|
|
}
|
|
|
|
const result = normalizeUser(input)
|
|
expect(result.id).toBe("id-from-id")
|
|
expect(result.user_id).toBe("id-from-id")
|
|
})
|
|
|
|
it("没有 id 时使用 user_id", () => {
|
|
const input = {
|
|
user_id: "fallback-user-id",
|
|
email: "a@b.com",
|
|
username: "user",
|
|
}
|
|
|
|
const result = normalizeUser(input as any)
|
|
expect(result.id).toBe("fallback-user-id")
|
|
expect(result.user_id).toBe("fallback-user-id")
|
|
})
|
|
|
|
it("id 和 user_id 都没有时返回空字符串", () => {
|
|
const input = {
|
|
email: "a@b.com",
|
|
username: "user",
|
|
}
|
|
|
|
const result = normalizeUser(input as any)
|
|
expect(result.id).toBe("")
|
|
expect(result.user_id).toBe("")
|
|
})
|
|
|
|
it("is_email_verified 优先于 email_verified", () => {
|
|
const input = {
|
|
id: "1",
|
|
email: "a@b.com",
|
|
username: "user",
|
|
is_email_verified: true,
|
|
email_verified: false,
|
|
}
|
|
|
|
const result = normalizeUser(input)
|
|
expect(result.is_email_verified).toBe(true)
|
|
expect(result.email_verified).toBe(true)
|
|
})
|
|
|
|
it("没有 is_email_verified 时使用 email_verified", () => {
|
|
const input = {
|
|
id: "1",
|
|
email: "a@b.com",
|
|
username: "user",
|
|
email_verified: true,
|
|
}
|
|
|
|
const result = normalizeUser(input as any)
|
|
expect(result.is_email_verified).toBe(true)
|
|
expect(result.email_verified).toBe(true)
|
|
})
|
|
|
|
it("两个都没有时默认为 false", () => {
|
|
const input = {
|
|
id: "1",
|
|
email: "a@b.com",
|
|
username: "user",
|
|
}
|
|
|
|
const result = normalizeUser(input as any)
|
|
expect(result.is_email_verified).toBe(false)
|
|
expect(result.email_verified).toBe(false)
|
|
})
|
|
|
|
it("缺失可选字段时返回 undefined", () => {
|
|
const input = {
|
|
id: "1",
|
|
email: "a@b.com",
|
|
username: "user",
|
|
}
|
|
|
|
const result = normalizeUser(input as any)
|
|
expect(result.display_name).toBeUndefined()
|
|
expect(result.created_at).toBeUndefined()
|
|
})
|
|
})
|