Javascript Mocha/Chai测试前的Auth0登录-NodeJS

Javascript Mocha/Chai测试前的Auth0登录-NodeJS,javascript,node.js,testing,mocha.js,auth0,Javascript,Node.js,Testing,Mocha.js,Auth0,我正在尝试使用Mocha和Chai测试NodeJS应用程序。我正在使用Auth0来处理应用程序的登录和注册。我希望能够测试,一旦登录,用户可以访问一组页面,但是我在处理实际登录时遇到问题 如我所料,.get('/login')将我重定向到Auth0登录页面。但是,尽管.send(userCredentials)提供了有效的登录详细信息,但它似乎没有登录。登录后,我希望它重定向到'localhost:3000/user',但据我所知,最终重定向到的是Auth0登录URL,我不确定这是否是阻止发送的

我正在尝试使用Mocha和Chai测试NodeJS应用程序。我正在使用Auth0来处理应用程序的登录和注册。我希望能够测试,一旦登录,用户可以访问一组页面,但是我在处理实际登录时遇到问题

如我所料,
.get('/login')
将我重定向到
Auth0
登录页面。但是,尽管
.send(userCredentials)
提供了有效的登录详细信息,但它似乎没有登录。登录后,我希望它重定向到
'localhost:3000/user'
,但据我所知,最终重定向到的是Auth0登录URL,我不确定这是否是阻止发送的原因,或者重定向需要一两秒钟的时间这一事实是否会导致问题

我的测试文件在下面

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();
            })
        })
    })  
})
它给出的失败原因是:

  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)

1)Listing.js
“每次之前”钩子:每次测试之前登录:
未捕获断言错误:预期未定义为true
在测试中。(test/test list.js:44:41)
在Test.assert(node_modules/supertest/lib/Test.js:181:6)
在localAssert(node_modules/supertest/lib/test.js:131:12)
在/Users//Documents/Node/NodeStuff/Node_modules/supertest/lib/test.js:128:5
在Test.Request.callback(node_modules/supertest/node_modules/superagent/lib/node/index.js:728:3)
在收到消息时。(node_modules/supertest/node_modules/superagent/lib/node/index.js:916:18)
在IncomingMessage.EventEmitter.emit(domain.js:476:20)
在endReadableNT(_stream_readable.js:1183:12)
在处理和拒绝时(内部/process/task_queues.js:80:21)
res
正文
是空的,
文本
也是空的,这对我来说很奇怪


任何帮助都将不胜感激。

您正在尝试将测试套件集成到交互式登录路径。您实际需要的是非交互式登录。使用Auth0,这可以通过POST oauth/token endpoint实现,您需要用户的电子邮件以及发送到该电子邮件地址的验证码(这样您就不能使用未经验证的电子邮件)和客户端密码:

或者,您可以使用管理API获得的管理令牌来执行操作