2023-07-09 00:37:41 +00:00
|
|
|
package api
|
2022-01-02 23:29:34 +00:00
|
|
|
|
|
|
|
import (
|
2023-07-09 00:37:41 +00:00
|
|
|
"io"
|
2022-01-02 23:29:34 +00:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2022-12-06 06:41:09 +00:00
|
|
|
"github.com/TwiN/gatus/v5/security"
|
2023-07-09 00:37:41 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2022-01-02 23:29:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfigHandler_ServeHTTP(t *testing.T) {
|
|
|
|
securityConfig := &security.Config{
|
|
|
|
OIDC: &security.OIDCConfig{
|
|
|
|
IssuerURL: "https://sso.gatus.io/",
|
|
|
|
RedirectURL: "http://localhost:80/authorization-code/callback",
|
|
|
|
Scopes: []string{"openid"},
|
|
|
|
AllowedSubjects: []string{"user1@example.com"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
handler := ConfigHandler{securityConfig: securityConfig}
|
|
|
|
// Create a fake router. We're doing this because I need the gate to be initialized.
|
2023-07-09 00:37:41 +00:00
|
|
|
app := fiber.New()
|
|
|
|
app.Get("/api/v1/config", handler.GetConfig)
|
|
|
|
err := securityConfig.ApplySecurityMiddleware(app)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("expected err to be nil, but was", err)
|
|
|
|
}
|
2022-01-02 23:29:34 +00:00
|
|
|
// Test the config handler
|
|
|
|
request, _ := http.NewRequest("GET", "/api/v1/config", http.NoBody)
|
2023-07-09 00:37:41 +00:00
|
|
|
response, err := app.Test(request)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("expected err to be nil, but was", err)
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
t.Error("expected code to be 200, but was", response.StatusCode)
|
|
|
|
}
|
|
|
|
body, err := io.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("expected err to be nil, but was", err)
|
2022-01-02 23:29:34 +00:00
|
|
|
}
|
2023-07-09 00:37:41 +00:00
|
|
|
if string(body) != `{"oidc":true,"authenticated":false}` {
|
|
|
|
t.Error("expected body to be `{\"oidc\":true,\"authenticated\":false}`, but was", string(body))
|
2022-01-02 23:29:34 +00:00
|
|
|
}
|
|
|
|
}
|