Skip to content

Instantly share code, notes, and snippets.

@weihanchen
Created November 19, 2016 00:17
Show Gist options
  • Save weihanchen/b0b0f7c88761be1c53720c48e778b786 to your computer and use it in GitHub Desktop.
Save weihanchen/b0b0f7c88761be1c53720c48e778b786 to your computer and use it in GitHub Desktop.
Nodejs API Test
describe('delete user...', () => {
let resourceUrl;
before(function() {
resourceUrl = usersUrl + '/' + userid;
})
it('should response success when deleting user exist', function(done) {
request.delete(resourceUrl)
.expect(200)
.end(function(err, res) {
res.body.should.have.property('success');
res.body.success.should.equal(true);
done(err);
})
})
it('should response not found when user not exist', function(done) {
request.delete(resourceUrl)
.expect(404)
.end(function(err, res) {
res.body.message.should.equal('resource not found');
done(err);
})
})
})
describe('initialize...', () => {
it('should response status 200 when initialize', function(done) {
this.timeout(10000); //set timeout limit
request.post(initializeUrl)
.set('Content-Type', 'application/json')
.expect(200)
.end(function(err, res) {
done(err);
})
})
})
describe('signup...', () => {
it('should response status 200 when signup', function(done) {
this.timeout(10000); //set timeout limit
request.post(usersUrl)
.set('Content-Type', 'application/json')
.send({
username: username,
displayName: displayName,
password: password,
})
.expect(200)
.end(function(err, res) {
done(err);
})
})
})
describe('login...', () => {
let token;
let userid;
before((done) => { //login and save token
request.post(loginUrl)
.set('Content-Type', 'application/json')
.send({
username: username,
password: password
})
.expect(200)
.end(function(err, res) {
token = res.body.token;
userid = res.body.uid;
done(err);
})
})
}
{
"name": "user_authentication_api",
"description": "api server",
"node-main": "./run.js",
"dependencies": {
"bcrypt-nodejs": "^0.0.3",
"body-parser": "^1.15.2",
"express": "^4.14.0",
"jwt-simple": "^0.5.0",
"morgan": "^1.7.0",
"moment": "^2.14.1",
"mongoose": "^4.6.1",
"passport": "^0.3.2",
"passport-jwt": "^2.1.0"
},
"devDependencies": {
"mocha": "^3.0.2",
"nodemon": "^1.9.1",
"supertest": "^2.0.0",
"should": "^11.1.0"
},
"scripts": {
"dev": "node dev.js",
"test": "./node_modules/.bin/mocha test",
"product": "node run.js"
},
"author": "will.chen",
"license": "ISC"
}
/**
* supertest是測試框架中用來模擬http行為的套件
* should是一種表述性、可讀性很強的斷言庫,可擴充Object的property檢查回傳內容,屬於BDD風格
*/
let supertest = require('supertest');
let moment = require('moment');
let should = require('should');
global.__testBase = __dirname + '/';
let app = require('../run');
let userTest = require(__testBase + 'routes/users');
describe('API Testing', () => {
userTest(app,'test','test','test');
})
/**
* supertest是測試框架中用來模擬http行為的套件
* should是一種表述性、可讀性很強的斷言庫,可擴充Object的property檢查回傳內容,屬於BDD風格
*/
let supertest = require('supertest');
let should = require('should');
module.exports = (app, username, displayName, password) => {
let request = supertest(app);
let initializeUrl = '/api/initialize';
let usersUrl = '/api/users';
let loginUrl = '/api/users/login';
let meUrl = '/api/users/me';
describe('initialize...', () => {
it('should response status 200 when initialize', function(done) {
this.timeout(10000); //set timeout limit
request.post(initializeUrl)
.set('Content-Type', 'application/json')
.expect(200)
.end(function(err, res) {
done(err);
})
})
})
describe('signup...', () => {
it('should response status 200 when signup', function(done) {
this.timeout(10000); //set timeout limit
request.post(usersUrl)
.set('Content-Type', 'application/json')
.send({
username: username,
displayName: displayName,
password: password,
})
.expect(200)
.end(function(err, res) {
done(err);
})
})
})
describe('login...', () => {
let token;
let userid;
before((done) => { //login and save token
request.post(loginUrl)
.set('Content-Type', 'application/json')
.send({
username: username,
password: password
})
.expect(200)
.end(function(err, res) {
token = res.body.token;
userid = res.body.uid;
done(err);
})
})
describe('get user info...', () => {
it('should response status 401 when not send with token', function(done) {
request.get(meUrl)
.expect(401)
.end(function(err, res) {
done(err);
})
})
it('should response status 200 and contains username、displayName when header has jwt token', function(done) {
request.get(meUrl)
.set('Authorization', token)
.expect(200)
.end(function(err, res) {
res.body.should.have.property('username');
res.body.should.have.property('displayName');
done(err);
})
})
})
describe('delete user...', () => {
let resourceUrl;
before(function() {
resourceUrl = usersUrl + '/' + userid;
})
it('should response success when deleting user exist', function(done) {
request.delete(resourceUrl)
.expect(200)
.end(function(err, res) {
res.body.should.have.property('success');
res.body.success.should.equal(true);
done(err);
})
})
it('should response not found when user not exist', function(done) {
request.delete(resourceUrl)
.expect(404)
.end(function(err, res) {
res.body.message.should.equal('resource not found');
done(err);
})
})
})
})
}
describe('get user info...', () => {
it('should response status 401 when not send with token', function(done) {
request.get(meUrl)
.expect(401)
.end(function(err, res) {
done(err);
})
})
it('should response status 200 and contains username、displayName when header has jwt token', function(done) {
request.get(meUrl)
.set('Authorization', token)
.expect(200)
.end(function(err, res) {
res.body.should.have.property('username');
res.body.should.have.property('displayName');
done(err);
})
})
})
let request = supertest(app);
let initializeUrl = '/api/initialize';
let usersUrl = '/api/users';
let loginUrl = '/api/users/login';
let meUrl = '/api/users/me';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment