refactor(api): remove test_auth.go and inline test helpers (#5235)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
boojack 2025-11-08 09:53:29 +08:00 committed by GitHub
parent 13fea64d15
commit 906412013f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 6 additions and 25 deletions

View File

@ -23,9 +23,9 @@ import (
type ContextKey int type ContextKey int
const ( const (
// userIDContextKey stores the authenticated user's ID in the context. // UserIDContextKey stores the authenticated user's ID in the context.
// Set for both session-based and token-based authentication. // Set for both session-based and token-based authentication.
userIDContextKey ContextKey = iota UserIDContextKey ContextKey = iota
// sessionIDContextKey stores the session ID in the context. // sessionIDContextKey stores the session ID in the context.
// Only set for session-based authentication (cookie auth). // Only set for session-based authentication (cookie auth).
@ -59,7 +59,7 @@ func NewGRPCAuthInterceptor(store *store.Store, secret string) *GRPCAuthIntercep
// 4. Reject: Return 401 Unauthenticated if none of the above succeed // 4. Reject: Return 401 Unauthenticated if none of the above succeed
// //
// On successful authentication, sets context values: // On successful authentication, sets context values:
// - userIDContextKey: The authenticated user's ID (always set) // - UserIDContextKey: The authenticated user's ID (always set)
// - sessionIDContextKey: Session ID (only for cookie auth) // - sessionIDContextKey: Session ID (only for cookie auth)
// - accessTokenContextKey: JWT token (only for Bearer token auth). // - accessTokenContextKey: JWT token (only for Bearer token auth).
func (in *GRPCAuthInterceptor) AuthenticationInterceptor(ctx context.Context, request any, serverInfo *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) { func (in *GRPCAuthInterceptor) AuthenticationInterceptor(ctx context.Context, request any, serverInfo *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
@ -115,7 +115,7 @@ func (in *GRPCAuthInterceptor) handleAuthenticatedRequest(ctx context.Context, r
} }
// Set context values // Set context values
ctx = context.WithValue(ctx, userIDContextKey, user.ID) ctx = context.WithValue(ctx, UserIDContextKey, user.ID)
if sessionID != "" { if sessionID != "" {
// Session-based authentication // Session-based authentication

View File

@ -325,7 +325,7 @@ func (*APIV1Service) buildSessionCookie(ctx context.Context, sessionCookieValue
} }
func (s *APIV1Service) GetCurrentUser(ctx context.Context) (*store.User, error) { func (s *APIV1Service) GetCurrentUser(ctx context.Context) (*store.User, error) {
userID, ok := ctx.Value(userIDContextKey).(int32) userID, ok := ctx.Value(UserIDContextKey).(int32)
if !ok { if !ok {
return nil, nil return nil, nil
} }

View File

@ -82,5 +82,5 @@ func (ts *TestService) CreateRegularUser(ctx context.Context, username string) (
// CreateUserContext creates a context with the given user's ID for authentication. // CreateUserContext creates a context with the given user's ID for authentication.
func (*TestService) CreateUserContext(ctx context.Context, userID int32) context.Context { func (*TestService) CreateUserContext(ctx context.Context, userID int32) context.Context {
// Use the real context key from the parent package // Use the real context key from the parent package
return apiv1.CreateTestUserContext(ctx, userID) return context.WithValue(ctx, apiv1.UserIDContextKey, userID)
} }

View File

@ -1,19 +0,0 @@
package v1
import (
"context"
"github.com/usememos/memos/store"
)
// CreateTestUserContext creates a context with user's ID for testing purposes.
// This function is only intended for use in tests.
func CreateTestUserContext(ctx context.Context, userID int32) context.Context {
return context.WithValue(ctx, userIDContextKey, userID)
}
// CreateTestUserContextWithUser creates a context and ensures the user exists for testing.
// This function is only intended for use in tests.
func CreateTestUserContextWithUser(ctx context.Context, _ *APIV1Service, user *store.User) context.Context {
return context.WithValue(ctx, userIDContextKey, user.ID)
}