Skip to content

Instantly share code, notes, and snippets.

@markotom
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markotom/dc41060158db6f4faa0c to your computer and use it in GitHub Desktop.
Save markotom/dc41060158db6f4faa0c to your computer and use it in GitHub Desktop.
Rest API tests (api/users) with Supertest, Mocha, Chai
var chai = require('chai'),
request = require('supertest'),
server = require('../server'),
model = require('../server/models/user');
chai.should();
describe('REST API (/users)', function () {
var json = {
nickname: 'user',
email: 'user@email.com',
password: '1234567890'
};
describe('Schema', function (){
var properties = 10,
schema = model.schema.paths;
it('should have only properties that will be tested', function (done) {
// Add two properties that were not considered (_id, __v)
Object.keys(schema).length.should.be.equal(properties + 2);
done();
});
it('should have property \'name\' correctly', function (done) {
schema.should.have.property('name');
schema.name.options.type.should.be.equal(String);
done();
});
it('should have property \'nickname\' correctly', function (done) {
schema.should.have.property('nickname');
schema.nickname.options.type.should.be.equal(String);
done();
});
it('should have property \'email\' correctly', function (done) {
schema.should.have.property('email');
schema.email.options.type.should.be.equal(String);
schema.email.options.required.should.be.equal(true);
done();
});
it('should have property \'password\' correctly', function (done) {
schema.should.have.property('password');
schema.password.options.type.should.be.equal(String);
schema.password.options.required.should.be.equal(true);
done();
});
it('should have property \'providers\' correctly', function (done) {
schema.should.have.property('providers');
schema.providers.options.type.should.be.equal(Object);
done();
});
it('should have property \'status\' correctly', function (done) {
schema.should.have.property('status');
schema.status.options.type.should.be.equal(String);
done();
});
it('should have property \'confirmation_token\' correctly', function (done) {
schema.should.have.property('confirmation_token');
schema.confirmation_token.options.type.should.be.equal(String);
done();
});
it('should have property \'confirmed_at\' correctly', function (done) {
schema.should.have.property('confirmed_at');
schema.confirmed_at.options.type.should.be.equal(Date);
done();
});
it('should have property \'created_at\' correctly', function (done) {
schema.should.have.property('created_at');
schema.created_at.options.type.should.be.equal(Date);
done();
});
it('should have property \'updated_at\' correctly', function (done) {
schema.should.have.property('updated_at');
schema.updated_at.options.type.should.be.equal(Date);
done();
});
});
it('POST /users', function (done) {
request(server)
.post('/api/users')
.send({
email: json.email,
password: json.password
})
.expect(201)
.end(function (err, res) {
if (err) {
return done(err);
}
json.id = res.body._id;
done();
});
});
it('GET /users/:id', function (done) {
request(server)
.get('/api/users/' + json.id)
.expect('Content-type', /json/)
.expect(200)
.end(function (err, res) {
if (err) {
return done(err);
}
res.body.email.should.equal(json.email);
res.body.password.should.have.length(64);
done();
});
});
it('PUT /users/:id', function (done) {
request(server)
.put('/api/users/' + json.id)
.send({ nickname: json.nickname })
.end(function (err, res) {
if (err) {
return done(err);
}
res.body.should.have.property('nickname');
res.body.nickname.should.have.length(4);
done();
});
});
it('DELETE /users/:id', function (done) {
request(server)
.delete('/api/users/' + json.id)
.expect(204)
.end(function (err) {
if (err) {
return done(err);
}
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment