package handler import ( "baron-sso-backend/internal/service" "bytes" "encoding/json" "net/http" "net/http/httptest" "testing" "github.com/gofiber/fiber/v2" "github.com/stretchr/testify/assert" ) func TestListClients_Success(t *testing.T) { transport := roundTripFunc(func(r *http.Request) (*http.Response, error) { if r.URL.Path == "/clients" { return httpJSONAny(r, http.StatusOK, []map[string]interface{}{ {"client_id": "client-1", "client_name": "App One", "metadata": map[string]interface{}{"status": "active"}}, {"client_id": "client-2", "client_name": "App Two", "metadata": map[string]interface{}{"status": "inactive"}}, }), nil } return httpJSONAny(r, http.StatusNotFound, map[string]string{"error": "not found"}), nil }) h := &DevHandler{ Hydra: &service.HydraAdminService{ AdminURL: "http://hydra.test", HTTPClient: &http.Client{Transport: transport}, }, } app := fiber.New() app.Get("/api/v1/dev/clients", h.ListClients) req := httptest.NewRequest(http.MethodGet, "/api/v1/dev/clients", nil) resp, _ := app.Test(req, -1) assert.Equal(t, http.StatusOK, resp.StatusCode) var res struct { Items []clientSummary `json:"items"` } json.NewDecoder(resp.Body).Decode(&res) assert.Equal(t, 2, len(res.Items)) assert.Equal(t, "client-1", res.Items[0].ID) assert.Equal(t, "App One", res.Items[0].Name) } func TestGetClient_Success(t *testing.T) { transport := roundTripFunc(func(r *http.Request) (*http.Response, error) { if r.URL.Path == "/clients/client-123" { return httpJSONAny(r, http.StatusOK, map[string]interface{}{ "client_id": "client-123", "client_name": "Test App", "metadata": map[string]interface{}{"status": "active"}, }), nil } return httpJSONAny(r, http.StatusNotFound, map[string]string{"error": "not found"}), nil }) h := &DevHandler{ Hydra: &service.HydraAdminService{ AdminURL: "http://hydra.test", PublicURL: "http://hydra-public.test", // PublicURL 추가 HTTPClient: &http.Client{Transport: transport}, }, } app := fiber.New() app.Get("/api/v1/dev/clients/:id", h.GetClient) req := httptest.NewRequest(http.MethodGet, "/api/v1/dev/clients/client-123", nil) resp, _ := app.Test(req, -1) assert.Equal(t, http.StatusOK, resp.StatusCode) var res clientDetailResponse json.NewDecoder(resp.Body).Decode(&res) assert.Equal(t, "client-123", res.Client.ID) assert.Equal(t, "Test App", res.Client.Name) assert.Equal(t, "http://hydra-public.test/oauth2/auth", res.Endpoints.Authorization) } func TestGetClient_NotFound(t *testing.T) { transport := roundTripFunc(func(r *http.Request) (*http.Response, error) { return httpJSONAny(r, http.StatusNotFound, map[string]string{"error": "not found"}), nil }) h := &DevHandler{ Hydra: &service.HydraAdminService{ AdminURL: "http://hydra.test", HTTPClient: &http.Client{Transport: transport}, }, } app := fiber.New() app.Get("/api/v1/dev/clients/:id", h.GetClient) req := httptest.NewRequest(http.MethodGet, "/api/v1/dev/clients/non-existent", nil) resp, _ := app.Test(req, -1) assert.Equal(t, http.StatusNotFound, resp.StatusCode) } func TestCreateClient_Success(t *testing.T) { transport := roundTripFunc(func(r *http.Request) (*http.Response, error) { if r.Method == http.MethodPost && r.URL.Path == "/clients" { return httpJSONAny(r, http.StatusCreated, map[string]interface{}{ "client_id": "new-client-123", "client_name": "New App", "client_secret": "secret-123", }), nil } return httpJSONAny(r, http.StatusInternalServerError, map[string]string{"error": "hydra error"}), nil }) secretRepo := &mockSecretRepo{secrets: make(map[string]string)} redisRepo := &mockRedisRepo{data: make(map[string]string)} h := &DevHandler{ Hydra: &service.HydraAdminService{ AdminURL: "http://hydra.test", HTTPClient: &http.Client{Transport: transport}, }, SecretRepo: secretRepo, Redis: redisRepo, } app := fiber.New() app.Post("/api/v1/dev/clients", h.CreateClient) body, _ := json.Marshal(map[string]interface{}{ "client_name": "New App", "type": "confidential", "redirectUris": []string{"http://localhost/cb"}, }) req := httptest.NewRequest(http.MethodPost, "/api/v1/dev/clients", bytes.NewReader(body)) req.Header.Set("Content-Type", "application/json") resp, _ := app.Test(req, -1) assert.Equal(t, http.StatusCreated, resp.StatusCode) secret, _ := secretRepo.GetByID(nil, "new-client-123") assert.Equal(t, "secret-123", secret) }