优化结构
This commit is contained in:
@@ -3,32 +3,31 @@ package middleware
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"kra/internal/biz/system"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"kra/internal/biz"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type tokenAuthenticatorStub struct {
|
||||
claims *biz.AuthClaims
|
||||
claims *system.AuthClaims
|
||||
err error
|
||||
got string
|
||||
}
|
||||
|
||||
func (s *tokenAuthenticatorStub) AuthenticateToken(_ context.Context, token string) (*biz.TokenAuthentication, error) {
|
||||
func (s *tokenAuthenticatorStub) AuthenticateToken(_ context.Context, token string) (*system.TokenAuthentication, error) {
|
||||
s.got = token
|
||||
if s.err != nil {
|
||||
return nil, s.err
|
||||
}
|
||||
return &biz.TokenAuthentication{Claims: s.claims}, nil
|
||||
return &system.TokenAuthentication{Claims: s.claims}, nil
|
||||
}
|
||||
|
||||
func TestAuthenticateWebSocketAcceptsQueryToken(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
stub := &tokenAuthenticatorStub{claims: &biz.AuthClaims{ID: 7}}
|
||||
stub := &tokenAuthenticatorStub{claims: &system.AuthClaims{ID: 7}}
|
||||
recorder := httptest.NewRecorder()
|
||||
context, _ := gin.CreateTestContext(recorder)
|
||||
context.Request = httptest.NewRequest(http.MethodGet, "/ws?token=query-token", nil)
|
||||
@@ -46,7 +45,7 @@ func TestAuthenticateWebSocketAcceptsQueryToken(t *testing.T) {
|
||||
|
||||
func TestHTTPAuthDoesNotAcceptQueryToken(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
stub := &tokenAuthenticatorStub{claims: &biz.AuthClaims{ID: 7}}
|
||||
stub := &tokenAuthenticatorStub{claims: &system.AuthClaims{ID: 7}}
|
||||
engine := gin.New()
|
||||
called := false
|
||||
engine.GET("/protected", Auth(stub), func(c *gin.Context) { called = true })
|
||||
@@ -60,7 +59,7 @@ func TestHTTPAuthDoesNotAcceptQueryToken(t *testing.T) {
|
||||
|
||||
func TestAuthenticateWebSocketSupportsBearerHeaderAndReportsInvalidToken(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
stub := &tokenAuthenticatorStub{claims: &biz.AuthClaims{ID: 7}}
|
||||
stub := &tokenAuthenticatorStub{claims: &system.AuthClaims{ID: 7}}
|
||||
context, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
context.Request = httptest.NewRequest(http.MethodGet, "/ws", nil)
|
||||
context.Request.Header.Set("Authorization", "Bearer header-token")
|
||||
|
||||
Reference in New Issue
Block a user