简体   繁体   English

NodeJS / Passport-使用Mocha和Superagent测试用户登录

[英]NodeJS/Passport - Testing user login with mocha and superagent

I have built a login process with a basic MEAN stack and using passport for the authentication process. 我已经建立了一个具有基本MEAN堆栈的登录过程,并使用了用于身份验证过程的通行证。

I am trying to set up a test to make sure the login process is working. 我试图设置一个测试以确保登录过程正在运行。 To do the login part here is the code I used: 在这里执行登录部分是我使用的代码:

  it ('login', function(done) {
    agent.post(config.web.vhost + '/login')
      .send({ email: 'bruce@wayne.inc', password: 'batman' })
      .end(function(err, res) {
      if (err) console.log('error' + err.message);

        res.should.have.status(200);

        done();
      });
  });

I get the following error: 我收到以下错误:

no error email: bruce@wayne.inc ․no error errorsocket hang up ․double callback! 没有错误的电子邮件:bruce@wayne.inc。没有错误errorocket挂断了。双重回调!

  1 passing (2s)
  1 failing

  1) User login:
     Uncaught TypeError: Cannot read property 'should' of undefined

I know my routes and credentials are good but I cant figure out whats not working here. 我知道我的路线和凭据都不错,但是我无法弄清这里什么不起作用。 This is my first steps with user testing so I am probably not getting something right. 这是我进行用户测试的第一步,因此我可能做得不好。

Here is the rest of my test: 这是我的其余测试:

var should = require("should");
var mongoose = require('mongoose');
var request = require('superagent');
var agent = request.agent();

var config = require("../settings/conf");
var dbUrl = require("../config/database.js");
var User = require('../server/models/user');

var db;

describe('User', function() {

before(function(done) {
  db = mongoose.connect(dbUrl.url);
    done();
  });

  after(function(done) {
     mongoose.connection.close()
    done();
  });

  beforeEach(function(done) {
    var user = new User({
      email: 'bruce@wayne.inc',
      password: 'batman',
      firstName: 'Bruce',
      lastName: 'Wayne'
    });

    user.save(function(err, user) {
      if (err) console.log('error' + err.message);
      else console.log('no error');

      done();
    });
  });

  it('find a user by username', function(done) {
    User.findOne({ email: 'bruce@wayne.inc' }, function(err, user) {
      user.email.should.eql('bruce@wayne.inc');
      console.log("   email: ", user.email)
      done();
    });
  });

  it ('login', function(done) {
    agent.post(config.web.vhost + '/login')
      .send({ email: 'bruce@wayne.inc', password: 'batman' })
      .end(function(err, res) {
      if (err) console.log('error' + err.message);

        res.should.have.status(200);

        done();
      });
  });

  afterEach(function(done) {
    User.remove({ email: 'bruce@wayne.inc' }, function() {
      done();
    });
  });
});

I notice that the failure you're seeing is connect ECONNREFUSED -- so it's not anything about passport, it's just that you're not connecting successfully to your server in your agent.post . 我注意到您看到的故障是connect ECONNREFUSED -所以与护照无关,只是您没有成功连接到agent.post的服务器。

I would guess either your config.web.vhost value isn't set correctly or isn't in the right form for what superagent is looking for (for instance, superagent may require a full absolute url with the 'http://' on the front, and your vhost value may only be the hostname). 我想您的config.web.vhost值设置不正确或格式不符合超级代理要查找的内容(例如,超级代理可能需要完整的绝对URL,并带有“ http://”前面,并且您的vhost值只能是主机名)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM