-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: finish integration tests for authentication
- Loading branch information
1 parent
bf04d08
commit 8409e3a
Showing
33 changed files
with
682 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { PrismaService, RoleService, UserService } from '@snipcode/domain'; | ||
import { isValidUUIDV4 } from '@snipcode/utils'; | ||
import { PrismaService, RoleService, SessionService, UserService } from '@snipcode/domain'; | ||
import { generateJwtToken, isValidUUIDV4 } from '@snipcode/utils'; | ||
import request from 'supertest'; | ||
|
||
import { TestHelper } from '../../../utils/tests/helpers'; | ||
|
@@ -12,14 +12,16 @@ describe('Test Authentication', () => { | |
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); | ||
userService = server.app.get<UserService>(UserService); | ||
roleService = server.app.get<RoleService>(RoleService); | ||
userService = server.app.get<UserService>(UserService); | ||
sessionService = server.app.get<SessionService>(SessionService); | ||
|
||
testHelper = new TestHelper(prismaService, roleService, userService); | ||
}); | ||
|
@@ -38,6 +40,7 @@ describe('Test Authentication', () => { | |
signupUser(input: $input) { | ||
__typename | ||
message | ||
userId | ||
} | ||
} | ||
`; | ||
|
@@ -57,6 +60,7 @@ describe('Test Authentication', () => { | |
expect(response.body.data.signupUser).toMatchObject({ | ||
__typename: 'SignupUserResult', | ||
message: 'Account created successfully!', | ||
userId: expect.any(String), | ||
}); | ||
}); | ||
|
||
|
@@ -84,8 +88,10 @@ describe('Test Authentication', () => { | |
.send({ query, variables }) | ||
.expect(200); | ||
|
||
expect(response.body.errors[0].extensions.code).toEqual('EMAIL_ALREADY_TAKEN'); | ||
expect(response.body.errors[0].message).toEqual('The email address is already taken'); | ||
const [error] = response.body.errors; | ||
|
||
expect(error.extensions.code).toEqual('EMAIL_ALREADY_TAKEN'); | ||
expect(error.message).toEqual('The email address is already taken'); | ||
}); | ||
|
||
test('Returns an error when authenticating with bad credentials', async () => { | ||
|
@@ -106,8 +112,10 @@ describe('Test Authentication', () => { | |
.send({ query, variables }) | ||
.expect(200); | ||
|
||
expect(response.body.errors[0].extensions.code).toEqual('LOGIN_FAILED'); | ||
expect(response.body.errors[0].message).toEqual('Invalid email address or password.'); | ||
const [error] = response.body.errors; | ||
|
||
expect(error.extensions.code).toEqual('LOGIN_FAILED'); | ||
expect(error.message).toEqual('Invalid email address or password.'); | ||
}); | ||
|
||
test('Returns a token when authenticating with correct credentials', async () => { | ||
|
@@ -136,8 +144,14 @@ describe('Test Authentication', () => { | |
.send({ query, variables }) | ||
.expect(200); | ||
|
||
expect(response.body.data.loginUser.token).toBeDefined(); | ||
expect(isValidUUIDV4(response.body.data.loginUser.token)).toBe(true); | ||
const { loginUser } = response.body.data; | ||
|
||
expect(loginUser.token).toBeDefined(); | ||
expect(isValidUUIDV4(loginUser.token)).toBe(true); | ||
|
||
const session = await sessionService.findByToken(loginUser.token); | ||
|
||
expect(session).toBeDefined(); | ||
}); | ||
|
||
test('Returns an error message when trying to authenticate with a disabled account', async () => { | ||
|
@@ -166,7 +180,256 @@ describe('Test Authentication', () => { | |
.send({ query, variables }) | ||
.expect(200); | ||
|
||
expect(response.body.errors[0].extensions.code).toEqual('ACCOUNT_DISABLED'); | ||
expect(response.body.errors[0].message).toEqual('Your account is disabled!'); | ||
const [error] = response.body.errors; | ||
|
||
expect(error.extensions.code).toEqual('ACCOUNT_DISABLED'); | ||
expect(error.message).toEqual('Your account is disabled!'); | ||
}); | ||
|
||
test('Returns when retrieving the authenticated user without an authentication token', async () => { | ||
const authenticatedUserQuery = ` | ||
query AuthenticatedUser { | ||
authenticatedUser { | ||
id | ||
} | ||
} | ||
`; | ||
|
||
const response = await request(server.app.getHttpServer()) | ||
.post(graphqlEndpoint) | ||
.send({ query: authenticatedUserQuery }) | ||
.expect(200); | ||
|
||
const [error] = response.body.errors; | ||
|
||
expect(error.extensions.code).toEqual('UNAUTHENTICATED'); | ||
expect(error.message).toEqual('You must be authenticated to access to this resource.'); | ||
}); | ||
|
||
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, | ||
}; | ||
|
||
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 { | ||
id | ||
name | ||
isEnabled | ||
timezone | ||
username | ||
pictureUrl | ||
role { | ||
name | ||
} | ||
rootFolder { | ||
id | ||
name | ||
} | ||
createdAt | ||
oauthProvider | ||
} | ||
} | ||
`; | ||
|
||
const response = await request(server.app.getHttpServer()) | ||
.post(graphqlEndpoint) | ||
.set('Authorization', authToken) | ||
.send({ query: authenticatedUserQuery }) | ||
.expect(200); | ||
|
||
const { authenticatedUser } = response.body.data; | ||
|
||
expect(authenticatedUser).toMatchObject({ | ||
createdAt: expect.any(Number), | ||
email: loginVariables.email, | ||
id: signUpResponse.body.data.signupUser.userId, | ||
isEnabled: true, | ||
name: signUpVariables.input.name, | ||
oauthProvider: 'email', | ||
pictureUrl: null, | ||
role: { | ||
name: 'user', | ||
}, | ||
rootFolder: { | ||
id: expect.any(String), | ||
name: `__${authenticatedUser.id}__`, | ||
}, | ||
timezone: null, | ||
username: expect.any(String), | ||
}); | ||
}); | ||
|
||
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 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 { | ||
id | ||
} | ||
} | ||
`; | ||
|
||
const response = await request(server.app.getHttpServer()) | ||
.post(graphqlEndpoint) | ||
.set('Authorization', authToken) | ||
.send({ query: authenticatedUserQuery }) | ||
.expect(200); | ||
|
||
const { authenticatedUser } = response.body.data; | ||
|
||
expect(authenticatedUser.id).toEqual(signUpResponse.body.data.signupUser.userId); | ||
|
||
const logoutQuery = ` | ||
mutation LogoutUser { | ||
logoutUser | ||
} | ||
`; | ||
|
||
await request(server.app.getHttpServer()) | ||
.post(graphqlEndpoint) | ||
.set('Authorization', authToken) | ||
.send({ query: logoutQuery }) | ||
.expect(200); | ||
|
||
const afterLogoutResponse = await request(server.app.getHttpServer()) | ||
.post(graphqlEndpoint) | ||
.set('Authorization', authToken) | ||
.send({ query: authenticatedUserQuery }) | ||
.expect(200); | ||
|
||
const [error] = afterLogoutResponse.body.errors; | ||
|
||
expect(error.extensions.code).toEqual('UNAUTHENTICATED'); | ||
expect(error.message).toEqual('You must be authenticated to access to this resource.'); | ||
}); | ||
}); |
Oops, something went wrong.