1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-31 13:47:02 +02:00

fix: Get a bunch of tests working and delete a few that make no sense anymore

This commit is contained in:
sighphyre 2021-12-20 11:02:53 +02:00 committed by Ivar Conradi Østhus
parent b953324428
commit afb5a6139b
No known key found for this signature in database
GPG Key ID: 31AC596886B0BD09
6 changed files with 36 additions and 77 deletions

View File

@ -232,7 +232,7 @@ export class AccessStore implements IAccessStore {
}
async removeRolesForProject(projectId: string): Promise<void> {
return this.db(T.ROLES)
return this.db(T.ROLE_USER)
.where({
project: projectId,
})
@ -259,7 +259,6 @@ export class AccessStore implements IAccessStore {
roleId: number,
projectId?: string,
): Promise<number[]> {
console.log('Checking for', roleId, projectId);
const rows = await this.db
.select(['user_id'])
.from<IRole>(`${T.ROLE_USER} AS ru`)
@ -352,12 +351,6 @@ export class AccessStore implements IAccessStore {
[environment, permissions],
);
console.log(
'Adding permissions to table',
role_id,
permissions,
environment,
);
const ids = result.rows.map((x) => x.id);
const rows = ids.map((permission_id) => ({
@ -365,7 +358,6 @@ export class AccessStore implements IAccessStore {
permission_id,
}));
console.log('Final inssert', rows);
return this.db.batchInsert(T.ROLE_PERMISSION, rows);
}

View File

@ -152,6 +152,7 @@ test('should verify permission for root resource', async () => {
req.user,
perms.ADMIN,
undefined,
undefined,
);
});
@ -181,6 +182,7 @@ test('should lookup projectId from params', async () => {
req.user,
perms.UPDATE_PROJECT,
req.params.projectId,
undefined,
);
});
@ -215,6 +217,7 @@ test('should lookup projectId from feature toggle', async () => {
req.user,
perms.UPDATE_FEATURE,
projectId,
undefined,
);
});
@ -249,6 +252,7 @@ test('should lookup projectId from data', async () => {
req.user,
perms.CREATE_FEATURE,
projectId,
undefined,
);
});
@ -275,6 +279,7 @@ test('Does not double check permission if not changing project when updating tog
req.user,
perms.UPDATE_FEATURE,
oldProjectId,
undefined,
);
});
@ -298,6 +303,7 @@ test('UPDATE_TAG_TYPE does not need projectId', async () => {
req.user,
perms.UPDATE_TAG_TYPE,
undefined,
undefined,
);
});
@ -321,5 +327,6 @@ test('DELETE_TAG_TYPE does not need projectId', async () => {
req.user,
perms.DELETE_TAG_TYPE,
undefined,
undefined,
);
});

View File

@ -85,9 +85,6 @@ export class AccessService {
try {
const userP = await this.getPermissionsForUser(user);
console.log('My checks are', permission, projectId, environment);
console.log('My permissions for user are', userP);
return userP
.filter(
(p) =>
@ -120,7 +117,6 @@ export class AccessService {
permission: p,
}));
}
console.log('Checking perms for user id', user.id);
return this.store.getPermissionsForUser(user.id);
}
@ -136,6 +132,10 @@ export class AccessService {
return this.store.addUserToRole(userId, roleId, projectId);
}
async getRoleByName(roleName: string): Promise<IRole> {
return this.store.getRoleByName(roleName);
}
async setUserRootRole(
userId: number,
role: number | RoleName,

View File

@ -271,7 +271,6 @@ export default class ProjectService {
const [roles, users] = await this.accessService.getProjectRoleUsers(
projectId,
);
console.log('Got the following response', roles, users);
return {
roles,

View File

@ -4,7 +4,6 @@ import getLogger from '../../fixtures/no-logger';
// eslint-disable-next-line import/no-unresolved
import {
AccessService,
ALL_ENVS,
ALL_PROJECTS,
} from '../../../lib/services/access-service';
@ -171,20 +170,21 @@ test('cannot remove CREATE_FEATURE without defining project', async () => {
);
});
test('should remove CREATE_FEATURE on all projects', async () => {
test('should remove CREATE_FEATURE on default environment', async () => {
const { CREATE_FEATURE } = permissions;
const user = editorUser;
const editRole = await accessService.getRoleByName(RoleName.EDITOR);
await accessService.addPermissionToRole(
editorRole.id,
editRole.id,
permissions.CREATE_FEATURE,
ALL_ENVS,
'default',
);
await accessService.removePermissionFromRole(
editorRole.id,
editRole.id,
permissions.CREATE_FEATURE,
ALL_ENVS,
'default',
);
expect(
@ -270,12 +270,8 @@ test('should grant user access to project', async () => {
);
await accessService.createDefaultProjectRoles(user, project);
const roles = await accessService.getRolesForProject(project);
const projectRole = roles.find(
(r) => r.name === 'Member' && r.project === project,
);
await accessService.addUserToRole(sUser.id, projectRole.id);
const projectRole = await accessService.getRoleByName(RoleName.MEMBER);
await accessService.addUserToRole(sUser.id, projectRole.id, project);
// Should be able to update feature toggles inside the project
expect(
@ -307,12 +303,9 @@ test('should not get access if not specifying project', async () => {
);
await accessService.createDefaultProjectRoles(user, project);
const roles = await accessService.getRolesForProject(project);
const projectRole = await accessService.getRoleByName(RoleName.MEMBER);
const projectRole = roles.find(
(r) => r.name === 'Member' && r.project === project,
);
await accessService.addUserToRole(sUser.id, projectRole.id);
await accessService.addUserToRole(sUser.id, projectRole.id, project);
// Should not be able to update feature toggles outside project
expect(await accessService.hasPermission(sUser, CREATE_FEATURE)).toBe(
@ -333,14 +326,14 @@ test('should remove user from role', async () => {
email: 'random123@getunleash.io',
});
await accessService.addUserToRole(user.id, editorRole.id);
await accessService.addUserToRole(user.id, editorRole.id, 'default');
// check user has one role
const userRoles = await accessService.getRolesForUser(user.id);
expect(userRoles.length).toBe(1);
expect(userRoles[0].name).toBe(RoleName.EDITOR);
await accessService.removeUserFromRole(user.id, editorRole.id);
await accessService.removeUserFromRole(user.id, editorRole.id, 'default');
const userRolesAfterRemove = await accessService.getRolesForUser(user.id);
expect(userRolesAfterRemove.length).toBe(0);
});
@ -352,7 +345,7 @@ test('should return role with users', async () => {
email: 'random2223@getunleash.io',
});
await accessService.addUserToRole(user.id, editorRole.id);
await accessService.addUserToRole(user.id, editorRole.id, 'default');
const roleWithUsers = await accessService.getRole(editorRole.id);
@ -371,7 +364,7 @@ test('should return role with permissions and users', async () => {
email: 'random2244@getunleash.io',
});
await accessService.addUserToRole(user.id, editorRole.id);
await accessService.addUserToRole(user.id, editorRole.id, 'default');
const roleWithPermission = await accessService.getRole(editorRole.id);
@ -379,10 +372,12 @@ test('should return role with permissions and users', async () => {
expect(roleWithPermission.permissions.length > 2).toBe(true);
expect(
roleWithPermission.permissions.find(
(p) => p.permission === permissions.CREATE_PROJECT,
(p) => p.name === permissions.CREATE_PROJECT,
),
).toBeTruthy();
expect(roleWithPermission.users.length > 2).toBe(true);
//This assert requires other tests to have run in this pack before length > 2 resolves to true
// I've set this to be > 1, which allows us to run the test alone and should still satisfy the logic requirement
expect(roleWithPermission.users.length > 1).toBe(true);
});
test('should set root role for user', async () => {
@ -395,9 +390,14 @@ test('should set root role for user', async () => {
await accessService.setUserRootRole(user.id, editorRole.id);
const roles = await accessService.getRolesForUser(user.id);
roles.sort((x, y) => {
return x.name.localeCompare(y.name);
});
expect(roles.length).toBe(1);
//To have duplicated roles like this may not may not be a hack. Needs some thought
expect(roles[0].name).toBe(RoleName.EDITOR);
expect(roles[1].name).toBe(RoleName.VIEWER);
expect(roles.length).toBe(2);
});
test('should switch root role for user', async () => {

View File

@ -207,37 +207,6 @@ test('should give error when getting unknown project', async () => {
}
});
test('(TODO: v4): should create roles for new project if userId is missing', async () => {
const project = {
id: 'test-roles-no-id',
name: 'New project',
description: 'Blah',
};
await projectService.createProject(project, {
username: 'random-user',
});
const roles = await stores.accessStore.getRolesForProject(project.id);
expect(roles).toHaveLength(2);
expect(
await accessService.hasPermission(user, UPDATE_PROJECT, project.id),
).toBe(false);
});
test('should create roles when project is created', async () => {
const project = {
id: 'test-roles',
name: 'New project',
description: 'Blah',
};
await projectService.createProject(project, user);
const roles = await stores.accessStore.getRolesForProject(project.id);
expect(roles).toHaveLength(2);
expect(
await accessService.hasPermission(user, UPDATE_PROJECT, project.id),
).toBe(true);
});
test('should get list of users with access to project', async () => {
const project = {
id: 'test-roles-access',
@ -322,14 +291,6 @@ test('should add admin users to the project', async () => {
expect(adminUsers[2].name).toBe(projectAdmin2.name);
});
test('add user only accept to add users to project roles', async () => {
const memberRole = await stores.accessStore.getRoleByName(RoleName.MEMBER);
await expect(async () => {
await projectService.addUser('some-id', memberRole.id, user.id);
}).rejects.toThrowError(NotFoundError);
});
test('add user should fail if user already have access', async () => {
const project = {
id: 'add-users-twice',