21

How can I test a file upload with a token being sent? I'm getting back "0" instead of a confirmation of upload.

This is a failed test:

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

This is a 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 are appreciated!

Christina Mitchell
  • 437
  • 2
  • 8
  • 17

3 Answers3

25

With supertest 4.0.2 I was able to set the token and attach the file:

import * as request from 'supertest';

return request(server)
  .post('/route')
  .set('Authorization', 'bearer ' + token)
  .attach('name', 'file/path/name.txt');

And even better, per the docs, you can make a Buffer object to attach:

const buffer = Buffer.from('some data');

return request(server)
  .post('/route')
  .set('Authorization', 'bearer ' + token)
  .attach('name', buffer, 'custom_file_name.txt');
Derek Hill
  • 5,965
  • 5
  • 55
  • 74
4

It seems like the token field is overrided when attaching a file. My workaround is to add token to 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 be set to extract the JWT from URL query parameter. Passport-JWT performs this extraction on my server.

Stav1
  • 132
  • 2
  • 11
3

The official document states

When you use .field() or .attach() you can't use .send() and you must not set Content-Type (the correct type will be set for you).

So replace .send({"token": config.token}) with .field("token", config.token)

Mike Mat
  • 632
  • 7
  • 15