Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Implement getCurrentUser graphQL test #1341

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
107 changes: 107 additions & 0 deletions packages/backend/src/graphql/queries/get-current-user.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import request, { Test } from 'supertest';
import app from '../../app';
import createAuthTokenByUserId from '../../helpers/create-auth-token-by-user-id';
import createRole from '../../../test/fixtures/role';
import createUser from '../../../test/fixtures/user';
import { IRole, IUser } from '@automatisch/types';

describe('getCurrentUser', () => {
describe('with unauthorized user', () => {
it('should throw not authorized error', async () => {
const invalidUserToken = 'invalid-token';

const query = `
query {
getCurrentUser {
id
email
}
}
`;

const response = await request(app)
.post('/graphql')
.set('Authorization', invalidUserToken)
.send({ query })
.expect(200);

expect(response.body.errors).toBeDefined();
expect(response.body.errors[0].message).toEqual('Not Authorised!');
});
});

describe('with authorized user', () => {
let role: IRole, currentUser: IUser, token: string, requestObject: Test;

beforeEach(async () => {
role = await createRole({
key: 'sample',
name: 'sample',
});

currentUser = await createUser({
roleId: role.id,
});

token = createAuthTokenByUserId(currentUser.id);
requestObject = request(app)
.post('/graphql')
.set('Authorization', `${token}`);
farukaydin marked this conversation as resolved.
Show resolved Hide resolved
});

it('should return user data', async () => {
const query = `
query {
getCurrentUser {
id
email
fullName
email
createdAt
updatedAt
role {
id
name
}
}
}
`;

const response = await requestObject.send({ query }).expect(200);

const expectedResponsePayload = {
data: {
getCurrentUser: {
createdAt: (currentUser.createdAt as Date).getTime().toString(),
email: currentUser.email,
fullName: currentUser.fullName,
id: currentUser.id,
role: { id: role.id, name: role.name },
updatedAt: (currentUser.updatedAt as Date).getTime().toString(),
},
},
};

expect(response.body).toEqual(expectedResponsePayload);
});

it('should not return user password', async () => {
const query = `
query {
getCurrentUser {
id
email
password
}
}
`;

const response = await requestObject.send({ query }).expect(400);

expect(response.body.errors).toBeDefined();
expect(response.body.errors[0].message).toEqual(
'Cannot query field "password" on type "User".'
);
});
});
});