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

98.11% Statements 104/106
100% Branches 0/0
100% Functions 38/38
98.11% Lines 104/106

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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326    1x   1x 1x 1x         1x 1x               1x 1x               1x 1x 1x 1x     1x 1x     1x 15x 15x   15x 15x 15x 15x 15x     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   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-non-null-assertion */
/* eslint-disable @typescript-eslint/no-explicit-any */
import {expect} from 'chai';
import supertest from 'supertest';
import config from '../../../../../../../config';
import db, {syncPromise} from '../../../../../../../db';
import {
  NotDriverOfCarError,
  NotLoggedInError,
  NotMemberOfGroupError,
} from '../../../../../../../errors';
import {TestUtils} from '../../../../../../../util/test-utils.spec';
import {
  Car,
  CarColor,
  Group,
  GroupCarAction,
} from '../../../../../../../models';
import {Server} from 'socket.io';
 
describe('put /api/group/:groupId/car/:carId/park', function() {
  const csrfName = config.jwt.securityOptions.tokenName.toLowerCase();
  let user: any;
  let csrf: string;
  let agent: supertest.SuperTest<supertest.Test>;
  let port: number;
  let io: Server;
  let jwtValue: string;
 
  before(async function() {
    const socketIo = await TestUtils.startSocketIo();
    port = socketIo.port;
    io = socketIo.io;
  });
 
  after(function() {
    io.close();
  });
 
  beforeEach(async function() {
    await syncPromise;
    await db.sync({force: true});
 
    const response = await TestUtils.signUp();
    csrf = response.csrf;
    user = response.user;
    agent = response.agent;
    jwtValue = response.jwtValue;
  });
 
  describe('if user not logged in', function() {
    it('responses 401 NotLoggedInError', async function() {
      await agent.put('/auth/logout')
          .set(csrfName, csrf)
          .send()
          .expect(204);
 
      return agent.put('/api/group/5/car/8/park')
          .set(csrfName, csrf)
          .send({latitude: 1, longitude: 1})
          .expect(401)
          .then((res) => {
            expect(res.body.message).to.be
                .equal(new NotLoggedInError().message);
          });
    });
  });
 
  describe('if user logged in', function() {
    describe('responses with 401 InvalidRequestError', function() {
      it('if groupId is not numeric', function() {
        return agent.put('/api/group/test/car/8/park')
            .set(csrfName, csrf)
            .send({latitude: 2, longitude: 3})
            .expect(400)
            .then((res) => {
              expect(res.body.message)
                  .to.include('groupId has to be a number');
            });
      });
 
      it('if carId is not numeric', function() {
        return agent.put('/api/group/4/car/test/park')
            .set(csrfName, csrf)
            .send({latitude: 2, longitude: 3})
            .expect(400)
            .then((res) => {
              expect(res.body.message)
                  .to.include('carId has to be a number');
            });
      });
 
      describe('if latitude is', function() {
        it('is missing', function() {
          return agent.put('/api/group/5/car/8/park')
              .set(csrfName, csrf)
              .send({longitude: 13})
              .expect(400)
              .then((res) => {
                expect(res.body.message)
                    .to.include('latitude is missing');
              });
        });
 
        it('is not a float', function() {
          return agent.put('/api/group/3/car/8/park')
              .set(csrfName, csrf)
              .send({latitude: 'test', longitude: 3})
              .expect(400)
              .then((res) => {
                expect(res.body.message)
                    .to.include('latitude has to be a number ' +
                    'between -90 and 90');
              });
        });
 
        it('lower than -90', function() {
          return agent.put('/api/group/5/car/8/park')
              .set(csrfName, csrf)
              .send({latitude: -91, longitude: 2})
              .expect(400)
              .then((res) => {
                expect(res.body.message)
                    .to.include('latitude has to be a ' +
                    'number between -90 and 90');
              });
        });
 
        it('higher than 90', function() {
          return agent.put('/api/group/5/car/8/park')
              .set(csrfName, csrf)
              .send({latitude: 91, longitude: 2})
              .expect(400)
              .then((res) => {
                expect(res.body.message)
                    .to.include('latitude has to be a ' +
                    'number between -90 and 90');
              });
        });
      });
 
      describe('if longitude is', function() {
        it('is missing', function() {
          return agent.put('/api/group/5/car/8/park')
              .set(csrfName, csrf)
              .send({latitude: 2})
              .expect(400)
              .then((res) => {
                expect(res.body.message)
                    .to.include('longitude is missing');
              });
        });
 
        it('is not a float', function() {
          return agent.put('/api/group/5/car/8/park')
              .set(csrfName, csrf)
              .send({latitude: 2, longitude: 'test'})
              .expect(400)
              .then((res) => {
                expect(res.body.message)
                    .to.include('longitude has to be a '+
                        'number between -180 and 180');
              });
        });
 
        it('is lower than -180', function() {
          return agent.put('/api/group/5/car/8/park')
              .set(csrfName, csrf)
              .send({latitude: 11, longitude: -181})
              .expect(400)
              .then((res) => {
                expect(res.body.message)
                    .to.include('longitude has to be a '+
                    'number between -180 and 180');
              });
        });
 
        it('is higher than 180', function() {
          return agent.put('/api/group/5/car/8/park')
              .set(csrfName, csrf)
              .send({latitude: 1, longitude: 181})
              .expect(400)
              .then((res) => {
                expect(res.body.message)
                    .to.include('longitude has to be a '+
                    'number between -180 and 180');
              });
        });
      });
    });
 
    it('responses with 401 NotMemberOfGroupError if ' +
    'user is not a member of the group', async function() {
      return agent.put('/api/group/5/car/8/park')
          .set(csrfName, csrf)
          .send({latitude: 1, longitude: 2})
          .expect(401)
          .then((res) => {
            expect(res.body.message)
                .to.include(new NotMemberOfGroupError().message);
          });
    });
 
    it('responses with 400 NotDriverOfCarError if user is ' +
    'not the driver of the car', async function() {
      const group = await Group.create({
        name: 'Group',
        ownerId: user.id,
      });
 
      const car = await Car.create({
        name: 'Car',
        groupId: group.id,
        color: CarColor.Black,
        carId: 1,
      });
 
      const res = await agent
          .put(`/api/group/${group.id}/car/${car.carId}/park`)
          .set(csrfName, csrf)
          .send({latitude: 1, longitude: 1})
          .expect(400);
 
      expect(res.body.message).to.equal(new NotDriverOfCarError().message);
 
      const newCar = await Car.findOne({
        where: {groupId: group.id, carId: car.carId},
      }) as Car;
 
      expect(newCar).to.be.not.null;
 
      expect(newCar.get({plain: true})).to.eql(car.get({plain: true}));
    });
 
    it('responses with 204 and sets driver to null ' +
    'and location if user is driver', async function() {
      const group = await Group.create({
        name: 'Group',
        ownerId: user.id,
      });
 
      const car = await Car.create({
        name: 'Car',
        groupId: group.id,
        color: CarColor.Black,
        driverId: user.id,
        carId: 1,
      });
 
      const latitude = 65;
      const longitude = 41;
 
      await agent
          .put(`/api/group/${group.id}/car/${car.carId}/park`)
          .set(csrfName, csrf)
          .send({latitude, longitude})
          .expect(204);
 
      // Check if car is updated
      const updatedCar = await Car.findOne({where: {
        groupId: group.id,
        carId: car.carId,
      }}) as Car;
 
      expect(updatedCar.driverId).to.be.null;
      expect(updatedCar.latitude).to.equal(latitude);
      expect(updatedCar.longitude).to.equal(longitude);
    });
 
    it('emits update event in group namespace ' +
    'with park action', function() {
      return new Promise(async (resolve, reject) => {
        try {
          const group = await Group.create({
            name: 'Group',
            ownerId: user.id,
          });
 
          const car = await Car.create({
            name: 'Car',
            groupId: group.id,
            color: CarColor.Black,
            driverId: user.id,
            carId: 1,
          });
 
          const socket = TestUtils.createSocket(
              port,
              `/group/${group.id}`,
              jwtValue,
          );
 
          const latitude = 65;
          const longitude = 41;
 
          socket.on('update', async (res: any) => {
            try {
              expect(res.action).to.equal(GroupCarAction.Park);
              const actualCar = res.car;
              expect(actualCar).to.be.an('object');
              expect(actualCar.color).to.equal(car.color);
              expect(actualCar.carId).to.equal(car.carId);
              expect(actualCar.groupId).to.equal(car.groupId);
              expect(actualCar.latitude).to.equal(latitude);
              expect(actualCar.longitude).to.equal(longitude);
              expect(actualCar.driverId).to.be.null;
              socket.close();
              resolve();
            } catch (e) {
              reject(e);
            }
          });
 
          await agent
              .put(`/api/group/${group.id}/car/${car.carId}/park`)
              .set(csrfName, csrf)
              .send({latitude, longitude})
              .expect(204);
        } catch (e) {
          reject(e);
        }
      });
    });
  });
});