package server import ( "net/http" "net/http/httptest" "strings" "testing" "git.breakpilot.com/platform/tenant-registry/internal/authn" "git.breakpilot.com/platform/tenant-registry/internal/store" ) // Only a workspace administrator may create, enter or end, and only in its // own workspace; another workspace's administrator gets a 404. func TestConnections_userRoles(t *testing.T) { acme := "00000000-0000-0000-0000-000000000001" routes := []struct{ method, path, body string }{ {http.MethodPost, "/connection-invites", ""}, {http.MethodGet, "/connection-invites", ""}, {http.MethodPost, "/connections", `{"code":"AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA"}`}, {http.MethodGet, "/connections", ""}, } cases := []struct { name string tenant string roles []string denied int }{ {"plain user", acme, []string{"USER"}, http.StatusForbidden}, {"finance", acme, []string{"FINANCE"}, http.StatusForbidden}, {"cxo", acme, []string{"CXO"}, http.StatusForbidden}, {"another workspace's admin", "11111111-1111-1111-1111-111111111111", []string{"IT_ADMIN"}, http.StatusNotFound}, } for _, c := range cases { for _, rt := range routes { p := &authn.Principal{Subject: "u-1", ClientID: "portal", TenantID: c.tenant, OrgRoles: c.roles} _, h, tenant := scopedServer(t, p) rr := httptest.NewRecorder() h.ServeHTTP(rr, httptest.NewRequest(rt.method, "/v1/tenants/"+tenant.ID+rt.path, strings.NewReader(rt.body))) if rr.Code != c.denied { t.Errorf("%s %s %s: %d %s", c.name, rt.method, rt.path, rr.Code, rr.Body.String()) } } } for _, role := range []string{"IT_ADMIN", "OWNER"} { p := &authn.Principal{Subject: "u-1", ClientID: "portal", TenantID: acme, OrgRoles: []string{role}} _, h, tenant := scopedServer(t, p) rr := httptest.NewRecorder() h.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/v1/tenants/"+tenant.ID+"/connection-invites", nil)) if rr.Code != http.StatusCreated { t.Errorf("%s creates a code: %d %s", role, rr.Code, rr.Body.String()) } } } // The product routes refuse a person's token, whatever its client holds; // they carry the peer's tenant id. func TestConnections_productRoutesRefuseUsers(t *testing.T) { p := &authn.Principal{Subject: "u-1", ClientID: "portal", TenantID: "00000000-0000-0000-0000-000000000001", OrgRoles: []string{"IT_ADMIN"}} _, h, tenant := scopedServer(t, p, "portal:*") for _, path := range []string{ "/v1/internal/tenants/" + tenant.ID + "/connections?state=active", "/v1/internal/connections/00000000-0000-0000-0000-000000000002", } { rr := httptest.NewRecorder() h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, path, nil)) if rr.Code != http.StatusForbidden { t.Errorf("%s: %d %s", path, rr.Code, rr.Body.String()) } } } // The person window follows the person the portal names, across // workspaces; the portal's own client is never a person. func TestConnections_personWindow(t *testing.T) { portal := &authn.Principal{Subject: "svc-portal", ClientID: "portal-svc"} s, h, acme := scopedServer(t, portal, "portal-svc:tenants,users.act") other, err := s.Store.CreateTenant(t.Context(), store.TenantCreate{Slug: "beta", Name: "Beta GmbH", Plan: "professional"}) if err != nil { t.Fatal(err) } redeem := func(tenantID, subject string) int { req := httptest.NewRequest(http.MethodPost, "/v1/tenants/"+tenantID+"/connections", strings.NewReader(`{"code":"x"}`)) if subject != "" { req.Header.Set(headerPortalUserSubject, subject) } rr := httptest.NewRecorder() h.ServeHTTP(rr, req) return rr.Code } for i := 0; i < connectionRedeemRate; i++ { if got := redeem(acme.ID, "u-7"); got != http.StatusNotFound { t.Fatalf("attempt %d: %d", i, got) } } if got := redeem(other.ID, "u-7"); got != http.StatusTooManyRequests { t.Fatalf("the same person in another workspace: %d", got) } if got := redeem(other.ID, "u-8"); got != http.StatusNotFound { t.Fatalf("another person there: %d", got) } if got := redeem(other.ID, ""); got != http.StatusNotFound { t.Fatalf("the portal naming nobody: %d", got) } }