Skip to content

Commit

Permalink
chore: cleanup all test users
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-wayner committed Jan 11, 2024
1 parent 6d65a8c commit d4fe7e6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
29 changes: 14 additions & 15 deletions tests/lib/e2e/buckets/buckets.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dataGenerator } from './../users.fixtures'
import { createTestUser, deleteTestUser, getAuth, shutdownEngine, } from '../utils'
import { cleanUpTestUsers, createTestUser, getAuth, shutdownEngine, } from '../utils'
import { engine, testServer } from '../setup'
import { type User } from '../users.fixtures';

Expand All @@ -12,8 +12,8 @@ describe('Bridge E2E Tests', () => {
})

afterAll(async () => {
await deleteTestUser()
await shutdownEngine(engine)
await cleanUpTestUsers()
await shutdownEngine()
})

beforeEach(() => {
Expand All @@ -26,7 +26,7 @@ describe('Bridge E2E Tests', () => {

describe('Creating a bucket', () => {

it('When you want create the root bucket, it should work without any arguments', async () => {
it('When you want to create the root bucket, it should work without any arguments', async () => {

// Act
const response = await testServer
Expand All @@ -37,11 +37,11 @@ describe('Bridge E2E Tests', () => {
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)
const bucket = await engine.storage.models.Bucket.findOne({ _id: response.body.id })
expect(bucket).not.toBeNull()

})
it('When you want create a bucket with name and pubkeys, it should work with correctly formatted pubkeys', async () => {
it('When you want to create a bucket with name and pubkeys, it should work with correctly formatted pubkeys', async () => {

// Act
const response = await testServer
Expand All @@ -56,11 +56,11 @@ describe('Bridge E2E Tests', () => {
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)
const bucket = await engine.storage.models.Bucket.findOne({ _id: response.body.id })
expect(bucket).not.toBeNull()

})
it('When you want create a bucket with name and pubkeys, it should fail with incorrectly formatted pubkeys', async () => {
it('When you want to create a bucket with name and pubkeys, it should fail with incorrectly formatted pubkeys', async () => {

// Act
const response = await testServer
Expand All @@ -79,7 +79,7 @@ describe('Bridge E2E Tests', () => {

describe('Updating a bucket', () => {

it('Whe you want to update a bucket, it should work with empty pubkeys list', async () => {
it('When you want to update a bucket, it should work with empty pubkeys list', async () => {
// Arrange
const { body: bucket } = await testServer
.post('/buckets')
Expand All @@ -104,7 +104,7 @@ describe('Bridge E2E Tests', () => {
expect(dbBucket.toObject().pubkeys).toEqual([])

})
it('Whe you want to update a bucket, it should fail with invalid pubkeys list', async () => {
it('When you want to update a bucket, it should fail with invalid pubkeys list', async () => {
// Arrange
const { body: bucket } = await testServer
.post('/buckets')
Expand Down Expand Up @@ -160,6 +160,7 @@ describe('Bridge E2E Tests', () => {

// Arrange: Create a bucket
const owner = await createTestUser({ user: { email: dataGenerator.email(), password: dataGenerator.hash({ length: 64 }), maxSpaceBytes: 321312313 } })
const notTheOwner = testUser

const { body: bucket } = await testServer
.post('/buckets')
Expand All @@ -173,14 +174,12 @@ describe('Bridge E2E Tests', () => {
// Act: Delete the bucket
const response = await testServer
.delete(`/buckets/${bucket.id}`)
.set('Authorization', getAuth(testUser))
.set('Authorization', getAuth(notTheOwner))


// Assert
expect(response.status).toBe(404)

await deleteTestUser({ user: owner })

})
})
})
Expand Down
26 changes: 10 additions & 16 deletions tests/lib/e2e/users/users.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { engine, testServer } from '../setup'
import { dataGenerator } from '../users.fixtures'


// NB: Mock SendGrid
// Mock SendGrid
jest.mock('@sendgrid/mail', () => ({
setApiKey: jest.fn(),
send: jest.fn((_, __, done) => typeof done === 'function' ? done() : Promise.resolve()),
}))

// NB: Mock JWT verification
// Mock JWT verification
jest.mock('jsonwebtoken', () => ({ verify: jest.fn((_, __, ___, cb) => cb(null, {})) }))

describe('Bridge E2E Tests', () => {
Expand All @@ -21,7 +21,7 @@ describe('Bridge E2E Tests', () => {
})

afterAll(async() => {
await shutdownEngine(engine)
await shutdownEngine()
})

describe('Users Management', () => {
Expand All @@ -43,12 +43,10 @@ describe('Bridge E2E Tests', () => {

// Assert
expect(response.status).toBe(201);
const users = await engine.storage.models.User.find({ _id: response.body.id })
expect(users).toHaveLength(1)
const user = await engine.storage.models.User.findOne({ _id: response.body.id })
expect(user).not.toBeNull()

expect(users[0].toObject().activated).toBe(true)

// expect(dispatchSendGridMock).toHaveBeenCalled()
expect(user.toObject().activated).toBe(true)

})
it('When creating a user, it should fail if email is in use', async () => {
Expand Down Expand Up @@ -204,11 +202,11 @@ describe('Bridge E2E Tests', () => {

// Assert
expect(response.status).toBe(200);
// expect(response.status).toBe(201);
const users = await engine.storage.models.User.find({ _id: response.body.id })
expect(users).toHaveLength(1)

expect(users[0].toObject().activated).toBe(true)
const user = await engine.storage.models.User.findOne({ _id: response.body.id })
expect(user).not.toBeNull()

expect(user.toObject().activated).toBe(true)
})
it('When creating a user, it should fail if email is in use', async () => {

Expand Down Expand Up @@ -239,7 +237,6 @@ describe('Bridge E2E Tests', () => {
const { body: user } = await testServer
.post('/v2/users')
.send(payload)
// .expect(201)
.expect(200);


Expand Down Expand Up @@ -269,7 +266,6 @@ describe('Bridge E2E Tests', () => {
const { body: user } = await testServer
.post('/v2/users')
.send(payload)
// .expect(201)
.expect(200);


Expand Down Expand Up @@ -297,7 +293,6 @@ describe('Bridge E2E Tests', () => {
const { body: user } = await testServer
.post('/v2/users')
.send(payload)
// .expect(201)
.expect(200);


Expand Down Expand Up @@ -330,7 +325,6 @@ describe('Bridge E2E Tests', () => {
const { body: user } = await testServer
.post('/v2/users')
.send(payload)
// .expect(201)
.expect(200);


Expand Down
29 changes: 18 additions & 11 deletions tests/lib/e2e/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { TestUser, testUser, User } from './users.fixtures'

type Args = { storage?: any, user?: TestUser }

const createdUsers: User[] = []
export const createTestUser = async (args: Args = {}): Promise<User> => {
const { storage = engine.storage, user = testUser } = args

const payload = { email: user.email, password: user.password }
const createdUser: User = await new Promise(resolve => storage.models.User.create(payload, (err: Error, user: any) => {
if (err) throw err
resolve(user.toObject())
const createdUser: User = await new Promise((resolve, reject) => storage.models.User.create(payload, (err: Error, user: any) => {
err ? reject(err) : resolve(user.toObject())
}))

await storage.models.User.updateOne(
Expand All @@ -19,18 +19,25 @@ export const createTestUser = async (args: Args = {}): Promise<User> => {

createdUser.password = user.password

createdUsers.push(createdUser)

return createdUser
}

export const deleteTestUser = async (args: Args = { }): Promise<void> => {
export const cleanUpTestUsers = (): Promise<void> => {
return new Promise((resolve, reject) => {
engine.storage.models.User.deleteMany({ email: { $in: [createdUsers.map(user => user.email)] } }, (err: Error) => {
err ? reject(err) : resolve()
})
})

}

export const deleteTestUser = (args: Args = {}): Promise<void> => {
const { storage = engine.storage, user = testUser } = args
return await new Promise(resolve => storage.models.User.deleteOne({
email: user.email,
}, (err: Error) => {
if (err) throw err
resolve()
return new Promise((resolve, reject) => storage.models.User.deleteOne({ email: user.email, }, (err: Error) => {
err ? reject(err) : resolve()
}))

}

export const getAuth = (user: Omit<TestUser, 'maxSpaceBytes'> = testUser) => {
Expand All @@ -39,7 +46,7 @@ export const getAuth = (user: Omit<TestUser, 'maxSpaceBytes'> = testUser) => {
}


export const shutdownEngine = async (engine: any) => {
export const shutdownEngine = async () => {

await Promise.all([
engine.storage.connection.close(),
Expand Down

0 comments on commit d4fe7e6

Please sign in to comment.