All files / app/routes/api/group/group-id/car/get get-cars.integration.spec.ts

100% Statements 65/65
100% Branches 0/0
100% Functions 14/14
100% Lines 63/63

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 150 151 152 153 154 155  1x 1x 1x 1x 1x 1x     1x 1x   1x 1x         1x 5x 5x   5x   5x 5x 5x     1x 1x 1x             1x 1x   1x         1x       1x   1x         1x         1x   1x           1x   1x 4x             4x 4x 4x 4x 4x     1x       1x   1x 1x 1x 1x     1x     1x           1x   1x 4x             4x 4x 4x 4x 4x       1x           1x             1x       1x   1x 1x 1x 1x 1x 4x        
/* eslint-disable @typescript-eslint/no-explicit-any */
import {expect} from 'chai';
import supertest from 'supertest';
import app from '../../../../../../app';
import config from '../../../../../../config';
import db, {syncPromise} from '../../../../../../db';
import {
  NotMemberOfGroupError,
} from '../../../../../../errors';
import {Car, CarColor, Group} from '../../../../../../models';
import {TestUtils} from '../../../../../../util/test-utils.spec';
 
describe('get /api/group/:groupId/car', function() {
  const csrfName = config.jwt.securityOptions.tokenName.toLowerCase();
  let agent: supertest.SuperTest<supertest.Test>;
  let user: any;
  let csrf: string;
 
  beforeEach(async function() {
    await syncPromise;
    await db.sync({force: true});
 
    const response = await TestUtils.signUp();
 
    agent = response.agent;
    csrf = response.csrf;
    user = response.user;
  });
 
  describe('if user not logged in', function() {
    it('responses with 401 UnauthorizedError', function() {
      return supertest(app).get('/api/group/4/car')
          .set(csrfName, csrf)
          .send()
          .expect(401);
    });
  });
 
  describe('if user logged in', function() {
    it('responses with 400 InvalidRequestError ' +
    'if groupId is not numeric', function() {
      return agent.get('/api/group/test/car')
          .set(csrfName, csrf)
          .send()
          .expect(400)
          .then((res) => {
            expect(res.body.message).to.include('groupId has to be a number');
          });
    });
 
    it('responses with 401 NotMemberOfGroup ' +
    'if user is not a member of the group', function() {
      return agent.get('/api/group/4/car')
          .set(csrfName, csrf)
          .send()
          .expect(401)
          .then((res) => {
            expect(res.body.message).to
                .equal(new NotMemberOfGroupError().message);
          });
    });
 
    it('responses with 200 and the list of cars', async function() {
      // Create group
      const group = await Group.create({
        name: 'GROUP',
        description: 'TEST',
        ownerId: user.id,
      });
 
      const expectedCars = [];
      // Create cars for the group
      for (let i = 0; i < 4; i++) {
        const car = await Car.create({
          groupId: group.id,
          name: `CAR-${i}`,
          color: Object.values(CarColor)[i],
          carId: i + 1,
        });
 
        const carObject = car.get({plain: true}) as any;
        carObject.createdAt = carObject.createdAt.toISOString();
        carObject.updatedAt = carObject.updatedAt.toISOString();
        carObject.Driver = null;
        expectedCars.push(car.get({plain: true}));
      }
 
      const response = await agent.get(`/api/group/${group.id}/car`)
          .set(csrfName, csrf)
          .send()
          .expect(200)
          .then((res) => res.body);
 
      expect(response).to.haveOwnProperty('cars');
      expect(response.cars).to.be.an('array');
      expect(response.cars).to.have.length(expectedCars.length);
      expect(response.cars).to.eql(expectedCars);
    });
 
    it('will only response with the cars of the ' +
    'specified group and not of other groups', async function() {
      // Create group
      const group = await Group.create({
        name: 'GROUP',
        description: 'TEST',
        ownerId: user.id,
      });
 
      const expectedCars = [];
      // Create cars for the group
      for (let i = 0; i < 4; i++) {
        const car = await Car.create({
          groupId: group.id,
          name: `CAR-${i}`,
          color: Object.values(CarColor)[i],
          carId: i + 1,
        });
 
        const carObject = car.get({plain: true}) as any;
        carObject.createdAt = carObject.createdAt.toISOString();
        carObject.updatedAt = carObject.updatedAt.toISOString();
        carObject.Driver = null;
        expectedCars.push(car.get({plain: true}));
      }
 
      // Create other group and cars
      const otherGroup = await Group.create({
        name: 'OTHER',
        description: 'OTHER',
        ownerId: user.id,
      });
 
      const otherCar = await Car.create({
        groupId: otherGroup.id,
        name: 'OTHER-CAR',
        color: 'Blue',
        carId: 1,
      });
 
      const response = await agent.get(`/api/group/${group.id}/car`)
          .set(csrfName, csrf)
          .send()
          .expect(200)
          .then((res) => res.body);
 
      expect(response).to.haveOwnProperty('cars');
      expect(response.cars).to.be.an('array');
      expect(response.cars).to.have.length(expectedCars.length);
      expect(response.cars).to.eql(expectedCars);
      expect(response.cars.some((car: any) =>
        car == otherCar)).to.be.false;
    });
  });
});