|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + |
| 11 | + "github.com/hookdeck/hookdeck-cli/pkg/hookdeck" |
| 12 | +) |
| 13 | + |
| 14 | +const testDashboard = "https://dashboard.hookdeck.com" |
| 15 | + |
| 16 | +func mfaRefusal(redirect string) error { |
| 17 | + return &hookdeck.APIError{ |
| 18 | + StatusCode: http.StatusForbidden, |
| 19 | + Message: "Acme requires multi-factor authentication. Please set up MFA to continue.", |
| 20 | + Redirect: redirect, |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func TestWithMFASetupLinkAddsTheDashboardLink(t *testing.T) { |
| 25 | + got := withMFASetupLink(mfaRefusal("/setup-mfa?organization_id=org_1"), testDashboard) |
| 26 | + |
| 27 | + assert.Equal(t, "error: Acme requires multi-factor authentication. Please set up MFA to continue.\n"+ |
| 28 | + "Set it up here: https://dashboard.hookdeck.com/setup-mfa?organization_id=org_1", got) |
| 29 | +} |
| 30 | + |
| 31 | +func TestMFASetupLinkFindsAWrappedRefusal(t *testing.T) { |
| 32 | + err := fmt.Errorf("listing sources: %w", mfaRefusal("/setup-mfa?organization_id=org_1")) |
| 33 | + |
| 34 | + assert.Equal(t, "https://dashboard.hookdeck.com/setup-mfa?organization_id=org_1", mfaSetupLink(err, testDashboard)) |
| 35 | +} |
| 36 | + |
| 37 | +func TestMFASetupLinkUsesTheCLIsDashboard(t *testing.T) { |
| 38 | + assert.Equal(t, "http://tokens.localhost/setup-mfa?organization_id=org_1", |
| 39 | + mfaSetupLink(mfaRefusal("/setup-mfa?organization_id=org_1"), "http://tokens.localhost")) |
| 40 | +} |
| 41 | + |
| 42 | +// The CLI only ever links to its own dashboard, whatever the response says. |
| 43 | +func TestMFASetupLinkIgnoresAnythingButASetupPath(t *testing.T) { |
| 44 | + cases := map[string]error{ |
| 45 | + "another site": mfaRefusal("https://evil.example.com/setup-mfa"), |
| 46 | + "another site, no host": mfaRefusal("//evil.example.com/setup-mfa"), |
| 47 | + "another page": mfaRefusal("/billing"), |
| 48 | + "no redirect": mfaRefusal(""), |
| 49 | + "not a 403": &hookdeck.APIError{ |
| 50 | + StatusCode: http.StatusUnauthorized, |
| 51 | + Redirect: "/setup-mfa?organization_id=org_1", |
| 52 | + }, |
| 53 | + "not an API error": errors.New("boom"), |
| 54 | + } |
| 55 | + for name, err := range cases { |
| 56 | + t.Run(name, func(t *testing.T) { |
| 57 | + assert.Empty(t, mfaSetupLink(err, testDashboard)) |
| 58 | + assert.Equal(t, err.Error(), withMFASetupLink(err, testDashboard)) |
| 59 | + }) |
| 60 | + } |
| 61 | +} |
0 commit comments