aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-10-08 15:51:38 +0200
committerChocobozzz <me@florianbigard.com>2018-10-08 15:55:32 +0200
commit24b9417cec5cc785a57b2fe169a1ae88b88801a4 (patch)
tree42adf8fb4355c96fe2585ebd6312563e8182d682 /server
parent791645e620fb98c6e7c32271d91d91ff7e41b892 (diff)
downloadPeerTube-24b9417cec5cc785a57b2fe169a1ae88b88801a4.tar.gz
PeerTube-24b9417cec5cc785a57b2fe169a1ae88b88801a4.tar.zst
PeerTube-24b9417cec5cc785a57b2fe169a1ae88b88801a4.zip
Add users search filter
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/users/index.ts2
-rw-r--r--server/models/account/user.ts23
-rw-r--r--server/tests/api/users/users.ts36
-rw-r--r--server/tests/utils/users/users.ts3
4 files changed, 59 insertions, 5 deletions
diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts
index 0b0081520..4f8137c03 100644
--- a/server/controllers/api/users/index.ts
+++ b/server/controllers/api/users/index.ts
@@ -238,7 +238,7 @@ async function autocompleteUsers (req: express.Request, res: express.Response, n
238} 238}
239 239
240async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) { 240async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
241 const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort) 241 const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort, req.query.search)
242 242
243 return res.json(getFormattedObjects(resultList.data, resultList.total)) 243 return res.json(getFormattedObjects(resultList.data, resultList.total))
244} 244}
diff --git a/server/models/account/user.ts b/server/models/account/user.ts
index e56b0bf40..39654cfcf 100644
--- a/server/models/account/user.ts
+++ b/server/models/account/user.ts
@@ -181,7 +181,25 @@ export class UserModel extends Model<UserModel> {
181 return this.count() 181 return this.count()
182 } 182 }
183 183
184 static listForApi (start: number, count: number, sort: string) { 184 static listForApi (start: number, count: number, sort: string, search?: string) {
185 let where = undefined
186 if (search) {
187 where = {
188 [Sequelize.Op.or]: [
189 {
190 email: {
191 [Sequelize.Op.iLike]: '%' + search + '%'
192 }
193 },
194 {
195 username: {
196 [ Sequelize.Op.iLike ]: '%' + search + '%'
197 }
198 }
199 ]
200 }
201 }
202
185 const query = { 203 const query = {
186 attributes: { 204 attributes: {
187 include: [ 205 include: [
@@ -204,7 +222,8 @@ export class UserModel extends Model<UserModel> {
204 }, 222 },
205 offset: start, 223 offset: start,
206 limit: count, 224 limit: count,
207 order: getSort(sort) 225 order: getSort(sort),
226 where
208 } 227 }
209 228
210 return UserModel.findAndCountAll(query) 229 return UserModel.findAndCountAll(query)
diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts
index 8b9c6b455..513bca8a0 100644
--- a/server/tests/api/users/users.ts
+++ b/server/tests/api/users/users.ts
@@ -180,7 +180,7 @@ describe('Test users', function () {
180 it('Should be able to upload a video again') 180 it('Should be able to upload a video again')
181 181
182 it('Should be able to create a new user', async function () { 182 it('Should be able to create a new user', async function () {
183 await createUser(server.url, accessToken, user.username,user.password, 2 * 1024 * 1024) 183 await createUser(server.url, accessToken, user.username, user.password, 2 * 1024 * 1024)
184 }) 184 })
185 185
186 it('Should be able to login with this user', async function () { 186 it('Should be able to login with this user', async function () {
@@ -322,6 +322,40 @@ describe('Test users', function () {
322 expect(users[ 1 ].nsfwPolicy).to.equal('display') 322 expect(users[ 1 ].nsfwPolicy).to.equal('display')
323 }) 323 })
324 324
325 it('Should search user by username', async function () {
326 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'oot')
327 const users = res.body.data as User[]
328
329 expect(res.body.total).to.equal(1)
330 expect(users.length).to.equal(1)
331
332 expect(users[ 0 ].username).to.equal('root')
333 })
334
335 it('Should search user by email', async function () {
336 {
337 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'r_1@exam')
338 const users = res.body.data as User[]
339
340 expect(res.body.total).to.equal(1)
341 expect(users.length).to.equal(1)
342
343 expect(users[ 0 ].username).to.equal('user_1')
344 expect(users[ 0 ].email).to.equal('user_1@example.com')
345 }
346
347 {
348 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'example')
349 const users = res.body.data as User[]
350
351 expect(res.body.total).to.equal(2)
352 expect(users.length).to.equal(2)
353
354 expect(users[ 0 ].username).to.equal('root')
355 expect(users[ 1 ].username).to.equal('user_1')
356 }
357 })
358
325 it('Should update my password', async function () { 359 it('Should update my password', async function () {
326 await updateMyUser({ 360 await updateMyUser({
327 url: server.url, 361 url: server.url,
diff --git a/server/tests/utils/users/users.ts b/server/tests/utils/users/users.ts
index 41d8ce265..d77233d62 100644
--- a/server/tests/utils/users/users.ts
+++ b/server/tests/utils/users/users.ts
@@ -112,7 +112,7 @@ function getUsersList (url: string, accessToken: string) {
112 .expect('Content-Type', /json/) 112 .expect('Content-Type', /json/)
113} 113}
114 114
115function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) { 115function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) {
116 const path = '/api/v1/users' 116 const path = '/api/v1/users'
117 117
118 return request(url) 118 return request(url)
@@ -120,6 +120,7 @@ function getUsersListPaginationAndSort (url: string, accessToken: string, start:
120 .query({ start }) 120 .query({ start })
121 .query({ count }) 121 .query({ count })
122 .query({ sort }) 122 .query({ sort })
123 .query({ search })
123 .set('Accept', 'application/json') 124 .set('Accept', 'application/json')
124 .set('Authorization', 'Bearer ' + accessToken) 125 .set('Authorization', 'Bearer ' + accessToken)
125 .expect(200) 126 .expect(200)