This commit is contained in:
130
internal/test/http_helper.go
Normal file
130
internal/test/http_helper.go
Normal file
@@ -0,0 +1,130 @@
|
||||
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
|
||||
}
|
||||
53
internal/test/test_helper.go
Normal file
53
internal/test/test_helper.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"clintonambulance.com/calculate_negative_points/internal/config"
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/onsi/ginkgo/v2"
|
||||
)
|
||||
|
||||
func CreateTestConfig() *config.ApplicationConfig {
|
||||
appSecret := "12345678901234567890123456789012"
|
||||
appSecretBlock := "09876543210987654321098765431098"
|
||||
|
||||
basepath := os.Getenv("APP_MIGRATION_DIRECTORY")
|
||||
if basepath == "" {
|
||||
_, b, _, _ := runtime.Caller(0)
|
||||
basepath = filepath.Dir(b)
|
||||
}
|
||||
|
||||
return &config.ApplicationConfig{
|
||||
AppSecret: config.SecretFromValue(appSecret),
|
||||
AppSecretBlock: config.SecretFromValue(appSecretBlock),
|
||||
CookieStore: sessions.NewCookieStore([]byte(appSecret)),
|
||||
Environment: "test",
|
||||
Listen: "127.0.0.1:3000",
|
||||
MatchThreshold: 3,
|
||||
SessionName: "calculate-negative-points-test",
|
||||
NocoDBConfig: config.NocoDBConfig{
|
||||
ApiToken: config.SecretFromValue("1234567890"),
|
||||
BaseUrl: "https://example.com",
|
||||
EmployeesTableId: "0987654321",
|
||||
InfractionsTableId: "2468013579",
|
||||
NegativeInfractionId: 1,
|
||||
NoPointsViewId: "1357924680",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func UnmarshalBody[K comparable, V any](rec *httptest.ResponseRecorder) map[K]V {
|
||||
var body map[K]V
|
||||
err := json.NewDecoder(rec.Body).Decode(&body)
|
||||
|
||||
if err != nil {
|
||||
ginkgo.Fail("Could not decode response")
|
||||
}
|
||||
|
||||
return body
|
||||
}
|
||||
Reference in New Issue
Block a user