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

- Export UserIDContextKey in acl.go to allow test access
- Update test_helper.go to use UserIDContextKey directly
- Remove test_auth.go file as functionality is now inlined
- Update all references from userIDContextKey to UserIDContextKey

This simplifies the test helper architecture by eliminating an
unnecessary indirection layer.
This commit is contained in:
Claude 2025-11-08 01:47:47 +00:00
parent 13fea64d15
commit 63ee0c0307
No known key found for this signature in database
4 changed files with 6 additions and 25 deletions

View File

@ -23,9 +23,9 @@ import (
type ContextKey int
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.
userIDContextKey ContextKey = iota
UserIDContextKey ContextKey = iota
// sessionIDContextKey stores the session ID in the context.
// 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
//
// 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)
// - 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) {
@ -115,7 +115,7 @@ func (in *GRPCAuthInterceptor) handleAuthenticatedRequest(ctx context.Context, r
}
// Set context values
ctx = context.WithValue(ctx, userIDContextKey, user.ID)
ctx = context.WithValue(ctx, UserIDContextKey, user.ID)
if sessionID != "" {
// 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) {
userID, ok := ctx.Value(userIDContextKey).(int32)
userID, ok := ctx.Value(UserIDContextKey).(int32)
if !ok {
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.
func (*TestService) CreateUserContext(ctx context.Context, userID int32) context.Context {
// 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)
}