-
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.
Merge pull request #75 from AbdulrhmanGoni/tests/huggingface-service-…
…related-tests Write some huggingface service related tests and refactor some files
- Loading branch information
Showing
10 changed files
with
233 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { expect, describe, it, afterAll, mock } from "bun:test"; | ||
import DatasetsModel from "../../../src/models/DatasetsModel"; | ||
import { fakeUserHuggingfaceAccount } from "../../fake-data/fakeUserHuggingfaceAccount"; | ||
import { request } from "../.."; | ||
|
||
const exampleDotCom = "https://example.com" | ||
|
||
mock.module("@huggingface/hub", () => { | ||
return { | ||
createRepo: async () => ({ | ||
repoUrl: exampleDotCom | ||
}), | ||
}; | ||
}); | ||
|
||
const path = "huggingface/datasets"; | ||
|
||
describe(`POST /${path}`, () => { | ||
it("Should complete creating huggingface dataset repository process successfully", async () => { | ||
await DatasetsModel.create({ | ||
_id: process.env.TESTING_USER_ID, | ||
huggingfaceAccount: fakeUserHuggingfaceAccount, | ||
datasets: [] | ||
}) | ||
|
||
const { resBody, status } = await request.POST(path, { | ||
name: "username", | ||
license: "mit" | ||
}) | ||
|
||
expect(status).toBe(200) | ||
expect(resBody.data).toMatchObject({ | ||
repoUrl: exampleDotCom | ||
}) | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
mock.restore() | ||
await DatasetsModel.deleteMany(); | ||
}); |
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,8 @@ | ||
export const fakeUserHuggingfaceAccount = { | ||
accessToken: "anyaccessToken", | ||
accessTokenExpiresIn: new Date().getTime() + 946334, | ||
refreshToken: "anyrefreshToken", | ||
username: "anyname", | ||
emailVerified: true, | ||
} | ||
|
54 changes: 54 additions & 0 deletions
54
tests/integration/huggingface/createHuggingfaceAccount.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,54 @@ | ||
import { describe, expect, it, afterAll, beforeAll, mock } from "bun:test"; | ||
import operationsResultsMessages from "../../../src/constants/operationsResultsMessages"; | ||
import DatasetsModel from "../../../src/models/DatasetsModel"; | ||
import databaseConnection from "../../../src/configurations/databaseConnection"; | ||
import huggingfaceService from "../../../src/services/huggingface"; | ||
|
||
mock.module("@huggingface/hub", () => { | ||
return { | ||
whoAmI: async () => ({ | ||
name: "username", | ||
emailVerified: true | ||
}), | ||
}; | ||
}); | ||
|
||
beforeAll(async () => { | ||
await databaseConnection(); | ||
}); | ||
|
||
describe("Test `createHuggingfaceAccount` service method", () => { | ||
|
||
const userHuggingfaceAccountCredentials = { | ||
accessTokenExpiresIn: 57478, // in seconds | ||
hfAccessToken: "n@!*&edVryme573@55n", | ||
hfRefreshToken: "kjbgv&B^#65jbN(*Y#@1hj" | ||
} | ||
|
||
it("Should fail to create the huggingface account for the user because the user is not existant", async () => { | ||
const result = await huggingfaceService.createHuggingfaceAccount( | ||
process.env.TESTING_USER_ID, | ||
userHuggingfaceAccountCredentials | ||
); | ||
|
||
expect(result.isSuccess).toBeFalse(); | ||
expect(result.message).toBe(operationsResultsMessages.failedHuggingfaceAccountCreation); | ||
}); | ||
|
||
it("Should create the huggingface account for the user", async () => { | ||
await DatasetsModel.create({ _id: process.env.TESTING_USER_ID }) | ||
|
||
const result = await huggingfaceService.createHuggingfaceAccount( | ||
process.env.TESTING_USER_ID, | ||
userHuggingfaceAccountCredentials | ||
); | ||
|
||
expect(result.isSuccess).toBeTrue(); | ||
expect(result.result).toBe(operationsResultsMessages.successfulHuggingfaceAccountCreation); | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
mock.restore() | ||
await DatasetsModel.deleteMany(); | ||
}); |
45 changes: 45 additions & 0 deletions
45
tests/integration/huggingface/getHuggingfaceAccount.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,45 @@ | ||
import { describe, expect, it, afterAll, beforeAll } from "bun:test"; | ||
import operationsResultsMessages from "../../../src/constants/operationsResultsMessages"; | ||
import DatasetsModel from "../../../src/models/DatasetsModel"; | ||
import databaseConnection from "../../../src/configurations/databaseConnection"; | ||
import huggingfaceService from "../../../src/services/huggingface"; | ||
import { fakeUserHuggingfaceAccount } from "../../fake-data/fakeUserHuggingfaceAccount"; | ||
|
||
beforeAll(async () => { | ||
await databaseConnection(); | ||
}); | ||
|
||
describe("Test `getHuggingfaceAccount` service method", () => { | ||
it("Should not return user's huggingface account because it is not existant", async () => { | ||
const result = await huggingfaceService.getHuggingfaceAccount( | ||
process.env.TESTING_USER_ID | ||
); | ||
|
||
expect(result.isSuccess).toBeTrue(); | ||
expect(result.message).toBe(operationsResultsMessages.noHuggingfaceAccount); | ||
}); | ||
|
||
it("Should return user's huggingface account successfully", async () => { | ||
await DatasetsModel.create({ | ||
_id: process.env.TESTING_USER_ID, | ||
huggingfaceAccount: fakeUserHuggingfaceAccount | ||
}) | ||
|
||
const result = await huggingfaceService.getHuggingfaceAccount( | ||
process.env.TESTING_USER_ID | ||
); | ||
|
||
expect(result.isSuccess).toBeTrue(); | ||
expect(result.result).toMatchObject({ | ||
accessToken: fakeUserHuggingfaceAccount.accessToken, | ||
accessTokenExpiresIn: expect.any(Date), | ||
refreshToken: fakeUserHuggingfaceAccount.refreshToken, | ||
username: fakeUserHuggingfaceAccount.username, | ||
emailVerified: fakeUserHuggingfaceAccount.emailVerified, | ||
}); | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await DatasetsModel.deleteMany(); | ||
}); |
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(); | ||
}); |
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