【问题标题】:How to test express-based API which requires Facebook authentication?如何测试需要 Facebook 身份验证的基于 express 的 API?
【发布时间】:2014-06-22 16:25:13
【问题描述】:

我正在编写一个仅使用基于 Facebook 的 PassportJS 身份验证的应用程序。部分提供的 API 仅在用户通过身份验证时可用。 我正在尝试找到一种方法来测试这部分 API。我发现了许多测试经过身份验证的 API 的示例,其中使用了登录名+密码,但这里不是这种情况。我也找到了这个thread,但不幸的是,我不明白这个答案有什么帮助。

你们有没有任何例子可以解释如何实现这一点?

假设我有一个 API:

app.get('/users/me, users.me);

此调用要求用户通过 Facebook 进行身份验证。

如何通过超级测试进行测试并使用 FB 模拟用户身份验证?

【问题讨论】:

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


    【解决方案1】:

    您可以使用zombie + mocha 来测试这个......Zombie 在后端打开一个虚拟浏览器(您看不到)并单击页面上的任何链接,然后通过使用 mocha 断言来测试此结果。在您启动应用程序之前登录到 facebook,然后使用zombie 为新路由(因为您将获得成功的重定向 url)进行 fb 身份验证和断言的链接。假设成功登录后的重定向 url 是 /auth/facebook/success 并且 /auth/facebook 是测试登录的 url。这里是可以实现它的代码

    var assert = require('assert'),
        virtualBrowser = require('zombie');
    describe('facebbok Login test', function () {
        describe('Checking whether login is successfull based o nthe redirection URL',         function () {
            it('Checking for the sucess URL', function (done) {
                virtualBrowser.visit('http://127.0.0.1:3000/auth/facebook', function (err, browser) {
                    if (err)
                        throw err;
                   console.log('Zombie visited the page,page loaded    ');
                   assert.equal(browser.location.pathname, '/auth/facebook/success');
    
                    //all your tests goes here
    
                    /*you'll be using this 'browser object in the callback for further tests */
    
                   done();
                });
            });
        });
    });
    

    通过对 mocha 运行测试来运行测试(将出现在已安装模块文件夹的 node_module/mocha 中) ~/node_modules/mocha/bin/mocha ./zombie-test/test_sign_facebook.js 如果 mocha 报告超时,因为登录可能很慢,只需使用 -t 参数添加更多阈值测试时间 ~/node_modules/mocha/bin/mocha -t 12000 ./zombie-test/test_sign

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      相关资源
      最近更新 更多