RezinaLMS-BackEnd/internal/web_server/api_docs_test.go

38 lines
1.0 KiB
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 document map[string]any
if err := json.NewDecoder(response.Body).Decode(&document); err != nil {
t.Fatal(err)
}
paths, _ := document["paths"].(map[string]any)
if len(paths) == 0 {
t.Fatal("empty API specification")
}
assertSwaggerReferences(t, document)
for path := range 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)
}
}
})
}
}