2016-07-15 5 views
0

トークンを送信してファイルアップロードをテストするにはどうすればよいですか?私はアップロードの確認の代わりに "0"を返しています。テストファイルのアップロードをSupertestと連動させる方法と、トークンを送信する方法は?

は、これは失敗したテストです:

var chai = require('chai'); 
var expect = chai.expect; 
var config = require("../config"); // contains call to supertest and token info 

    describe('Upload Endpoint', function(){ 

    it('Attach photos - should return 200 response & accepted text', function (done){ 
     this.timeout(15000); 
     setTimeout(done, 15000); 
     config.api.post('/customer/upload') 
       .set('Accept', 'application.json') 
       .send({"token": config.token}) 
       .field('vehicle_vin', "randomVIN") 
       .attach('file', '/Users/moi/Desktop/unit_test_extravaganza/hardwork.jpg') 

       .end(function(err, res) { 
        expect(res.body.ok).to.equal(true); 
        expect(res.body.result[0].web_link).to.exist; 
       done(); 
      }); 
    }); 
}); 

これはワーキングテストです:

describe('Upload Endpoint - FL token ', function(){ 
    this.timeout(15000); 
    it('Press Send w/out attaching photos returns error message', function (done){ 
    config.api.post('/customer/upload') 
     .set('Accept', 'application.json') 
     .send({"token": config.token }) 
     .expect(200) 
     .end(function(err, res) { 
     expect(res.body.ok).to.equal(false); 
     done(); 
    }); 
}); 

任意の提案が高く評価されています!

答えて

0

ファイルを添付するときにトークンフィールドが上書きされるようです。私の問題を回避するには、URLのクエリパラメータにトークンを追加することです:

describe('Upload Endpoint - FL token ', function(){ 
    this.timeout(15000); 
    it('Press Send w/out attaching photos returns error message', function (done){ 
    config.api.post('/customer/upload/?token='+config.token) 
     .attach('file', '/Users/moi/Desktop/unit_test_extravaganza/hardwork.jpg') 
     .expect(200) 
     .end(function(err, res) { 
     expect(res.body.ok).to.equal(false); 
     done(); 
    }); 
}); 

あなたの認証ミドルウェアは、URLクエリパラメータからJWTを抽出するために設定する必要があります。 Passport-JWTは私のサーバーでこの抽出を実行します。

関連する問題