feat: Implement getUser graphQL query test
This commit is contained in:
@@ -1,3 +1,161 @@
|
|||||||
test('adds 1 + 2 to equal 3', () => {
|
import request from 'supertest';
|
||||||
expect(1 + 2).toBe(3);
|
import app from '../../app';
|
||||||
|
import createAuthTokenByUserId from '../../helpers/create-auth-token-by-user-id';
|
||||||
|
import Crypto from 'crypto';
|
||||||
|
|
||||||
|
describe('getUser', () => {
|
||||||
|
it('should throw not authorized error for an unauthorized user', async () => {
|
||||||
|
const invalidUserId = '123123123';
|
||||||
|
|
||||||
|
const query = `
|
||||||
|
query {
|
||||||
|
getUser(id: "${invalidUserId}") {
|
||||||
|
id
|
||||||
|
email
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
const response = await request(app)
|
||||||
|
.post('/graphql')
|
||||||
|
.set('Authorization', 'invalid-token')
|
||||||
|
.send({ query })
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
expect(response.body.errors).toBeDefined();
|
||||||
|
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
|
||||||
|
.table('users')
|
||||||
|
.insert({
|
||||||
|
full_name: 'Test User',
|
||||||
|
email: 'sample@sample.com',
|
||||||
|
password: 'secret',
|
||||||
|
role_id: role.id,
|
||||||
|
})
|
||||||
|
.returning('*');
|
||||||
|
|
||||||
|
const [anotherUser] = await global.knex
|
||||||
|
.table('users')
|
||||||
|
.insert({
|
||||||
|
full_name: 'Another User',
|
||||||
|
email: 'another@sample.com',
|
||||||
|
password: 'secret',
|
||||||
|
role_id: role.id,
|
||||||
|
})
|
||||||
|
.returning('*');
|
||||||
|
|
||||||
|
const query = `
|
||||||
|
query {
|
||||||
|
getUser(id: "${anotherUser.id}") {
|
||||||
|
id
|
||||||
|
email
|
||||||
|
fullName
|
||||||
|
email
|
||||||
|
createdAt
|
||||||
|
updatedAt
|
||||||
|
role {
|
||||||
|
id
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
const token = createAuthTokenByUserId(currentUser.id);
|
||||||
|
|
||||||
|
const response = await request(app)
|
||||||
|
.post('/graphql')
|
||||||
|
.set('Authorization', `${token}`)
|
||||||
|
.send({ query })
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
const expectedResponsePayload = {
|
||||||
|
data: {
|
||||||
|
getUser: {
|
||||||
|
createdAt: anotherUser.created_at.getTime().toString(),
|
||||||
|
email: anotherUser.email,
|
||||||
|
fullName: anotherUser.full_name,
|
||||||
|
id: anotherUser.id,
|
||||||
|
role: { id: role.id, name: role.name },
|
||||||
|
updatedAt: anotherUser.updated_at.getTime().toString(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(response.body).toEqual(expectedResponsePayload);
|
||||||
|
});
|
||||||
|
|
||||||
|
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 query = `
|
||||||
|
query {
|
||||||
|
getUser(id: "${invalidUserId}") {
|
||||||
|
id
|
||||||
|
email
|
||||||
|
fullName
|
||||||
|
email
|
||||||
|
createdAt
|
||||||
|
updatedAt
|
||||||
|
role {
|
||||||
|
id
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
const token = createAuthTokenByUserId(currentUser.id);
|
||||||
|
|
||||||
|
const response = await request(app)
|
||||||
|
.post('/graphql')
|
||||||
|
.set('Authorization', `${token}`)
|
||||||
|
.send({ query })
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
expect(response.body.errors).toBeDefined();
|
||||||
|
expect(response.body.errors[0].message).toEqual('NotFoundError');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
3
packages/backend/src/types/global.d.ts
vendored
3
packages/backend/src/types/global.d.ts
vendored
@@ -1,11 +1,10 @@
|
|||||||
import { Knex } from 'knex';
|
import { Knex } from 'knex';
|
||||||
import { Transaction } from 'objection';
|
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
declare namespace globalThis {
|
declare namespace globalThis {
|
||||||
// eslint-disable-next-line no-var
|
// eslint-disable-next-line no-var
|
||||||
var knexInstance: Knex;
|
var knexInstance: Knex;
|
||||||
// eslint-disable-next-line no-var
|
// eslint-disable-next-line no-var
|
||||||
var knex: Transaction;
|
var knex: Knex.Transaction;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { transaction, Model } from 'objection';
|
import { Model } from 'objection';
|
||||||
import { client as knex } from '../../src/config/database';
|
import { client as knex } from '../../src/config/database';
|
||||||
import logger from '../../src/helpers/logger';
|
import logger from '../../src/helpers/logger';
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ global.beforeEach(async () => {
|
|||||||
// It's assigned as global.knex for the convenience even though
|
// It's assigned as global.knex for the convenience even though
|
||||||
// it's a transaction. It's rolled back after each test.
|
// it's a transaction. It's rolled back after each test.
|
||||||
// by assigning to knex, we can use it as knex.table('example') in tests files.
|
// by assigning to knex, we can use it as knex.table('example') in tests files.
|
||||||
global.knex = await transaction.start(knex);
|
global.knex = await knex.transaction();
|
||||||
Model.knex(global.knex);
|
Model.knex(global.knex);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user