[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,45 +47,33 @@ describe('API', () => {
let me; let me;
it('create account', done => { it('create account', done => {
chai.request(server) request('/signup', account).then(res => {
.post('/signup') res.should.have.status(200);
.set('content-type', 'application/x-www-form-urlencoded') res.body.should.be.a('object');
.send(account) res.body.should.have.property('username').eql(account.username);
.end((err, res) => { done();
res.should.have.status(200); });
res.body.should.be.a('object');
res.body.should.have.property('username').eql(account.username);
done();
});
}); });
it('signin', done => { it('signin', done => {
chai.request(server) request('/signin', account).then(res => {
.post('/signin') res.should.have.status(204);
.set('content-type', 'application/x-www-form-urlencoded') me = res.header['set-cookie'][0].match(/i=(!\w+)/)[1];
.send(account) done();
.end((err, res) => { });
res.should.have.status(204);
me = res.header['set-cookie'][0].match(/i=(!\w+)/)[1];
done();
});
}); });
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') name: myName
.set('content-type', 'application/x-www-form-urlencoded') }, me).then(res => {
.send(Object.assign({ i: me }, { res.should.have.status(200);
name: myName res.body.should.be.a('object');
})) res.body.should.have.property('name').eql(myName);
.end((err, res) => { done();
res.should.have.status(200); });
res.body.should.be.a('object');
res.body.should.have.property('name').eql(myName);
done();
});
}); });
}); });
@ -84,15 +82,11 @@ describe('API', () => {
const post = { const post = {
text: 'Hi' text: 'Hi'
}; };
chai.request(server) request('/posts/create', post, me).then(res => {
.post('/posts/create') res.should.have.status(200);
.set('content-type', 'application/x-www-form-urlencoded') res.body.should.be.a('object');
.send(Object.assign({ i: me }, post)) done();
.end((err, res) => { });
res.should.have.status(200);
res.body.should.be.a('object');
done();
});
}); });
it('reply', () => { it('reply', () => {