2015-10-14 37 views
1

我想在我的Node/Express/Mongoose後端測試一個具有身份驗證的路由。爲什麼我在測試中得到401迴應?

下面是測試文件

var should = require('should'); 
    var _ = require('lodash'); 
    var async = require('async'); 
    var app = require('../../../../app'); 
    var request = require('supertest'); 
    var mongoose = require('mongoose'); 
    var User = mongoose.model('User'); 
    var Firm = mongoose.model('Firm'); 
    var firm, user, userPassword, createdFirm, loggedInUser; 

    describe('GET /api/firms', function(){ 

    beforeEach(function (done) { 

     firm = new Firm({ 
      company: 'My test company', 
      corporateMail: 'test.com' 
     }); 

     userPassword = 'password'; 
     user = new User({ 
      fistname: 'Fake User', 
      lastname: 'Fake User', 
      email: '[email protected]', 
      job: 'Partner', 
      firmName:firm.company, 
      password:userPassword, 
      isActivated:true, 
      _firmId:firm._id 
     }); 

     function createFirm(cb){ 
      request(app) 
       .post('/api/firms') 
       .send(firm) 
       .expect(201) 
       .end(function(err, res){ 
        if (err) throw err; 
        createdFirm = res.body; 
        cb(); 
       }); 
     } 

     function createUser(cb){ 
      request(app) 
       .post('/api/common/users') 
       .send(user) 
       .expect(200) 
       .end(function(err, res){ 
        createdUser = res.body; 
        if (err) throw err; 
        cb(); 
       }); 
     }; 

     async.series([function(cb){ 
      createFirm(cb); 
     }, function(cb){ 
      createUser(cb); 
     }], done);  

    }); 


    afterEach(function (done) { 
     firm.remove(); 
     user.remove(); 
     done(); 
    }); 

    it('should respond with 401 error', function(done) { 
     request(app) 
      .get('/api/firms') 
      .expect(401) 
      .end(function(err, res) { 
      if (err) return done(err); 
      done(); 
      }); 
    }); 

    it('should login', function(done) { 
     request(app) 
      .post('/auth/local') 
      .send({email:user.email, password:user.password}) 
      .expect(200) 
      .end(function(err, res) { 
      if (err) return done(err); 
      done(); 
      }); 
    }); 

    it('should respond with 200 after login', function(done) { 
     request(app) 
      .get('/api/firms') 
      .expect(200) 
      .end(function(err, res) { 
      if (err) return done(err); 
      done(); 
      }); 
    }); 

});

在工作流程中,首先創建對象firm,然後返回其標識,以便我可以創建用戶以firmId作爲參考。

我想測試/ API /企業路由用戶進行身份驗證,但儘管我各種嘗試(使用SuperAgent的,在before部分記錄)我總是在最後should部分401響應,而不是之後一個預期的200.

回答

1

事實上,正如KJ3所說,記住的重要事項是如何設置身份驗證。在我的情況下,我忘了提及我正在使用jwt。它的工作方式如下,你提供一個用戶名和密碼,服務器返回一個用jwt創建的令牌。

因此,在測試中爲每個請求發回令牌是有意義的。

實現這一目標是通過第一存儲認證後的令牌在部分

function createUser(cb){ 
      request(app) 
       .post('/api/users') 
       .send(user) 
       .expect(200) 
       .end(function(err, res){ 
        if (err) throw err; 
        authToken = res.body.token; 
        cb(); 
       }); 
     }; 
之前

然後,通過以正確格式(「承載」 +令牌與令牌請求添加.SET的方式,這是在驗證服務定義):

it('should respond with 200', function(done) { 
     var authToken = 'Bearer ' + createdUser.token; 
     request(app) 
      .get('/api/firms') 
      .set('Authorization', authToken) 
      .expect(200) 
      .end(function(err, res) { 
      if (err) return done(err); 
      done(); 
      }); 
    }); 

在這種情況下所述測試發送200背部,預計如果.SET(...)被註釋了發送401。

好消息是,這是用supertest實現的,所以不需要添加任何東西,更少的好消息是您需要將.set(...)添加到每個測試請求。

0

如果你要通過瀏覽器中的最後2個測試,取決於你如何設置,是的,它會工作得益於餅乾和會話,但這裏的/ api /公司測試是獨立的的auth/local測試。所以401是正確的迴應。

這實際上取決於您的身份驗證如何設置,但您還需要在/ api/firm測試中進行身份驗證。無論是通過再次發送憑證(每次我的摩卡測試中的每一次都會驗證身份)還是將會話實施到測試中,請參閱this SO帖子瞭解某些方向。

+0

我試過這個,但沒有成功,通過在獲得後添加'.auth({email:user.email,password:user.password})'但是第三次​​測試仍然會拋出401 – vonwolf

+0

你能舉一個你的摩卡的例子你在每個測試中使用的測試認證?謝謝 – vonwolf

相關問題