carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3import (
4 "bytes"
5 "encoding/json"
6 "net/http"
7 "net/http/httptest"
8 "strings"
9 "testing"
10 "time"
11)
12
13// signedInConsole plants a credential for an httptest console that serves
14// GET /api/cli/apps, and returns that console's URL.
15func signedInConsole(t *testing.T, handler http.HandlerFunc) string {
16 t.Helper()
17 ts := httptest.NewServer(handler)
18 t.Cleanup(ts.Close)
19 f, err := loadCredentials()
20 if err != nil {
21 t.Fatal(err)
22 }
23 f.put(credential{Console: ts.URL, Address: "tom@keymail.dev", Token: "tok",
24 CreatedAt: time.Now(), ExpiresAt: time.Now().Add(time.Hour)})
25 if err := f.save(); err != nil {
26 t.Fatal(err)
27 }
28 return ts.URL
29}
30
31// accounts list, signed-in: no registry on this machine, a credential and a
32// console that answers — the no-infra path #157 was filed for.
33func TestAccountsListSignedIn(t *testing.T) {
34 useIsolatedConfig(t)
35 signedInConsole(t, func(w http.ResponseWriter, r *http.Request) {
36 if r.URL.Path != "/api/cli/apps" || r.Header.Get("Authorization") != "Bearer tok" {
37 http.NotFound(w, r)
38 return
39 }
40 json.NewEncoder(w).Encode(map[string]any{"accounts": []map[string]any{
41 {"sqid": "bdk", "name": "tom@keymail.dev", "role": "owner", "apps": []string{}},
42 {"sqid": "bce", "name": "acme", "role": "member", "apps": []string{"shop"}},
43 }})
44 })
45
46 var out bytes.Buffer
47 if err := accountsListAPI(&out, ""); err != nil {
48 t.Fatalf("accountsListAPI: %v", err)
49 }
50 for _, want := range []string{"NAME", "SQID", "ROLE",
51 "tom@keymail.dev", "bdk", "owner", "acme", "bce", "member"} {
52 if !strings.Contains(out.String(), want) {
53 t.Fatalf("table missing %q:\n%s", want, out.String())
54 }
55 }
56}
57
58// A console from before the listing endpoint gets a plain named refusal,
59// not the membership-404 riddle.
60func TestAccountsListOldConsole(t *testing.T) {
61 useIsolatedConfig(t)
62 url := signedInConsole(t, func(w http.ResponseWriter, r *http.Request) {
63 http.NotFound(w, r)
64 })
65
66 var out bytes.Buffer
67 err := accountsListAPI(&out, "")
68 if err == nil || !strings.Contains(err.Error(), url) || !strings.Contains(err.Error(), "predates") {
69 t.Fatalf("old console = %v, want a refusal naming the console", err)
70 }
71}
72
73// Not logged in and no registry: the error names the fix, exactly pick()'s.
74func TestAccountsListNeitherMode(t *testing.T) {
75 useTempCredentials(t)
76 var out bytes.Buffer
77 if err := accountsListAPI(&out, ""); err == nil ||
78 !strings.Contains(err.Error(), "carlos auth login") {
79 t.Fatalf("no credential = %v, want the login hint", err)
80 }
81}
82