Skip to content

Commit 79283f8

Browse files
committed
fix: add rls tests for buckets
1 parent 0924609 commit 79283f8

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/routes/bucket/emptyBucket.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default async function routes(fastify: FastifyInstance) {
5252

5353
if (bucketResponse.error) {
5454
const { status, error } = bucketResponse
55-
return response.status(status).send(transformPostgrestError(error, status))
55+
return response.status(400).send(transformPostgrestError(error, status))
5656
}
5757
const { data: bucket } = bucketResponse
5858
const bucketName = bucket.name

src/test/bucket.test.ts

+64-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ describe('testing GET bucket', () => {
4343
expect(responseJSON.id).toBe(bucketId)
4444
})
4545

46+
test('checking RLS: anon user is not able to get bucket details', async () => {
47+
const bucketId = 'bucket2'
48+
const response = await app().inject({
49+
method: 'GET',
50+
url: `/bucket/${bucketId}`,
51+
headers: {
52+
authorization: `Bearer ${anonKey}`,
53+
},
54+
})
55+
expect(response.statusCode).toBe(400)
56+
})
57+
4658
test('user is not able to get bucket details without Auth header', async () => {
4759
const response = await app().inject({
4860
method: 'GET',
@@ -80,6 +92,19 @@ describe('testing GET all buckets', () => {
8092
expect(responseJSON.length).toBe(4)
8193
})
8294

95+
test('checking RLS: anon user is not able to get all buckets', async () => {
96+
const response = await app().inject({
97+
method: 'GET',
98+
url: `/bucket`,
99+
headers: {
100+
authorization: `Bearer ${anonKey}`,
101+
},
102+
})
103+
expect(response.statusCode).toBe(200)
104+
const responseJSON = JSON.parse(response.body)
105+
expect(responseJSON.length).toBe(0)
106+
})
107+
83108
test('user is not able to all buckets details without Auth header', async () => {
84109
const response = await app().inject({
85110
method: 'GET',
@@ -108,6 +133,20 @@ describe('testing POST bucket', () => {
108133
expect(responseJSON.name).toBe('newbucket')
109134
})
110135

136+
test('checking RLS: anon user is not able to create a bucket', async () => {
137+
const response = await app().inject({
138+
method: 'POST',
139+
url: `/bucket`,
140+
headers: {
141+
authorization: `Bearer ${anonKey}`,
142+
},
143+
payload: {
144+
name: 'newbucket1',
145+
},
146+
})
147+
expect(response.statusCode).toBe(400)
148+
})
149+
111150
test('user is not able to create a bucket without Auth header', async () => {
112151
const response = await app().inject({
113152
method: 'POST',
@@ -149,6 +188,18 @@ describe('testing DELETE bucket', () => {
149188
expect(responseJSON.message).toBe('Deleted')
150189
})
151190

191+
test('checking RLS: anon user is not able to delete a bucket', async () => {
192+
const bucketId = 'bucket5'
193+
const response = await app().inject({
194+
method: 'DELETE',
195+
url: `/bucket/${bucketId}`,
196+
headers: {
197+
authorization: `Bearer ${anonKey}`,
198+
},
199+
})
200+
expect(response.statusCode).toBe(400)
201+
})
202+
152203
test('user is not able to delete bucket without Auth header', async () => {
153204
const bucketId = 'bucket5'
154205
const response = await app().inject({
@@ -198,6 +249,18 @@ describe('testing EMPTY bucket', () => {
198249
expect(responseJSON.message).toBe('Emptied')
199250
})
200251

252+
test('user is able to delete a bucket', async () => {
253+
const bucketId = 'bucket3'
254+
const response = await app().inject({
255+
method: 'POST',
256+
url: `/bucket/${bucketId}/empty`,
257+
headers: {
258+
authorization: `Bearer ${anonKey}`,
259+
},
260+
})
261+
expect(response.statusCode).toBe(400)
262+
})
263+
201264
test('user is not able to empty a bucket without Auth Header', async () => {
202265
const bucketId = 'bucket3'
203266
const response = await app().inject({
@@ -216,7 +279,7 @@ describe('testing EMPTY bucket', () => {
216279
authorization: `Bearer ${process.env.AUTHENTICATED_KEY}`,
217280
},
218281
})
219-
expect(response.statusCode).toBe(406)
282+
expect(response.statusCode).toBe(400)
220283
})
221284

222285
test('user is able to empty an already empty bucket', async () => {

0 commit comments

Comments
 (0)