Skip to content
This repository was archived by the owner on Dec 28, 2020. It is now read-only.

Add API integration tests #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ before_script:
script:
- make build

after_script:
- yarn test-integration

before_deploy:
- if [ ! -d "$HOME/google-cloud-sdk/bin" ]; then rm -rf $HOME/google-cloud-sdk; export CLOUDSDK_CORE_DISABLE_PROMPTS=1; curl https://sdk.cloud.google.com | bash >/dev/null; fi
- source /home/travis/google-cloud-sdk/path.bash.inc
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"private": true,
"scripts": {
"start": "node index.js",
"test": "jest",
"test": "jest --testPathIgnorePatterns tests/integration",
"test-integration": "jest --testPathPattern tests/integration",
"lint": "eslint -c .eslintrc.js ./*.js"
},
"jest": {
Expand All @@ -20,7 +21,7 @@
"*.js"
],
"collectCoverage": false,
"testRegex": ".*test.js$"
"testRegex": "[a-zA-Z]*\\.test\\.js$"
},
"dependencies": {
"email-addresses": "^3.0.3",
Expand All @@ -32,12 +33,15 @@
"winston": "^3.0.0"
},
"devDependencies": {
"node-docker-api": "^1.1.22",
"eslint": "^5.9.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"jest": "^24.8.0"
"jest": "^24.8.0",
"request": "^2.88.0",
"supertest": "^4.0.2"
}
}
106 changes: 106 additions & 0 deletions tests/integration/docker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const request = require('request')
const { Docker } = require('node-docker-api')

const docker = new Docker({ socketPath: '/var/run/docker.sock' })

let container = null
let logstream = null

function create() {
return docker.container.create({
Image: 'multipl/mailgun:latest',
Env: [
"PROGRAM_ALIAS=mailgun_integration_test",
"PORT=3000",
"LOG_FILE=false",
"LOG_LEVEL=debug",
"MAILGUN_API_KEY=api_key",
"MAILGUN_DOMAIN=mail.multipl.io",

"http_proxy=http://127.0.0.1:3002",
"https_proxy=http://127.0.0.1:3002",
],
HostConfig: {
PortBindings: {
"3000/tcp": [{
"HostPort": "3000",
}],
},
NetworkMode: "host",
},
})
.then(c => {
container = c
console.log('Container created')
return c.start()
})
.then((c) => c.logs({
follow: true,
stdout: true,
stderr: true,
}))
.then(stream => {
logstream = stream
stream.on('data', info => console.log(info.toString()))
stream.on('error', err => console.log(err.toString()))
})
.then(() => {
console.log('Container started')
return ready("/ready")
})
.catch(error => console.log(error))
}

function ready(path) {
const timeout = 1000
let tries = 100

return new Promise((resolve, reject) => {
const check = () => {
console.log("Ready check")

if (tries-- <= 0) {
reject("Max readiness tries reached.")
}

request
.get("http://127.0.0.1:3000" + path)
.on('response', function(response) {
if (response.statusCode == 200) {
console.log("Container ready")
resolve(response)
}
else {
setTimeout(check, timeout)
}
})
.on('error', (error) => {
setTimeout(check, timeout)
})
}

setTimeout(check, timeout)
})
}

function kill() {
// shutdown log stream
logstream.destroy()

// kill container
if (container != null) {
return container.stop()
.then(c => c.delete())
.catch((error) => console.log(error))
}
else {
return Promise.resolve(true)
}
}

module.exports = {
create,
ready,
kill,
}

62 changes: 62 additions & 0 deletions tests/integration/integration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* eslint-disable */

const PORT = 3000
const ADDRESS = `localhost:${PORT}`

const request = require('supertest')(ADDRESS)

const { create, ready, kill } = require('./docker')

const express = require('express')
const app = express()

describe('api', () => {
let mailgunServer = null

beforeAll(() => {
// setup mailgun-mock server
app.all('*', (req, res) => {
console.log('mock-mailgun : ' + req.url)
res.send('OK')
})
mailgunServer = app.listen(3002, () => console.log(`Mock mailgun app listening on port 3002`))

// create the container
return create()
}, 60 * 1000)

it('readiness probe route responds positivelly', () => {
return request.get('/ready')
.then(response => {
expect(response.statusCode).toBe(200)
})
})

it('subscribes an email', () => {
const email = "[email protected]"

return request.get('/subscribe/' + email)
.then(response => {
expect(response.statusCode).toBe(200)
})
})

it('onboard a new user', () => {
const email = "[email protected]"
const name = "user"

return request.get('/onboard/' + email + '/' + name)
.then(response => {
expect(response.statusCode).toBe(200)
})
})

afterAll(() => {
// shutdown mock-mailgun
mailgunServer.close()

// kill container
kill()
})
})

Loading