Merge pull request #1727 from automatisch/AUT-840

refactor: rewrite useRoles with RQ
This commit is contained in:
Ali BARIN
2024-03-15 13:03:44 +01:00
committed by GitHub
12 changed files with 79 additions and 185 deletions

View File

@@ -1,9 +0,0 @@
import Role from '../../models/role.js';
const getRoles = async (_parent, params, context) => {
context.currentUser.can('read', 'Role');
return await Role.query().orderBy('name');
};
export default getRoles;

View File

@@ -1,134 +0,0 @@
import { vi, describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../../app';
import createAuthTokenByUserId from '../../helpers/create-auth-token-by-user-id';
import { createRole } from '../../../test/factories/role';
import { createPermission } from '../../../test/factories/permission';
import { createUser } from '../../../test/factories/user';
import * as license from '../../helpers/license.ee';
describe('graphQL getRoles query', () => {
let currentUserRole,
roleOne,
roleSecond,
query,
userWithPermissions,
userWithoutPermissions,
tokenWithPermissions,
tokenWithoutPermissions;
beforeEach(async () => {
currentUserRole = await createRole({ name: 'Current user role' });
roleOne = await createRole({ name: 'Role one' });
roleSecond = await createRole({ name: 'Role second' });
query = `
query {
getRoles {
id
key
name
description
isAdmin
}
}
`;
await createPermission({
action: 'read',
subject: 'Role',
roleId: currentUserRole.id,
});
userWithPermissions = await createUser({
roleId: currentUserRole.id,
});
userWithoutPermissions = await createUser({
roleId: roleOne.id,
});
tokenWithPermissions = createAuthTokenByUserId(userWithPermissions.id);
tokenWithoutPermissions = createAuthTokenByUserId(
userWithoutPermissions.id
);
});
describe('and with valid license', () => {
beforeEach(async () => {
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
});
describe('and without permissions', () => {
it('should throw not authorized error', async () => {
const response = await request(app)
.post('/graphql')
.set('Authorization', tokenWithoutPermissions)
.send({ query })
.expect(200);
expect(response.body.errors).toBeDefined();
expect(response.body.errors[0].message).toEqual('Not authorized!');
});
});
describe('and correct permissions', () => {
it('should return roles data', async () => {
const response = await request(app)
.post('/graphql')
.set('Authorization', tokenWithPermissions)
.send({ query })
.expect(200);
const expectedResponsePayload = {
data: {
getRoles: [
{
description: currentUserRole.description,
id: currentUserRole.id,
isAdmin: currentUserRole.key === 'admin',
key: currentUserRole.key,
name: currentUserRole.name,
},
{
description: roleOne.description,
id: roleOne.id,
isAdmin: roleOne.key === 'admin',
key: roleOne.key,
name: roleOne.name,
},
{
description: roleSecond.description,
id: roleSecond.id,
isAdmin: roleSecond.key === 'admin',
key: roleSecond.key,
name: roleSecond.name,
},
],
},
};
expect(response.body).toEqual(expectedResponsePayload);
});
});
});
describe('and without valid license', () => {
beforeEach(async () => {
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(false);
});
describe('and correct permissions', () => {
it('should throw not authorized error', async () => {
const response = await request(app)
.post('/graphql')
.set('Authorization', tokenWithPermissions)
.send({ query })
.expect(200);
expect(response.body.errors).toBeDefined();
expect(response.body.errors[0].message).toEqual('Not authorized!');
});
});
});
});

View File

@@ -13,7 +13,6 @@ import getInvoices from './queries/get-invoices.ee.js';
import getNotifications from './queries/get-notifications.js';
import getPermissionCatalog from './queries/get-permission-catalog.ee.js';
import getRole from './queries/get-role.ee.js';
import getRoles from './queries/get-roles.ee.js';
import getSamlAuthProviderRoleMappings from './queries/get-saml-auth-provider-role-mappings.ee.js';
import getSamlAuthProvider from './queries/get-saml-auth-provider.ee.js';
import getStepWithTestExecutions from './queries/get-step-with-test-executions.js';
@@ -40,7 +39,6 @@ const queryResolvers = {
getNotifications,
getPermissionCatalog,
getRole,
getRoles,
getSamlAuthProvider,
getSamlAuthProviderRoleMappings,
getStepWithTestExecutions,

View File

@@ -29,7 +29,6 @@ type Query {
getInvoices: [Invoice]
getPermissionCatalog: PermissionCatalog
getRole(id: String!): Role
getRoles: [Role]
getNotifications: [Notification]
getSamlAuthProvider: SamlAuthProvider
getSamlAuthProviderRoleMappings(id: String!): [SamlAuthProvidersRoleMapping]