package console import ( "io" "net/http" "regexp" "strings" "testing" "time" "github.com/carlosframework/platform/internal/svcrole" ) type rawReply struct { status int body string } // doRaw sends one request with an optional bearer token. Non-GET methods // carry the body {}. func doRaw(t *testing.T, method, url, tok string) rawReply { t.Helper() var body io.Reader if method != http.MethodGet && method != http.MethodHead { body = strings.NewReader("{}") } req, err := http.NewRequest(method, url, body) if err != nil { t.Fatal(err) } if body != nil { req.Header.Set("Content-Type", "application/json") } if tok != "" { req.Header.Set("Authorization", "Bearer "+tok) } resp, err := http.DefaultClient.Do(req) if err != nil { t.Fatal(err) } defer resp.Body.Close() b, err := io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } return rawReply{status: resp.StatusCode, body: string(b)} } // TestUnknownStoredRoleIsNotSignedInAtEveryDoor is spec ยง9's first check: a // console that does not know a stored role (as v0.21.0 does not know // deploy) answers its token exactly as it answers no token at all. func TestUnknownStoredRoleIsNotSignedInAtEveryDoor(t *testing.T) { fx := newConsoleFixture(t) if _, err := fx.st.CreateAppIn(OpsAccountID, "hello", "paul@keymail.dev", time.Now()); err != nil { t.Fatal(err) } raw, err := fx.st.NewServicePrincipalScoped(OpsAccountID, "ci", "hello", svcrole.Deploy, []string{"edge"}, nil, "paul@keymail.dev", time.Now()) if err != nil { t.Fatal(err) } // Positive control: before the role is rewritten the token authenticates // as deploy, so a deploy door does not answer 401. if pre := doRaw(t, http.MethodGet, fx.ts.URL+"/api/cli/apps/bab/hello/channels", raw); pre.status == http.StatusUnauthorized { t.Fatalf("deploy token was refused before the role change: %d %q", pre.status, pre.body) } res, err := fx.st.db.Exec(`UPDATE cli_tokens SET role = 'futurerole' WHERE token_hash = ?`, hashToken(raw)) if err != nil { t.Fatal(err) } if n, err := res.RowsAffected(); err != nil || n != 1 { t.Fatalf("role rewrite changed %d rows (err %v), want 1", n, err) } if len(fx.srv.cliRoles) == 0 { t.Fatal("no registered CLI doors to check") } fill := regexp.MustCompile(`\{[a-z]+\}`) for pattern := range fx.srv.cliRoles { method, path, _ := strings.Cut(pattern, " ") path = strings.Replace(path, "{account}", "bab", 1) path = strings.Replace(path, "{app}", "hello", 1) path = fill.ReplaceAllString(path, "x") with := doRaw(t, method, fx.ts.URL+path, raw) without := doRaw(t, method, fx.ts.URL+path, "") // The no-token side must be the gate's own refusal, so an unrouted // path (404/405 on both sides) cannot pass for a door. if without.status != http.StatusUnauthorized || !strings.Contains(without.body, "not signed in") { t.Errorf("%s: no token answered %d %q, want the gate's 401 not signed in", pattern, without.status, without.body) continue } if with.status != without.status || with.body != without.body { t.Errorf("%s: unknown role answered %d %q; no token answered %d %q", pattern, with.status, with.body, without.status, without.body) } } }