37 lines
984 B
Go
37 lines
984 B
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"hfb_sys/backend/internal/modules/auth"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestAuthRejectsRevokedUserToken(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
manager := auth.NewJWTManager("test-secret")
|
|
pair, err := manager.GenerateSubjectPairWithVersion(7, "13900000007", "user", 0)
|
|
if err != nil {
|
|
t.Fatalf("生成令牌失败:%v", err)
|
|
}
|
|
|
|
engine := gin.New()
|
|
engine.GET("/protected", Auth(manager, func(_ context.Context, _ uint64, _ int64) error {
|
|
return auth.ErrTokenVersionMismatch
|
|
}), func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
request := httptest.NewRequest(http.MethodGet, "/protected", nil)
|
|
request.Header.Set("Authorization", "Bearer "+pair.AccessToken)
|
|
response := httptest.NewRecorder()
|
|
engine.ServeHTTP(response, request)
|
|
|
|
if response.Code != http.StatusUnauthorized {
|
|
t.Fatalf("响应状态 = %d, want %d", response.Code, http.StatusUnauthorized)
|
|
}
|
|
}
|