All files / app/routes/user/generate-profile-pic generate-profile-pic-controller.unit.spec.ts

100% Statements 38/38
100% Branches 0/0
100% Functions 7/7
100% Lines 38/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91  1x 1x 1x   1x   1x             1x 2x   2x       1x 2x     1x   1x 1x         1x 1x   1x 1x 1x 1x 1x           1x 1x   1x     1x     1x     1x 1x                 1x 1x   1x 1x 1x 1x 1x           1x 1x   1x     1x      
/* eslint-disable @typescript-eslint/no-explicit-any */
import sinon, {createSandbox} from 'sinon';
import generateProfilePicController from './generate-profile-pic-controller';
import {UserService} from '../../../models';
 
const sandbox = createSandbox();
 
describe('GenerateProfilePicController', function() {
  let req: any;
  let res: any;
  let next: any;
  let generatePicStub: sinon.SinonStub;
  let fakeData: string;
 
  beforeEach(function() {
    fakeData = 'TEST DATA';
 
    generatePicStub = sandbox.stub(UserService, 'generateProfilePicture')
        .resolves(fakeData as any);
  });
 
  afterEach(function() {
    sandbox.restore();
  });
 
  it('creates profile picture with given username and offset', function(done) {
    // Mock express
    req = sandbox.stub();
    const queryData = {
      username: 'TEST',
      offset: 12,
    };
 
    req.query = queryData;
    req.ip = 'TEST_IP';
 
    next = sandbox.stub();
    res = sandbox.stub();
    res.type = sandbox.stub();
    res.send = sandbox.stub().callsFake(() => {
      sandbox.assert.calledOnceWithExactly(
          generatePicStub,
          req.ip,
          queryData.username,
          queryData.offset,
      );
      sandbox.assert.calledOnceWithExactly(res.type, 'image/jpeg');
      sandbox.assert.calledOnceWithExactly(res.send, fakeData);
 
      done();
    });
 
    generateProfilePicController(req, res, next);
  });
 
  it('creates profile picture with offset ' +
  '0 if no offset provided', function(done) {
    // Mock express
    req = sandbox.stub();
    const queryData = {
      username: 'TEST',
      /*
       * Because of sanitization chain the
       * parameter offset is always provided either as NaN or as number
       */
      offset: NaN,
    };
 
    req.query = queryData;
    req.ip = 'TEST_IP';
 
    next = sandbox.stub();
    res = sandbox.stub();
    res.type = sandbox.stub();
    res.send = sandbox.stub().callsFake(() => {
      sandbox.assert.calledOnceWithExactly(
          generatePicStub,
          req.ip,
          queryData.username,
          0,
      );
      sandbox.assert.calledOnceWithExactly(res.type, 'image/jpeg');
      sandbox.assert.calledOnceWithExactly(res.send, fakeData);
 
      done();
    });
 
    generateProfilePicController(req, res, next);
  });
});