tercul-backend/internal/adapters/graphql/graphql_test_utils_test.go
google-labs-jules[bot] f675c98e80 Fix: Correct authorization logic in integration tests
The integration tests for admin-only mutations were failing due to an authorization issue. The root cause was that the JWT token used in the tests did not reflect the user's admin role, which was being set directly in the database.

This commit fixes the issue by:
1.  Updating the `CreateAuthenticatedUser` test helper to generate a new JWT token after a user's role is changed. This ensures the token contains the correct, up-to-date role.
2.  Removing all uses of `auth.ContextWithAdminUser` from the integration tests, making the JWT token the single source of truth for authorization.

This change also removes unused imports and variables that were causing build failures after the refactoring. All integration tests now pass.
2025-10-04 23:48:44 +00:00

105 lines
2.5 KiB
Go

package graphql_test
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
)
// GraphQLRequest represents a GraphQL request
type GraphQLRequest struct {
Query string `json:"query"`
OperationName string `json:"operationName,omitempty"`
Variables map[string]interface{} `json:"variables,omitempty"`
}
// GraphQLResponse represents a generic GraphQL response
type GraphQLResponse[T any] struct {
Data T `json:"data,omitempty"`
Errors []map[string]interface{} `json:"errors,omitempty"`
}
// graphQLTestServer defines the interface for a test server that can execute GraphQL requests.
type graphQLTestServer interface {
getURL() string
getClient() *http.Client
}
// executeGraphQL executes a GraphQL query against a test server and decodes the response.
func executeGraphQL[T any](s graphQLTestServer, query string, variables map[string]interface{}, token *string, ctx ...context.Context) (*GraphQLResponse[T], error) {
request := GraphQLRequest{
Query: query,
Variables: variables,
}
requestBody, err := json.Marshal(request)
if err != nil {
return nil, err
}
var reqCtx context.Context
if len(ctx) > 0 {
reqCtx = ctx[0]
} else {
reqCtx = context.Background()
}
req, err := http.NewRequestWithContext(reqCtx, "POST", s.getURL(), bytes.NewBuffer(requestBody))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if token != nil {
req.Header.Set("Authorization", "Bearer "+*token)
}
resp, err := s.getClient().Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var response GraphQLResponse[T]
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
return &response, nil
}
// Implement the graphQLTestServer interface for GraphQLIntegrationSuite
func (s *GraphQLIntegrationSuite) getURL() string {
return s.server.URL
}
func (s *GraphQLIntegrationSuite) getClient() *http.Client {
return s.client
}
// MockGraphQLServer provides a mock server for unit tests that don't require the full integration suite.
type MockGraphQLServer struct {
Server *httptest.Server
Client *http.Client
}
func NewMockGraphQLServer(h http.Handler) *MockGraphQLServer {
ts := httptest.NewServer(h)
return &MockGraphQLServer{
Server: ts,
Client: ts.Client(),
}
}
func (s *MockGraphQLServer) getURL() string {
return s.Server.URL
}
func (s *MockGraphQLServer) getClient() *http.Client {
return s.Client
}
func (s *MockGraphQLServer) Close() {
s.Server.Close()
}