【问题标题】:How to authenticate Supertest requests with Passport /Facebook strategy/?如何使用 Passport /Facebook strategy/ 验证 Supertest 请求?
【发布时间】:2014-01-05 14:32:35
【问题描述】:

我正在使用 Passport.js 进行身份验证(Facebook 策略)并使用 Mocha 和 Supertest 进行测试。如何使用 Supertest for Facebook 策略创建会话并发出经过身份验证的请求?

这是用户未登录时的示例测试:

  describe 'when user not logged in', ->

    describe 'POST /api/posts', ->
      it 'respond with 401', (done)->
        request(app).
          post(API.url('posts')).
          set('Accept', 'application/json').
          send(post: data).
          expect('Content-Type', /json/).
          expect(401, done)

谢谢你的建议:D

【问题讨论】:

    标签: node.js express mocha.js passport.js supertest


    【解决方案1】:

    这里看起来没什么不同,所以我把我的答案分成了两部分。

    1) 您首先必须通过 Facebook 创建测试用户。您可以通过以下两种方法之一执行此操作:1) Facebook 的 Graph API,或 2) 通过应用程序的角色页面。

    2) 使用 SuperTest 保持会话的推荐方法是使用名为 .agent() 的 SuperAgent 方法来保持会话。您可以使用 SuperAgent 做的任何事情,都可以使用 SuperTest。有关更多信息,请参阅此 Github 帖子。

    var supertest = require('supertest');
    var app = require('../lib/your_app_location');
    
    describe('when user not logged in', function() {
        describe('POST /api/posts', function() {
            var agent1 = supertest.agent(app);
    
            agent1
                .post(API.url('posts'))
                .set('Accept', 'application/json')
                .send(post: data)
                .(end(function(err, res) {
                    should.not.exist(err);
                    res.should.have.status(401);
                    should.exist(res.headers['set-cookie']);
                    done();
                }));
        });
    });
    

    VisionMedia Github 上还有其他一些不错的代码 sn-ps。请找到他们here

    【讨论】:

    • 谢谢。你的回答对我有很大帮助:D
    • 你能举一个例子来说明你是如何使用 facebook 进行身份验证的吗?即您如何进行登录?我觉得我可能错过了什么。
    【解决方案2】:

    一般的解决方案是创建一个 cookie jar 将在请求之间重复使用。

    以下示例不是特定于护照的,但应该可以:

    var request = require('request');
    
    describe('POST /api/posts', function () {
        // Create a new cookie jar
        var j = request.jar();
        var requestWithCookie = request.defaults({jar: j}),
    
        // Authenticate, thus setting the cookie in the cookie jar
        before(function(done) {
            requestWithCookie.post('http://localhost/user', {user: 'foo', password: 'bar'}, done);
        });
    
        it('should get the user profile', function (done) {
            requestWithCookie.get('http://localhost/user', function (err, res, user) {
                assert.equal(user.login, 'foo');
                done();
            });
        });
    });
    

    【讨论】:

    • 这是本地策略的解决方案吗?还是基本身份验证?
    【解决方案3】:

    这个example 展示了如何进行测试的 SuperTest 部分:

    describe('request', function() {
      describe('persistent agent', function() {
        var agent1 = request.agent();
        var agent2 = request.agent();
        var agent3 = request.agent();
        var agent4 = request.agent();
    
        it('should gain a session on POST', function(done) {
          agent3
            .post('http://localhost:4000/signin')
            .end(function(err, res) {
              should.not.exist(err);
              res.should.have.status(200);
              should.not.exist(res.headers['set-cookie']);
              res.text.should.include('dashboard');
              done();
            });
        });
    

    这是一个关于它的blog post

    【讨论】:

      猜你喜欢
      • 2012-12-09
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      • 2013-11-21
      • 2015-03-16
      • 2020-12-11
      相关资源
      最近更新 更多