Skip to content

Commit 408dacf

Browse files
committed
tests for public buckets
1 parent a33424e commit 408dacf

4 files changed

Lines changed: 114 additions & 5 deletions

File tree

src/routes/object/getPublicObject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface getObjectRequestInterface {
2222

2323
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
2424
export default async function routes(fastify: FastifyInstance) {
25-
const summary = 'Retrieve an object'
25+
const summary = 'Retrieve a public object'
2626
fastify.get<getObjectRequestInterface>(
2727
'/public/:bucketName/*',
2828
{

src/test/bucket.test.ts

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as utils from '../utils/s3'
77
dotenv.config({ path: '.env.test' })
88
const { anonKey } = getConfig()
99

10-
let mockDeleteObjects: any
10+
let mockDeleteObjects: any, mockGetObject: any
1111

1212
beforeAll(() => {
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

2337
beforeEach(() => {
@@ -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+
176277
describe('testing DELETE bucket', () => {
177278
test('user is able to delete a bucket', async () => {
178279
const bucketId = 'bucket4'

src/test/db/03-dummy-data.sql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ INSERT INTO "storage"."buckets" ("id", "name", "owner", "created_at", "updated_a
99
('bucket2', 'bucket2', '4d56e902-f0a0-4662-8448-a4d9e643c142', '2021-02-17 04:43:32.770206+00', '2021-02-17 04:43:32.770206+00'),
1010
('bucket3', 'bucket3', '4d56e902-f0a0-4662-8448-a4d9e643c142', '2021-02-17 04:43:32.770206+00', '2021-02-17 04:43:32.770206+00'),
1111
('bucket4', 'bucket4', '317eadce-631a-4429-a0bb-f19a7a517b4a', '2021-02-25 09:23:01.58385+00', '2021-02-25 09:23:01.58385+00'),
12-
('bucket5', 'bucket5', '317eadce-631a-4429-a0bb-f19a7a517b4a', '2021-02-27 03:04:25.6386+00', '2021-02-27 03:04:25.6386+00');
12+
('bucket5', 'bucket5', '317eadce-631a-4429-a0bb-f19a7a517b4a', '2021-02-27 03:04:25.6386+00', '2021-02-27 03:04:25.6386+00'),
13+
('public-bucket', 'public-bucket', '317eadce-631a-4429-a0bb-f19a7a517b4a', '2021-02-27 03:04:25.6386+00', '2021-02-27 03:04:25.6386+00');
1314

1415

1516
-- insert objects
@@ -33,7 +34,9 @@ INSERT INTO "storage"."objects" ("id", "bucket_id", "name", "owner", "created_at
3334
('8377527d-3518-4dc8-8290-c6926470e795', 'bucket2', 'folder/subfolder/public-all-permissions.png', 'd8c7bce9-cfeb-497b-bd61-e66ce2cbdaa2', '2021-02-17 10:26:42.791214+00', '2021-02-17 11:03:30.025116+00', '2021-02-17 10:26:42.791214+00', '{"size": 1234}'),
3435
('b39ae4ab-802b-4c42-9271-3f908c34363c', 'bucket2', 'private/sadcat-upload3.png', '317eadce-631a-4429-a0bb-f19a7a517b4a', '2021-03-01 08:53:29.567975+00', '2021-03-01 08:53:29.567975+00', '2021-03-01 08:53:29.567975+00', '{"mimetype": "image/svg+xml", "size": 1234}'),
3536
('8098E1AC-C744-4368-86DF-71B60CCDE221', 'bucket3', 'sadcat-upload3.png', '317eadce-631a-4429-a0bb-f19a7a517b4a', '2021-03-01 08:53:29.567975+00', '2021-03-01 08:53:29.567975+00', '2021-03-01 08:53:29.567975+00', '{"mimetype": "image/svg+xml", "size": 1234}'),
36-
('D3EB488E-94F4-46CD-86D3-242C13B95BAC', 'bucket3', 'sadcat-upload2.png', '317eadce-631a-4429-a0bb-f19a7a517b4a', '2021-03-01 08:53:29.567975+00', '2021-03-01 08:53:29.567975+00', '2021-03-01 08:53:29.567975+00', '{"mimetype": "image/svg+xml", "size": 1234}');
37+
('D3EB488E-94F4-46CD-86D3-242C13B95BAC', 'bucket3', 'sadcat-upload2.png', '317eadce-631a-4429-a0bb-f19a7a517b4a', '2021-03-01 08:53:29.567975+00', '2021-03-01 08:53:29.567975+00', '2021-03-01 08:53:29.567975+00', '{"mimetype": "image/svg+xml", "size": 1234}'),
38+
('746180e8-8029-4134-8a21-48ab35485d81', 'public-bucket', 'favicon.ico', '317eadce-631a-4429-a0bb-f19a7a517b4a', '2021-03-01 08:53:29.567975+00', '2021-03-01 08:53:29.567975+00', '2021-03-01 08:53:29.567975+00', '{"mimetype": "image/svg+xml", "size": 1234}');
39+
;
3740

3841
-- add policies
3942
-- allows user to CRUD all buckets

src/test/object.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,11 @@ describe('testing generating signed URL', () => {
659659
})
660660
})
661661

662+
/**
663+
* GET /public/
664+
*/
665+
// these tests are written in bucket.test.ts since its easier
666+
662667
/**
663668
* GET /sign/
664669
*/

0 commit comments

Comments
 (0)