-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removing getUserByEmail and removing id as query param. We now can us…
…e the jwt token request.user.sub
- Loading branch information
1 parent
c3f5887
commit 946c7bf
Showing
3 changed files
with
12 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,60 +2,39 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest' | |
import request from 'supertest' | ||
import { app } from '../../app' | ||
import { createAndAuthenticateUser } from '../../utils/create-and-authenticate-user' | ||
import { randomUUID } from 'crypto' | ||
|
||
let userAuth: { | ||
token: string | ||
userId: string | ||
} | ||
|
||
describe('Get User By Id E2E', () => { | ||
beforeAll(async () => { | ||
await app.ready() | ||
userAuth = await createAndAuthenticateUser(app) | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
it('should be able to get an user by ID', async () => { | ||
const { token } = await createAndAuthenticateUser(app) | ||
const getUserByIdResponse = await request(app.server) | ||
.get(`/user/${userAuth.userId}`) | ||
.set('Authorization', `Bearer ${userAuth.token}`) | ||
.get(`/user`) | ||
.set('Authorization', `Bearer ${token}`) | ||
|
||
expect(getUserByIdResponse.statusCode).toEqual(200) | ||
expect(getUserByIdResponse.body.user).toEqual( | ||
expect.objectContaining({ | ||
id: userAuth.userId, | ||
country: 'brasil', | ||
name: 'John', | ||
surname: 'Doe', | ||
email: '[email protected]', | ||
}), | ||
) | ||
}) | ||
|
||
it('should not be able to get an user by ID that does exists', async () => { | ||
const getUserByIdResponse = await request(app.server) | ||
.get(`/user/${randomUUID()}`) | ||
.set('Authorization', `Bearer ${userAuth.token}`) | ||
|
||
expect(getUserByIdResponse.statusCode).toEqual(404) | ||
expect(getUserByIdResponse.body.user).toEqual(expect.objectContaining({})) | ||
}) | ||
|
||
it('should not be able to get an user requesting with id that is not uuid', async () => { | ||
const id = 'id_not_uuid' | ||
|
||
const getUserByIdResponse = await request(app.server) | ||
.get(`/user/${id}`) | ||
.set('Authorization', `Bearer ${userAuth.token}`) | ||
|
||
expect(getUserByIdResponse.statusCode).toEqual(400) | ||
it('should not be able to get an user without authenticate', async () => { | ||
const getUserByIdResponse = await request(app.server).get(`/user`) | ||
|
||
expect(getUserByIdResponse.statusCode).toEqual(401) | ||
expect(getUserByIdResponse.body).toEqual( | ||
expect.objectContaining({ | ||
message: expect.any(String), | ||
issues: expect.any(Object), | ||
}), | ||
expect.objectContaining({ message: 'Unauthorized' }), | ||
) | ||
}) | ||
}) |
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