| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestFetchLatestReleaseAuthenticatesWithGitHubToken(t *testing.T) { |
| 13 | t.Setenv("GITHUB_TOKEN", "ghp_example") |
| 14 | t.Setenv("GH_TOKEN", "") |
| 15 | var gatewayAuth, apiAuth string |
| 16 | |
| 17 | client := &http.Client{Transport: upgradeRoundTripFunc(func(request *http.Request) (*http.Response, error) { |
| 18 | switch request.URL.String() { |
| 19 | case cliGatewayBase + "/stable/latest.json": |
| 20 | gatewayAuth = request.Header.Get("Authorization") |
| 21 | return &http.Response{StatusCode: http.StatusNotFound, Status: "404 Not Found", Header: make(http.Header), |
| 22 | Body: io.NopCloser(strings.NewReader("")), Request: request}, nil |
| 23 | case ghAPIReleases: |
| 24 | apiAuth = request.Header.Get("Authorization") |
| 25 | body, err := json.Marshal([]ghRelease{completeCLIRelease("v1.9.0", false)}) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | return &http.Response{StatusCode: http.StatusOK, Status: "200 OK", Header: make(http.Header), |
| 30 | Body: io.NopCloser(bytes.NewReader(body)), Request: request}, nil |
| 31 | default: |
| 32 | t.Fatalf("unexpected release request: %s", request.URL) |
| 33 | return nil, nil |
| 34 | } |
| 35 | })} |
| 36 | |
| 37 | if _, err := fetchLatestRelease(client, cliReleaseStable); err != nil { |
| 38 | t.Fatalf("fetchLatestRelease: %v", err) |
| 39 | } |
| 40 | if apiAuth != "Bearer ghp_example" { |
| 41 | t.Errorf("GitHub API Authorization = %q, want the configured token", apiAuth) |
| 42 | } |
| 43 | if gatewayAuth != "" { |
| 44 | t.Errorf("release gateway received Authorization %q; the token must not leave api.github.com", gatewayAuth) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestFetchLatestReleaseWithoutTokenStaysAnonymous(t *testing.T) { |
| 49 | t.Setenv("GITHUB_TOKEN", "") |
| 50 | t.Setenv("GH_TOKEN", "") |
| 51 | var apiAuth string |
| 52 | seen := false |
| 53 | |
| 54 | client := &http.Client{Transport: upgradeRoundTripFunc(func(request *http.Request) (*http.Response, error) { |
| 55 | if request.URL.String() == ghAPIReleases { |
| 56 | seen = true |
| 57 | apiAuth = request.Header.Get("Authorization") |
| 58 | header := make(http.Header) |
| 59 | header.Set("X-RateLimit-Remaining", "0") |
| 60 | return &http.Response{StatusCode: http.StatusForbidden, Status: "403 Forbidden", Header: header, |
| 61 | Body: io.NopCloser(strings.NewReader("")), Request: request}, nil |
| 62 | } |
| 63 | return &http.Response{StatusCode: http.StatusNotFound, Status: "404 Not Found", Header: make(http.Header), |
| 64 | Body: io.NopCloser(strings.NewReader("")), Request: request}, nil |
| 65 | })} |
| 66 | |
| 67 | _, err := fetchLatestRelease(client, cliReleaseStable) |
| 68 | if !seen { |
| 69 | t.Fatal("GitHub API fallback was not attempted") |
| 70 | } |
| 71 | if apiAuth != "" { |
| 72 | t.Errorf("Authorization = %q, want no header when no token is configured", apiAuth) |
| 73 | } |
| 74 | if err == nil || !strings.Contains(err.Error(), "GITHUB_TOKEN") { |
| 75 | t.Errorf("rate-limited failure should name the fix, got %v", err) |
| 76 | } |
| 77 | } |
| 78 |