identity_repo_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // IdentityRepo 仓储集成测试(t_identity 登录身份,(type,identifier,appid) 唯一键·v1.2)。
  2. //
  3. // 依赖 test_helpers_test.go 的 setup/insertFixture;未设置 UNIFIED_TEST_DSN 时跳过。
  4. package repository
  5. import (
  6. "context"
  7. stderrors "errors"
  8. "testing"
  9. apperrors "yuansiwei/unified-backend/internal/infra/errors"
  10. identitymodel "yuansiwei/unified-backend/internal/identity/model"
  11. )
  12. // TestIdentityRepo_GetByTypeIdentifierAppID (type,identifier,appid) 唯一键命中 / 未命中(v1.2)。
  13. // 非微信类型 appid 恒 "",微信类型 appid 必填。
  14. func TestIdentityRepo_GetByTypeIdentifierAppID(t *testing.T) {
  15. gdb := setup(t)
  16. ctx := context.Background()
  17. fx := insertFixture(t, gdb, ctx)
  18. repo := NewIdentityRepo(gdb)
  19. t.Run("命中_phone", func(t *testing.T) {
  20. it, err := repo.GetByTypeIdentifierAppID(ctx, identitymodel.IdentityTypePhone, fx.phone, "")
  21. if err != nil {
  22. t.Fatalf("GetByTypeIdentifierAppID phone: %v", err)
  23. }
  24. if it.CanonicalUserID != fx.canonical {
  25. t.Errorf("返回 canonical_user_id = %q,期望 %q", it.CanonicalUserID, fx.canonical)
  26. }
  27. if !it.Verified {
  28. t.Errorf("phone 身份应 verified=true")
  29. }
  30. })
  31. t.Run("命中_email", func(t *testing.T) {
  32. it, err := repo.GetByTypeIdentifierAppID(ctx, identitymodel.IdentityTypeEmail, fx.email, "")
  33. if err != nil {
  34. t.Fatalf("GetByTypeIdentifierAppID email: %v", err)
  35. }
  36. if it.CanonicalUserID != fx.canonical {
  37. t.Errorf("返回 canonical_user_id = %q,期望 %q", it.CanonicalUserID, fx.canonical)
  38. }
  39. if it.Verified {
  40. t.Errorf("email 身份应 verified=false")
  41. }
  42. })
  43. t.Run("未命中_返回ErrNotFound", func(t *testing.T) {
  44. _, err := repo.GetByTypeIdentifierAppID(ctx, identitymodel.IdentityTypeWechat, "unknown_openid", "appid-x")
  45. if !stderrors.Is(err, apperrors.ErrNotFound) {
  46. t.Fatalf("期望 ErrNotFound,实际 %v", err)
  47. }
  48. })
  49. }