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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 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 {Server} from 'socket.io';
import supertest from 'supertest';
import config from '../../../../../../../config';
import db, {syncPromise} from '../../../../../../../db';
import {
CarInUseError,
NotLoggedInError,
NotMemberOfGroupError,
} from '../../../../../../../errors';
import {
Car,
CarColor,
Group,
GroupCarAction,
User,
} from '../../../../../../../models';
import {TestUtils} from '../../../../../../../util/test-utils.spec';
describe('put /api/group/:groupId/car/:carId/drive', function() {
const csrfName = config.jwt.securityOptions.tokenName.toLowerCase();
let user: any;
let agent: supertest.SuperTest<supertest.Test>;
let csrf: string;
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();
user = response.user;
agent = response.agent;
csrf = response.csrf;
jwtValue = response.jwtValue;
});
describe('if user not logged in', function() {
it('responses with 401 NotLoggedInError', async function() {
await agent.put('/auth/logout').set(csrfName, csrf).send().expect(204);
const res = await agent.put('/api/group/1/car/1/drive')
.set(csrfName, csrf)
.send()
.expect(401);
expect(res.body.message).to.equal(new NotLoggedInError().message);
});
});
describe('if user logged in', function() {
describe('responses with 400 InvalidRequestError', function() {
it('if groupId is not a numeric', function() {
return agent.put('/api/group/test/car/1/drive')
.set(csrfName, csrf)
.send()
.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/1/car/test/drive')
.set(csrfName, csrf)
.send()
.expect(400)
.then((res) => {
expect(res.body.message).to.include('carId has to be a number');
});
});
});
it('responses with 401 NotMemberOfGroupError if logged ' +
'in user is not a member of the specified group', async function() {
const res = await agent.put('/api/group/1/car/1/drive')
.set(csrfName, csrf)
.send()
.expect(401);
expect(res.body.message).to.equal(new NotMemberOfGroupError().message);
});
it('responses with 400 CarInUseError if car ' +
'is currently in use', async function() {
// Create group
const group = await Group.create({
name: 'Test',
ownerId: user.id,
});
// Create user which will use the car
const driver = await User.create({
username: 'driver',
email: 'driver@mail.com',
password: 'driverpassword',
});
// Create car
const car = await Car.create({
name: 'car',
carId: 1,
groupId: group.id,
driverId: driver.id,
color: CarColor.Black,
});
const res = await agent
.put(`/api/group/${group.id}/car/${car.carId}/drive`)
.set(csrfName, csrf)
.send()
.expect(400);
expect(res.body.message).to.equal(new CarInUseError().message);
});
it('responses with 204 and sets the logged '+
'in user as driver', async function() {
// Create group
const group = await Group.create({
name: 'Test',
ownerId: user.id,
});
// Create car
const car = await Car.create({
name: 'car',
carId: 1,
groupId: group.id,
color: CarColor.Black,
latitude: 1.2,
longitude: 6.4,
});
await agent
.put(`/api/group/${group.id}/car/${car.carId}/drive`)
.set(csrfName, csrf)
.send()
.expect(204);
// Test if current user is driver of car
const updatedCar = await Car.findOne({
where: {
groupId: group.id,
carId: car.carId,
},
});
expect(updatedCar).to.not.be.null;
expect(updatedCar!.driverId).to.equal(user.id);
expect(updatedCar!.latitude).to.be.null;
expect(updatedCar!.longitude).to.be.null;
});
it('emit update event in group namespace ' +
'with updated car', function() {
return new Promise(async (resolve, reject) => {
try {
// Create group
const group = await Group.create({
name: 'Test',
ownerId: user.id,
});
// Create car
const car = await Car.create({
name: 'car',
carId: 1,
groupId: group.id,
color: CarColor.Black,
latitude: 1.2,
longitude: 6.4,
});
const socket = TestUtils.createSocket(
port,
`/group/${group.id}`,
jwtValue,
);
socket.on('update', async (res: any) => {
try {
expect(res.action).to.equal(GroupCarAction.Drive);
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.be.null;
expect(actualCar.longitude).to.be.null;
expect(actualCar.driverId).to.equal(user.id);
socket.close();
resolve();
} catch (e) {
reject(e);
}
});
await agent
.put(`/api/group/${group.id}/car/${car.carId}/drive`)
.set(csrfName, csrf)
.send()
.expect(204);
} catch (e) {
reject(e);
}
});
});
});
});
|