mirror of
https://github.com/strapi/strapi.git
synced 2025-11-23 13:40:58 +00:00
Merge branch 'releases/v4' into v4/graphql-schema-generation-refactor
This commit is contained in:
commit
c7a0747f96
@ -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,5 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const { toLower } = require('lodash/fp');
|
||||||
const { Strategy: LocalStrategy } = require('passport-local');
|
const { Strategy: LocalStrategy } = require('passport-local');
|
||||||
|
|
||||||
const createLocalStrategy = strapi => {
|
const createLocalStrategy = strapi => {
|
||||||
@ -11,7 +12,7 @@ const createLocalStrategy = strapi => {
|
|||||||
},
|
},
|
||||||
(email, password, done) => {
|
(email, password, done) => {
|
||||||
return strapi.admin.services.auth
|
return strapi.admin.services.auth
|
||||||
.checkCredentials({ email, password })
|
.checkCredentials({ email: toLower(email), password })
|
||||||
.then(([error, user, message]) => done(error, user, message))
|
.then(([error, user, message]) => done(error, user, message))
|
||||||
.catch(error => done(error));
|
.catch(error => done(error));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
||||||
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
|
||||||
|
|||||||
@ -193,8 +193,6 @@ describe('Publication State', () => {
|
|||||||
products = res.body.data.map(res => ({ id: res.id, ...res.attributes }));
|
products = res.body.data.map(res => ({ id: res.id, ...res.attributes }));
|
||||||
});
|
});
|
||||||
|
|
||||||
const getApiRef = id => data.product.find(product => product.id === id);
|
|
||||||
|
|
||||||
test('Payload integrity', () => {
|
test('Payload integrity', () => {
|
||||||
expect(products).toHaveLength(lengthFor(contentTypes.product.name));
|
expect(products).toHaveLength(lengthFor(contentTypes.product.name));
|
||||||
});
|
});
|
||||||
@ -205,27 +203,31 @@ describe('Publication State', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('First level (categories)', () => {
|
// const getApiRef = id => data.product.find(product => product.id === id);
|
||||||
products.forEach(({ id, categories }) => {
|
|
||||||
const length = getApiRef(id).categories.filter(c => c.publishedAt !== null).length;
|
|
||||||
expect(categories).toHaveLength(length);
|
|
||||||
|
|
||||||
categories.forEach(category => {
|
test.todo('First level (categories)');
|
||||||
expect(category.publishedAt).toBeISODate();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Second level through component (countries)', () => {
|
// products.forEach(({ id, categories }) => {
|
||||||
products.forEach(({ id, comp: { countries } }) => {
|
// const length = getApiRef(id).categories.filter(c => c.publishedAt !== null).length;
|
||||||
const length = getApiRef(id).comp.countries.filter(c => c.publishedAt !== null).length;
|
// expect(categories).toHaveLength(length);
|
||||||
expect(countries).toHaveLength(length);
|
|
||||||
|
|
||||||
countries.forEach(country => {
|
// categories.forEach(category => {
|
||||||
expect(country.publishedAt).toBeISODate();
|
// expect(category.publishedAt).toBeISODate();
|
||||||
});
|
// });
|
||||||
});
|
// });
|
||||||
});
|
// });
|
||||||
|
|
||||||
|
test.todo('Second level through component (countries)');
|
||||||
|
|
||||||
|
// products.forEach(({ id, comp: { countries } }) => {
|
||||||
|
// const length = getApiRef(id).comp.countries.filter(c => c.publishedAt !== null).length;
|
||||||
|
// expect(countries).toHaveLength(length);
|
||||||
|
|
||||||
|
// countries.forEach(country => {
|
||||||
|
// expect(country.publishedAt).toBeISODate();
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
// });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -5,7 +5,7 @@ const _ = require('lodash');
|
|||||||
const createUtils = strapi => {
|
const createUtils = strapi => {
|
||||||
const login = async userInfo => {
|
const login = async userInfo => {
|
||||||
const sanitizedUserInfo = _.pick(userInfo, ['email', 'id']);
|
const sanitizedUserInfo = _.pick(userInfo, ['email', 'id']);
|
||||||
const user = await strapi.admin.services.user.findOne(sanitizedUserInfo);
|
const user = await strapi.query('admin::user').findOne({ where: sanitizedUserInfo });
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new Error('User not found');
|
throw new Error('User not found');
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user