|
| 1 | +package setup |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/fastclaw-ai/fastclaw/internal/users" |
| 15 | +) |
| 16 | + |
| 17 | +var setupRouteTestHTTPClient = &http.Client{Timeout: 2 * time.Second} |
| 18 | + |
| 19 | +func TestListTasksRouteRequiresPlatformAdmin(t *testing.T) { |
| 20 | + ctx := context.Background() |
| 21 | + s, resolver, adminUser, regularUser := newAuthTestServer(t, ctx) |
| 22 | + s.port = freeTCPPort(t) |
| 23 | + |
| 24 | + runCtx, cancel := context.WithCancel(ctx) |
| 25 | + defer cancel() |
| 26 | + |
| 27 | + errCh := make(chan error, 1) |
| 28 | + go func() { |
| 29 | + errCh <- s.Run(runCtx) |
| 30 | + }() |
| 31 | + baseURL := "http://127.0.0.1:" + strconv.Itoa(s.port) |
| 32 | + waitForSetupServer(t, baseURL, errCh) |
| 33 | + |
| 34 | + t.Run("unauthenticated request is rejected", func(t *testing.T) { |
| 35 | + resp := tasksListHTTPResponse(t, baseURL, nil) |
| 36 | + defer resp.Body.Close() |
| 37 | + if resp.StatusCode != http.StatusUnauthorized { |
| 38 | + t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusUnauthorized) |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + t.Run("regular user is rejected", func(t *testing.T) { |
| 43 | + cookie, err := resolver.IssueSession(ctx, regularUser.ID) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("IssueSession: %v", err) |
| 46 | + } |
| 47 | + resp := tasksListHTTPResponse(t, baseURL, cookie) |
| 48 | + defer resp.Body.Close() |
| 49 | + if resp.StatusCode != http.StatusForbidden { |
| 50 | + t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusForbidden) |
| 51 | + } |
| 52 | + }) |
| 53 | + |
| 54 | + t.Run("super admin is allowed", func(t *testing.T) { |
| 55 | + cookie, err := resolver.IssueSession(ctx, adminUser.ID) |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("IssueSession: %v", err) |
| 58 | + } |
| 59 | + resp := tasksListHTTPResponse(t, baseURL, cookie) |
| 60 | + defer resp.Body.Close() |
| 61 | + if resp.StatusCode != http.StatusOK { |
| 62 | + t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusOK) |
| 63 | + } |
| 64 | + body, err := io.ReadAll(resp.Body) |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("ReadAll: %v", err) |
| 67 | + } |
| 68 | + if got := strings.TrimSpace(string(body)); got != "[]" { |
| 69 | + t.Fatalf("body = %q, want []", got) |
| 70 | + } |
| 71 | + }) |
| 72 | + |
| 73 | + t.Run("admin api key is allowed", func(t *testing.T) { |
| 74 | + if s.apikeys == nil { |
| 75 | + t.Fatal("apikeys not configured") |
| 76 | + } |
| 77 | + _, token, err := s.apikeys.Create(ctx, adminUser.ID, "tasks-admin", users.APIKeyTypeAdmin, nil) |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("Create admin apikey: %v", err) |
| 80 | + } |
| 81 | + resp := tasksListBearerHTTPResponse(t, baseURL, token) |
| 82 | + defer resp.Body.Close() |
| 83 | + if resp.StatusCode != http.StatusOK { |
| 84 | + t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusOK) |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + cancel() |
| 89 | + select { |
| 90 | + case err := <-errCh: |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("Run: %v", err) |
| 93 | + } |
| 94 | + case <-time.After(2 * time.Second): |
| 95 | + t.Fatal("server did not stop after context cancellation") |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func freeTCPPort(t *testing.T) int { |
| 100 | + t.Helper() |
| 101 | + |
| 102 | + ln, err := net.Listen("tcp", "127.0.0.1:0") |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("Listen: %v", err) |
| 105 | + } |
| 106 | + defer ln.Close() |
| 107 | + return ln.Addr().(*net.TCPAddr).Port |
| 108 | +} |
| 109 | + |
| 110 | +func waitForSetupServer(t *testing.T, baseURL string, errCh <-chan error) { |
| 111 | + t.Helper() |
| 112 | + |
| 113 | + deadline := time.Now().Add(2 * time.Second) |
| 114 | + for time.Now().Before(deadline) { |
| 115 | + select { |
| 116 | + case err := <-errCh: |
| 117 | + t.Fatalf("Run exited before server was ready: %v", err) |
| 118 | + default: |
| 119 | + } |
| 120 | + resp, err := setupRouteTestHTTPClient.Get(baseURL + "/healthz") |
| 121 | + if err == nil { |
| 122 | + _ = resp.Body.Close() |
| 123 | + if resp.StatusCode == http.StatusOK { |
| 124 | + return |
| 125 | + } |
| 126 | + } |
| 127 | + time.Sleep(10 * time.Millisecond) |
| 128 | + } |
| 129 | + t.Fatalf("server did not become ready at %s", baseURL) |
| 130 | +} |
| 131 | + |
| 132 | +func tasksListHTTPResponse(t *testing.T, baseURL string, cookie *http.Cookie) *http.Response { |
| 133 | + t.Helper() |
| 134 | + |
| 135 | + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api/tasks", baseURL), nil) |
| 136 | + if err != nil { |
| 137 | + t.Fatalf("NewRequest: %v", err) |
| 138 | + } |
| 139 | + if cookie != nil { |
| 140 | + req.AddCookie(cookie) |
| 141 | + } |
| 142 | + resp, err := setupRouteTestHTTPClient.Do(req) |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("Do: %v", err) |
| 145 | + } |
| 146 | + return resp |
| 147 | +} |
| 148 | + |
| 149 | +func tasksListBearerHTTPResponse(t *testing.T, baseURL, token string) *http.Response { |
| 150 | + t.Helper() |
| 151 | + |
| 152 | + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api/tasks", baseURL), nil) |
| 153 | + if err != nil { |
| 154 | + t.Fatalf("NewRequest: %v", err) |
| 155 | + } |
| 156 | + req.Header.Set("Authorization", "Bearer "+token) |
| 157 | + resp, err := setupRouteTestHTTPClient.Do(req) |
| 158 | + if err != nil { |
| 159 | + t.Fatalf("Do: %v", err) |
| 160 | + } |
| 161 | + return resp |
| 162 | +} |
0 commit comments