From f8d920a332b79562234bee8df81576fb9fa6ac39 Mon Sep 17 00:00:00 2001 From: harimkims Date: Thu, 3 Mar 2022 22:57:37 +0900 Subject: [PATCH] Add test to verify population in users-api --- .../tests/content-api/users-api.test.e2e.js | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/packages/plugins/users-permissions/tests/content-api/users-api.test.e2e.js b/packages/plugins/users-permissions/tests/content-api/users-api.test.e2e.js index e527b369d2..1dc8b67404 100644 --- a/packages/plugins/users-permissions/tests/content-api/users-api.test.e2e.js +++ b/packages/plugins/users-permissions/tests/content-api/users-api.test.e2e.js @@ -8,6 +8,12 @@ const { createContentAPIRequest } = require('../../../../../test/helpers/request let strapi; let rq; +const internals = { + role: { + name: 'Test Role', + description: 'Some random test role', + }, +}; const data = {}; describe('Users API', () => { @@ -20,11 +26,42 @@ describe('Users API', () => { await strapi.destroy(); }); + test('Create and get Role', async () => { + const createRes = await rq({ + method: 'POST', + url: '/users-permissions/roles', + body: { + ...internals.role, + permissions: [], + }, + }); + + expect(createRes.statusCode).toBe(200); + expect(createRes.body).toMatchObject({ ok: true }); + + const findRes = await rq({ + method: 'GET', + url: '/users-permissions/roles', + }); + + expect(findRes.statusCode).toBe(200); + expect(findRes.body.roles).toEqual( + expect.arrayContaining([expect.objectContaining(internals.role)]) + ); + + // eslint-disable-next-line no-unused-vars + const { nb_users, ...role } = findRes.body.roles.find(r => r.name === internals.role.name); + expect(role).toMatchObject(internals.role); + + data.role = role; + }); + test('Create User', async () => { const user = { username: 'User 1', email: 'user1@strapi.io', password: 'test1234', + role: data.role.id, }; const res = await rq({ @@ -37,6 +74,7 @@ describe('Users API', () => { expect(res.body).toMatchObject({ username: user.username, email: user.email, + role: data.role, }); data.user = res.body; @@ -87,6 +125,103 @@ describe('Users API', () => { }, ]); }); + + test('should populate role', async () => { + const res = await rq({ + method: 'GET', + url: '/users?populate=role', + }); + + const { statusCode, body } = res; + + expect(statusCode).toBe(200); + expect(Array.isArray(body)).toBe(true); + expect(body).toHaveLength(1); + expect(body).toMatchObject([ + { + id: expect.anything(), + username: data.user.username, + email: data.user.email, + role: data.role, + }, + ]); + }); + + test('should not populate users in role', async () => { + const res = await rq({ + method: 'GET', + url: '/users?populate[role][populate][0]=users', + }); + + const { statusCode, body } = res; + + expect(statusCode).toBe(200); + expect(Array.isArray(body)).toBe(true); + expect(body).toHaveLength(1); + expect(body).toMatchObject([ + { + id: expect.anything(), + username: data.user.username, + email: data.user.email, + role: data.role, + }, + ]); + expect(body[0].role).not.toHaveProperty('users'); + }); + }); + + describe('Read an user', () => { + test('should populate role', async () => { + const res = await rq({ + method: 'GET', + url: `/users/${data.user.id}?populate=role`, + }); + + const { statusCode, body } = res; + + expect(statusCode).toBe(200); + expect(body).toMatchObject({ + id: data.user.id, + username: data.user.username, + email: data.user.email, + role: data.role, + }); + }); + + test('should not populate role', async () => { + const res = await rq({ + method: 'GET', + url: `/users/${data.user.id}`, + }); + + const { statusCode, body } = res; + + expect(statusCode).toBe(200); + expect(body).toMatchObject({ + id: data.user.id, + username: data.user.username, + email: data.user.email, + }); + expect(body).not.toHaveProperty('role'); + }); + + test('should not populate users in role', async () => { + const res = await rq({ + method: 'GET', + url: `/users/${data.user.id}?populate[role][populate][0]=users`, + }); + + const { statusCode, body } = res; + + expect(statusCode).toBe(200); + expect(body).toMatchObject({ + id: data.user.id, + username: data.user.username, + email: data.user.email, + role: data.role, + }); + expect(body.role).not.toHaveProperty('users'); + }); }); test('Delete user', async () => {