package server

import (
	"encoding/json"
	"testing"

	"github.com/gin-gonic/gin"
)

func TestSwaggerMarksOnlyPublicRoutesWithoutAuth(t *testing.T) {
	document := buildSwaggerDocument([]gin.RouteInfo{
		{Method: "GET", Path: "/api/freshCasbin"},
		{Method: "GET", Path: "/info/getInfoDataSource"},
		{Method: "GET", Path: "/info/getInfoPublic"},
		{Method: "POST", Path: "/payment/callback/:provider"},
	}, "", "test")
	var payload struct {
		Paths map[string]map[string]map[string]any `json:"paths"`
	}
	if err := json.Unmarshal([]byte(document), &payload); err != nil {
		t.Fatal(err)
	}
	for _, path := range []string{"/api/freshCasbin", "/info/getInfoDataSource"} {
		if _, ok := payload.Paths[path]["get"]["security"]; !ok {
			t.Fatalf("private route %s was marked public", path)
		}
	}
	for path, method := range map[string]string{"/info/getInfoPublic": "get", "/payment/callback/{provider}": "post"} {
		if _, ok := payload.Paths[path][method]["security"]; ok {
			t.Fatalf("public route %s requires authentication", path)
		}
	}
}
