mirror of
https://github.com/Unleash/unleash.git
synced 2025-11-10 01:19:53 +01:00
fix: update tests
This commit is contained in:
parent
fb109d79a6
commit
84f4a9083f
@ -35,6 +35,139 @@ const createUserViewerAccess = async (name, email) => {
|
|||||||
return user;
|
return user;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const hasCommonProjectAccess = async (user, projectName, condition) => {
|
||||||
|
const defaultEnv = 'default';
|
||||||
|
const developmentEnv = 'development';
|
||||||
|
const productionEnv = 'production';
|
||||||
|
|
||||||
|
const {
|
||||||
|
CREATE_FEATURE,
|
||||||
|
UPDATE_FEATURE,
|
||||||
|
DELETE_FEATURE,
|
||||||
|
CREATE_FEATURE_STRATEGY,
|
||||||
|
UPDATE_FEATURE_STRATEGY,
|
||||||
|
DELETE_FEATURE_STRATEGY,
|
||||||
|
UPDATE_FEATURE_ENVIRONMENT,
|
||||||
|
} = permissions;
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(user, CREATE_FEATURE, projectName),
|
||||||
|
).toBe(condition);
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(user, UPDATE_FEATURE, projectName),
|
||||||
|
).toBe(condition);
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(user, DELETE_FEATURE, projectName),
|
||||||
|
).toBe(condition);
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(
|
||||||
|
user,
|
||||||
|
CREATE_FEATURE_STRATEGY,
|
||||||
|
projectName,
|
||||||
|
defaultEnv,
|
||||||
|
),
|
||||||
|
).toBe(condition);
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(
|
||||||
|
user,
|
||||||
|
UPDATE_FEATURE_STRATEGY,
|
||||||
|
projectName,
|
||||||
|
defaultEnv,
|
||||||
|
),
|
||||||
|
).toBe(condition);
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(
|
||||||
|
user,
|
||||||
|
DELETE_FEATURE_STRATEGY,
|
||||||
|
projectName,
|
||||||
|
defaultEnv,
|
||||||
|
),
|
||||||
|
).toBe(condition);
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(
|
||||||
|
user,
|
||||||
|
UPDATE_FEATURE_ENVIRONMENT,
|
||||||
|
projectName,
|
||||||
|
defaultEnv,
|
||||||
|
),
|
||||||
|
).toBe(condition);
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(
|
||||||
|
user,
|
||||||
|
CREATE_FEATURE_STRATEGY,
|
||||||
|
projectName,
|
||||||
|
developmentEnv,
|
||||||
|
),
|
||||||
|
).toBe(condition);
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(
|
||||||
|
user,
|
||||||
|
UPDATE_FEATURE_STRATEGY,
|
||||||
|
projectName,
|
||||||
|
developmentEnv,
|
||||||
|
),
|
||||||
|
).toBe(condition);
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(
|
||||||
|
user,
|
||||||
|
DELETE_FEATURE_STRATEGY,
|
||||||
|
projectName,
|
||||||
|
developmentEnv,
|
||||||
|
),
|
||||||
|
).toBe(condition);
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(
|
||||||
|
user,
|
||||||
|
UPDATE_FEATURE_ENVIRONMENT,
|
||||||
|
projectName,
|
||||||
|
developmentEnv,
|
||||||
|
),
|
||||||
|
).toBe(condition);
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(
|
||||||
|
user,
|
||||||
|
CREATE_FEATURE_STRATEGY,
|
||||||
|
projectName,
|
||||||
|
productionEnv,
|
||||||
|
),
|
||||||
|
).toBe(condition);
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(
|
||||||
|
user,
|
||||||
|
UPDATE_FEATURE_STRATEGY,
|
||||||
|
projectName,
|
||||||
|
productionEnv,
|
||||||
|
),
|
||||||
|
).toBe(condition);
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(
|
||||||
|
user,
|
||||||
|
DELETE_FEATURE_STRATEGY,
|
||||||
|
projectName,
|
||||||
|
productionEnv,
|
||||||
|
),
|
||||||
|
).toBe(condition);
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(
|
||||||
|
user,
|
||||||
|
UPDATE_FEATURE_ENVIRONMENT,
|
||||||
|
projectName,
|
||||||
|
productionEnv,
|
||||||
|
),
|
||||||
|
).toBe(condition);
|
||||||
|
};
|
||||||
|
|
||||||
|
const hasFullProjectAccess = async (user, projectName, condition) => {
|
||||||
|
const { DELETE_PROJECT, UPDATE_PROJECT } = permissions;
|
||||||
|
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(user, DELETE_PROJECT, projectName),
|
||||||
|
).toBe(condition);
|
||||||
|
expect(
|
||||||
|
await accessService.hasPermission(user, UPDATE_PROJECT, projectName),
|
||||||
|
).toBe(condition);
|
||||||
|
hasCommonProjectAccess(user, projectName, condition);
|
||||||
|
};
|
||||||
|
|
||||||
const createSuperUser = async () => {
|
const createSuperUser = async () => {
|
||||||
const { userStore } = stores;
|
const { userStore } = stores;
|
||||||
const user = await userStore.insert({
|
const user = await userStore.insert({
|
||||||
@ -116,262 +249,15 @@ test('should not have admin permission', async () => {
|
|||||||
|
|
||||||
test('should have project admin to default project as editor', async () => {
|
test('should have project admin to default project as editor', async () => {
|
||||||
const projectName = 'default';
|
const projectName = 'default';
|
||||||
const defaultEnv = 'default';
|
|
||||||
const developmentEnv = 'development';
|
|
||||||
const productionEnv = 'production';
|
|
||||||
const {
|
|
||||||
DELETE_PROJECT,
|
|
||||||
UPDATE_PROJECT,
|
|
||||||
CREATE_FEATURE,
|
|
||||||
UPDATE_FEATURE,
|
|
||||||
DELETE_FEATURE,
|
|
||||||
CREATE_FEATURE_STRATEGY,
|
|
||||||
UPDATE_FEATURE_STRATEGY,
|
|
||||||
DELETE_FEATURE_STRATEGY,
|
|
||||||
UPDATE_FEATURE_ENVIRONMENT,
|
|
||||||
} = permissions;
|
|
||||||
const user = editorUser;
|
const user = editorUser;
|
||||||
expect(
|
hasFullProjectAccess(user, projectName, true);
|
||||||
await accessService.hasPermission(user, DELETE_PROJECT, projectName),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(user, UPDATE_PROJECT, projectName),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(user, CREATE_FEATURE, projectName),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(user, UPDATE_FEATURE, projectName),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(user, DELETE_FEATURE, projectName),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
CREATE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
defaultEnv,
|
|
||||||
),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
UPDATE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
defaultEnv,
|
|
||||||
),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
DELETE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
defaultEnv,
|
|
||||||
),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
UPDATE_FEATURE_ENVIRONMENT,
|
|
||||||
projectName,
|
|
||||||
defaultEnv,
|
|
||||||
),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
CREATE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
developmentEnv,
|
|
||||||
),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
UPDATE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
developmentEnv,
|
|
||||||
),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
DELETE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
developmentEnv,
|
|
||||||
),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
UPDATE_FEATURE_ENVIRONMENT,
|
|
||||||
projectName,
|
|
||||||
developmentEnv,
|
|
||||||
),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
CREATE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
productionEnv,
|
|
||||||
),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
UPDATE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
productionEnv,
|
|
||||||
),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
DELETE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
productionEnv,
|
|
||||||
),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
UPDATE_FEATURE_ENVIRONMENT,
|
|
||||||
projectName,
|
|
||||||
productionEnv,
|
|
||||||
),
|
|
||||||
).toBe(true);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should not have project admin to other projects as editor', async () => {
|
test('should not have project admin to other projects as editor', async () => {
|
||||||
const projectName = 'unusedprojectname';
|
const projectName = 'unusedprojectname';
|
||||||
const defaultEnv = 'default';
|
|
||||||
const developmentEnv = 'development';
|
|
||||||
const productionEnv = 'production';
|
|
||||||
const {
|
|
||||||
DELETE_PROJECT,
|
|
||||||
UPDATE_PROJECT,
|
|
||||||
CREATE_FEATURE,
|
|
||||||
UPDATE_FEATURE,
|
|
||||||
DELETE_FEATURE,
|
|
||||||
CREATE_FEATURE_STRATEGY,
|
|
||||||
UPDATE_FEATURE_STRATEGY,
|
|
||||||
DELETE_FEATURE_STRATEGY,
|
|
||||||
UPDATE_FEATURE_ENVIRONMENT,
|
|
||||||
} = permissions;
|
|
||||||
const user = editorUser;
|
const user = editorUser;
|
||||||
expect(
|
hasFullProjectAccess(projectName, user, false);
|
||||||
await accessService.hasPermission(user, DELETE_PROJECT, projectName),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(user, UPDATE_PROJECT, projectName),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(user, CREATE_FEATURE, projectName),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(user, UPDATE_FEATURE, projectName),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(user, DELETE_FEATURE, projectName),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
CREATE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
defaultEnv,
|
|
||||||
),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
UPDATE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
defaultEnv,
|
|
||||||
),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
DELETE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
defaultEnv,
|
|
||||||
),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
UPDATE_FEATURE_ENVIRONMENT,
|
|
||||||
projectName,
|
|
||||||
defaultEnv,
|
|
||||||
),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
CREATE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
developmentEnv,
|
|
||||||
),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
UPDATE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
developmentEnv,
|
|
||||||
),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
DELETE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
developmentEnv,
|
|
||||||
),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
UPDATE_FEATURE_ENVIRONMENT,
|
|
||||||
projectName,
|
|
||||||
developmentEnv,
|
|
||||||
),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
CREATE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
productionEnv,
|
|
||||||
),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
UPDATE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
productionEnv,
|
|
||||||
),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
DELETE_FEATURE_STRATEGY,
|
|
||||||
projectName,
|
|
||||||
productionEnv,
|
|
||||||
),
|
|
||||||
).toBe(false);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(
|
|
||||||
user,
|
|
||||||
UPDATE_FEATURE_ENVIRONMENT,
|
|
||||||
projectName,
|
|
||||||
productionEnv,
|
|
||||||
),
|
|
||||||
).toBe(false);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('cannot add CREATE_FEATURE without defining project', async () => {
|
test('cannot add CREATE_FEATURE without defining project', async () => {
|
||||||
@ -447,31 +333,10 @@ test('admin should be admin', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should create default roles to project', async () => {
|
test('should create default roles to project', async () => {
|
||||||
const {
|
|
||||||
DELETE_PROJECT,
|
|
||||||
UPDATE_PROJECT,
|
|
||||||
CREATE_FEATURE,
|
|
||||||
UPDATE_FEATURE,
|
|
||||||
DELETE_FEATURE,
|
|
||||||
} = permissions;
|
|
||||||
const project = 'some-project';
|
const project = 'some-project';
|
||||||
const user = editorUser;
|
const user = editorUser;
|
||||||
await accessService.createDefaultProjectRoles(user, project);
|
await accessService.createDefaultProjectRoles(user, project);
|
||||||
expect(
|
hasFullProjectAccess(user, project, true);
|
||||||
await accessService.hasPermission(user, UPDATE_PROJECT, project),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(user, DELETE_PROJECT, project),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(user, CREATE_FEATURE, project),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(user, UPDATE_FEATURE, project),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(user, DELETE_FEATURE, project),
|
|
||||||
).toBe(true);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should require name when create default roles to project', async () => {
|
test('should require name when create default roles to project', async () => {
|
||||||
@ -500,15 +365,7 @@ test('should grant user access to project', async () => {
|
|||||||
await accessService.addUserToRole(sUser.id, projectRole.id, project);
|
await accessService.addUserToRole(sUser.id, projectRole.id, project);
|
||||||
|
|
||||||
// // Should be able to update feature toggles inside the project
|
// // Should be able to update feature toggles inside the project
|
||||||
expect(
|
hasCommonProjectAccess(sUser, project, true);
|
||||||
await accessService.hasPermission(sUser, CREATE_FEATURE, project),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(sUser, UPDATE_FEATURE, project),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
await accessService.hasPermission(sUser, DELETE_FEATURE, project),
|
|
||||||
).toBe(true);
|
|
||||||
|
|
||||||
// Should not be able to admin the project itself.
|
// Should not be able to admin the project itself.
|
||||||
expect(
|
expect(
|
||||||
@ -520,7 +377,6 @@ test('should grant user access to project', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should not get access if not specifying project', async () => {
|
test('should not get access if not specifying project', async () => {
|
||||||
const { CREATE_FEATURE, UPDATE_FEATURE, DELETE_FEATURE } = permissions;
|
|
||||||
const project = 'another-project-2';
|
const project = 'another-project-2';
|
||||||
const user = editorUser;
|
const user = editorUser;
|
||||||
const sUser = await createUserViewerAccess(
|
const sUser = await createUserViewerAccess(
|
||||||
@ -534,15 +390,7 @@ test('should not get access if not specifying project', async () => {
|
|||||||
await accessService.addUserToRole(sUser.id, projectRole.id, project);
|
await accessService.addUserToRole(sUser.id, projectRole.id, project);
|
||||||
|
|
||||||
// Should not be able to update feature toggles outside project
|
// Should not be able to update feature toggles outside project
|
||||||
expect(await accessService.hasPermission(sUser, CREATE_FEATURE)).toBe(
|
hasCommonProjectAccess(sUser, undefined, false);
|
||||||
false,
|
|
||||||
);
|
|
||||||
expect(await accessService.hasPermission(sUser, UPDATE_FEATURE)).toBe(
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
expect(await accessService.hasPermission(sUser, DELETE_FEATURE)).toBe(
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should remove user from role', async () => {
|
test('should remove user from role', async () => {
|
||||||
@ -712,12 +560,12 @@ test('Should have access to create a strategy in an environment', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('Should be denied access to create a strategy in an environment the user does not have access to', async () => {
|
test('Should be denied access to create a strategy in an environment the user does not have access to', async () => {
|
||||||
const { CREATE_FEATURE } = permissions;
|
const { CREATE_FEATURE_STRATEGY } = permissions;
|
||||||
const user = editorUser;
|
const user = editorUser;
|
||||||
expect(
|
expect(
|
||||||
await accessService.hasPermission(
|
await accessService.hasPermission(
|
||||||
user,
|
user,
|
||||||
CREATE_FEATURE,
|
CREATE_FEATURE_STRATEGY,
|
||||||
'default',
|
'default',
|
||||||
'noaccess',
|
'noaccess',
|
||||||
),
|
),
|
||||||
@ -725,12 +573,12 @@ test('Should be denied access to create a strategy in an environment the user do
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('Should have access to edit a strategy in an environment', async () => {
|
test('Should have access to edit a strategy in an environment', async () => {
|
||||||
const { UPDATE_FEATURE } = permissions;
|
const { UPDATE_FEATURE_STRATEGY } = permissions;
|
||||||
const user = editorUser;
|
const user = editorUser;
|
||||||
expect(
|
expect(
|
||||||
await accessService.hasPermission(
|
await accessService.hasPermission(
|
||||||
user,
|
user,
|
||||||
UPDATE_FEATURE,
|
UPDATE_FEATURE_STRATEGY,
|
||||||
'default',
|
'default',
|
||||||
'development',
|
'development',
|
||||||
),
|
),
|
||||||
@ -738,12 +586,12 @@ test('Should have access to edit a strategy in an environment', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('Should have access to delete a strategy in an environment', async () => {
|
test('Should have access to delete a strategy in an environment', async () => {
|
||||||
const { UPDATE_FEATURE } = permissions;
|
const { DELETE_FEATURE_STRATEGY } = permissions;
|
||||||
const user = editorUser;
|
const user = editorUser;
|
||||||
expect(
|
expect(
|
||||||
await accessService.hasPermission(
|
await accessService.hasPermission(
|
||||||
user,
|
user,
|
||||||
UPDATE_FEATURE,
|
DELETE_FEATURE_STRATEGY,
|
||||||
'default',
|
'default',
|
||||||
'development',
|
'development',
|
||||||
),
|
),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user