package test import ( "bytes" "encoding/json" "fmt" "log" "net/http" "net/http/httptest" "os" "clintonambulance.com/calculate_negative_points/internal/config" "clintonambulance.com/calculate_negative_points/internal/server" "github.com/onsi/gomega/ghttp" "github.com/swaggest/rest/web" ) var version = config.Version{Release: "test-version", Commit: "test-commit"} func CreateRequestBodyFromStruct(body interface{}) *bytes.Buffer { return bytes.NewBuffer(CreateResponseBodyFromStruct(body)) } func CreateResponseBodyFromStruct(body interface{}) []byte { jsonValue, err := json.Marshal(body) if err != nil { log.Fatal("Error marshaling struct") } return jsonValue } func URLFromServerAndPath(server *ghttp.Server, path string) string { return fmt.Sprintf("%s%s", server.URL(), path) } func CreateHttpServer(appConfig *config.ApplicationConfig) *web.Service { logger, flushLogs := config.NewLogger(version, os.Stdout, []string{"cmd", "-e", "testEnvironment"}) defer flushLogs() service, _ := server.NewHttpServer(logger, version) server.MountAllEndpoints(service, version, appConfig, logger) return service } // TestClaims represents the mock user claims for testing type TestClaims struct { Sub string Name string Email string Groups []string } // DefaultTestClaims returns default test user claims func DefaultTestClaims() TestClaims { return TestClaims{ Sub: "test-user-id", Name: "Test User", Email: "test@example.com", Groups: []string{"calculate-negative-points-users"}, } } // addMockSessionCookie adds a mock session cookie to the request with the given claims func addMockSessionCookie(req *http.Request, appConfig *config.ApplicationConfig) { // Create a mock session with a fake id_token session, _ := appConfig.CookieStore.New(req, appConfig.SessionName) session.Values["id_token"] = "mock-test-token" session.Values["refresh_token"] = "mock-refresh-token" // Encode the session to a cookie rec := httptest.NewRecorder() session.Save(req, rec) // Copy the Set-Cookie header to the request as a Cookie header for _, cookie := range rec.Result().Cookies() { req.AddCookie(cookie) } } func PerformHttpRequest(method string, path string, requestParams ...map[string]interface{}) *httptest.ResponseRecorder { return PerformHttpRequestWithClaims(method, path, DefaultTestClaims(), requestParams...) } func PerformHttpRequestWithClaims(method string, path string, claims TestClaims, requestParams ...map[string]interface{}) *httptest.ResponseRecorder { var body map[string]interface{} headers := map[string]string{} query := map[string]string{} appConfig := CreateTestConfig() srv := CreateHttpServer(appConfig) for i, arg := range requestParams { switch i { case 0: body = arg case 1: for header, value := range arg { headers[header] = fmt.Sprintf("%v", value) } case 2: for key, value := range arg { query[key] = fmt.Sprintf("%v", value) } default: panic("Unknown argument") } } req := httptest.NewRequest(method, path, CreateRequestBodyFromStruct(body)) q := req.URL.Query() for key, value := range query { q.Add(key, value) } req.URL.RawQuery = q.Encode() for header, value := range headers { req.Header.Set(header, value) } // Add mock session cookie for authentication addMockSessionCookie(req, appConfig) rec := httptest.NewRecorder() srv.ServeHTTP(rec, req) return rec }