2016-07-13 93 views
2

我有一個使用Express和Passport的應用程序。我正在使用Passport的Google OAuth2策略。我有幾條路線需要通過此策略進行登錄。使用Mocha和Chai測試Express和護照OAuth2

我正在與Mocha和Chai進行集成測試,但我不確定如何繞過或使用某些路由所需的OAuth2身份驗證。

舉例來說,這裏是我的測試之一:

it("should list a single item on /items/<id> GET", function(done) {                    
    chai.request(server) 
    .get('/items/' + id) 
    .end(function(err, res) { 
     res.should.have.status(200); 
     res.should.be.json; 
     res.body.should.be.a('object'); 
     res.body.should.have.property('description'); 
     done(); 
    }); 
}); 

我的路線/items/:id

router.get('/items/:id', auth.isLoggedIn, function(req, res) { 
    var item = getItem(); 
    res.json(item); 
}); 

/items/:id需要登錄。有沒有辦法繞過登錄進行測試,或者嘲笑用戶我的集成測試會起作用?

回答

0

你可以讓你自己的方法來繞過它,如果你想要的,但最好是還測試登錄,那麼你可以測試需要先登錄用戶的功能。

我從來二手柴你也許可以做這樣的事情之前,但:

var user = null; 

it("should log the user in", function(done) {                    
    chai.request(server) 
    .post('/login', userCredentials) 
    .end(function(err, res) { 
     res.should.have.status(200); 
     user = res.body; 
     done(); 
    }); 
}); 
+2

的問題,這是它的使用谷歌的OAuth2認證方法,它需要大量的重定向和回調的谷歌的認證。這不是典型的本地認證方法。 – intargc

+0

@intargc - 我爲github工作 - 請參閱示例代碼,OAuth和API請求的nocking應類似於谷歌,讓我知道你遇到什麼問題! – stujo

1

我想通了如何通過我的摩卡測試中在請求對象嘲諷isAuthenticated,使這項工作。

var chai = require('chai'); 
var chaiHttp = require('chaiHttp'); 
var server = require('../app'); 
var should = chai.should(); 

chai.use(chaiHttp); 

// Allows the middleware to think we're already authenticated. 
server.request.isAuthenticated = function() { 
    return true; 
} 

describe('Items', function() { 
    it('should list all items on/GET', function(done) { 
    chai.request(server) 
     .get('/') 
     .end(function(err, res) { 
     res.should.have.status(200); 
     res.should.be.json; 
     // more tests... 
     done(); 
     }); 
    }); 
}); 
3

我能測試的github的OAuth /護照用摩卡柴齊HTTP諾克和洛坦github上-的OAuth

nock-github-oauth存根出令牌網址

另外手動nocked GitHub的用戶和電子郵件API從GitHub的API文檔樣本調用

這是我auth_controller_spec.js

//During the test the env variable is set to test 
process.env.NODE_ENV = 'test'; 

var chai = require('chai'); 
var chaiHttp = require('chai-http'); 
var should = chai.should(); 
var expect = chai.expect 

var User = require.main.require('models/User'); 

// https://gist.github.com/branneman/8048520#7-the-wrapper 
var app = require.main.require('app'); 

chai.use(chaiHttp); 


function nockGitHubUserAPI(nock) { 
    /** 
    * Intercept `https://api.github.com:443/user` API Call. 
    */ 
    nock('https://api.github.com:443') 
    .filteringPath(/\/user.+/, '/user') 
    .get('/user') 
    .reply(200, 
     { 
     "login": "octocat", 
     "id": 1, 
     "avatar_url": "https://github.com/images/error/octocat_happy.gif", 
     "gravatar_id": "", 
     "url": "https://api.github.com/users/octocat", 
     "html_url": "https://github.com/octocat", 
     "followers_url": "https://api.github.com/users/octocat/followers", 
     "following_url": "https://api.github.com/users/octocat/following{/other_user}", 
     "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", 
     "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", 
     "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", 
     "organizations_url": "https://api.github.com/users/octocat/orgs", 
     "repos_url": "https://api.github.com/users/octocat/repos", 
     "events_url": "https://api.github.com/users/octocat/events{/privacy}", 
     "received_events_url": "https://api.github.com/users/octocat/received_events", 
     "type": "User", 
     "site_admin": false, 
     "name": "monalisa octocat", 
     "company": "GitHub", 
     "blog": "https://github.com/blog", 
     "location": "San Francisco", 
     "email": "[email protected]", 
     "hireable": false, 
     "bio": "There once was...", 
     "public_repos": 2, 
     "public_gists": 1, 
     "followers": 20, 
     "following": 0, 
     "created_at": "2008-01-14T04:33:35Z", 
     "updated_at": "2008-01-14T04:33:35Z" 
     } 
    ); 

/** 
* Intercept `https://api.github.com:443/user/emails` API Call. 
*/ 
    nock('https://api.github.com:443') 
    .filteringPath(/\/user\/emails.+/, '/user/emails') 
    .get('/user/emails') 
    .reply(200, 
     [ 
     { 
      "email": "[email protected]", 
      "verified": true, 
      "primary": true 
     } 
     ] 
    ); 
} 


describe('Auth Controller', (done) => { 

    var user, nock, github, mockToken, githubHost; 

    before((done) => { 
    nock = require('nock'); 
    nock.enableNetConnect('127.0.0.1'); 
    github = require('nock-github-oauth'); 

    nockGitHubUserAPI(nock) 

    github.nock(done); 
    }) 

    beforeEach((done) => { //Before each test we reset the database 
    User.query().del().then(() => { 
     var params = {name: 'bonzo', authtype: 'github', authid: '12345678'} 
     // Create a user so the db isn't empty 
     // May help us uncover odd bugs 
     new User(params).save() 
     .then((bonzo) => { 
      user = bonzo; 
      done(); 
     }) 
    }) 
    }); 

    after(function(done) { 
     nock.cleanAll(); 
     done(); 
    }); 

    describe('github link',() => { 
     it('it should redirect to github.com login/approve page', (done) => { 
     chai.request(app) 
      .get('/auth/github') 
      .redirects(0) 
      .end((err, res) => { 
       expect(res.headers['location']).to.match(/^https:\/\/github.com\/login\/oauth\/authorize/); 
       done(); 
      }); 
     }); 
    }); 

    describe('github callback',() => { 
     it(' should poll github api for details, upsert the user and log them in', (done) => { 
     var agent = chai.request.agent(app) 
      agent.get('/auth/github/callback') 
      .query({code : '9835b716e83875665b21' }) 
      .end((err, res) => { 
       // If successful layout displays username on page in (brackets) 
       expect(res.text).to.match(/\(octocat\)/); 
       done(); 
      }); 
     }); 
    }); 


    describe('logout',() => { 
     it('it should end the session and show login', (done) => { 
     chai.request(app) 
      .get('/auth/logout') 
      .end((err, res) => { 
       expect(res.redirects[0]).to.match(/\/$/); 
       // If successful layout displays Login links 
       expect(res.text).to.match(/Login/); 
       done(); 
      }); 
     }); 
    }); 

}); 

完整的源代碼在這裏:https://github.com/stujo/node-express-gamebase

相關問題