Skip to content

Instantly share code, notes, and snippets.

@Turbiani
Created February 21, 2018 23:07
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 Turbiani/c326fa30ec277bc9e06c513ce9ddfd7f to your computer and use it in GitHub Desktop.
Save Turbiani/c326fa30ec277bc9e06c513ce9ddfd7f to your computer and use it in GitHub Desktop.
Rest teste example
/**
* Created by lcunha on 27/02/17.
*/
const assert = require('assert');
const app = require('../../config/express')();
const request = require('supertest')(app);
const {expect} = require('chai');
describe('Rota: Auth', () => {
const CreateSenha = app.infra.factory.PasswordFactory;
beforeEach((done) => {
const Usuario = app.models.Usuario;
let newUsuario = {
name: "Usuario Teste",
email: "usuario@teste.com.br",
password: CreateSenha.criar("12345678"),
type: "R"
};
Usuario.remove({}).then(()=>{
Usuario.create(newUsuario, (error, usuario) => {
if(error){
console.log(error);
}else{
done();
}
});
}).catch((error) => {
console.log(error);
});
});
describe('POST /auth', () => {
context('should status 200', () => {
it("retorna token autenticado", done => {
request.post('/auth')
.send({
email: 'usuario@teste.com.br',
password: '12345678'
})
.expect(200)
.end((err, res) => {
expect(res.body).to.include.keys('token');
done(err);
});
});
});
context('should status 400', () => {
it("retorna erro quando senha e email são vazios", done => {
request.post('/auth')
.send({
email: '',
password: ''
})
.expect(400)
.end((err, res) => {
done(err);
});
});
});
context('should status 401', () => {
it("retorna erro quando senha é incorreta e email válido", done => {
request.post('/auth')
.send({
email: 'usuario@teste.com.br',
password: '1234567'
})
.expect(401)
.end((err, res) => {
done(err);
});
});
});
context('should status 404', () => {
it("retorna erro quando senha é correta e email não existe", done => {
request.post('/auth')
.send({
email: 'usuario2@teste.com.br',
password: '12345678'
})
.expect(404)
.end((err, res) => {
done(err);
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment