[feature/backend] implement /auth/logout handling + overall enhancement
This commit is contained in:
parent
d8d8e4b0d7
commit
e5fc8691bf
12 changed files with 420 additions and 64 deletions
|
@ -2,6 +2,7 @@ package middleware
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -9,16 +10,22 @@ import (
|
|||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"tss-rocks-be/internal/service"
|
||||
)
|
||||
|
||||
func createTestToken(secret string, claims jwt.MapClaims) string {
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
signedToken, _ := token.SignedString([]byte(secret))
|
||||
signedToken, err := token.SignedString([]byte(secret))
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Failed to sign token: %v", err))
|
||||
}
|
||||
return signedToken
|
||||
}
|
||||
|
||||
func TestAuthMiddleware(t *testing.T) {
|
||||
jwtSecret := "test-secret"
|
||||
tokenBlacklist := service.NewTokenBlacklist()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
|
@ -55,7 +62,7 @@ func TestAuthMiddleware(t *testing.T) {
|
|||
name: "Valid token",
|
||||
setupAuth: func(req *http.Request) {
|
||||
claims := jwt.MapClaims{
|
||||
"sub": "user123",
|
||||
"sub": "123",
|
||||
"role": "user",
|
||||
"exp": time.Now().Add(time.Hour).Unix(),
|
||||
}
|
||||
|
@ -64,14 +71,14 @@ func TestAuthMiddleware(t *testing.T) {
|
|||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
checkUserData: true,
|
||||
expectedUserID: "user123",
|
||||
expectedUserID: "123",
|
||||
expectedRole: "user",
|
||||
},
|
||||
{
|
||||
name: "Expired token",
|
||||
setupAuth: func(req *http.Request) {
|
||||
claims := jwt.MapClaims{
|
||||
"sub": "user123",
|
||||
"sub": "123",
|
||||
"role": "user",
|
||||
"exp": time.Now().Add(-time.Hour).Unix(),
|
||||
}
|
||||
|
@ -89,18 +96,22 @@ func TestAuthMiddleware(t *testing.T) {
|
|||
router := gin.New()
|
||||
|
||||
// 添加认证中间件
|
||||
router.Use(AuthMiddleware(jwtSecret))
|
||||
router.Use(func(c *gin.Context) {
|
||||
// 设置日志级别为 debug
|
||||
gin.SetMode(gin.DebugMode)
|
||||
c.Next()
|
||||
}, AuthMiddleware(jwtSecret, tokenBlacklist))
|
||||
|
||||
// 测试路由
|
||||
router.GET("/test", func(c *gin.Context) {
|
||||
if tc.checkUserData {
|
||||
userID, exists := c.Get("user_id")
|
||||
assert.True(t, exists)
|
||||
assert.Equal(t, tc.expectedUserID, userID)
|
||||
assert.True(t, exists, "user_id should exist in context")
|
||||
assert.Equal(t, tc.expectedUserID, userID, "user_id should match")
|
||||
|
||||
role, exists := c.Get("user_role")
|
||||
assert.True(t, exists)
|
||||
assert.Equal(t, tc.expectedRole, role)
|
||||
assert.True(t, exists, "user_role should exist in context")
|
||||
assert.Equal(t, tc.expectedRole, role, "user_role should match")
|
||||
}
|
||||
c.Status(http.StatusOK)
|
||||
})
|
||||
|
@ -114,13 +125,13 @@ func TestAuthMiddleware(t *testing.T) {
|
|||
router.ServeHTTP(rec, req)
|
||||
|
||||
// 验证响应
|
||||
assert.Equal(t, tc.expectedStatus, rec.Code)
|
||||
assert.Equal(t, tc.expectedStatus, rec.Code, "HTTP status code should match")
|
||||
|
||||
if tc.expectedBody != nil {
|
||||
var response map[string]string
|
||||
err := json.NewDecoder(rec.Body).Decode(&response)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tc.expectedBody, response)
|
||||
assert.NoError(t, err, "Response body should be valid JSON")
|
||||
assert.Equal(t, tc.expectedBody, response, "Response body should match")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue