Debian Codegraph @

internal/oauth2/providers/github/api_test.go

raw
1 package github //nolint:testpackage
2
3 import (
4 "errors"
5 "net/http"
6 "net/http/httptest"
7 "strings"
8 "testing"
9
10 "github.com/stretchr/testify/require"
11 )
12
13 type apiRoundTripper func(*http.Request) (*http.Response, error)
14
15 func (fn apiRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
16 return fn(req)
17 }
18
19 func TestGetResponseBodyLimits(t *testing.T) {
20 t.Parallel()
21
22 tests := []struct {
23 name string
24 statusCode int
25 body string
26 wantErrContains string
27 wantErrExcludes string
28 }{
29 {
30 name: "successful response at limit",
31 statusCode: http.StatusOK,
32 body: "[]" + strings.Repeat(" ", maxResponseBodySize-2),
33 },
34 {
35 name: "successful response exceeds limit",
36 statusCode: http.StatusOK,
37 body: strings.Repeat(" ", maxResponseBodySize+1),
38 wantErrContains: "response body from GitHub API https://api.github.com/test exceeds the 1048576 byte limit",
39 },
40 {
41 name: "error response is truncated",
42 statusCode: http.StatusInternalServerError,
43 body: "visible" + strings.Repeat("x", maxErrorResponseBodySize) + "hidden",
44 wantErrContains: "visible",
45 wantErrExcludes: "hidden",
46 },
47 }
48
49 for _, testCase := range tests {
50 t.Run(testCase.name, func(t *testing.T) {
51 t.Parallel()
52
53 httpClient := &http.Client{
54 Transport: apiRoundTripper(func(_ *http.Request) (*http.Response, error) {
55 response := httptest.NewRecorder()
56 response.WriteHeader(testCase.statusCode)
57 _, _ = response.WriteString(testCase.body)
58
59 return response.Result(), nil
60 }),
61 }
62
63 var data any
64
65 _, err := get[any](t.Context(), httpClient, "token", "https://api.github.com/test", &data)
66
67 if testCase.wantErrContains == "" {
68 require.NoError(t, err)
69
70 return
71 }
72
73 require.ErrorContains(t, err, testCase.wantErrContains)
74
75 if testCase.wantErrExcludes != "" {
76 require.NotContains(t, err.Error(), testCase.wantErrExcludes)
77 require.ErrorContains(t, err, "[truncated]")
78 }
79 })
80 }
81 }
82
83 func TestGetRejectsUntrustedAPIURL(t *testing.T) {
84 t.Parallel()
85
86 tests := []struct {
87 name string
88 apiURL string
89 }{
90 {
91 name: "insecure scheme",
92 apiURL: "http://api.github.com/user/orgs",
93 },
94 {
95 name: "different host",
96 apiURL: "https://example.com/user/orgs",
97 },
98 {
99 name: "host suffix",
100 apiURL: "https://api.github.com.example.com/user/orgs",
101 },
102 {
103 name: "explicit port",
104 apiURL: "https://api.github.com:443/user/orgs",
105 },
106 }
107
108 for _, testCase := range tests {
109 t.Run(testCase.name, func(t *testing.T) {
110 t.Parallel()
111
112 requestSent := false
113 httpClient := &http.Client{
114 Transport: apiRoundTripper(func(_ *http.Request) (*http.Response, error) {
115 requestSent = true
116
117 return nil, errors.New("unexpected request")
118 }),
119 }
120
121 var data any
122
123 _, err := get[any](t.Context(), httpClient, "token", testCase.apiURL, &data)
124
125 require.ErrorContains(t, err, "untrusted GitHub API URL")
126 require.False(t, requestSent)
127 })
128 }
129 }
130
131 func TestGetPagination(t *testing.T) {
132 t.Parallel()
133
134 tests := []struct {
135 name string
136 apiURL string
137 link string
138 expected string
139 wantErr string
140 }{
141 {
142 name: "nil response",
143 apiURL: "https://api.github.com/user/orgs?page=1",
144 },
145 {
146 name: "missing link header",
147 apiURL: "https://api.github.com/user/orgs?page=1",
148 },
149 {
150 name: "malformed link header",
151 apiURL: "https://api.github.com/user/orgs?page=1",
152 link: `not a github link header`,
153 },
154 {
155 name: "last page",
156 apiURL: "https://api.github.com/user/orgs?page=2",
157 link: `<https://api.github.com/user/orgs?page=2>; rel="last"`,
158 },
159 {
160 name: "next page before last",
161 apiURL: "https://api.github.com/user/orgs?page=1",
162 link: `<https://api.github.com/user/orgs?page=2>; rel="next", <https://api.github.com/user/orgs?page=3>; rel="last"`,
163 expected: "https://api.github.com/user/orgs?page=2",
164 },
165 {
166 name: "last link without next link",
167 apiURL: "https://api.github.com/user/orgs?page=1",
168 link: `<https://api.github.com/user/orgs?page=3>; rel="last"`,
169 },
170 {
171 name: "next link after last link",
172 apiURL: "https://api.github.com/user/orgs?page=1",
173 link: `<https://api.github.com/user/orgs?page=3>; rel="last", <https://api.github.com/user/orgs?page=2>; rel="next"`,
174 expected: "https://api.github.com/user/orgs?page=2",
175 },
176 {
177 name: "next url contains separators",
178 apiURL: "https://api.github.com/user/orgs?affiliation=owner,collaborator;admin&page=1",
179 link: `<https://api.github.com/user/orgs?affiliation=owner,collaborator;admin&page=2>; rel="next", <https://api.github.com/user/orgs?affiliation=owner,collaborator;admin&page=3>; rel="last"`,
180 expected: "https://api.github.com/user/orgs?affiliation=owner,collaborator;admin&page=2",
181 },
182 {
183 name: "untrusted next url",
184 apiURL: "https://api.github.com/user/orgs?page=1",
185 link: `<https://example.com/user/orgs?page=2>; rel="next", <https://api.github.com/user/orgs?page=3>; rel="last"`,
186 wantErr: "invalid GitHub pagination URL: untrusted GitHub API URL \"https://example.com/user/orgs?page=2\"",
187 },
188 {
189 name: "untrusted last url",
190 apiURL: "https://api.github.com/user/orgs?page=1",
191 link: `<https://api.github.com/user/orgs?page=2>; rel="next", <http://api.github.com/user/orgs?page=3>; rel="last"`,
192 wantErr: "invalid GitHub pagination URL: untrusted GitHub API URL \"http://api.github.com/user/orgs?page=3\"",
193 },
194 }
195
196 for _, testCase := range tests {
197 t.Run(testCase.name, func(t *testing.T) {
198 t.Parallel()
199
200 var response *http.Response
201 if testCase.name != "nil response" {
202 response = &http.Response{Header: http.Header{}}
203 if testCase.link != "" {
204 response.Header.Set("Link", testCase.link)
205 }
206 }
207
208 nextURL, err := getPagination(testCase.apiURL, response)
209 if testCase.wantErr != "" {
210 require.EqualError(t, err, testCase.wantErr)
211 require.Empty(t, nextURL)
212
213 return
214 }
215
216 require.NoError(t, err)
217 require.Equal(t, testCase.expected, nextURL)
218 })
219 }
220 }