Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
integration tests for services (#72)
Browse files Browse the repository at this point in the history
KILTprotocol/ticket#617
adds integration tests that perform various calls to all service endpoints, using in-memory db for testing
- availability tests for all endpoints
- integration tests for all available requests against 
    - messaging service
    - ctype registry
    - faucet service
    - contact repository
  • Loading branch information
rflechtner authored Sep 2, 2020
1 parent 8bdaa55 commit 48d2057
Show file tree
Hide file tree
Showing 16 changed files with 1,675 additions and 27 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
yarn install --frozen-lockfile
yarn lint
yarn test
yarn test:e2e --forceExit
buildECR:
needs: test

Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
build:

runs-on: ubuntu-latest
timeout-minutes: 15

strategy:
matrix:
Expand All @@ -38,3 +39,23 @@ jobs:
env:
CI: true
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

integration-tests:

runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v1
- name: Use Node.js 10
uses: actions/setup-node@v1
with:
node-version: 10
registry-url: https://npm.pkg.github.com
scope: '@kiltprotocol'
- name: yarn install, lint, test and build
run: |
yarn upgrade --scope @kiltprotocol --latest
yarn test:e2e --forceExit
env:
CI: true
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions environment/test.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BOOT_NODE_ADDRESS=ws://127.0.0.1:9944
MONGODB_HOST=localhost
MONGODB_USER=''
MONGODB_PASS=''
SECRET=thisIsASecret
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@types/node": "^10.7.1",
"@types/supertest": "^2.0.5",
"jest": "^23.5.0",
"mongodb-memory-server": "^6.6.3",
"nodemon": "^1.18.3",
"prettier": "^1.14.2",
"supertest": "^3.1.0",
Expand Down
1 change: 0 additions & 1 deletion src/config/config.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as dotenv from 'dotenv'
import * as fs from 'fs'
import { Injectable } from '@nestjs/common'

export class ConfigService {
private readonly envConfig: { [key: string]: string }
Expand Down
2 changes: 1 addition & 1 deletion src/contacts/contacts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class ContactsController {
throw new BadRequestException('bad signature for hash')
}
}
this.contactService.add(contact)
await this.contactService.add(contact)
}

@Get()
Expand Down
2 changes: 2 additions & 0 deletions src/faucet/interfaces/faucet.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ export declare interface FaucetService {
): Promise<FaucetDrop>

updateOnTransactionFailure(drop: FaucetDrop): Promise<void>

reset(): Promise<void>
}
4 changes: 4 additions & 0 deletions src/faucet/mongodb-faucet.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,8 @@ export class MongoDbFaucetService implements FaucetService {
const updatedFaucetDrop = new this.faucetDropDBModel(drop)
await updatedFaucetDrop.save()
}

public async reset(): Promise<void> {
await this.faucetDropDBModel.deleteMany({}).exec()
}
}
23 changes: 23 additions & 0 deletions test/MockMongooseModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { MongooseModule } from '@nestjs/mongoose'
import { ConfigModule } from '../src/config/config.module'
import { MongoMemoryServer } from 'mongodb-memory-server'
import { ConfigService } from '../src/config/config.service'

export const mongodbInstance = new MongoMemoryServer()

/**
* A MongooseModule that creates a new ephemeral in-memory db for testing purposes.
* This module should be used instead of MyMongooseModule to allow concurrent testing -
* multiple independent in-memory db's can run at the same time.
*/
export const MockMongooseModule = MongooseModule.forRootAsync({
imports: [ConfigModule],
useFactory: async () => {
await mongodbInstance.ensureInstance()
const uri = await mongodbInstance.getUri()
return {
uri,
}
},
inject: [ConfigService],
})
88 changes: 73 additions & 15 deletions test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,82 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import { INestApplication } from '@nestjs/common'
import { Test } from '@nestjs/testing'
import request from 'supertest'
import { Identity } from '@kiltprotocol/sdk-js'
import { MockMongooseModule, mongodbInstance } from './MockMongooseModule'
import { AppModule } from '../src/app.module'

describe('AppController (e2e)', () => {
let app: INestApplication;
jest.mock(
'@kiltprotocol/sdk-js/build/blockchainApiConnection/BlockchainApiConnection'
)
jest.mock('../src/mongoose/mongoose.module', () => ({
MyMongooseModule: MockMongooseModule,
}))

describe('AppController availability (e2e)', () => {
let app: INestApplication

beforeAll(async () => {
const moduleFixture = await Test.createTestingModule({
imports: [AppModule],
}).compile();
}).compile()

app = moduleFixture.createNestApplication();
await app.init();
});
app = moduleFixture.createNestApplication()
await app.init()
}, 30000)

it('/ (GET)', () => {
it('root responds with 404 (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!');
});
});
.expect(404)
})

it('ctypes endpoint available (GET)', () => {
return request(app.getHttpServer())
.get('/ctype')
.expect(200, [])
})

it('contacts endpoint available (GET)', () => {
return request(app.getHttpServer())
.get('/contacts')
.expect(200, [])
})

it('outgoing messages enpoint available (POST)', async () => {
return request(app.getHttpServer())
.post(`/messaging`)
.send({})
.expect(400)
})

it('message inbox endpoint available (GET)', async () => {
const idAlice = await Identity.buildFromURI('//Alice')
return request(app.getHttpServer())
.get(`/messaging/inbox/${idAlice.address}`)
.expect(200, [])
})

it('message outbox endpoint available (GET)', async () => {
const idAlice = await Identity.buildFromURI('//Alice')
return request(app.getHttpServer())
.get(`/messaging/sent/${idAlice.address}`)
.expect(200, [])
})

it('health enpoint available (GET)', () => {
return request(app.getHttpServer())
.get('/health')
.expect(200, { status: 'ok', info: {} })
})

it('faucet endpoint available (POST)', async () => {
return request(app.getHttpServer())
.post(`/faucet/drop`)
.send({})
.expect(400)
})

afterAll(async () => {
await Promise.all([app.close(), mongodbInstance.stop()])
})
})
Loading

0 comments on commit 48d2057

Please sign in to comment.