81 lines
3.5 KiB
Go
81 lines
3.5 KiB
Go
package httpapi
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"os"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"data-terminal-cloud/internal/audit"
|
||
|
|
"data-terminal-cloud/internal/identity"
|
||
|
|
"data-terminal-cloud/internal/pagination"
|
||
|
|
"data-terminal-cloud/internal/saas"
|
||
|
|
|
||
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestSaaSReadHandlersWithAuthenticatedSeedData(t *testing.T) {
|
||
|
|
databaseURL := os.Getenv("TEST_DATABASE_URL")
|
||
|
|
if databaseURL == "" {
|
||
|
|
t.Skip("TEST_DATABASE_URL is not set")
|
||
|
|
}
|
||
|
|
pool, err := pgxpool.New(context.Background(), databaseURL)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
defer pool.Close()
|
||
|
|
cursorCodec, err := pagination.NewCodec([]byte("http-saas-read-cursor-signing-key-32-bytes"))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
service := saas.NewService(pool, audit.NewRepository(), nil, cursorCodec)
|
||
|
|
principal := identity.Principal{Issuer: "http://localhost:58180/realms/voice-local", Subject: "00000000-0000-0000-0000-000000000101"}
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
handler http.Handler
|
||
|
|
pathValue string
|
||
|
|
header string
|
||
|
|
}{
|
||
|
|
{"households", ListHouseholdsHandler(service), "", "10000000-0000-0000-0000-000000000010"},
|
||
|
|
{"household", GetHouseholdHandler(service), "10000000-0000-0000-0000-000000000211", ""},
|
||
|
|
{"household members", ListHouseholdMembersHandler(service), "10000000-0000-0000-0000-000000000211", ""},
|
||
|
|
{"member invitations", ListMemberInvitationsHandler(service), "10000000-0000-0000-0000-000000000211", ""},
|
||
|
|
{"subjects", ListSubjectsHandler(service), "10000000-0000-0000-0000-000000000211", ""},
|
||
|
|
{"subject", GetSubjectHandler(service), "10000000-0000-0000-0000-000000000411", ""},
|
||
|
|
{"conversation readiness", GetConversationReadinessHandler(service), "10000000-0000-0000-0000-000000000411", ""},
|
||
|
|
{"consents", ListConsentsHandler(service), "10000000-0000-0000-0000-000000000411", ""},
|
||
|
|
{"facts", ListSubjectFactsHandler(service), "10000000-0000-0000-0000-000000000411", ""},
|
||
|
|
{"conversations", ListSubjectConversationsHandler(service), "10000000-0000-0000-0000-000000000411", ""},
|
||
|
|
{"stories", ListSubjectStoriesHandler(service), "10000000-0000-0000-0000-000000000411", ""},
|
||
|
|
{"memories", ListSubjectMemoriesHandler(service), "10000000-0000-0000-0000-000000000411", ""},
|
||
|
|
{"privacy requests", ListPrivacyRequestsHandler(service), "10000000-0000-0000-0000-000000000411", ""},
|
||
|
|
{"exports", ListExportTasksHandler(service), "10000000-0000-0000-0000-000000000411", ""},
|
||
|
|
{"devices", ListManagedDevicesHandler(service), "", ""},
|
||
|
|
{"device", GetManagedDeviceHandler(service), "10000000-0000-0000-0000-000000000511", ""},
|
||
|
|
{"device commands", ListDeviceCommandsHandler(service), "10000000-0000-0000-0000-000000000511", ""},
|
||
|
|
{"presence", GetDevicePresenceHandler(service), "10000000-0000-0000-0000-000000000511", ""},
|
||
|
|
{"twin", GetDeviceTwinHandler(service), "10000000-0000-0000-0000-000000000511", ""},
|
||
|
|
}
|
||
|
|
for _, item := range tests {
|
||
|
|
t.Run(item.name, func(t *testing.T) {
|
||
|
|
request := httptest.NewRequest(http.MethodGet, "/api/test", nil)
|
||
|
|
request = request.WithContext(identity.WithPrincipal(request.Context(), principal))
|
||
|
|
if item.pathValue != "" {
|
||
|
|
request.SetPathValue("id", item.pathValue)
|
||
|
|
}
|
||
|
|
if item.header != "" {
|
||
|
|
request.Header.Set("X-Tenant-Id", item.header)
|
||
|
|
}
|
||
|
|
recorder := httptest.NewRecorder()
|
||
|
|
item.handler.ServeHTTP(recorder, request)
|
||
|
|
if recorder.Code != http.StatusOK {
|
||
|
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||
|
|
}
|
||
|
|
if contentType := recorder.Header().Get("Content-Type"); contentType != "application/json; charset=utf-8" {
|
||
|
|
t.Fatalf("content type=%q", contentType)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|