@@ -19,7 +19,10 @@ import { AuthorizeResult } from '@backstage/plugin-permission-common';
1919import { rest } from 'msw' ;
2020import request from 'supertest' ;
2121
22- import { LOCAL_ADDR } from '../../../../__fixtures__/handlers' ;
22+ import {
23+ CATALOG_API_LOCATIONS_LOCAL_ADDR ,
24+ LOCAL_ADDR ,
25+ } from '../../../../__fixtures__/handlers' ;
2326import {
2427 addHandlersForGHTokenAppErrors ,
2528 setupTest ,
@@ -137,6 +140,201 @@ describe('repositories', () => {
137140 ] ,
138141 } ) ;
139142 } ) ;
143+
144+ describe ( 'filtering repositories' , ( ) => {
145+ it ( 'returns all repos when no repos are imported yet' , async ( ) => {
146+ const { mockCatalogClient } = useTestData ( ) ;
147+
148+ const backendServer = await startBackendServer (
149+ mockCatalogClient ,
150+ AuthorizeResult . ALLOW ,
151+ ) ;
152+
153+ const response = await request ( backendServer ) . get (
154+ '/api/bulk-import/repositories' ,
155+ ) ;
156+
157+ expect ( response . status ) . toEqual ( 200 ) ;
158+ expect ( response . body ) . toEqual ( {
159+ errors : [ ] ,
160+ repositories : [
161+ {
162+ defaultBranch : 'master' ,
163+ errors : [ ] ,
164+ id : 'octocat/animated-happiness' ,
165+ lastUpdate : '2011-01-26T19:14:43Z' ,
166+ name : 'animated-happiness' ,
167+ organization : 'octocat' ,
168+ url : 'http://localhost:8765/octocat/animated-happiness' ,
169+ } ,
170+ {
171+ defaultBranch : 'master' ,
172+ errors : [ ] ,
173+ id : 'octocat/Hello-World' ,
174+ lastUpdate : '2011-01-26T19:14:43Z' ,
175+ name : 'Hello-World' ,
176+ organization : 'octocat' ,
177+ url : 'http://localhost:8765/octocat/Hello-World' ,
178+ } ,
179+ {
180+ defaultBranch : 'master' ,
181+ errors : [ ] ,
182+ id : 'my-user/Lorem-Ipsum' ,
183+ lastUpdate : '2011-01-26T19:14:43Z' ,
184+ name : 'Lorem-Ipsum' ,
185+ organization : 'my-user' ,
186+ url : 'http://localhost:8765/my-user/Lorem-Ipsum' ,
187+ } ,
188+ ] ,
189+ totalCount : 3 ,
190+ } ) ;
191+ } ) ;
192+
193+ it ( 'returns empty array when there are no repos to be imported' , async ( ) => {
194+ const { server, mockCatalogClient } = useTestData ( ) ;
195+
196+ server . use (
197+ rest . get ( `${ LOCAL_ADDR } /user/repos` , ( _ , res , ctx ) =>
198+ res ( ctx . status ( 200 ) , ctx . json ( [ ] ) ) ,
199+ ) ,
200+ ) ;
201+ server . use (
202+ rest . get ( `${ LOCAL_ADDR } /installation/repositories` , ( _ , res , ctx ) =>
203+ res (
204+ ctx . status ( 200 ) ,
205+ ctx . json ( { total_count : 0 , repositories : [ ] } ) ,
206+ ) ,
207+ ) ,
208+ ) ;
209+
210+ const backendServer = await startBackendServer (
211+ mockCatalogClient ,
212+ AuthorizeResult . ALLOW ,
213+ ) ;
214+
215+ const response = await request ( backendServer ) . get (
216+ '/api/bulk-import/repositories' ,
217+ ) ;
218+
219+ expect ( response . status ) . toEqual ( 200 ) ;
220+ expect ( response . body ) . toEqual ( {
221+ errors : [ ] ,
222+ repositories : [ ] ,
223+ totalCount : 0 ,
224+ } ) ;
225+ } ) ;
226+
227+ it ( 'returns filtered (not yet imported) repos when some repos are already imported' , async ( ) => {
228+ const { server, mockCatalogClient } = useTestData ( ) ;
229+
230+ server . use (
231+ rest . get ( CATALOG_API_LOCATIONS_LOCAL_ADDR , ( _ , res , ctx ) =>
232+ res (
233+ ctx . status ( 200 ) ,
234+ ctx . json ( [
235+ {
236+ data : {
237+ id : 'imported-hello-world' ,
238+ target :
239+ 'http://localhost:8765/octocat/Hello-World/catalog-info.yaml' ,
240+ type : 'url' ,
241+ } ,
242+ } ,
243+ ] ) ,
244+ ) ,
245+ ) ,
246+ ) ;
247+
248+ const backendServer = await startBackendServer (
249+ mockCatalogClient ,
250+ AuthorizeResult . ALLOW ,
251+ ) ;
252+
253+ const response = await request ( backendServer ) . get (
254+ '/api/bulk-import/repositories' ,
255+ ) ;
256+
257+ expect ( response . status ) . toEqual ( 200 ) ;
258+ expect ( response . body ) . toEqual ( {
259+ errors : [ ] ,
260+ repositories : [
261+ {
262+ defaultBranch : 'master' ,
263+ errors : [ ] ,
264+ id : 'octocat/animated-happiness' ,
265+ lastUpdate : '2011-01-26T19:14:43Z' ,
266+ name : 'animated-happiness' ,
267+ organization : 'octocat' ,
268+ url : 'http://localhost:8765/octocat/animated-happiness' ,
269+ } ,
270+ {
271+ defaultBranch : 'master' ,
272+ errors : [ ] ,
273+ id : 'my-user/Lorem-Ipsum' ,
274+ lastUpdate : '2011-01-26T19:14:43Z' ,
275+ name : 'Lorem-Ipsum' ,
276+ organization : 'my-user' ,
277+ url : 'http://localhost:8765/my-user/Lorem-Ipsum' ,
278+ } ,
279+ ] ,
280+ totalCount : 2 ,
281+ } ) ;
282+ } ) ;
283+
284+ it ( 'returns empty array when all repos are already imported' , async ( ) => {
285+ const { server, mockCatalogClient } = useTestData ( ) ;
286+
287+ server . use (
288+ rest . get ( CATALOG_API_LOCATIONS_LOCAL_ADDR , ( _ , res , ctx ) =>
289+ res (
290+ ctx . status ( 200 ) ,
291+ ctx . json ( [
292+ {
293+ data : {
294+ id : 'imported-animated-happiness' ,
295+ target :
296+ 'http://localhost:8765/octocat/animated-happiness/catalog-info.yaml' ,
297+ type : 'url' ,
298+ } ,
299+ } ,
300+ {
301+ data : {
302+ id : 'imported-hello-world' ,
303+ target :
304+ 'http://localhost:8765/octocat/Hello-World/catalog-info.yaml' ,
305+ type : 'url' ,
306+ } ,
307+ } ,
308+ {
309+ data : {
310+ id : 'imported-lorem-ipsum' ,
311+ target :
312+ 'http://localhost:8765/my-user/Lorem-Ipsum/catalog-info.yaml' ,
313+ type : 'url' ,
314+ } ,
315+ } ,
316+ ] ) ,
317+ ) ,
318+ ) ,
319+ ) ;
320+
321+ const backendServer = await startBackendServer (
322+ mockCatalogClient ,
323+ AuthorizeResult . ALLOW ,
324+ ) ;
325+
326+ const response = await request ( backendServer ) . get (
327+ '/api/bulk-import/repositories' ,
328+ ) ;
329+
330+ expect ( response . status ) . toEqual ( 200 ) ;
331+ expect ( response . body ) . toEqual ( {
332+ errors : [ ] ,
333+ repositories : [ ] ,
334+ totalCount : 0 ,
335+ } ) ;
336+ } ) ;
337+ } ) ;
140338 } ) ;
141339
142340 describe ( 'GET /organizations/{org}/repositories' , ( ) => {
0 commit comments