-
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.
- Loading branch information
1 parent
cac7403
commit 5893b5c
Showing
5 changed files
with
137 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`User > me > should get me 1`] = ` | ||
{ | ||
"created_at": 2022-04-05T23:50:19.920Z, | ||
"description": "description", | ||
"email": "my@email.com", | ||
"features": [ | ||
"create:session", | ||
"read:session", | ||
"create:content", | ||
"create:content:text_root", | ||
"create:content:text_child", | ||
"update:content", | ||
"update:user", | ||
], | ||
"id": "id", | ||
"notifications": true, | ||
"tabcash": 10, | ||
"tabcoins": 9, | ||
"updated_at": 2022-04-05T23:50:56.545Z, | ||
"username": "username", | ||
} | ||
`; | ||
|
||
exports[`User > me > should throw an error when has no session 1`] = `"Usuário não pode executar esta operação."`; |
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,65 +1,89 @@ | ||
import 'vitest-fetch-mock'; | ||
|
||
import { expect, describe, it, beforeEach } from 'vitest'; | ||
import { expect, describe, it, beforeEach, afterEach } from 'vitest'; | ||
|
||
import { TABNEWS_ENDPOINTS, TABNEWS_HEADERS } from '@/commons'; | ||
import { TabNews } from '@/tabnews'; | ||
import { createTabNews } from '@test/utils'; | ||
import { | ||
createTabNews, | ||
expectRequest, | ||
mockOnceApiError, | ||
mockOnceResponse, | ||
mockOnceSession, | ||
mockedRequest, | ||
resetMocks, | ||
} from '@test/utils'; | ||
|
||
let tabNews: TabNews; | ||
|
||
const user = { | ||
id: 'id', | ||
username: 'username', | ||
email: 'my@email.com', | ||
description: 'description', | ||
notifications: true, | ||
features: [ | ||
'create:session', | ||
'read:session', | ||
'create:content', | ||
'create:content:text_root', | ||
'create:content:text_child', | ||
'update:content', | ||
'update:user', | ||
], | ||
tabcoins: 9, | ||
tabcash: 10, | ||
created_at: '2022-04-05T23:50:19.920Z', | ||
updated_at: '2022-04-05T23:50:56.545Z', | ||
}; | ||
|
||
describe('User', () => { | ||
beforeEach(() => { | ||
tabNews = createTabNews(); | ||
}); | ||
|
||
afterEach(() => { | ||
resetMocks(); | ||
}); | ||
|
||
describe('me', () => { | ||
it('should get me', async () => { | ||
fetchMock.mockOnceIf( | ||
(request) => request.url.endsWith('/user'), | ||
JSON.stringify({ | ||
id: 'id', | ||
username: 'username', | ||
email: 'my@email.com', | ||
description: 'description', | ||
notifications: true, | ||
features: [ | ||
'create:session', | ||
'read:session', | ||
'create:content', | ||
'create:content:text_root', | ||
'create:content:text_child', | ||
'update:content', | ||
'update:user', | ||
], | ||
tabcoins: 9, | ||
tabcash: 10, | ||
created_at: '2022-04-05T23:50:19.920Z', | ||
updated_at: '2022-04-05T23:50:56.545Z', | ||
}), | ||
); | ||
|
||
const user = await tabNews.user.me(); | ||
|
||
expect(user).toMatchObject({ | ||
id: 'id', | ||
username: 'username', | ||
email: 'my@email.com', | ||
description: 'description', | ||
notifications: true, | ||
features: [ | ||
'create:session', | ||
'read:session', | ||
'create:content', | ||
'create:content:text_root', | ||
'create:content:text_child', | ||
'update:content', | ||
'update:user', | ||
], | ||
tabcoins: 9, | ||
tabcash: 10, | ||
created_at: new Date('2022-04-05T23:50:19.920Z'), | ||
updated_at: new Date('2022-04-05T23:50:56.545Z'), | ||
mockOnceSession(); | ||
|
||
mockOnceResponse(TABNEWS_ENDPOINTS.user, user); | ||
|
||
await tabNews.session.create(); | ||
|
||
const me = await tabNews.user.me(); | ||
|
||
expect(me).toMatchSnapshot(); | ||
|
||
const request = mockedRequest(); | ||
|
||
expectRequest(request).method.toBeGet(); | ||
|
||
expectRequest(request).cookie(TABNEWS_HEADERS.sessionId).toBeDefined(); | ||
}); | ||
|
||
it('should throw an error when has no session', async () => { | ||
mockOnceApiError(TABNEWS_ENDPOINTS.user, { | ||
name: 'ForbiddenError', | ||
message: 'Usuário não pode executar esta operação.', | ||
action: 'Verifique se este usuário possui a feature "read:session".', | ||
status_code: 403, | ||
error_id: '1ca4236b-1a68-4573-acb8-40b220b2fd76', | ||
request_id: 'c17c3107-8cce-4b35-b510-0c1b5b92e3b6', | ||
error_location_code: | ||
'MODEL:AUTHORIZATION:CAN_REQUEST:FEATURE_NOT_FOUND', | ||
}); | ||
|
||
expect(() => tabNews.user.me()).rejects.toThrowErrorMatchingSnapshot(); | ||
|
||
const request = mockedRequest(); | ||
|
||
expectRequest(request).method.toBeGet(); | ||
|
||
expectRequest(request).cookie(TABNEWS_HEADERS.sessionId).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
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