[test] refactor

This commit is contained in:
syuilo 2017-01-17 11:20:19 +09:00
parent b4e3d42e96
commit 3f8f0b8af8

View file

@ -14,6 +14,16 @@ chai.use(chaiHttp);
const server = require('../built/api/server'); const server = require('../built/api/server');
const db = require('../built/db/mongodb').default; const db = require('../built/db/mongodb').default;
const request = (endpoint, params, me) => new Promise((ok, ng) => {
chai.request(server)
.post(endpoint)
.set('content-type', 'application/x-www-form-urlencoded')
.send(Object.assign({ i: me }, params))
.end((err, res) => {
ok(res);
});
});
describe('API', () => { describe('API', () => {
// Reset database // Reset database
db.get('users').drop(); db.get('users').drop();
@ -37,11 +47,7 @@ describe('API', () => {
let me; let me;
it('create account', done => { it('create account', done => {
chai.request(server) request('/signup', account).then(res => {
.post('/signup')
.set('content-type', 'application/x-www-form-urlencoded')
.send(account)
.end((err, res) => {
res.should.have.status(200); res.should.have.status(200);
res.body.should.be.a('object'); res.body.should.be.a('object');
res.body.should.have.property('username').eql(account.username); res.body.should.have.property('username').eql(account.username);
@ -50,11 +56,7 @@ describe('API', () => {
}); });
it('signin', done => { it('signin', done => {
chai.request(server) request('/signin', account).then(res => {
.post('/signin')
.set('content-type', 'application/x-www-form-urlencoded')
.send(account)
.end((err, res) => {
res.should.have.status(204); res.should.have.status(204);
me = res.header['set-cookie'][0].match(/i=(!\w+)/)[1]; me = res.header['set-cookie'][0].match(/i=(!\w+)/)[1];
done(); done();
@ -64,13 +66,9 @@ describe('API', () => {
describe('i/update', () => { describe('i/update', () => {
it('update my name', done => { it('update my name', done => {
const myName = '大室櫻子'; const myName = '大室櫻子';
chai.request(server) request('/i/update', {
.post('/i/update')
.set('content-type', 'application/x-www-form-urlencoded')
.send(Object.assign({ i: me }, {
name: myName name: myName
})) }, me).then(res => {
.end((err, res) => {
res.should.have.status(200); res.should.have.status(200);
res.body.should.be.a('object'); res.body.should.be.a('object');
res.body.should.have.property('name').eql(myName); res.body.should.have.property('name').eql(myName);
@ -84,11 +82,7 @@ describe('API', () => {
const post = { const post = {
text: 'Hi' text: 'Hi'
}; };
chai.request(server) request('/posts/create', post, me).then(res => {
.post('/posts/create')
.set('content-type', 'application/x-www-form-urlencoded')
.send(Object.assign({ i: me }, post))
.end((err, res) => {
res.should.have.status(200); res.should.have.status(200);
res.body.should.be.a('object'); res.body.should.be.a('object');
done(); done();