mocha - How to unit test file upload with Supertest -and- send a token? -


how can test file upload token being sent? i'm getting "0" instead of confirmation of upload.

this failed test:

var chai = require('chai'); var expect = chai.expect; var config = require("../config");  // contains call supertest , 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();            });     }); }); 

this working test:

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();     }); }); 

any suggestions appreciated!

it seems token field overrided when attaching file. workaround add token url query parameter:

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();     }); }); 

your authentication middleware must set extract jwt url query parameter. passport-jwt performs extraction on server.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

javascript - Rivets.js rv-show not working with rv-each -