Mocha + Chai do not access routes that require authentication

Asked

Viewed 88 times

0

Talk, y'all, good afternoon.

So I’m running some tests on my Node.js API using Mocha and chai, but I have a middleware that checks my user’s token so that it can access some HTTP requirements.

The login test works correctly, but when I have to do tests with the routes protected by middleware it’s not right.

process.env.ESTACAO_HACK_ALUNOS_DATABASE_MODE = "test"; 
process.env.ESTACAO_HACK_ALUNOS_DATABASE_HOST = "localhost";

const chai = require('chai');
const expect = chai.expect;
const chaiHttp = require('chai-http');
const mongoose = require('mongoose');
const Student = require('../models/student');

chai.use(chaiHttp);

const app = require('../server.js');

let activeStudent = new Student({
    name: "Active Student",
    email: "[email protected]",
    cpf: "192.834.586-11",
    picture: "public/images/gas.jpg-1532709990908",
    resume: "public/docs/docteste.pdf-1532709991846",
    description: "Descrição básica",
    group: "Turma 1",
    isWorking: false,
    birthDate: "01/09/1998",
    active: true
});

let inactiveStudent = new Student({
    name: "Inactive Student",
    email: "[email protected]",
    cpf: "012.345.678-99",
    picture: "public/images/ney.jpg-1532709990908",
    resume: "public/docs/DOCUMENTO TESTE PDF.pdf-1532709991846",
    description: "Descrição básica",
    group: "Turma 1",
    isWorking: false,
    birthDate: "01/09/1998"
});

before((done) => {
    mongoose.connection.collections.students.drop(() => {
        console.log("[Admin Test START] Cleared 'students' collection");
        activeStudent.save()
        .then((activeStudent) => {
            activeStudent = activeStudent;
            inactiveStudent.save()
            .then((inactiveStudent) => {
                console.log("[Admin Test START] Students added");
                inactiveStudent = inactiveStudent;
                done();
            })
        })
    });
});

describe('Admin', () => {
    describe('Get Students', () => {
        it('All', (done) => {
            chai.request(app)
            .get('/students')
            .then(response => {
                expect(response).to.have.status(200);
                expect(response.body).to.be.an('array');
                expect(response.headers.authorization);
                expect(response.body.length).to.be.equal(2);
                done();
            })
            .catch(err => {
                return err;
            });       
        });
        
        it('Inactive', (done) => {
            chai.request(app)
            .get('/students/inactive')
            .then(response => {
                expect(response).to.have.status(200);
                expect(response.body).to.be.an('array');
                expect(response.body.length).to.be.equal(1);
                done();
            })
            .catch(err => {
                return err;
            });       
        });
        
        it('Active', (done) => {
            chai.request(app)
            .get('/students/active')
            .then(response => {
                expect(response).to.have.status(200);
                expect(response.body).to.be.an('array');
                expect(response.body.length).to.be.equal(1);
                done();
            })
            .catch(err => {
                return err;
            });       
        });
      });
    });

In this test, I would have to return the Students but when I run the test I get the following message:

1) Admin Get Students All: Error: Timeout of 10000ms exceeded. For async tests and Hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/raphaelmelo/Desktop/estacao-hack-alunos-api/test/adminTests.js)

2) Admin Get Students Inactive: Error: Timeout of 10000ms exceeded. For async tests and Hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/raphaelmelo/Desktop/estacao-hack-alunos-api/test/adminTests.js)

3) Admin Get Students Active: Error: Timeout of 10000ms exceeded. For async tests and Hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/raphaelmelo/Desktop/estacao-hack-alunos-api/test/adminTests.js).

all other tests that do not depend on the token pass normally.

I already understood that I need to set the token in these tests, but I’m not able to understand the syntax of this, will someone help me ?

  • How’s your middleware code? And where are you going with the authentication?

1 answer

0

Increase the execution timeout of the mocha. In my case my command yarn test has the flag --timeout mocha.

./node_modules/mocha/bin/mocha --timeout 10000 --exit test

Browser other questions tagged

You are not signed in. Login or sign up in order to post.