RezinaLMS-BackEnd/internal/web_server/api_docs_test.go

38 lines
1008 B
Go

package httpserver
import (
"encoding/json"
"github.com/gofiber/fiber/v2"
"net/http/httptest"
"strings"
"testing"
)
func TestAudienceSpecifications(t *testing.T) {
for _, audience := range []string{"admin", "client"} {
t.Run(audience, func(t *testing.T) {
server := fiber.New()
server.Get("/doc", audienceSpecification(audience))
response, err := server.Test(httptest.NewRequest("GET", "/doc", nil))
if err != nil {
t.Fatal(err)
}
defer response.Body.Close()
var spec struct {
Paths map[string]any `json:"paths"`
}
if err := json.NewDecoder(response.Body).Decode(&spec); err != nil {
t.Fatal(err)
}
if len(spec.Paths) == 0 {
t.Fatal("empty API specification")
}
for path := range spec.Paths {
if (audience == "admin" && !strings.HasPrefix(path, "/api/v1/admin/")) || (audience == "client" && (!strings.HasPrefix(path, "/api/v1/") || strings.HasPrefix(path, "/api/v1/admin/"))) {
t.Errorf("foreign path %s", path)
}
}
})
}
}