-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: write tests for
refreshHuggingfaceAccessToken
method
Create `refreshHuggingfaceAccessToken.test.ts` file and write in it two integration tests for `refreshHuggingfaceAccessToken` service method of `HuggingfaceService` class.
- Loading branch information
1 parent
4fb7144
commit c6f8385
Showing
3 changed files
with
72 additions
and
1 deletion.
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
69 changes: 69 additions & 0 deletions
69
tests/integration/huggingface/refreshHuggingfaceAccessToken.test.ts
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,69 @@ | ||
import { describe, expect, it, afterAll, beforeAll, mock } from "bun:test"; | ||
import databaseConnection from "../../../src/configurations/databaseConnection"; | ||
import huggingfaceService from "../../../src/services/huggingface"; | ||
import DatasetsModel from "../../../src/models/DatasetsModel"; | ||
import operationsResultsMessages from "../../../src/constants/operationsResultsMessages"; | ||
import { fakeUserHuggingfaceAccount } from "../../fake-data/fakeUserHuggingfaceAccount"; | ||
|
||
beforeAll(async () => { | ||
await databaseConnection(); | ||
}); | ||
|
||
const originalFetch = global.fetch | ||
|
||
describe("Test `refreshHuggingfaceAccessToken` service method", () => { | ||
it('Should complete refreshing access token process successfully', async () => { | ||
const mockFetch = mock(async () => { | ||
const payload = { | ||
access_token: "-p0__8dso$@pyv94wmm6@#7", | ||
refresh_token: "gj%*dqvp082_+9-ym8nmhj", | ||
expires_in: 46346 | ||
} | ||
return new Response(JSON.stringify(payload), { status: 200 }) | ||
}) | ||
|
||
global.fetch = mockFetch | ||
|
||
await DatasetsModel.create({ | ||
_id: process.env.TESTING_USER_ID, | ||
huggingfaceAccount: fakeUserHuggingfaceAccount | ||
}) | ||
|
||
const result = await huggingfaceService.refreshHuggingfaceAccessToken( | ||
process.env.TESTING_USER_ID, | ||
"f5@*)d#@dSjykgyk*&t84gCk" | ||
); | ||
|
||
expect(mockFetch).toHaveBeenCalled() | ||
expect(result.isSuccess).toBeTrue(); | ||
expect(result.result).toMatchObject({ | ||
accessToken: expect.any(String), | ||
accessTokenExpiresIn: expect.any(Date), | ||
refreshToken: expect.any(String), | ||
username: "anyname", | ||
emailVerified: true, | ||
}); | ||
}); | ||
|
||
it('Should catch and return an error because of failed external API request', async () => { | ||
const mockFetch = mock(async () => { | ||
return new Response("{}", { status: 400 }) | ||
}) | ||
|
||
global.fetch = mockFetch | ||
|
||
const result = await huggingfaceService.refreshHuggingfaceAccessToken( | ||
process.env.TESTING_USER_ID, | ||
"f5@*)d#@dSjykgyk*&t84gCk" | ||
); | ||
|
||
expect(mockFetch).toHaveBeenCalled() | ||
expect(result.isSuccess).toBeFalse(); | ||
expect(result.message).toBe(operationsResultsMessages.failedAccessTokenRefreshing); | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
global.fetch = originalFetch | ||
await DatasetsModel.deleteMany(); | ||
}); |