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 | 1x 1x 1x 1x 1x 1x 1x 8x 4x 4x 4x 1x 8x 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 | import {
TestContext,
TestUtils,
} from '../../../../../../../util/test-utils.spec';
import supertest, {agent as unauthorizedAgent} from 'supertest';
import app from '../../../../../../../app';
import {expect} from 'chai';
import {
CarNotFoundError,
MembershipNotFoundError,
NotAdminOfGroupError,
} from '../../../../../../../errors';
import {
CarColor,
CarRepository,
Group,
Membership,
User,
} from '../../../../../../../models';
describe('delete /api/group/:groupId/car/:carId', function() {
/**
* Creates the url to delete the car of the group.
* @param groupId - Group ID
* @param carId - Car ID
*/
function createUrl(groupId: number, carId: number) {
return `/api/group/${groupId}/car/${carId}`;
}
/**
* Creates a test group and its owner.
*
* @returns The test group and its owner
*/
async function setupTestGroup() {
// Create other user which will be owner of test group
const owner = await User.create({
username: 'OWNER',
email: 'OWNER@MAIL.COM',
password: 'OWNER_PASSWORD',
isBetaUser: false,
});
// Create test group
const group = await Group.create({
name: 'TEST_GROUP',
ownerId: owner.id,
});
return {group, owner};
}
let agent: supertest.SuperAgentTest;
let user: TestContext['user'];
beforeEach(async function() {
({agent, user} = await TestUtils.setupIntegrationTest());
});
it('rejects with UnauthorizedError if user is not logged in', function() {
return unauthorizedAgent(app)
.delete(createUrl(1, 1))
.send()
.expect(401);
});
describe('rejects with BadRequestError if ', function() {
describe('groupId ', function() {
it('is not parseable as an integer', function() {
return agent
.delete(createUrl('NOT_THE_GROUP_ID' as unknown as number, 42))
.send()
.expect(400)
.then((res) => {
expect(res.body.message).to.include('groupId has to be a number');
});
});
});
describe('carId ', function() {
it('is not parseable as an integer', function() {
return agent
.delete(createUrl(42, 'NOT_THE_CAR_ID' as unknown as number))
.send()
.expect(400)
.then((res) => {
expect(res.body.message).to.include('carId has to be a number');
});
});
});
});
it('if the group doesn\'t exist, ' +
'reject with MembershipNotFoundError', function() {
return agent
.delete(createUrl(1, 1))
.send()
.expect(404)
.then((res) => {
expect(res.body.message).to
.eql(new MembershipNotFoundError(
{groupId: 1, userId: user.id},
).message);
expect(res.body.detail.errorName).to
.eql(MembershipNotFoundError.name);
});
});
it('if the user is not a member of the group, ' +
'reject with MembershipNotFoundError', async function() {
const {group} = await setupTestGroup();
return agent
.delete(createUrl(group.id, 1))
.send()
.expect(404)
.then((res) => {
expect(res.body.message).to
.eql(new MembershipNotFoundError(
{groupId: group.id, userId: user.id},
).message);
expect(res.body.detail.errorName).to
.eql(MembershipNotFoundError.name);
});
});
it('if the user is a member of the group but not an admin, ' +
'reject with NotAdminOfGroupError', async function() {
const {group} = await setupTestGroup();
// Make the user a member but not an admin of that group
await Membership.create({
userId: user.id,
groupId: group.id,
isAdmin: false, // Important that user is not an admin
});
// Test if admin check is correct
await agent
.delete(createUrl(group.id, 1))
.send()
.expect(401)
.then((res) => {
expect(res.body.message).to.eql(new NotAdminOfGroupError().message);
expect(res.body.detail.errorName).to.eql(NotAdminOfGroupError.name);
});
});
it('if the user is an admin of the group but the car doesn\'t exist, ' +
'reject with CarNotFoundError', async function() {
const {group} = await setupTestGroup();
// Make the user admin of group
await Membership.create({
userId: user.id,
groupId: group.id,
isAdmin: true, // Important that user is admin of the group
});
// Test if admin check is correct
await agent
.delete(createUrl(group.id, 1))
.send()
.expect(404)
.then((res) => {
expect(res.body.message).to
.eql(new CarNotFoundError(group.id, 1).message);
expect(res.body.detail.errorName).to.eql(CarNotFoundError.name);
});
});
it('if the user is an admin of the group and the car exists, ' +
'delete the car and respond with an empty message and ' +
'status code 204', async function() {
const {group} = await setupTestGroup();
// Make the user admin of group
await Membership.create({
userId: user.id,
groupId: group.id,
isAdmin: true, // Important that user is admin of the group
});
// Create car to test deletion
const car = await CarRepository.create(group.id, 'TEST_CAR', CarColor.Blue);
await agent
.delete(createUrl(group.id, car.carId))
.send()
.expect(204)
.then((res) => {
expect(res.body.message).to.be.undefined;
});
});
});
|