jeudi 16 janvier 2020

Auth0 login before Mocha/Chai test - NodeJS

I'm attempting to test a NodeJS app using Mocha and Chai. I'm using Auth0 to handle the login and signups for the app. I want to be able to test that, once logged in, the user can visit a set of pages, however I'm having trouble handling the actual logging in.

The .get('/login') redirects me to the Auth0 login page, as I expect. However, despite the .send(userCredentials) providing valid login details, it doesn't seem to login. After login, I expect it to redirect to 'localhost:3000/user' but as far as I can tell the final redirect is to the Auth0 login URL, and I'm unsure if this redirect is what prevents the send, or if the fact that the redirect takes a second or two could be causing the issue.

My test file is below.

var chai = require("chai");
var chaiHTTP = require("chai-http");
var chaiAP = require("chai-as-promised");
var server = require("../app");
var listing = require('../models/RDSModel');
var should = chai.should();
var expect = chai.expect;
var request = require("supertest");

const {Client} = require('pg');

var config = require('../config');

const connection = {
    user: 'Admin',
    host: process.env.DB_URL,
    database: 'postgres_test',
    password: config.postgresPass,
    port: 5432,
};

var pg = require('knex')({
    client: 'pg',
    connection: connection,
    searchPath: ["knex", "public"]
});

chai.use(chaiHTTP);
chai.use(chaiAP);

describe("Listing.js", function() {

    beforeEach("login before each test", function(done) {
        const userCredentials = {
            email: 'hello@email.co.uk',
            password: 'this_is_a_password!'
        };
        request(server)
        .get('/login')
        .send(userCredentials)
        .end(function(err, res) {
            console.log(res);
            expect(res).to.have.status(302);
            expect(res.body.state).to.be.true;
            res.body.data.should.be.an("object");
            done();
        })
    });

    describe("get /", function() {
        it("#return 200 status code", function(done) {
            this.timeout(5000);
            chai.request(server)
            .get('/Listing/')
            .end(function(err, res) {
                console.log(res.body);
                expect(res).to.have.status(200);
                expect('Location', '/Listing')
                expect(res.body).to.be.an("object");
                done();
            })
        }) 
    });

    describe("get /Ignore", function() {
        it("should return 200 status code", function(done) {
            this.timeout(5000);
            chai.request(server)
            .get('/Listing/Ignore')
            .end(function(err, res) {
                expect(res).to.have.status(200);
                expect(res.body).to.be.an("object");
                done();
            })
        })
    })
    describe("get /Report", function() {
        it("should return 200 status code", function(done) {
            this.timeout(5000);
            chai.request(server)
            .get('/Listing/Report')
            .end(function(err, res) {
                expect(res).to.have.status(200);
                expect(res.body).to.be.an("object");
                done();
            })
        })
    })  
})

The reason it gives for failure is:

  1) Listing.js
       "before each" hook: login before each test:
     Uncaught AssertionError: expected undefined to be true
      at Test.<anonymous> (test/test-listing.js:44:41)
      at Test.assert (node_modules/supertest/lib/test.js:181:6)
      at localAssert (node_modules/supertest/lib/test.js:131:12)
      at /Users/<Name>/Documents/Node/NodeStuff/node_modules/supertest/lib/test.js:128:5
      at Test.Request.callback (node_modules/supertest/node_modules/superagent/lib/node/index.js:728:3)
      at IncomingMessage.<anonymous> (node_modules/supertest/node_modules/superagent/lib/node/index.js:916:18)
      at IncomingMessage.EventEmitter.emit (domain.js:476:20)
      at endReadableNT (_stream_readable.js:1183:12)
      at processTicksAndRejections (internal/process/task_queues.js:80:21)

The body of res is empty, as is the text, which seems odd to me.

Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire