Merge pull request #1319 from automatisch/refactor-get-user-tests

refactor: Use fixtures for getUser graphQL tests
This commit is contained in:
Ömer Faruk Aydın
2023-10-05 10:25:49 +02:00
committed by GitHub
7 changed files with 162 additions and 101 deletions

View File

@@ -1,3 +1,4 @@
import { knexSnakeCaseMappers } from 'objection';
import appConfig from './src/config/app'; import appConfig from './src/config/app';
const fileExtension = appConfig.isDev || appConfig.isTest ? 'ts' : 'js'; const fileExtension = appConfig.isDev || appConfig.isTest ? 'ts' : 'js';
@@ -23,6 +24,7 @@ const knexConfig = {
seeds: { seeds: {
directory: __dirname + '/src/db/seeds', directory: __dirname + '/src/db/seeds',
}, },
...(appConfig.isTest ? knexSnakeCaseMappers() : {}),
}; };
export default knexConfig; export default knexConfig;

View File

@@ -1,10 +1,15 @@
import request from 'supertest'; import request, { Test } from 'supertest';
import app from '../../app'; import app from '../../app';
import createAuthTokenByUserId from '../../helpers/create-auth-token-by-user-id'; import createAuthTokenByUserId from '../../helpers/create-auth-token-by-user-id';
import Crypto from 'crypto'; import Crypto from 'crypto';
import createRole from '../../../test/fixtures/role';
import createPermission from '../../../test/fixtures/permission';
import createUser from '../../../test/fixtures/user';
import { IRole, IUser } from '@automatisch/types';
describe('getUser', () => { describe('getUser', () => {
it('should throw not authorized error for an unauthorized user', async () => { describe('with unauthorized user', () => {
it('should throw not authorized error', async () => {
const invalidUserId = '123123123'; const invalidUserId = '123123123';
const query = ` const query = `
@@ -25,43 +30,42 @@ describe('getUser', () => {
expect(response.body.errors).toBeDefined(); expect(response.body.errors).toBeDefined();
expect(response.body.errors[0].message).toEqual('Not Authorised!'); expect(response.body.errors[0].message).toEqual('Not Authorised!');
}); });
describe('with authorized user', () => {
it('should return user data for a valid user id', async () => {
const [role] = await knex
.table('roles')
.insert({
key: 'sample',
name: 'sample',
})
.returning('*');
await knex.table('permissions').insert({
action: 'read',
subject: 'User',
role_id: role.id,
}); });
const [currentUser] = await knex describe('with authorized user', () => {
.table('users') let role: IRole,
.insert({ currentUser: IUser,
full_name: 'Test User', anotherUser: IUser,
email: 'sample@sample.com', token: string,
password: 'secret', requestObject: Test;
role_id: role.id,
})
.returning('*');
const [anotherUser] = await global.knex beforeEach(async () => {
.table('users') role = await createRole({
.insert({ key: 'sample',
full_name: 'Another User', name: 'sample',
email: 'another@sample.com', });
password: 'secret',
role_id: role.id,
})
.returning('*');
await createPermission({
action: 'read',
subject: 'User',
roleId: role.id,
});
currentUser = await createUser({
roleId: role.id,
});
anotherUser = await createUser({
roleId: role.id,
});
token = createAuthTokenByUserId(currentUser.id);
requestObject = request(app)
.post('/graphql')
.set('Authorization', `${token}`);
});
it('should return user data for a valid user id', async () => {
const query = ` const query = `
query { query {
getUser(id: "${anotherUser.id}") { getUser(id: "${anotherUser.id}") {
@@ -79,23 +83,17 @@ describe('getUser', () => {
} }
`; `;
const token = createAuthTokenByUserId(currentUser.id); const response = await requestObject.send({ query }).expect(200);
const response = await request(app)
.post('/graphql')
.set('Authorization', `${token}`)
.send({ query })
.expect(200);
const expectedResponsePayload = { const expectedResponsePayload = {
data: { data: {
getUser: { getUser: {
createdAt: anotherUser.created_at.getTime().toString(), createdAt: (anotherUser.createdAt as Date).getTime().toString(),
email: anotherUser.email, email: anotherUser.email,
fullName: anotherUser.full_name, fullName: anotherUser.fullName,
id: anotherUser.id, id: anotherUser.id,
role: { id: role.id, name: role.name }, role: { id: role.id, name: role.name },
updatedAt: anotherUser.updated_at.getTime().toString(), updatedAt: (anotherUser.updatedAt as Date).getTime().toString(),
}, },
}, },
}; };
@@ -103,30 +101,26 @@ describe('getUser', () => {
expect(response.body).toEqual(expectedResponsePayload); expect(response.body).toEqual(expectedResponsePayload);
}); });
it('should return not found for invalid user id', async () => { it('should not return user password for a valid user id', async () => {
const [role] = await knex('roles') const query = `
.insert({ query {
key: 'sample', getUser(id: "${anotherUser.id}") {
name: 'sample', id
}) email
.returning('*'); password
}
}
`;
await knex.table('permissions').insert({ const response = await requestObject.send({ query }).expect(400);
action: 'read',
subject: 'User', expect(response.body.errors).toBeDefined();
role_id: role.id, expect(response.body.errors[0].message).toEqual(
'Cannot query field "password" on type "User".'
);
}); });
const [currentUser] = await knex it('should return not found for invalid user id', async () => {
.table('users')
.insert({
full_name: 'Test User',
email: 'sample@sample.com',
password: 'secret',
role_id: role.id,
})
.returning('*');
const invalidUserId = Crypto.randomUUID(); const invalidUserId = Crypto.randomUUID();
const query = ` const query = `
@@ -146,13 +140,7 @@ describe('getUser', () => {
} }
`; `;
const token = createAuthTokenByUserId(currentUser.id); const response = await requestObject.send({ query }).expect(200);
const response = await request(app)
.post('/graphql')
.set('Authorization', `${token}`)
.send({ query })
.expect(200);
expect(response.body.errors).toBeDefined(); expect(response.body.errors).toBeDefined();
expect(response.body.errors[0].message).toEqual('NotFoundError'); expect(response.body.errors[0].message).toEqual('NotFoundError');

View File

@@ -0,0 +1,27 @@
import { IPermission } from '@automatisch/types';
import createRole from './role';
type PermissionParams = {
roleId?: string;
action?: string;
subject?: string;
};
const createPermission = async (
params: PermissionParams = {}
): Promise<IPermission> => {
const permissionData = {
roleId: params?.roleId || (await createRole()).id,
action: params?.action || 'read',
subject: params?.subject || 'User',
};
const [permission] = await global.knex
.table('permissions')
.insert(permissionData)
.returning('*');
return permission;
};
export default createPermission;

17
packages/backend/test/fixtures/role.ts vendored Normal file
View File

@@ -0,0 +1,17 @@
import { IRole } from '@automatisch/types';
type RoleParams = {
name?: string;
key?: string;
};
const createRole = async (params: RoleParams = {}): Promise<IRole> => {
params.name = params?.name || 'Viewer';
params.key = params?.key || 'viewer';
const [role] = await knex.table('roles').insert(params).returning('*');
return role;
};
export default createRole;

27
packages/backend/test/fixtures/user.ts vendored Normal file
View File

@@ -0,0 +1,27 @@
import createRole from './role';
import { faker } from '@faker-js/faker';
type UserParams = {
roleId?: string;
fullName?: string;
email?: string;
password?: string;
};
const createUser = async (params: UserParams = {}) => {
const userData = {
roleId: params?.roleId || (await createRole()).id,
fullName: params?.fullName || faker.person.fullName(),
email: params?.email || faker.internet.email(),
password: params?.password || faker.internet.password(),
};
const [user] = await global.knex
.table('users')
.insert(userData)
.returning('*');
return user;
};
export default createUser;

View File

@@ -3,7 +3,6 @@ import { client as knex } from '../../src/config/database';
import logger from '../../src/helpers/logger'; import logger from '../../src/helpers/logger';
global.beforeAll(async () => { global.beforeAll(async () => {
global.knexInstance = knex;
global.knex = null; global.knex = null;
logger.silent = true; logger.silent = true;
}); });
@@ -22,6 +21,5 @@ global.afterEach(async () => {
}); });
global.afterAll(async () => { global.afterAll(async () => {
global.knexInstance.destroy();
logger.silent = false; logger.silent = false;
}); });

View File

@@ -99,6 +99,8 @@ export interface IUser {
steps: IStep[]; steps: IStep[];
role: IRole; role: IRole;
permissions: IPermission[]; permissions: IPermission[];
createdAt: string | Date;
updatedAt: string | Date;
} }
export interface IRole { export interface IRole {