All files / app/routes/api/user/settings/change-password change-password-controler.unit.spec.ts

100% Statements 61/61
100% Branches 0/0
100% Functions 8/8
100% Lines 61/61

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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149  1x 1x 1x 1x 1x   1x     1x 4x                   1x 4x     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 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 {assert, restore, stub} from 'sinon';
import {UserService} from '../../../../../models';
import ChangePasswordController from './change-password-controller';
import {expect} from 'chai';
import {BadRequestError} from '../../../../../errors';
 
describe('ChangePasswordController', () => {
  let user: Express.User;
 
  beforeEach(() => {
    user = {
      id: 1,
      username: 'test',
      email: 'test@mail.com',
      isBetaUser: false,
      createdAt: new Date(100),
      updatedAt: new Date(100),
    };
  });
 
  afterEach(() => {
    restore();
  });
 
  describe('throws BadRequestError, if ', () => {
    it('oldPassword is not in body of request', async function() {
      const changePasswordStub = stub(UserService, 'changePassword');
      const req = {
        body: {
          newPassword: '123456',
        },
        user,
      };
      const res = {
        status: stub(),
        send: stub(),
      };
 
      let error;
      try {
        await ChangePasswordController(
          req as any,
          res as any,
          undefined as any,
        );
      } catch (e) {
        error = e;
      }
 
      expect(error).to.not.be.undefined;
      expect(error).to.be.instanceOf(BadRequestError);
      assert.notCalled(res.status);
      assert.notCalled(res.send);
      assert.notCalled(changePasswordStub);
    });
 
    it('newPassword is not in body of request', async function() {
      const changePasswordStub = stub(UserService, 'changePassword');
      const req = {
        body: {
          oldPassword: '123456',
        },
        user,
      };
      const res: any = {};
      res.status = stub().returns(res);
      res.send = stub();
 
      let error;
      try {
        await ChangePasswordController(
          req as any,
          res as any,
          undefined as any,
        );
      } catch (e) {
        error = e;
      }
 
      expect(error).to.not.be.undefined;
      expect(error).to.be.instanceOf(BadRequestError);
      assert.notCalled(res.status);
      assert.notCalled(res.send);
      assert.notCalled(changePasswordStub);
    });
 
    it('user is not set on request', async function() {
      const changePasswordStub = stub(UserService, 'changePassword');
      const req = {
        body: {
          oldPassword: '123456',
          newPassword: 'test',
        },
      };
      const res: any = {};
      res.status = stub().returns(res);
      res.send = stub();
 
      let error;
      try {
        await ChangePasswordController(
          req as any,
          res as any,
          undefined as any,
        );
      } catch (e) {
        error = e;
      }
 
      expect(error).to.not.be.undefined;
      expect(error).to.be.instanceOf(BadRequestError);
      assert.notCalled(res.status);
      assert.notCalled(res.send);
      assert.notCalled(changePasswordStub);
    });
  });
 
  it('calls UserService to change password with old and new password, ' +
    'and sends empty message with code 204', async function() {
    const changePasswordStub = stub(UserService, 'changePassword');
    const req = {
      body: {
        oldPassword: '123456',
        newPassword: 'test',
      },
      user,
    };
    const res: any = {};
    res.status = stub().returns(res);
    res.send = stub();
 
    await ChangePasswordController(
      req as any,
      res as any,
      undefined as any,
    );
 
    assert.calledOnceWithExactly(
        changePasswordStub,
        user,
        req.body.oldPassword,
        req.body.newPassword,
    );
    assert.calledOnceWithExactly(res.status, 204);
    assert.calledOnceWithExactly(res.send);
  });
});