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(backend): add integration tests on the backend #69

Merged
merged 6 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
test(backend): improvements on integration tests related to auth
  • Loading branch information
tericcabrel committed Jun 6, 2024
commit 8ba3198989ea5506445a13d436bbc6fa18afb8e2
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
Loading