@@ -24,11 +24,17 @@ describe('fetch', () => {
2424 } ) ;
2525
2626 it ( 'should return a response object for unknown' , async ( ) => {
27- const resp = await handler . fetch ( { url : 'https://www.example.com/endpoint/repo/path/file.html' , method : 'BLAH' } , { } ) ;
27+ const hnd = await esmock ( '../src/index.js' , {
28+ '../src/utils/daCtx.js' : {
29+ default :
async ( ) => ( { authorized :
true , users :
[ { email :
'[email protected] ' } ] , path :
'/endpoint/repo/path/file.html' } ) , 30+ } ,
31+ } ) ;
32+
33+ const resp = await hnd . fetch ( { url : 'https://www.example.com/endpoint/repo/path/file.html' , method : 'BLAH' } , { } ) ;
2834 assert . strictEqual ( resp . status , 405 ) ;
2935 } ) ;
3036
31- it ( 'should return 401 when not authorized and not logged in ' , async ( ) => {
37+ it ( 'should return 401 when user is anonymous ' , async ( ) => {
3238 const hnd = await esmock ( '../src/index.js' , {
3339 '../src/utils/daCtx.js' : {
3440 default : async ( ) => ( { authorized : false , users : [ { email : 'anonymous' } ] } ) ,
@@ -39,6 +45,17 @@ describe('fetch', () => {
3945 assert . strictEqual ( resp . status , 401 ) ;
4046 } ) ;
4147
48+ it ( 'should return 401 when not authorized and not logged in' , async ( ) => {
49+ const hnd = await esmock ( '../src/index.js' , {
50+ '../src/utils/daCtx.js' : {
51+ default :
async ( ) => ( { authorized :
false , users :
[ { email :
'[email protected] ' } ] } ) , 52+ } ,
53+ } ) ;
54+
55+ const resp = await hnd . fetch ( { method : 'GET' } , { } ) ;
56+ assert . strictEqual ( resp . status , 403 ) ;
57+ } ) ;
58+
4259 it ( 'should return 403 when logged in but not authorized' , async ( ) => {
4360 const hnd = await esmock ( '../src/index.js' , {
4461 '../src/utils/daCtx.js' : {
@@ -51,7 +68,13 @@ describe('fetch', () => {
5168 } ) ;
5269
5370 it ( 'return 404 for unknown get route' , async ( ) => {
54- const resp = await handler . fetch ( { method : 'GET' , url : 'http://www.example.com/' } , { } ) ;
71+ const hnd = await esmock ( '../src/index.js' , {
72+ '../src/utils/daCtx.js' : {
73+ default :
async ( ) => ( { authorized :
true , users :
[ { email :
'[email protected] ' } ] , path :
'/' } ) , 74+ } ,
75+ } ) ;
76+
77+ const resp = await hnd . fetch ( { method : 'GET' , url : 'http://www.example.com/' } , { } ) ;
5578 assert . strictEqual ( resp . status , 404 ) ;
5679 } ) ;
5780
@@ -70,8 +93,32 @@ describe('fetch', () => {
7093} ) ;
7194
7295describe ( 'invalid routes' , ( ) => {
96+ let hnd ;
97+
98+ before ( async ( ) => {
99+ hnd = await esmock ( '../src/index.js' , {
100+ '../src/utils/daCtx.js' : {
101+ default : async ( req ) => {
102+ const { pathname } = new URL ( req . url ) ;
103+ // For invalid paths, throw the error that getDaCtx would throw
104+ if ( pathname . includes ( '//' ) ) {
105+ throw new Error ( 'Invalid path' ) ;
106+ }
107+ return {
108+ authorized : true ,
109+ users :
[ { email :
'[email protected] ' } ] , 110+ path : pathname ,
111+ api : 'source' ,
112+ org : 'owner' ,
113+ key : 'repo/path/file.html' ,
114+ } ;
115+ } ,
116+ } ,
117+ } ) ;
118+ } ) ;
119+
73120 const fetchStatus = async ( path , method ) => {
74- const resp = await handler . fetch ( { method, url : `http://www.sample.com${ path } ` } , { } ) ;
121+ const resp = await hnd . fetch ( { method, url : `http://www.sample.com${ path } ` } , { } ) ;
75122 return resp . status ;
76123 } ;
77124
0 commit comments