@@ -7,7 +7,7 @@ import * as utils from '../utils/s3'
77dotenv . config ( { path : '.env.test' } )
88const { anonKey } = getConfig ( )
99
10- let mockDeleteObjects : any
10+ let mockDeleteObjects : any , mockGetObject : any
1111
1212beforeAll ( ( ) => {
1313 mockDeleteObjects = jest . spyOn ( utils , 'deleteObjects' )
@@ -18,6 +18,20 @@ beforeAll(() => {
1818 } ,
1919 } )
2020 )
21+ mockGetObject = jest . spyOn ( utils , 'getObject' )
22+ mockGetObject . mockImplementation ( ( ) =>
23+ Promise . resolve ( {
24+ $metadata : {
25+ httpStatusCode : 200 ,
26+ } ,
27+ CacheControl : undefined ,
28+ ContentDisposition : undefined ,
29+ ContentEncoding : undefined ,
30+ ContentLength : 3746 ,
31+ ContentType : 'image/png' ,
32+ Metadata : { } ,
33+ } )
34+ )
2135} )
2236
2337beforeEach ( ( ) => {
@@ -89,7 +103,7 @@ describe('testing GET all buckets', () => {
89103 } )
90104 expect ( response . statusCode ) . toBe ( 200 )
91105 const responseJSON = JSON . parse ( response . body )
92- expect ( responseJSON . length ) . toBe ( 4 )
106+ expect ( responseJSON . length ) . toBe ( 5 )
93107 } )
94108
95109 test ( 'checking RLS: anon user is not able to get all buckets' , async ( ) => {
@@ -173,6 +187,93 @@ describe('testing POST bucket', () => {
173187 } )
174188} )
175189
190+ /*
191+ * PUT /bucket
192+ */
193+ describe ( 'testing public bucket functionality' , ( ) => {
194+ test ( 'user is able to make a bucket public and private' , async ( ) => {
195+ const bucketId = 'public-bucket'
196+ const makePublicResponse = await app ( ) . inject ( {
197+ method : 'PUT' ,
198+ url : `/bucket/${ bucketId } ` ,
199+ headers : {
200+ authorization : `Bearer ${ process . env . AUTHENTICATED_KEY } ` ,
201+ } ,
202+ payload : {
203+ public : true ,
204+ } ,
205+ } )
206+ expect ( makePublicResponse . statusCode ) . toBe ( 200 )
207+ const makePublicJSON = JSON . parse ( makePublicResponse . body )
208+ expect ( makePublicJSON . message ) . toBe ( 'Successfully updated' )
209+
210+ const publicResponse = await app ( ) . inject ( {
211+ method : 'GET' ,
212+ url : `/object/public/public-bucket/favicon.ico` ,
213+ } )
214+ expect ( publicResponse . statusCode ) . toBe ( 200 )
215+
216+ const makePrivateResponse = await app ( ) . inject ( {
217+ method : 'PUT' ,
218+ url : `/bucket/${ bucketId } ` ,
219+ headers : {
220+ authorization : `Bearer ${ process . env . AUTHENTICATED_KEY } ` ,
221+ } ,
222+ payload : {
223+ public : false ,
224+ } ,
225+ } )
226+ expect ( makePrivateResponse . statusCode ) . toBe ( 200 )
227+ const makePrivateJSON = JSON . parse ( makePrivateResponse . body )
228+ expect ( makePrivateJSON . message ) . toBe ( 'Successfully updated' )
229+
230+ const privateResponse = await app ( ) . inject ( {
231+ method : 'GET' ,
232+ url : `/object/public/public-bucket/favicon.ico` ,
233+ } )
234+ expect ( privateResponse . statusCode ) . toBe ( 400 )
235+ } )
236+
237+ test ( 'checking RLS: anon user is not able to update a bucket' , async ( ) => {
238+ const bucketId = 'public-bucket'
239+ const response = await app ( ) . inject ( {
240+ method : 'PUT' ,
241+ url : `/bucket/${ bucketId } ` ,
242+ headers : {
243+ authorization : `Bearer ${ anonKey } ` ,
244+ } ,
245+ payload : {
246+ public : true ,
247+ } ,
248+ } )
249+ expect ( response . statusCode ) . toBe ( 400 )
250+ } )
251+
252+ test ( 'user is not able to update a bucket without a auth header' , async ( ) => {
253+ const bucketId = 'public-bucket'
254+ const response = await app ( ) . inject ( {
255+ method : 'PUT' ,
256+ url : `/bucket/${ bucketId } ` ,
257+ payload : {
258+ public : true ,
259+ } ,
260+ } )
261+ expect ( response . statusCode ) . toBe ( 400 )
262+ } )
263+
264+ test ( 'user is not able to update a non-existent bucket' , async ( ) => {
265+ const bucketId = 'notfound'
266+ const response = await app ( ) . inject ( {
267+ method : 'PUT' ,
268+ url : `/bucket/${ bucketId } ` ,
269+ payload : {
270+ public : true ,
271+ } ,
272+ } )
273+ expect ( response . statusCode ) . toBe ( 400 )
274+ } )
275+ } )
276+
176277describe ( 'testing DELETE bucket' , ( ) => {
177278 test ( 'user is able to delete a bucket' , async ( ) => {
178279 const bucketId = 'bucket4'
0 commit comments