@@ -154,19 +154,19 @@ func Test_ListDependabotAlerts(t *testing.T) {
154154 }
155155
156156 tests := []struct {
157- name string
158- mockedClient * http.Client
159- requestArgs map [string ]any
160- expectError bool
161- expectedAlerts []* github.DependabotAlert
162- expectedErrMsg string
157+ name string
158+ mockedClient * http.Client
159+ requestArgs map [string ]any
160+ expectError bool
161+ expectedAlerts []* github.DependabotAlert
162+ expectedNextCursor string
163+ expectedErrMsg string
163164 }{
164165 {
165166 name : "successful open alerts listing" ,
166167 mockedClient : MockHTTPClientWithHandlers (map [string ]http.HandlerFunc {
167168 GetReposDependabotAlertsByOwnerByRepo : expectQueryParams (t , map [string ]string {
168169 "state" : "open" ,
169- "page" : "1" ,
170170 "per_page" : "30" ,
171171 }).andThen (
172172 mockResponse (t , http .StatusOK , []* github.DependabotAlert {& criticalAlert }),
@@ -185,7 +185,6 @@ func Test_ListDependabotAlerts(t *testing.T) {
185185 mockedClient : MockHTTPClientWithHandlers (map [string ]http.HandlerFunc {
186186 GetReposDependabotAlertsByOwnerByRepo : expectQueryParams (t , map [string ]string {
187187 "severity" : "high" ,
188- "page" : "1" ,
189188 "per_page" : "30" ,
190189 }).andThen (
191190 mockResponse (t , http .StatusOK , []* github.DependabotAlert {& highSeverityAlert }),
@@ -203,7 +202,6 @@ func Test_ListDependabotAlerts(t *testing.T) {
203202 name : "successful all alerts listing" ,
204203 mockedClient : MockHTTPClientWithHandlers (map [string ]http.HandlerFunc {
205204 GetReposDependabotAlertsByOwnerByRepo : expectQueryParams (t , map [string ]string {
206- "page" : "1" ,
207205 "per_page" : "30" ,
208206 }).andThen (
209207 mockResponse (t , http .StatusOK , []* github.DependabotAlert {& criticalAlert , & highSeverityAlert }),
@@ -217,10 +215,10 @@ func Test_ListDependabotAlerts(t *testing.T) {
217215 expectedAlerts : []* github.DependabotAlert {& criticalAlert , & highSeverityAlert },
218216 },
219217 {
220- name : "successful alerts listing with custom pagination" ,
218+ name : "successful alerts listing with cursor pagination" ,
221219 mockedClient : MockHTTPClientWithHandlers (map [string ]http.HandlerFunc {
222220 GetReposDependabotAlertsByOwnerByRepo : expectQueryParams (t , map [string ]string {
223- "page " : "3 " ,
221+ "after " : "Y3Vyc29yOnYyOpK5 " ,
224222 "per_page" : "100" ,
225223 }).andThen (
226224 mockResponse (t , http .StatusOK , []* github.DependabotAlert {& criticalAlert }),
@@ -229,12 +227,35 @@ func Test_ListDependabotAlerts(t *testing.T) {
229227 requestArgs : map [string ]any {
230228 "owner" : "owner" ,
231229 "repo" : "repo" ,
232- "page " : float64 ( 3 ) ,
230+ "after " : "Y3Vyc29yOnYyOpK5" ,
233231 "perPage" : float64 (100 ),
234232 },
235233 expectError : false ,
236234 expectedAlerts : []* github.DependabotAlert {& criticalAlert },
237235 },
236+ {
237+ name : "successful alerts listing surfaces next page cursor" ,
238+ mockedClient : MockHTTPClientWithHandlers (map [string ]http.HandlerFunc {
239+ GetReposDependabotAlertsByOwnerByRepo : expectQueryParams (t , map [string ]string {
240+ "per_page" : "30" ,
241+ }).andThen (
242+ func (w http.ResponseWriter , _ * http.Request ) {
243+ w .Header ().Set ("Link" , `<https://api.github.com/repos/owner/repo/dependabot/alerts?after=nextcursor123&per_page=30>; rel="next"` )
244+ w .WriteHeader (http .StatusOK )
245+ b , err := json .Marshal ([]* github.DependabotAlert {& criticalAlert })
246+ require .NoError (t , err )
247+ _ , _ = w .Write (b )
248+ },
249+ ),
250+ }),
251+ requestArgs : map [string ]any {
252+ "owner" : "owner" ,
253+ "repo" : "repo" ,
254+ },
255+ expectError : false ,
256+ expectedAlerts : []* github.DependabotAlert {& criticalAlert },
257+ expectedNextCursor : "nextcursor123" ,
258+ },
238259 {
239260 name : "alerts listing fails" ,
240261 mockedClient : MockHTTPClientWithHandlers (map [string ]http.HandlerFunc {
@@ -291,11 +312,17 @@ func Test_ListDependabotAlerts(t *testing.T) {
291312 textContent := getTextResult (t , result )
292313
293314 // Unmarshal and verify the result
294- var returnedAlerts []* github.DependabotAlert
295- err = json .Unmarshal ([]byte (textContent .Text ), & returnedAlerts )
315+ var returnedResult struct {
316+ Alerts []* github.DependabotAlert `json:"alerts"`
317+ PageInfo struct {
318+ HasNextPage bool `json:"hasNextPage"`
319+ NextCursor string `json:"nextCursor"`
320+ } `json:"pageInfo"`
321+ }
322+ err = json .Unmarshal ([]byte (textContent .Text ), & returnedResult )
296323 assert .NoError (t , err )
297- assert .Len (t , returnedAlerts , len (tc .expectedAlerts ))
298- for i , alert := range returnedAlerts {
324+ assert .Len (t , returnedResult . Alerts , len (tc .expectedAlerts ))
325+ for i , alert := range returnedResult . Alerts {
299326 assert .Equal (t , * tc .expectedAlerts [i ].Number , * alert .Number )
300327 assert .Equal (t , * tc .expectedAlerts [i ].HTMLURL , * alert .HTMLURL )
301328 assert .Equal (t , * tc .expectedAlerts [i ].State , * alert .State )
@@ -304,6 +331,8 @@ func Test_ListDependabotAlerts(t *testing.T) {
304331 assert .Equal (t , * tc .expectedAlerts [i ].SecurityAdvisory .Severity , * alert .SecurityAdvisory .Severity )
305332 }
306333 }
334+ assert .Equal (t , tc .expectedNextCursor , returnedResult .PageInfo .NextCursor )
335+ assert .Equal (t , tc .expectedNextCursor != "" , returnedResult .PageInfo .HasNextPage )
307336 })
308337 }
309338}
0 commit comments