refactor: Use fixtures for getUser graphQL tests

This commit is contained in:
Faruk AYDIN
2023-10-04 18:15:55 +02:00
parent ffb2f4f5db
commit a29b3c6db4
4 changed files with 102 additions and 69 deletions

View File

@@ -2,6 +2,9 @@ import request 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';
describe('getUser', () => { describe('getUser', () => {
describe('with unauthorized user', () => { describe('with unauthorized user', () => {
@@ -29,41 +32,32 @@ describe('getUser', () => {
}); });
describe('with authorized user', () => { describe('with authorized user', () => {
it('should return user data for a valid user id', async () => { let role: any, currentUser: any, anotherUser: any, token: any;
const [role] = await knex
.table('roles')
.insert({
key: 'sample',
name: 'sample',
})
.returning('*');
await knex.table('permissions').insert({ beforeEach(async () => {
action: 'read', role = await createRole({
subject: 'User', key: 'sample',
role_id: role.id, name: 'sample',
}); });
const [currentUser] = await knex await createPermission({
.table('users') action: 'read',
.insert({ subject: 'User',
full_name: 'Test User', roleId: role.id,
email: 'sample@sample.com', });
password: 'secret',
role_id: role.id,
})
.returning('*');
const [anotherUser] = await global.knex currentUser = await createUser({
.table('users') roleId: role.id,
.insert({ });
full_name: 'Another User',
email: 'another@sample.com',
password: 'secret',
role_id: role.id,
})
.returning('*');
anotherUser = await createUser({
roleId: role.id,
});
token = createAuthTokenByUserId(currentUser.id);
});
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}") {
@@ -81,8 +75,6 @@ describe('getUser', () => {
} }
`; `;
const token = createAuthTokenByUserId(currentUser.id);
const response = await request(app) const response = await request(app)
.post('/graphql') .post('/graphql')
.set('Authorization', `${token}`) .set('Authorization', `${token}`)
@@ -106,49 +98,24 @@ describe('getUser', () => {
}); });
it('should return not found for invalid user id', async () => { it('should return not found for invalid user id', async () => {
const [role] = await knex('roles')
.insert({
key: 'sample',
name: 'sample',
})
.returning('*');
await knex.table('permissions').insert({
action: 'read',
subject: 'User',
role_id: role.id,
});
const [currentUser] = await knex
.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 = `
query { query {
getUser(id: "${invalidUserId}") { getUser(id: "${invalidUserId}") {
id
email
fullName
email
createdAt
updatedAt
role {
id id
email name
fullName
email
createdAt
updatedAt
role {
id
name
}
} }
} }
`; }
`;
const token = createAuthTokenByUserId(currentUser.id);
const response = await request(app) const response = await request(app)
.post('/graphql') .post('/graphql')

View File

@@ -0,0 +1,24 @@
import createRole from './role';
type PermissionParams = {
roleId?: string;
action?: string;
subject?: string;
};
const createPermission = async (params: PermissionParams = {}) => {
const permissionData = {
role_id: 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;

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

@@ -0,0 +1,15 @@
type RoleParams = {
name?: string;
key?: string;
};
const createRole = async (params: RoleParams = {}) => {
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 = {
role_id: params?.roleId || (await createRole()).id,
full_name: 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;