CI / changes (push) Successful in 10s
CI / commitlint (push) Has been skipped
CI / openapi (push) Successful in 28s
CI / web (push) Successful in 1m0s
CI / go (push) Successful in 1m11s
CI / bird2 (push) Successful in 16s
CI / release (push) Successful in 4m5s
Updated the settings query options to eliminate the tenant ID parameter, streamlining the settings retrieval process. Adjusted the TenantSettingsComponent to reflect this change, ensuring it now queries settings without relying on tenant-specific data. This refactor enhances code clarity and reduces complexity in the settings management flow.
102 lines
2.6 KiB
Go
102 lines
2.6 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestBearerDevGetSettings(t *testing.T) {
|
|
srv, err := New(Options{SeedDemo: true, BundleSeedHex: testBundleSeed})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer srv.Close()
|
|
|
|
ts := httptest.NewServer(srv.Handler())
|
|
defer ts.Close()
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, ts.URL+"/v1/settings", nil)
|
|
req.Header.Set("Authorization", "Bearer dev")
|
|
resp, err := ts.Client().Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
if resp.StatusCode != http.StatusOK {
|
|
b, _ := io.ReadAll(resp.Body)
|
|
t.Fatalf("status=%d body=%s", resp.StatusCode, b)
|
|
}
|
|
}
|
|
|
|
func TestBearerDevPrefersDemoTenantOverEnvKey(t *testing.T) {
|
|
srv, err := New(Options{SeedDemo: true, BundleSeedHex: testBundleSeed})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer srv.Close()
|
|
|
|
demoTenant, _, _, _, _ := srv.Store().DemoIDs()
|
|
otherTenant := "00000000-0000-4000-8000-000000000001"
|
|
mustSetTestAPIKeys(t, srv, "dev|"+otherTenant+"|operator")
|
|
|
|
ts := httptest.NewServer(srv.Handler())
|
|
defer ts.Close()
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, ts.URL+"/v1/auth/session", nil)
|
|
req.Header.Set("Authorization", "Bearer dev")
|
|
resp, err := ts.Client().Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
if resp.StatusCode != http.StatusOK {
|
|
b, _ := io.ReadAll(resp.Body)
|
|
t.Fatalf("session status=%d body=%s", resp.StatusCode, b)
|
|
}
|
|
var body map[string]any
|
|
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ := body["tenant_id"].(string)
|
|
if got != demoTenant {
|
|
t.Fatalf("tenant_id=%q want demo tenant %q (env=%q)", got, demoTenant, otherTenant)
|
|
}
|
|
}
|
|
|
|
func TestBearerDevFallsBackToEnvKeyWithoutDemo(t *testing.T) {
|
|
srv, err := New(Options{SeedDemo: false, BundleSeedHex: testBundleSeed})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer srv.Close()
|
|
|
|
otherTenant := "00000000-0000-4000-8000-000000000001"
|
|
mustSetTestAPIKeys(t, srv, "dev|"+otherTenant+"|operator")
|
|
|
|
ts := httptest.NewServer(srv.Handler())
|
|
defer ts.Close()
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, ts.URL+"/v1/auth/session", nil)
|
|
req.Header.Set("Authorization", "Bearer dev")
|
|
resp, err := ts.Client().Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
if resp.StatusCode != http.StatusOK {
|
|
b, _ := io.ReadAll(resp.Body)
|
|
t.Fatalf("session status=%d body=%s", resp.StatusCode, b)
|
|
}
|
|
var body map[string]any
|
|
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ := body["tenant_id"].(string)
|
|
if got != otherTenant {
|
|
t.Fatalf("tenant_id=%q want env key tenant %q", got, otherTenant)
|
|
}
|
|
}
|