mirror of
https://github.com/strapi/strapi.git
synced 2025-11-09 22:59:14 +00:00
fix sort on admin user
This commit is contained in:
parent
412a834f98
commit
95d9b19588
@ -87,7 +87,7 @@ describe('User Controller', () => {
|
|||||||
|
|
||||||
await userController.findOne(ctx);
|
await userController.findOne(ctx);
|
||||||
|
|
||||||
expect(findOne).toHaveBeenCalledWith({ id: user.id });
|
expect(findOne).toHaveBeenCalledWith(user.id);
|
||||||
expect(sanitizeUser).toHaveBeenCalledWith(user);
|
expect(sanitizeUser).toHaveBeenCalledWith(user);
|
||||||
expect(ctx.body).toStrictEqual({ data: user });
|
expect(ctx.body).toStrictEqual({ data: user });
|
||||||
});
|
});
|
||||||
@ -108,7 +108,7 @@ describe('User Controller', () => {
|
|||||||
|
|
||||||
await userController.findOne(ctx);
|
await userController.findOne(ctx);
|
||||||
|
|
||||||
expect(findOne).toHaveBeenCalledWith({ id: fakeId });
|
expect(findOne).toHaveBeenCalledWith(fakeId);
|
||||||
expect(notFound).toHaveBeenCalledWith('User does not exist');
|
expect(notFound).toHaveBeenCalledWith('User does not exist');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -59,7 +59,7 @@ module.exports = {
|
|||||||
async findOne(ctx) {
|
async findOne(ctx) {
|
||||||
const { id } = ctx.params;
|
const { id } = ctx.params;
|
||||||
|
|
||||||
const user = await getService('user').findOne({ id });
|
const user = await getService('user').findOne(id);
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return ctx.notFound('User does not exist');
|
return ctx.notFound('User does not exist');
|
||||||
|
|||||||
@ -383,16 +383,18 @@ describe('User', () => {
|
|||||||
const defaults = { page: 1, pageSize: 100 };
|
const defaults = { page: 1, pageSize: 100 };
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const fetchPage = jest.fn(({ page = defaults.page, pageSize = defaults.pageSize } = {}) => {
|
const findPage = jest.fn(
|
||||||
|
(uid, { page = defaults.page, pageSize = defaults.pageSize } = {}) => {
|
||||||
return {
|
return {
|
||||||
results: Array.from({ length: pageSize }).map((_, i) => i + (page - 1) * pageSize),
|
results: Array.from({ length: pageSize }).map((_, i) => i + (page - 1) * pageSize),
|
||||||
pagination: { page, pageSize, total: page * pageSize, pageCount: page },
|
pagination: { page, pageSize, total: page * pageSize, pageCount: page },
|
||||||
};
|
};
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
global.strapi = {
|
global.strapi = {
|
||||||
query() {
|
entityService: {
|
||||||
return { findPage: fetchPage };
|
findPage,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@ -428,7 +430,7 @@ describe('User', () => {
|
|||||||
const user = { firstname: 'Kai', lastname: 'Doe', email: 'kaidoe@email.com' };
|
const user = { firstname: 'Kai', lastname: 'Doe', email: 'kaidoe@email.com' };
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const findOne = jest.fn(({ where: { id } }) =>
|
const findOne = jest.fn((uid, id) =>
|
||||||
Promise.resolve(
|
Promise.resolve(
|
||||||
{
|
{
|
||||||
1: user,
|
1: user,
|
||||||
@ -437,23 +439,23 @@ describe('User', () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
global.strapi = {
|
global.strapi = {
|
||||||
query() {
|
entityService: {
|
||||||
return { findOne };
|
findOne,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Finds and returns a user by its ID', async () => {
|
test('Finds and returns a user by its ID', async () => {
|
||||||
const input = { id: 1 };
|
const id = 1;
|
||||||
const res = await userService.findOne(input);
|
const res = await userService.findOne(id);
|
||||||
|
|
||||||
expect(res).not.toBeNull();
|
expect(res).not.toBeNull();
|
||||||
expect(res).toMatchObject(user);
|
expect(res).toMatchObject(user);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Fails to find a user with provided params', async () => {
|
test('Fails to find a user with provided params', async () => {
|
||||||
const input = { id: 27 };
|
const id = 27;
|
||||||
const res = await userService.findOne(input);
|
const res = await userService.findOne(id);
|
||||||
|
|
||||||
expect(res).toBeNull();
|
expect(res).toBeNull();
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
const { defaults } = require('lodash/fp');
|
||||||
const { stringIncludes } = require('@strapi/utils');
|
const { stringIncludes } = require('@strapi/utils');
|
||||||
const { createUser, hasSuperAdminRole } = require('../domain/user');
|
const { createUser, hasSuperAdminRole } = require('../domain/user');
|
||||||
const { password: passwordValidator } = require('../validation/common-validators');
|
const { password: passwordValidator } = require('../validation/common-validators');
|
||||||
@ -103,7 +104,7 @@ const updateById = async (id, attributes) => {
|
|||||||
* @param {string} password - new password
|
* @param {string} password - new password
|
||||||
*/
|
*/
|
||||||
const resetPasswordByEmail = async (email, password) => {
|
const resetPasswordByEmail = async (email, password) => {
|
||||||
const user = await findOne({ email });
|
const user = await strapi.query('admin::user').findOne({ where: { email }, populate: ['roles'] });
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new Error(`User not found for email: ${email}`);
|
throw new Error(`User not found for email: ${email}`);
|
||||||
@ -125,7 +126,7 @@ const resetPasswordByEmail = async (email, password) => {
|
|||||||
* @param {int|string} userId user's id to look for
|
* @param {int|string} userId user's id to look for
|
||||||
*/
|
*/
|
||||||
const isLastSuperAdminUser = async userId => {
|
const isLastSuperAdminUser = async userId => {
|
||||||
const user = await findOne({ id: userId }, ['roles']);
|
const user = await findOne(userId, ['roles']);
|
||||||
const superAdminRole = await getService('role').getSuperAdminWithUsersCount();
|
const superAdminRole = await getService('role').getSuperAdminWithUsersCount();
|
||||||
|
|
||||||
return superAdminRole.usersCount === 1 && hasSuperAdminRole(user);
|
return superAdminRole.usersCount === 1 && hasSuperAdminRole(user);
|
||||||
@ -180,8 +181,8 @@ const register = async ({ registrationToken, userInfo }) => {
|
|||||||
/**
|
/**
|
||||||
* Find one user
|
* Find one user
|
||||||
*/
|
*/
|
||||||
const findOne = async (where = {}, populate = ['roles']) => {
|
const findOne = async (id, populate = ['roles']) => {
|
||||||
return strapi.query('admin::user').findOne({ where, populate });
|
return strapi.entityService.findOne('admin::user', id, { populate });
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Find many users (paginated)
|
/** Find many users (paginated)
|
||||||
@ -189,15 +190,8 @@ const findOne = async (where = {}, populate = ['roles']) => {
|
|||||||
* @returns {Promise<user>}
|
* @returns {Promise<user>}
|
||||||
*/
|
*/
|
||||||
const findPage = async (query = {}) => {
|
const findPage = async (query = {}) => {
|
||||||
const { page = 1, pageSize = 100, populate = ['roles'] } = query;
|
const enrichedQuery = defaults({ populate: ['roles'] }, query);
|
||||||
|
return strapi.entityService.findPage('admin::user', enrichedQuery);
|
||||||
return strapi.query('admin::user').findPage({
|
|
||||||
where: query.filters,
|
|
||||||
_q: query._q,
|
|
||||||
populate,
|
|
||||||
page,
|
|
||||||
pageSize,
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Delete a user
|
/** Delete a user
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user