| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/carlosframework/platform/internal/hostclaim" |
| 12 | "github.com/carlosframework/platform/internal/hostops" |
| 13 | ) |
| 14 | |
| 15 | func TestPlan9ClaimResponseDoesNotExposeInternalErrors(t *testing.T) { |
| 16 | for _, tc := range []struct { |
| 17 | err error |
| 18 | status int |
| 19 | }{ |
| 20 | {fmt.Errorf("private-bucket/key: %w", hostops.ErrPending), http.StatusConflict}, |
| 21 | {fmt.Errorf("private-bucket/key: %w", hostclaim.ErrConflict), http.StatusConflict}, |
| 22 | {errors.New("private-bucket/key: secret backend failure"), http.StatusServiceUnavailable}, |
| 23 | } { |
| 24 | ref := claimResponseRefusal(tc.err) |
| 25 | if ref.Status != tc.status || strings.Contains(ref.Msg, "private-bucket") || strings.Contains(ref.Msg, "secret") { |
| 26 | t.Fatalf("unsafe response: %+v", ref) |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestPlan9BrowserClaimAcceptanceIsHTML(t *testing.T) { |
| 32 | fx := newConsoleFixture(t) |
| 33 | r := httptest.NewRequest(http.MethodPost, "/apps/kass/domains/attach", nil) |
| 34 | w := httptest.NewRecorder() |
| 35 | fx.srv.renderClaimAccepted(w, r, "<host>", "<operation>", "<phase>") |
| 36 | if w.Code != http.StatusAccepted || !strings.HasPrefix(w.Header().Get("Content-Type"), "text/html") { |
| 37 | t.Fatalf("response = %d %s", w.Code, w.Body.String()) |
| 38 | } |
| 39 | body := w.Body.String() |
| 40 | if strings.Contains(body, "<operation>") || !strings.Contains(body, "<operation>") || w.Header().Get("Retry-After") != "2" { |
| 41 | t.Fatalf("unsafe or unhelpful acceptance: %s", body) |
| 42 | } |
| 43 | } |
| 44 | |