Files
query-orchestration/api/queryAPI/client_test.go
T
jay brown a9d81a1094 remove parallel
remove parallel to stabilize ci/cd
2025-07-22 10:29:44 -07:00

164 lines
4.2 KiB
Go

package queryapi_test
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
queryapi "queryorchestration/api/queryAPI"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/serviceconfig"
"queryorchestration/internal/test"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCreateClient(t *testing.T) {
cfg := &ControllerConfig{}
test.CreateDB(t, cfg)
initializeTestConfig(t, cfg)
svc := createControllerServices(cfg)
cons := queryapi.NewControllers(svc, cfg)
body := queryapi.ClientCreate{
Name: "example_name",
Id: "external_id",
}
ctx, rec := createContextWithJSONBody(t, body)
err := cons.CreateClient(ctx)
require.NoError(t, err)
assert.Equal(t, http.StatusCreated, rec.Code)
assertBody(t, rec, queryapi.ClientIDBody{
Id: body.Id,
})
client, err := cfg.GetDBQueries().GetClient(t.Context(), "external_id")
require.NoError(t, err)
assert.Equal(t, "example_name", client.Name)
assert.Equal(t, "external_id", client.Clientid)
}
func TestGetClient(t *testing.T) {
cfg := &ControllerConfig{}
test.CreateDB(t, cfg)
initializeTestConfig(t, cfg)
svc := createControllerServices(cfg)
cons := queryapi.NewControllers(svc, cfg)
id := "client_id"
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
Clientid: id,
Name: "client_name",
})
require.NoError(t, err)
err = cfg.GetDBQueries().AddClientCanSync(t.Context(), &repository.AddClientCanSyncParams{
Clientid: id,
Cansync: true,
})
require.NoError(t, err)
ctx, rec := createContext(t)
err = cons.GetClient(ctx, id)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assertBody(t, rec, queryapi.DocClient{
Id: id,
Name: "client_name",
CanSync: true,
})
}
func TestUpdateClient(t *testing.T) {
cfg := &ControllerConfig{}
test.CreateDB(t, cfg)
initializeTestConfig(t, cfg)
svc := createControllerServices(cfg)
cons := queryapi.NewControllers(svc, cfg)
id := "client_id"
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
Clientid: id,
Name: "client_name",
})
require.NoError(t, err)
newClientName := "new_name"
body := queryapi.ClientUpdate{
Name: &newClientName,
}
ctx, rec := createContextWithJSONBody(t, body)
err = cons.UpdateClient(ctx, id)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Empty(t, rec.Body.String())
client, err := cfg.GetDBQueries().GetClient(t.Context(), "client_id")
require.NoError(t, err)
assert.Equal(t, "new_name", client.Name)
}
func initializeTestConfig(t testing.TB, cfg *ControllerConfig) {
// Reset the singleton config state for testing
serviceconfig.ResetConfigInitOnceTestOnly()
// Initialize the base configuration
err := serviceconfig.InitializeConfig(cfg)
require.NoError(t, err)
// Initialize auth configuration with test defaults
cfg.SetAuthUserPoolID("test-pool-id")
cfg.SetAuthClientID("test-client-id")
cfg.SetAuthClientSecret("test-client-secret")
cfg.SetAuthDomain("https://test-domain.com")
cfg.SetAuthRegion("us-east-1")
err = cfg.InitializeAuthConfig("http://localhost:8080", cfg.GetLogger())
require.NoError(t, err)
}
func createContext(t testing.TB) (echo.Context, *httptest.ResponseRecorder) {
return createContextWithJSONBody(t, struct{}{})
}
func createContextWithJSONBody(t testing.TB, body interface{}) (echo.Context, *httptest.ResponseRecorder) {
bodyBytes, err := json.Marshal(body)
require.NoError(t, err)
return createContextWithBody(t, bodyBytes)
}
func createContextWithBody(t testing.TB, body []byte) (echo.Context, *httptest.ResponseRecorder) {
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(body))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
return e.NewContext(req, rec), rec
}
func assertBody[T any](t testing.TB, rec *httptest.ResponseRecorder, expected T) {
res := getBody[T](t, rec)
assert.EqualExportedValues(t, expected, res)
}
func getBody[T any](t testing.TB, rec *httptest.ResponseRecorder) T {
var res T
err := json.Unmarshal(rec.Body.Bytes(), &res)
require.NoError(t, err)
return res
}