Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用Passport身份验证轻松地测试Node JS应用程序?_Node.js_Authentication_Testing_Passport.js_Middleware - Fatal编程技术网

Node.js 如何使用Passport身份验证轻松地测试Node JS应用程序?

Node.js 如何使用Passport身份验证轻松地测试Node JS应用程序?,node.js,authentication,testing,passport.js,middleware,Node.js,Authentication,Testing,Passport.js,Middleware,在Node JS应用程序中实现“EnsureReauthenticated”功能时,始终需要登录用户,并且还需要Oauth2登录需要HTTPS站点,每次我想要测试路由时,我都需要将我的应用程序部署到主机上。如何在不浪费部署时间的情况下继续测试?我实现了一个测试中间件,当我在本地运行并且将环境变量设置为test时,它允许我绕过身份验证。以下是代码示例: 我的设置 const express = require('express'); const expressLayouts = require('

在Node JS应用程序中实现“EnsureReauthenticated”功能时,始终需要登录用户,并且还需要Oauth2登录需要HTTPS站点,每次我想要测试路由时,我都需要将我的应用程序部署到主机上。如何在不浪费部署时间的情况下继续测试?

我实现了一个测试中间件,当我在本地运行并且将环境变量设置为test时,它允许我绕过身份验证。以下是代码示例:

我的设置

const express = require('express');
const expressLayouts = require('express-ejs-layouts');
const mongoose = require('mongoose');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const MongoStore = require('connect-mongo')(session);
const cookieParser = require('cookie-parser');
const testconfig = require('./config/testconfig');
const { ensureAuthenticated } = require('../config/auth');
然后在我的auth.js中,一个典型的ensureAuthenticated方法如下所示

module.exports = {
    ensureAuthenticated: function(req, res, next) {
        if (req.isAuthenticated()) {
            return next();  
        }
        req.flash('error_msg', 'Please log in to view this resource');
        res.redirect('/');
    }
}
这使我能够以如下简单方式创建路由:

router.get('/dashboard', ensureAuthenticated, function(req, res) {
    res.render('dashboard', {name: req.user.id});
})
NODE_ENV='test'
NODE_ENV='production'
请务必注意,我的特定passport实现为我提供了正在使用的req.user.id变量,该变量标识当前登录的用户。 在建立了路由之后,我现在被锁定,并且必须始终将我的解决方案部署到我的主机上,以我的情况为例,Herokuapps.com提供免费的主机开发

参加测试

为了在本地绕过身份验证,我使用dotenv npm包来设置环境变量。我的.env文件如下所示:

router.get('/dashboard', ensureAuthenticated, function(req, res) {
    res.render('dashboard', {name: req.user.id});
})
NODE_ENV='test'
NODE_ENV='production'
现在我可以实现自己的中间件了,您可能已经注意到了这一行:

const testconfig = require('./config/testconfig');
我使用的testconfig.js的代码如下:

module.exports = function () {
    return function (req, res, next) {
      // Implement the middleware function based on the options object
      if(process.env.NODE_ENV === 'test'){
        req.user = {
          id: 'testuser',
          battletag:'testbattletag'
        }
      }
      next()
    }
  }
我通过以下方式让应用程序使用它:

app.use(testconfig());
最后,我将ensureAuthenticated方法编辑为:

module.exports = {
    ensureAuthenticated: function(req, res, next) {
        if(process.env.NODE_ENV === 'test'){
            return next();
        }
        if (req.isAuthenticated()) {
            return next();  
        }
        req.flash('error_msg', 'Please log in to view this resource');
        res.redirect('/');
    }
}
一切就绪,准备出发!现在我可以安心地使用nodemon在开发路线的同时立即看到变化,而路线仍然可以生产。我只需要在.env文件中将环境变量设置为类似“production”的值,如下所示:

router.get('/dashboard', ensureAuthenticated, function(req, res) {
    res.render('dashboard', {name: req.user.id});
})
NODE_ENV='test'
NODE_ENV='production'

下次请把它做好。答案应该在答案空间中。问题应该在问题中。评论应该消失,或者在评论部分。制定问题,将部分编辑成格式良好的问题,然后将我自己的answe移至实际答案,这是一个好主意吗?