Skip to content

Commit

Permalink
test(backend): improvements on integration tests related to auth
Browse files Browse the repository at this point in the history
  • Loading branch information
tericcabrel committed Jun 9, 2024
1 parent 52f3a9f commit 3d327ca
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 203 deletions.
4 changes: 3 additions & 1 deletion apps/backend/src/features/app/app.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const prismaServiceMock = mock<PrismaService>();
const roleServiceMock = mock<RoleService>();
const userServiceMock = mock<UserService>();

const { ADMIN_PASSWORD } = process.env;

describe('Test App Service', () => {
let appService: AppService;
let roleService: RoleService;
Expand Down Expand Up @@ -46,6 +48,6 @@ describe('Test App Service', () => {

expect(roleService.loadRoles).toHaveBeenCalledTimes(1);
expect(userService.loadAdminUser).toHaveBeenCalledTimes(1);
expect(userService.loadAdminUser).toHaveBeenCalledWith(role, 'qwerty');
expect(userService.loadAdminUser).toHaveBeenCalledWith(role, ADMIN_PASSWORD);
});
});
176 changes: 25 additions & 151 deletions apps/backend/src/features/auth/graphql/auth.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PrismaService, RoleService, SessionService, UserService } from '@snipcode/domain';
import { generateJwtToken, isValidUUIDV4 } from '@snipcode/utils';
import { SessionService } from '@snipcode/domain';
import { isValidUUIDV4 } from '@snipcode/utils';
import request from 'supertest';

import { TestHelper } from '../../../utils/tests/helpers';
Expand All @@ -10,20 +10,14 @@ const graphqlEndpoint = '/graphql';
describe('Test Authentication', () => {
let server: TestServer;
let testHelper: TestHelper;
let prismaService: PrismaService;
let roleService: RoleService;
let sessionService: SessionService;
let userService: UserService;

beforeAll(async () => {
server = await startTestServer();

prismaService = server.app.get<PrismaService>(PrismaService);
roleService = server.app.get<RoleService>(RoleService);
userService = server.app.get<UserService>(UserService);
sessionService = server.app.get<SessionService>(SessionService);

testHelper = new TestHelper(prismaService, roleService, userService);
testHelper = new TestHelper(server.app, graphqlEndpoint);
});

beforeEach(async () => {
Expand Down Expand Up @@ -81,7 +75,7 @@ describe('Test Authentication', () => {
},
};

await testHelper.createTestUser({ email: variables.input.email });
await testHelper.signupUser({ email: variables.input.email });

const response = await request(server.app.getHttpServer())
.post(graphqlEndpoint)
Expand Down Expand Up @@ -127,11 +121,10 @@ describe('Test Authentication', () => {
}
`;

await testHelper.createTestUser({
await testHelper.signupUser({
email: '[email protected]',
isEnabled: true,
password: 'password',
role: 'user',
});

const variables = {
Expand Down Expand Up @@ -163,11 +156,10 @@ describe('Test Authentication', () => {
}
`;

await testHelper.createTestUser({
await testHelper.signupUser({
email: '[email protected]',
isEnabled: false,
password: 'password',
role: 'user',
});

const variables = {
Expand All @@ -186,14 +178,14 @@ describe('Test Authentication', () => {
expect(error.message).toEqual('Your account is disabled!');
});

test('Returns when retrieving the authenticated user without an authentication token', async () => {
test('Returns an error when retrieving the authenticated user without an authentication token', async () => {
const authenticatedUserQuery = `
query AuthenticatedUser {
authenticatedUser {
id
}
query AuthenticatedUser {
authenticatedUser {
id
}
`;
}
`;

const response = await request(server.app.getHttpServer())
.post(graphqlEndpoint)
Expand All @@ -207,71 +199,13 @@ describe('Test Authentication', () => {
});

test('Retrieve the authenticated user', async () => {
const signUpQuery = `
mutation SignupUser($input: SignupUserInput!) {
signupUser(input: $input) {
__typename
message
userId
}
}
`;

const signUpVariables = {
input: {
email: '[email protected]',
name: 'John Doe',
password: 'password',
},
};

const signUpResponse = await request(server.app.getHttpServer())
.post(graphqlEndpoint)
.send({ query: signUpQuery, variables: signUpVariables })
.expect(200);

const confirmationToken = generateJwtToken({
expiresIn: '1h',
payload: { userId: signUpResponse.body.data.signupUser.userId },
secret: process.env.JWT_SECRET,
});

const confirmUserQuery = `
mutation ConfirmUser($token: String!) {
confirmUser(token: $token) {
message
}
}
`;

const confirmUserVariables = {
token: confirmationToken,
const input = {
email: '[email protected]',
name: 'John Doe',
password: 'password',
};

await request(server.app.getHttpServer())
.post(graphqlEndpoint)
.send({ query: confirmUserQuery, variables: confirmUserVariables })
.expect(200);

const loginQuery = `
mutation LoginUser($email: String!, $password: String!) {
loginUser(email: $email, password: $password) {
token
}
}
`;

const loginVariables = {
email: signUpVariables.input.email,
password: signUpVariables.input.password,
};

const loginResponse = await request(server.app.getHttpServer())
.post(graphqlEndpoint)
.send({ query: loginQuery, variables: loginVariables })
.expect(200);

const authToken = loginResponse.body.data.loginUser.token;
const { authToken, userId } = await testHelper.createAuthenticatedUser({ ...input });

const authenticatedUserQuery = `
query AuthenticatedUser {
Expand Down Expand Up @@ -306,10 +240,10 @@ describe('Test Authentication', () => {

expect(authenticatedUser).toMatchObject({
createdAt: expect.any(Number),
email: loginVariables.email,
id: signUpResponse.body.data.signupUser.userId,
email: input.email,
id: userId,
isEnabled: true,
name: signUpVariables.input.name,
name: input.name,
oauthProvider: 'email',
pictureUrl: null,
role: {
Expand All @@ -325,72 +259,12 @@ describe('Test Authentication', () => {
});

test('Log out the authenticated user', async () => {
const signUpQuery = `
mutation SignupUser($input: SignupUserInput!) {
signupUser(input: $input) {
__typename
message
userId
}
}
`;

const signUpVariables = {
input: {
email: '[email protected]',
name: 'Jane Doe',
password: 'password',
},
};

const signUpResponse = await request(server.app.getHttpServer())
.post(graphqlEndpoint)
.send({ query: signUpQuery, variables: signUpVariables })
.expect(200);

const confirmationToken = generateJwtToken({
expiresIn: '1h',
payload: { userId: signUpResponse.body.data.signupUser.userId },
secret: process.env.JWT_SECRET,
const { authToken, userId } = await testHelper.createAuthenticatedUser({
email: '[email protected]',
name: 'Jane Doe',
password: 'password',
});

const confirmUserQuery = `
mutation ConfirmUser($token: String!) {
confirmUser(token: $token) {
message
}
}
`;

const confirmUserVariables = {
token: confirmationToken,
};

await request(server.app.getHttpServer())
.post(graphqlEndpoint)
.send({ query: confirmUserQuery, variables: confirmUserVariables })
.expect(200);

const loginQuery = `
mutation LoginUser($email: String!, $password: String!) {
loginUser(email: $email, password: $password) {
token
}
}
`;

const loginVariables = {
email: signUpVariables.input.email,
password: signUpVariables.input.password,
};

const loginResponse = await request(server.app.getHttpServer())
.post(graphqlEndpoint)
.send({ query: loginQuery, variables: loginVariables })
.expect(200);

const authToken = loginResponse.body.data.loginUser.token;

const authenticatedUserQuery = `
query AuthenticatedUser {
authenticatedUser {
Expand All @@ -407,7 +281,7 @@ describe('Test Authentication', () => {

const { authenticatedUser } = response.body.data;

expect(authenticatedUser.id).toEqual(signUpResponse.body.data.signupUser.userId);
expect(authenticatedUser.id).toEqual(userId);

const logoutQuery = `
mutation LogoutUser {
Expand Down

0 comments on commit 3d327ca

Please sign in to comment.