Skip to content

Commit

Permalink
RLS policy tests (#306)
Browse files Browse the repository at this point in the history
chore: add rls policy tests
  • Loading branch information
fenos authored May 18, 2023
1 parent 5d425d5 commit 73346e0
Show file tree
Hide file tree
Showing 11 changed files with 821 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
ignorePatterns: ['src/test/assets/**', 'src/test/db/**'],
ignorePatterns: ['src/test/assets/**', 'src/test/db/**', 'src/test/*.yaml'],
parser: '@typescript-eslint/parser',
extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'],
parserOptions: {
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
preset: 'ts-jest',
testSequencer: './jest.sequencer.js',
transform: {
'^.+\\.(t|j)sx?$': 'ts-jest',
},
Expand Down
27 changes: 27 additions & 0 deletions jest.sequencer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const Sequencer = require('@jest/test-sequencer').default

// https://stackoverflow.com/a/68009048

const isRLSTest = (test) => {
return test.path.includes('rls')
}

const isTusTest = (test) => {
return test.path.includes('tus')
}

class CustomSequencer extends Sequencer {
sort(tests) {
const copyTests = Array.from(tests)
const normalTests = copyTests.filter((t) => !isRLSTest(t) && !isTusTest(t))
const tusTests = copyTests.filter((t) => isTusTest(t))
const rlsTests = copyTests.filter((t) => isRLSTest(t))
return super
.sort(normalTests)
.concat(tusTests)
.concat(rlsTests.sort((a, b) => (a.path > b.path ? 1 : -1)))
}
}

module.exports = CustomSequencer
43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
"migration:run": "ts-node-dev ./src/scripts/migrate-call.ts",
"docs:export": "ts-node-dev ./src/scripts/export-docs.ts",
"test:dummy-data": "ts-node-dev -r dotenv/config ./src/test/db/import-dummy-data.ts",
"test": "npm run infra:restart && npm run test:dummy-data && jest --runInBand",
"test:coverage": "npm run infra:restart && npm run test:dummy-data && jest --runInBand --coverage",
"test": "npm run infra:restart && npm run test:dummy-data && jest --runInBand --forceExit",
"test:coverage": "npm run infra:restart && npm run test:dummy-data && jest --runInBand --coverage --forceExit",
"prettier:check": "prettier -c src/**",
"format": "prettier -c --write src/**",
"eslint:check": "eslint 'src/**'",
"infra:stop": "docker-compose --project-directory . -f src/test/db/docker-compose.yml down --remove-orphans",
"infra:start": "docker-compose --project-directory . -f src/test/db/docker-compose.yml up -d && sleep 5 && npm run migration:run",
Expand Down Expand Up @@ -66,8 +67,10 @@
"@types/crypto-js": "^4.1.1",
"@types/fs-extra": "^9.0.13",
"@types/jest": "^29.2.1",
"@types/js-yaml": "^4.0.5",
"@types/jsonwebtoken": "^8.5.8",
"@types/lru-cache": "^7.10.10",
"@types/mustache": "^4.2.2",
"@types/node": "^18.14.6",
"@types/pg": "^8.6.4",
"@typescript-eslint/eslint-plugin": "^5.12.1",
Expand All @@ -78,7 +81,9 @@
"eslint-plugin-prettier": "^4.0.0",
"form-data": "^4.0.0",
"jest": "^29.2.2",
"js-yaml": "^4.1.0",
"json-schema-to-ts": "^2.5.4",
"mustache": "^4.2.0",
"pino-pretty": "^8.1.0",
"prettier": "^2.5.1",
"ts-jest": "^29.0.3",
Expand Down
2 changes: 1 addition & 1 deletion src/storage/database/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export interface Database {
updateBucket(
bucketId: string,
fields: Pick<Bucket, 'public' | 'file_size_limit' | 'allowed_mime_types'>
): Promise<Bucket>
): Promise<void>

upsertObject(
data: Pick<Obj, 'name' | 'owner' | 'bucket_id' | 'metadata' | 'version'>
Expand Down
20 changes: 8 additions & 12 deletions src/storage/database/knex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,25 +206,21 @@ export class StorageKnexDB implements Database {
bucketId: string,
fields: Pick<Bucket, 'public' | 'file_size_limit' | 'allowed_mime_types'>
) {
const [bucket] = await this.runQuery('UpdateBucket', (knex) => {
return knex
.from<Bucket>('buckets')
.update({
public: fields.public,
file_size_limit: fields.file_size_limit,
allowed_mime_types: fields.allowed_mime_types,
})
.where('id', bucketId)
.returning('*')
const bucket = await this.runQuery('UpdateBucket', (knex) => {
return knex.from('buckets').where('id', bucketId).update({
public: fields.public,
file_size_limit: fields.file_size_limit,
allowed_mime_types: fields.allowed_mime_types,
})
})

if (!bucket) {
if (bucket === 0) {
throw new DBError('Bucket not found', 404, 'Bucket not found', undefined, {
bucketId,
})
}

return bucket
return
}

async upsertObject(data: Pick<Obj, 'name' | 'owner' | 'bucket_id' | 'metadata' | 'version'>) {
Expand Down
20 changes: 20 additions & 0 deletions src/test/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import app from '../admin-app'
import { S3Backend } from '../storage/backend'
import { Queue } from '../queue'
import { HeadBucketCommand, S3Client } from '@aws-sdk/client-s3'
import { isS3Error } from '../storage'

export const adminApp = app({})

Expand Down Expand Up @@ -78,3 +80,21 @@ export function useMockObject() {
jest.clearAllMocks()
})
}

export const checkBucketExists = async (client: S3Client, bucket: string) => {
const options = {
Bucket: bucket,
}

try {
await client.send(new HeadBucketCommand(options))
return true
} catch (error) {
const err = error as Error

if (err && isS3Error(err) && err.$metadata.httpStatusCode === 404) {
return false
}
throw error
}
}
Loading

0 comments on commit 73346e0

Please sign in to comment.