strapi/tests/api/core/admin/ee/provider-login.test.api.js

161 lines
4.5 KiB
JavaScript
Raw Normal View History

2021-01-28 16:07:54 +01:00
'use strict';
const { createStrapiInstance } = require('api-tests/strapi');
const { createAuthRequest, createRequest } = require('api-tests/request');
const { createUtils, describeOnCondition } = require('api-tests/utils');
2021-01-28 16:07:54 +01:00
const edition = process.env.STRAPI_DISABLE_EE === 'true' ? 'CE' : 'EE';
let strapi;
let utils;
const requests = {
public: undefined,
admin: undefined,
noPermissions: undefined,
};
const localData = {
restrictedUser: null,
restrictedRole: null,
};
const restrictedUser = {
email: 'restricted@user.io',
password: 'Restricted123',
};
const restrictedRole = {
name: 'restricted-role',
description: '',
};
const createFixtures = async () => {
const role = await utils.createRole(restrictedRole);
const user = await utils.createUserIfNotExists({
...restrictedUser,
roles: [role.id],
});
localData.restrictedUser = user;
localData.restrictedRole = role;
return { role, user };
};
const deleteFixtures = async () => {
await utils.deleteUserById(localData.restrictedUser.id);
await utils.deleteRolesById([localData.restrictedRole.id]);
};
2023-01-26 12:17:36 +01:00
describeOnCondition(edition === 'EE')('Provider Login', () => {
2021-01-28 16:07:54 +01:00
let hasSSO;
beforeAll(async () => {
I18n/ permissions rework (#9535) * Add a domain layer for the permission, rework the engine handling of the permissions Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Add permissions-fields-to-properties migration for the admin Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Removes useless console.log Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove debug logLevel from provider-login.test.e2e.js Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Adds the new layout for the GET permissions, allow to subscribe to actionRegistered events, adds i18n handlers Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix typo Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update permissions validators Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update unit tests Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update integrations test + fix some validation issues Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Change plugins & settings section format for the permissions layout * only return locales property to localized subjects for the permission's layout * Do not send the locales property to the permission's layout when there is no locales created * Add the 'locales' property to publish & delete routes * Fix unwanted mutation of the sections builder states on multiple builds * Fix units tests with (new engine) * Fix admin-role e2e test - Add locales property to the update payload * fix e2e testsé * Update e2e snapshots * Fix unit test for i18n bootstrap * Add mocks for i18n/bootstrap test * Fix has-locale condition & updatePermission validator * Avoid mutation in migration, always authorize super admin for has-locales condition * Rework rbac domain objects, add a hook module and a provider factory * Remove old providers * Update the admin services & tests for the new rbac domain & providers * Fix tests, bootstrap functions & services following rbac domain rework * Update migration runner * PR comments Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove useless console.log * Fix sanitizeCondition bug * Section builder rework * Add test for the section-builder section & add jsdoc for the permission domain * pr comments (without the migrations) * fix fields-to-properties migration * Add jsdoc for the sections-builder * Moves createBoundAbstractDomain from permission domain to the engine service * Remove debug logLevel for admin role test (e2e) * Fix core-store * Fix hooks & move business logic from i18n bootstrap to dedicated services * add route get-non-localized-fields * use write and read permission * refacto * add input validator * add route doc * handle ST Co-authored-by: Pierre Noël <petersg83@gmail.com> Co-authored-by: Alexandre BODIN <alexandrebodin@users.noreply.github.com>
2021-03-25 14:59:44 +01:00
strapi = await createStrapiInstance();
2021-01-28 16:07:54 +01:00
utils = createUtils(strapi);
2021-02-02 12:27:24 +01:00
// eslint-disable-next-line node/no-extraneous-require
hasSSO = require('@strapi/strapi/dist/utils/ee').features.isEnabled('sso');
2021-01-28 16:07:54 +01:00
await createFixtures();
requests.public = createRequest({ strapi });
requests.admin = await createAuthRequest({ strapi });
requests.noPermissions = await createAuthRequest({ strapi, userInfo: restrictedUser });
});
2021-01-28 16:07:54 +01:00
afterAll(async () => {
await deleteFixtures();
await strapi.destroy();
});
2021-01-28 16:07:54 +01:00
describe('Get the provider list', () => {
2022-08-08 23:33:39 +02:00
test.each(Object.keys(requests))('It should be available for everyone (%s)', async (type) => {
2021-01-28 16:07:54 +01:00
const rq = requests[type];
const res = await rq.get('/admin/providers');
if (hasSSO) {
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBeTruthy();
expect(res.body).toHaveLength(0);
} else {
expect(res.status).toBe(404);
expect(Array.isArray(res.body)).toBeFalsy();
}
});
});
describe('Read the provider login options', () => {
test('It should fail with a public request', async () => {
const res = await requests.public.get('/admin/providers/options');
2021-09-07 21:03:30 +02:00
expect(res.status).toBe(hasSSO ? 401 : 404);
2021-01-28 16:07:54 +01:00
});
test('It should fail with an authenticated request (restricted user)', async () => {
const res = await requests.noPermissions.get('/admin/providers/options');
expect(res.status).toBe(hasSSO ? 403 : 404);
});
test('It should succeed with an authenticated request (admin)', async () => {
const res = await requests.admin.get('/admin/providers/options');
if (hasSSO) {
expect(res.status).toBe(200);
expect(res.body.data).toBeDefined();
expect(typeof res.body.data.autoRegister).toBe('boolean');
expect(res.body.data.defaultRole).toBeDefined();
} else {
expect(res.status).toBe(404);
}
});
});
describe('Update the provider login options', () => {
let newOptions;
beforeAll(async () => {
const superAdminRole = await utils.getSuperAdminRole();
newOptions = {
defaultRole: superAdminRole.id,
autoRegister: false,
};
});
test('It should fail with a public request', async () => {
const res = await requests.public.put('/admin/providers/options', { body: newOptions });
2021-09-07 21:03:30 +02:00
expect(res.status).toBe(hasSSO ? 401 : 405);
2021-01-28 16:07:54 +01:00
});
test('It should fail with an authenticated request (restricted user)', async () => {
const res = await requests.noPermissions.put('/admin/providers/options', {
body: newOptions,
});
expect(res.status).toBe(hasSSO ? 403 : 405);
});
test('It should succeed with an authenticated request (admin)', async () => {
const res = await requests.admin.put('/admin/providers/options', { body: newOptions });
if (hasSSO) {
expect(res.status).toBe(200);
expect(res.body.data).toBeDefined();
expect(res.body.data).toMatchObject(newOptions);
} else {
expect(res.status).toBe(405);
}
});
test('It should fail with an invalid payload', async () => {
const res = await requests.admin.put('/admin/providers/options', {
body: { ...newOptions, autoRegister: 'foobar' },
});
expect(res.status).toBe(hasSSO ? 400 : 405);
});
});
});