Skip to content

Commit

Permalink
refactor(e2e): split tests into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-wayner committed Jan 8, 2024
1 parent a3ac93f commit b766a32
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 136 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"make-docs": "./node_modules/.bin/jsdoc index.js lib -r -R README.md -u ./doc -c .jsdoc.json --verbose -d ./jsdoc",
"publish-docs": "gh-pages -d jsdoc --repo git@github.com:internxt/bridge.git",
"build": "tsc",
"test": "jest --testPathIgnorePatterns ./tests/lib/mongo",
"test": "jest --testPathIgnorePatterns ./tests/lib/mongo ./tests/lib/e2e",
"test:cov": "jest --coverage",
"test:e2e": "NODE_ENV=test jest --config ./tests/lib/e2e/jest-e2e.json --setupFiles dotenv/config",
"test-mongo-init": "ts-node ./tests/lib/mongo/init",
Expand Down
115 changes: 115 additions & 0 deletions tests/lib/e2e/buckets/buckets.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { createTestUser, deleteTestUser, getAuth, testUser, } from '../utils'

import { engine, testServer } from '../setup'



describe('Bridge E2E Tests', () => {

beforeAll(async () => {
await engine.storage.models.Bucket.deleteMany({})
})

afterAll(async () => {
await engine.storage.models.Bucket.deleteMany({})
})

beforeEach(() => {
jest.clearAllMocks()
})

describe('Buckets Management', () => {

beforeAll(async () => {
await createTestUser(engine.storage)
})

afterAll(async () => {
await deleteTestUser(engine.storage)
})

describe('Bucket creation v1', () => {

it('should create a bucket with name and pubkeys', async () => {

// Act
const response = await testServer
.post('/buckets')
.set('Authorization', getAuth(testUser))
.send({
pubkeys: ['031a259ee122414f57a63bbd6887ee17960e9106b0adcf89a298cdad2108adf4d9'],
name: 'test-bucket-name'
})

// Assert
expect(response.status).toBe(201)
expect(response.body).toHaveProperty('id')

const buckets = await engine.storage.models.Bucket.find({ _id: response.body.id })
expect(buckets).toHaveLength(1)

})
})


describe('Bucket update v1', () => {

it('should be able to update a bucket to empty pubkeys', async () => {
// Arrange
const { body: bucket } = await testServer
.post('/buckets')
.set('Authorization', getAuth(testUser))
.send({
pubkeys: ['031a259ee122414f57a63bbd6887ee17960e9106b0adcf89a298cdad2108adf4d9'],
name: 'test-bucket-name-1'
})
.expect(201);

// Act


const response = await testServer
.patch(`/buckets/${bucket.id}`)
.set('Authorization', getAuth(testUser))
.send({ pubkeys: [] })


// Assert
expect(response.status).toBe(200);

const dbBucket = await engine.storage.models.Bucket.findOne({ _id: response.body.id })
expect(dbBucket.toObject().pubkeys).toEqual([])

})
})

describe('Bucket deletion v1', () => {
it('should be able to delete a bucket', async () => {

// Arrange: Create a bucket
const { body: bucket } = await testServer
.post('/buckets')
.set('Authorization', getAuth(testUser))
.send({
pubkeys: ['031a259ee122414f57a63bbd6887ee17960e9106b0adcf89a298cdad2108adf4d9'],
name: 'test-bucket-name-2'
})
.expect(201);

// Act: Delete the bucket
const response = await testServer
.delete(`/buckets/${bucket.id}`)
.set('Authorization', getAuth(testUser))


// Assert
expect(response.status).toBe(204)
const buckets = await engine.storage.models.Bucket.findOne({ _id: bucket.id })
expect(buckets).toBeNull()

})
})
})

})

3 changes: 3 additions & 0 deletions tests/lib/e2e/global-teardown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default async () => {
process.emit('SIGINT')
}
1 change: 1 addition & 0 deletions tests/lib/e2e/jest-e2e.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"globalTeardown": "./global-teardown.ts",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/e2e/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import supertest from 'supertest';
import { checkConnection } from './utils';

if (process.env.inxtbridge_server__port !== '0') {
console.warn('Warning: inxtbridge_server__port is not set to 0, this may cause conflicts with the test server');
}
// Remove jest options from process.argv
process.argv = process.argv.slice(0, 2);
export const engine = require('../../../bin/storj-bridge.ts');

checkConnection(engine.storage);

export const testServer = supertest(engine.server.app);

1 change: 1 addition & 0 deletions tests/lib/e2e/users.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ const usersTest: MongoUserModel[] = [

export const users: MongoUserModel[] = usersTest;
export const userFixtures: User[] = usersTest.map(formatUser);
export const [testUser] = users
Loading

0 comments on commit b766a32

Please sign in to comment.