Created
October 2, 2015 04:44
-
-
Save AlanJui/5a578c84b647fb2bf8cc to your computer and use it in GitHub Desktop.
API 測試,使用 NPM Modules: supertest, should
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Created by AlanJui on 15/10/1. | |
| */ | |
| var should = require('should'); | |
| var request = require('supertest'); | |
| var mongodb = require('mongodb'); | |
| var MongoClient = mongodb.MongoClient; | |
| var ObjectID = mongodb.ObjectID; | |
| var tools = require('../libs/tools'); | |
| var model = require('../modules/user/model')(); | |
| describe('User API Test', function () { | |
| var url = 'http://localhost:3000'; | |
| var id; | |
| describe('POST /api/users', function () { | |
| it('should return error typing password not consistent', function (done) { | |
| var user = { | |
| name: 'TigerWang', | |
| password: '1234', | |
| passwordConfirm: '5678', | |
| email: 'tiger.wang@gmail.com' | |
| }; | |
| request(url) | |
| .post('/api/users') | |
| .send(user) | |
| .end(function (err, res) { | |
| should.not.exist(err); | |
| res.status.should.equal(422); | |
| res.error.text.should.equal('兩次輸入的密碼不一致'); | |
| done(); | |
| }); | |
| }); | |
| it('should create an user on DB', function (done) { | |
| var user = { | |
| name: 'TigerWang', | |
| password: '1234', | |
| passwordConfirm: '1234', | |
| email: 'tiger.wang@gmail.com' | |
| }; | |
| request(url) | |
| .post('/api/users') | |
| .send(user) | |
| .end(function (err, res) { | |
| should.not.exist(err); | |
| res.status.should.equal(200); | |
| res.body.name.should.equal(user.name); | |
| res.body.password.should.equal(tools.getCryptoPassword(user.password)); | |
| res.body.email.should.equal(user.email); | |
| id = res.body._id; | |
| done(); | |
| }); | |
| }); | |
| it('should return error trying to save duplicate username', function (done) { | |
| var user = { | |
| name: 'TigerWang', | |
| password: '1234', | |
| passwordConfirm: '1234', | |
| email: 'tiger.wang@gmail.com' | |
| }; | |
| request(url) | |
| .post('/api/users') | |
| .send(user) | |
| .end(function (err, res) { | |
| should.not.exist(err); | |
| res.status.should.equal(422); | |
| res.error.text.should.equal('該帳號已有人使用'); | |
| done(); | |
| }); | |
| }); | |
| }); | |
| describe('GET /api/users/:id', function () { | |
| it('should read an user from DB', function (done) { | |
| var user = { | |
| name: 'TigerWang', | |
| password: '1234', | |
| passwordConfirm: '1234', | |
| email: 'tiger.wang@gmail.com' | |
| }; | |
| request(url) | |
| .get('/api/users/' + id) | |
| .end(function (err, res) { | |
| should.not.exist(err); | |
| res.status.should.equal(200); | |
| res.body.name.should.equal(user.name); | |
| res.body.password.should.equal(tools.getCryptoPassword(user.password)); | |
| res.body.email.should.equal(user.email); | |
| done(); | |
| }); | |
| }); | |
| }); | |
| describe('PUT /api/users/:id', function () { | |
| it('should update email for an user', function (done) { | |
| var user = { | |
| name: 'TigerWang', | |
| password: '1234', | |
| passwordConfirm: '1234', | |
| email: 'TigerWang@gmail.com' | |
| }; | |
| request(url) | |
| .put('/api/users/' + id) | |
| .send({email: user.email}) | |
| .end(function (err, res) { | |
| should.not.exist(err); | |
| res.status.should.equal(200); | |
| res.body.name.should.equal(user.name); | |
| res.body.password.should.equal(tools.getCryptoPassword(user.password)); | |
| res.body.email.should.equal(user.email); | |
| done(); | |
| }); | |
| }); | |
| }); | |
| describe('DELETE /api/users/:id', function () { | |
| it('should delete an user from DB', function (done) { | |
| request(url) | |
| .delete('/api/users/' + id) | |
| .end(function (err, res) { | |
| should.not.exist(err); | |
| res.status.should.equal(200); | |
| done(); | |
| }); | |
| }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment