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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 1x 4x 1x 1x 1x 1x 1x | /* eslint-disable @typescript-eslint/no-explicit-any */
import config from '../../../../config';
import supertest from 'supertest';
import db, {syncPromise} from '../../../../db';
import {TestUtils} from '../../../../util/test-utils.spec';
import app from '../../../../app';
import {expect} from 'chai';
import {User} from '../../../../models';
describe('get /api/user/search', function() {
const csrfHeaderName = config.jwt.securityOptions.tokenName.toLowerCase();
let agent: supertest.SuperTest<supertest.Test>;
let csrf: string;
beforeEach(async function() {
await syncPromise;
await db.sync({force: true});
const response = await TestUtils.signUp();
agent = response.agent;
csrf = response.csrf;
});
describe('if user is not logged in', function() {
it('responses with UnauthorizedError', function() {
return supertest(app).get('/api/user/search?filter=test')
.set(csrfHeaderName, csrf)
.send()
.expect(401);
});
});
describe('if user is logged in', function() {
describe('responses with BadRequestError', function() {
it('if filter query does not exist', function() {
return agent.get('/api/user/search?')
.set(csrfHeaderName, csrf)
.send()
.expect(400)
.then((res) => {
expect(res.body.message).to.contain('filter has to be set');
});
});
it('if limit query is defined but is not numeric', function() {
return agent.get(`/api/user/search?filter=test&limit=test`)
.set(csrfHeaderName, csrf)
.send()
.expect(400)
.then((res) => {
expect(res.body.message)
.to.contain('limit has to be a number');
});
});
it('if filter query is not a string', function() {
return agent.get(`/api/user/search?filter[]=test&filter[]=other`)
.set(csrfHeaderName, csrf)
.send()
.expect(400)
.then((res) => {
expect(res.body.message)
.to.contain('filter has to be a string');
});
});
});
it('responses with list of users', async function() {
// Put a few users into the database
const expectedList: any = [];
for (let i = 0; i < 5; i++) {
const user = await User.create({
username: `user_${i}`,
password: `user_${i}_password`,
email: `user_${i}@mail.com`,
});
expectedList.push({id: user.id, username: user.username});
}
// Put users into the database which should not match query
for (let i = 0; i <4; i++) {
await User.create({
username: `other_${i}`,
password: `other_${i}_password`,
email: `other_${i}@mail.com`,
});
}
const users = await agent.get(`/api/user/search?filter=user`)
.set(csrfHeaderName, csrf)
.send()
.expect(200)
.then((res) => res.body.users);
expect(users).to.be.an('array');
expect(users).to.have.length(5);
expect(users).to.eql(expectedList);
});
it('responses with list of users but limits to ' +
'specified limit', async function() {
// Put a few users into the database
const expectedList: any[] = [];
for (let i = 0; i < 5; i++) {
const user = await User.create({
username: `user_${i}`,
password: `user_${i}_password`,
email: `user_${i}@mail.com`,
});
expectedList.push({id: user.id, username: user.username});
}
// Put users into the database which should not match query
for (let i = 0; i <4; i++) {
await User.create({
username: `other_${i}`,
password: `other_${i}_password`,
email: `other_${i}@mail.com`,
});
}
const users = await agent.get(`/api/user/search?filter=user&limit=2`)
.set(csrfHeaderName, csrf)
.send()
.expect(200)
.then((res) => res.body.users);
expect(users).to.be.an('array');
expect(users).to.have.length(2);
expect(users).to.have.deep.members(expectedList.slice(0, 2));
});
it('does not return more users than max query limit even ' +
'if query larger', async function() {
// Put a few users into the database
const expectedList: any[] = [];
for (let i = 0; i < 30; i++) {
const user = await User.create({
username: `user_${i}`,
password: `user_${i}_password`,
email: `user_${i}@mail.com`,
});
expectedList.push({id: user.id, username: user.username});
}
// Put users into the database which should not match query
for (let i = 0; i <4; i++) {
await User.create({
username: `other_${i}`,
password: `other_${i}_password`,
email: `other_${i}@mail.com`,
});
}
const users = await agent.get(`/api/user/search?filter=user&limit=30`)
.set(csrfHeaderName, csrf)
.send()
.expect(200)
.then((res) => res.body.users);
expect(users).to.be.an('array');
expect(users).to.have.length(20);
expect(expectedList).to.include.deep.members(users);
});
it('responses with maximum amount of users if limit ' +
'not defined', async function() {
// Put a few users into the database
const expectedList: any[] = [];
for (let i = 0; i < 30; i++) {
const user = await User.create({
username: `user_${i}`,
password: `user_${i}_password`,
email: `user_${i}@mail.com`,
});
expectedList.push({id: user.id, username: user.username});
}
// Put users into the database which should not match query
for (let i = 0; i <4; i++) {
await User.create({
username: `other_${i}`,
password: `other_${i}_password`,
email: `other_${i}@mail.com`,
});
}
const users = await agent.get(`/api/user/search?filter=user`)
.set(csrfHeaderName, csrf)
.send()
.expect(200)
.then((res) => res.body.users);
expect(users).to.be.an('array');
expect(users).to.have.length(20);
expect(expectedList).to.include.deep.members(users);
});
});
});
|