| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestDownloadRetryUsesAuthenticatedFixedGuestAction(t *testing.T) { |
| 12 | s := testServer(t) |
| 13 | s.ready = true |
| 14 | s.credentials.Token = "guest-test-token" |
| 15 | called := 0 |
| 16 | s.browserControl = browserClientForTest(t, s, browserTransport(func(r *http.Request) (*http.Response, error) { |
| 17 | called++ |
| 18 | if r.URL.String() != "http://127.0.0.1:18500/download-retry" || r.Header.Get("Authorization") != "Bearer guest-test-token" { |
| 19 | t.Fatal("retry did not use the fixed authenticated guest endpoint") |
| 20 | } |
| 21 | var input struct{ ID string } |
| 22 | if err := json.NewDecoder(r.Body).Decode(&input); err != nil || input.ID != "download-id" { |
| 23 | t.Fatal("retry lost the selected download", err) |
| 24 | } |
| 25 | return &http.Response{StatusCode: 200, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(`{"ok":true}`))}, nil |
| 26 | })) |
| 27 | for _, action := range []string{"download-retry", "editor-restart"} { |
| 28 | if w := request(s, "POST", "/api/browser/"+action, s.origin, `{}`, false); w.Code != 401 { |
| 29 | t.Fatal("recovery accepted without session", action, w.Code) |
| 30 | } |
| 31 | if w := request(s, "POST", "/api/browser/"+action, "https://example.com", `{}`, true); w.Code != 403 { |
| 32 | t.Fatal("cross-origin recovery accepted", action, w.Code) |
| 33 | } |
| 34 | if w := request(s, "POST", "/api/browser/"+action, s.origin, `{"unit":"ssh.service"}`, true); w.Code != 400 { |
| 35 | t.Fatal("arbitrary service parameters accepted", action, w.Code) |
| 36 | } |
| 37 | } |
| 38 | if called != 0 { |
| 39 | t.Fatal("rejected request reached the guest") |
| 40 | } |
| 41 | if w := request(s, "POST", "/api/browser/download-retry", s.origin, `{"id":"download-id"}`, true); w.Code != 200 || called != 1 { |
| 42 | t.Fatal("authorized retry failed", w.Code, w.Body.String()) |
| 43 | } |
| 44 | } |