111 lines
3.1 KiB
Go
111 lines
3.1 KiB
Go
|
|
package middleware
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"errors"
|
|||
|
|
"net/http"
|
|||
|
|
"net/http/httptest"
|
|||
|
|
"testing"
|
|||
|
|
|
|||
|
|
"with_you/internal/model"
|
|||
|
|
"with_you/internal/service"
|
|||
|
|
|
|||
|
|
"github.com/gin-gonic/gin"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// stubUserService 通过嵌入 service.UserService 接口(nil)仅覆盖 GetUserByID。
|
|||
|
|
// 未覆盖方法若被调用会 panic,可在测试中暴露非预期的依赖调用。
|
|||
|
|
type stubUserService struct {
|
|||
|
|
service.UserService // nil 嵌入
|
|||
|
|
|
|||
|
|
user *model.User
|
|||
|
|
err error
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *stubUserService) GetUserByID(ctx context.Context, id string) (*model.User, error) {
|
|||
|
|
if s.err != nil {
|
|||
|
|
return nil, s.err
|
|||
|
|
}
|
|||
|
|
return s.user, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// runMiddleware 构造一个 gin 引擎,挂载 RequireVerified + 一个标记 handler,
|
|||
|
|
// 发起请求并返回 (status, body, nextCalled)。
|
|||
|
|
func runMiddleware(t *testing.T, svc service.UserService, setUserID bool, userID any) (int, bool) {
|
|||
|
|
t.Helper()
|
|||
|
|
gin.SetMode(gin.TestMode)
|
|||
|
|
|
|||
|
|
nextCalled := false
|
|||
|
|
r := gin.New()
|
|||
|
|
r.Use(func(c *gin.Context) {
|
|||
|
|
if setUserID {
|
|||
|
|
c.Set("user_id", userID)
|
|||
|
|
}
|
|||
|
|
c.Next()
|
|||
|
|
})
|
|||
|
|
r.GET("/test", RequireVerified(svc), func(c *gin.Context) {
|
|||
|
|
nextCalled = true
|
|||
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
w := httptest.NewRecorder()
|
|||
|
|
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
|||
|
|
r.ServeHTTP(w, req)
|
|||
|
|
|
|||
|
|
return w.Code, nextCalled
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// TestRequireVerified_Approved 已认证用户 → 放行(200,next 被调用)。
|
|||
|
|
func TestRequireVerified_Approved(t *testing.T) {
|
|||
|
|
svc := &stubUserService{
|
|||
|
|
user: &model.User{ID: "u1", VerificationStatus: model.VerificationStatusApproved},
|
|||
|
|
}
|
|||
|
|
code, next := runMiddleware(t, svc, true, "u1")
|
|||
|
|
if code != http.StatusOK {
|
|||
|
|
t.Errorf("status = %d, want %d", code, http.StatusOK)
|
|||
|
|
}
|
|||
|
|
if !next {
|
|||
|
|
t.Error("next handler should be called for verified user")
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// TestRequireVerified_NotApproved 未通过认证用户 → 403 VERIFICATION_REQUIRED。
|
|||
|
|
func TestRequireVerified_NotApproved(t *testing.T) {
|
|||
|
|
svc := &stubUserService{
|
|||
|
|
user: &model.User{ID: "u1", VerificationStatus: model.VerificationStatusPending},
|
|||
|
|
}
|
|||
|
|
code, next := runMiddleware(t, svc, true, "u1")
|
|||
|
|
if code != http.StatusForbidden {
|
|||
|
|
t.Errorf("status = %d, want %d", code, http.StatusForbidden)
|
|||
|
|
}
|
|||
|
|
if next {
|
|||
|
|
t.Error("next handler should NOT be called for unverified user")
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// TestRequireVerified_UserLookupError 查询用户失败 → 500。
|
|||
|
|
func TestRequireVerified_UserLookupError(t *testing.T) {
|
|||
|
|
svc := &stubUserService{err: errors.New("db down")}
|
|||
|
|
code, next := runMiddleware(t, svc, true, "u1")
|
|||
|
|
if code != http.StatusInternalServerError {
|
|||
|
|
t.Errorf("status = %d, want %d", code, http.StatusInternalServerError)
|
|||
|
|
}
|
|||
|
|
if next {
|
|||
|
|
t.Error("next handler should NOT be called on lookup error")
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// TestRequireVerified_NoUserID 未登录(无 user_id)→ 401。
|
|||
|
|
func TestRequireVerified_NoUserID(t *testing.T) {
|
|||
|
|
svc := &stubUserService{
|
|||
|
|
user: &model.User{ID: "u1", VerificationStatus: model.VerificationStatusApproved},
|
|||
|
|
}
|
|||
|
|
code, next := runMiddleware(t, svc, false, nil)
|
|||
|
|
if code != http.StatusUnauthorized {
|
|||
|
|
t.Errorf("status = %d, want %d", code, http.StatusUnauthorized)
|
|||
|
|
}
|
|||
|
|
if next {
|
|||
|
|
t.Error("next handler should NOT be called when user_id missing")
|
|||
|
|
}
|
|||
|
|
}
|