|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// newMockTokenServer returns an httptest.Server that serves the given sequence |
| 16 | +// of AccessTokenResponse values, one per request. Once the sequence is |
| 17 | +// exhausted the final response is reused for any extra requests. |
| 18 | +func newMockTokenServer(t *testing.T, responses []AccessTokenResponse) *httptest.Server { |
| 19 | + t.Helper() |
| 20 | + var i int |
| 21 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 22 | + resp := responses[len(responses)-1] |
| 23 | + if i < len(responses) { |
| 24 | + resp = responses[i] |
| 25 | + } |
| 26 | + i++ |
| 27 | + w.Header().Set("Content-Type", "application/json") |
| 28 | + _ = json.NewEncoder(w).Encode(resp) // #nosec G117 -- test fixture token, not a real secret |
| 29 | + })) |
| 30 | + t.Cleanup(srv.Close) |
| 31 | + return srv |
| 32 | +} |
| 33 | + |
| 34 | +// newPollTestProvider builds a provider wired to a mock token endpoint. The |
| 35 | +// initial poll interval is 0 (so the back-off starts from a known base) and |
| 36 | +// sleeps are captured rather than performed, making the tests deterministic and |
| 37 | +// instant. The returned slice records every interval passed to sleep. |
| 38 | +func newPollTestProvider(tokenURL string) (*GitHubATProvider, *[]time.Duration) { |
| 39 | + slept := &[]time.Duration{} |
| 40 | + p := &GitHubATProvider{ |
| 41 | + clientID: "test-client-id", |
| 42 | + accessTokenURL: tokenURL, |
| 43 | + pollInterval: 0, |
| 44 | + sleep: func(d time.Duration) { *slept = append(*slept, d) }, |
| 45 | + } |
| 46 | + return p, slept |
| 47 | +} |
| 48 | + |
| 49 | +// TestPollForToken_SlowDownThenSuccess confirms that a slow_down response is |
| 50 | +// treated as retriable: polling continues and the success path is reached. |
| 51 | +func TestPollForToken_SlowDownThenSuccess(t *testing.T) { |
| 52 | + srv := newMockTokenServer(t, []AccessTokenResponse{ |
| 53 | + {Error: "slow_down"}, |
| 54 | + {AccessToken: "gh-token"}, |
| 55 | + }) |
| 56 | + p, slept := newPollTestProvider(srv.URL) |
| 57 | + |
| 58 | + token, err := p.pollForToken(context.Background(), "device-code") |
| 59 | + require.NoError(t, err) |
| 60 | + assert.Equal(t, "gh-token", token) |
| 61 | + |
| 62 | + // One slow_down => one back-off, interval bumped 0 -> 5. |
| 63 | + require.Len(t, *slept, 1) |
| 64 | + assert.Equal(t, 5*time.Second, (*slept)[0]) |
| 65 | +} |
| 66 | + |
| 67 | +// TestPollForToken_MixedRetryableErrorsCompoundInterval confirms the interval |
| 68 | +// increment compounds correctly across mixed retryable errors. Per RFC 8628 |
| 69 | +// §3.5 the interval is increased by 5 seconds on each slow_down and is NOT |
| 70 | +// reset between slow_down instances, while authorization_pending leaves it |
| 71 | +// unchanged. |
| 72 | +func TestPollForToken_MixedRetryableErrorsCompoundInterval(t *testing.T) { |
| 73 | + srv := newMockTokenServer(t, []AccessTokenResponse{ |
| 74 | + {Error: "slow_down"}, // 0 -> 5 |
| 75 | + {Error: "authorization_pending"}, // stays 5 |
| 76 | + {Error: "slow_down"}, // 5 -> 10 |
| 77 | + {AccessToken: "gh-token"}, |
| 78 | + }) |
| 79 | + p, slept := newPollTestProvider(srv.URL) |
| 80 | + |
| 81 | + token, err := p.pollForToken(context.Background(), "device-code") |
| 82 | + require.NoError(t, err) |
| 83 | + assert.Equal(t, "gh-token", token) |
| 84 | + |
| 85 | + require.Len(t, *slept, 3) |
| 86 | + assert.Equal(t, 5*time.Second, (*slept)[0]) // after first slow_down |
| 87 | + assert.Equal(t, 5*time.Second, (*slept)[1]) // authorization_pending: unchanged |
| 88 | + assert.Equal(t, 10*time.Second, (*slept)[2]) // second slow_down: compounded |
| 89 | +} |
| 90 | + |
| 91 | +// TestPollForToken_IntervalCappedAtMax confirms the polling interval is capped |
| 92 | +// so a misbehaving auth server returning slow_down repeatedly cannot grow it |
| 93 | +// without bound. |
| 94 | +func TestPollForToken_IntervalCappedAtMax(t *testing.T) { |
| 95 | + // 13 slow_downs starting from interval 0 would otherwise reach 65s |
| 96 | + // (5, 10, ..., 60, 65); the cap holds it at 60. |
| 97 | + responses := make([]AccessTokenResponse, 0, 14) |
| 98 | + for range [13]struct{}{} { |
| 99 | + responses = append(responses, AccessTokenResponse{Error: "slow_down"}) |
| 100 | + } |
| 101 | + responses = append(responses, AccessTokenResponse{AccessToken: "gh-token"}) |
| 102 | + |
| 103 | + srv := newMockTokenServer(t, responses) |
| 104 | + p, slept := newPollTestProvider(srv.URL) |
| 105 | + |
| 106 | + token, err := p.pollForToken(context.Background(), "device-code") |
| 107 | + require.NoError(t, err) |
| 108 | + assert.Equal(t, "gh-token", token) |
| 109 | + |
| 110 | + require.Len(t, *slept, 13) |
| 111 | + for _, d := range *slept { |
| 112 | + assert.LessOrEqual(t, d, time.Duration(maxPollInterval)*time.Second) |
| 113 | + } |
| 114 | + // The last two back-offs are both pinned at the cap. |
| 115 | + assert.Equal(t, time.Duration(maxPollInterval)*time.Second, (*slept)[11]) |
| 116 | + assert.Equal(t, time.Duration(maxPollInterval)*time.Second, (*slept)[12]) |
| 117 | +} |
| 118 | + |
| 119 | +// TestPollForToken_FatalErrorStopsPolling confirms a non-retryable error ends |
| 120 | +// polling immediately with an error. |
| 121 | +func TestPollForToken_FatalErrorStopsPolling(t *testing.T) { |
| 122 | + srv := newMockTokenServer(t, []AccessTokenResponse{ |
| 123 | + {Error: "access_denied"}, |
| 124 | + }) |
| 125 | + p, slept := newPollTestProvider(srv.URL) |
| 126 | + |
| 127 | + _, err := p.pollForToken(context.Background(), "device-code") |
| 128 | + require.Error(t, err) |
| 129 | + assert.Contains(t, err.Error(), "access_denied") |
| 130 | + assert.Empty(t, *slept, "fatal error should not trigger a back-off sleep") |
| 131 | +} |
0 commit comments