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 | 1x 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 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 {syncPromise} from '../../../../../../../../db';
import {TestUtils} from '../../../../../../../../util/test-utils.spec';
import db from '../../../../../../../../db';
import supertest from 'supertest';
import app from '../../../../../../../../app';
import config from '../../../../../../../../config';
import {expect} from 'chai';
import {Membership} from '../../../../../../../../models';
import {
NotAdminOfGroupError,
NotMemberOfGroupError,
CannotChangeOwnerMembershipError,
} from '../../../../../../../../errors';
const csrfHeaderName = config.jwt.securityOptions.tokenName.toLowerCase();
describe('put /api/group/:groupId/member/:userId/admin/revoke', function() {
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;
user = response.user;
csrf = response.csrf;
});
it('responses with 401 if user not logged in', function() {
return supertest(app).put('/api/group/1/member/1/admin/revoke')
.set(csrfHeaderName, csrf)
.send()
.expect(401);
});
it('responses with NotMemberOfGroupError if current ' +
'user is not member of the group', function() {
return agent.put('/api/group/1/member/2/admin/revoke')
.set(csrfHeaderName, csrf)
.send()
.expect(401)
.then((res) => {
expect(res.body.message).to
.equal(new NotMemberOfGroupError().message);
});
});
it('responses with NotAdminOfGroupError if current ' +
'user is an admin of the group', async function() {
const group = await agent.post('/api/group')
.set(csrfHeaderName, csrf)
.send({
name: 'NAME',
description: 'DESC',
})
.expect(201)
.then((res) => res.body);
// Create new user
const notAdminUser = await agent.post('/auth/sign-up')
.set(csrfHeaderName, csrf)
.send({
username: 'NOT_ADMIN_USER',
password: 'NOT_ADMIN_USER',
email: 'NOT_ADMIN@mail.com',
}).expect(201)
.then((res) => res.body);
// Shortcut by creating membership for new not admin user
await Membership.create({
groupId: group.id,
userId: notAdminUser.id,
isAdmin: false,
});
await agent.put(`/api/group/${group.id}/member/8/admin/revoke`)
.set(csrfHeaderName, csrf)
.send()
.expect(401)
.then((res) => {
expect(res.body.message).to.equal(new NotAdminOfGroupError().message);
});
});
it('responses with CannotChangeOwnerMembershipError ' +
'if current user tries to change admin value of owner', async function() {
const group = await agent.post('/api/group')
.set(csrfHeaderName, csrf)
.send({
name: 'NAME',
description: 'DESC',
})
.expect(201)
.then((res) => res.body);
// Create new user
const notAdminUser = await agent.post('/auth/sign-up')
.set(csrfHeaderName, csrf)
.send({
username: 'NOT_ADMIN_USER',
password: 'NOT_ADMIN_USER',
email: 'NOT_ADMIN@mail.com',
}).expect(201)
.then((res) => res.body);
// Shortcut by creating membership for new not admin user
await Membership.create({
groupId: group.id,
userId: notAdminUser.id,
isAdmin: true,
});
await agent.put(`/api/group/${group.id}/member/${user.id}/admin/revoke`)
.set(csrfHeaderName, csrf)
.send()
.expect(400)
.then((res) => {
expect(res.body.message).to
.equal(new CannotChangeOwnerMembershipError().message);
});
});
it('changes admin value to true', async function() {
const group = await agent.post('/api/group')
.set(csrfHeaderName, csrf)
.send({
name: 'NAME',
description: 'DESC',
})
.expect(201)
.then((res) => res.body);
// Create new user
const firstAdminUser = await agent.post('/auth/sign-up')
.set(csrfHeaderName, csrf)
.send({
username: 'NOT_ADMIN_USER',
password: 'NOT_ADMIN_USER',
email: 'NOT_ADMIN@mail.com',
}).expect(201)
.then((res) => res.body);
// Create admin for group
const adminUser = await agent.post('/auth/sign-up')
.set(csrfHeaderName, csrf)
.send({
username: 'ADMIN',
password: 'ADMIN_PASSWORD',
email: 'ADMIN@mail.com',
})
.expect(201)
.then((res) => res.body);
// Shortcut by creating membership for new admin user
await Membership.create({
groupId: group.id,
userId: firstAdminUser.id,
isAdmin: true,
});
// Shortcut by creating membership for another
// admin user which will revoke admin for first admin user
await Membership.create({
groupId: group.id,
userId: adminUser.id,
isAdmin: true,
});
// Send request while being logged in as admin
await agent
.put(`/api/group/${group.id}/member/${firstAdminUser.id}/admin/revoke`)
.set(csrfHeaderName, csrf)
.send()
.expect(204);
// Check if not admin user is now an admin
const notAdminMembership = await Membership.findOne({
where: {
groupId: group.id,
userId: firstAdminUser.id,
},
});
expect(notAdminMembership).to.be.not.null;
expect(notAdminMembership!.isAdmin).to.be.false;
});
});
|