Skip to content

Commit

Permalink
test: write tests for refreshHuggingfaceAccessToken method
Browse files Browse the repository at this point in the history
Create `refreshHuggingfaceAccessToken.test.ts` file and write in
it two integration tests for `refreshHuggingfaceAccessToken` service
method of `HuggingfaceService` class.
  • Loading branch information
AbdulrhmanGoni committed Nov 4, 2024
1 parent 4fb7144 commit c6f8385
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/constants/operationsResultsMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const operationsResultsMessages = {
noLinkedDatasetRepository: "The dataset has no linked huggingface repository",
successfulDatasetSyncWithRepository:
"The Dataset has been synced with the repository successfully",
failedAccessTokenRefreshing: "Failed to refresh user's Huggingface access token"
};

export default operationsResultsMessages;
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import DatasetsModel from "../../models/DatasetsModel";
import ServiceOperationResult from "../../utilities/ServiceOperationResult";
import { refreshAccessToken } from "./huggingFaceOAuthTokenRequests";
import operationsResultsMessages from "../../constants/operationsResultsMessages";

export default async function refreshHuggingfaceAccessToken_service(
userId: string,
Expand Down Expand Up @@ -34,6 +35,6 @@ export default async function refreshHuggingfaceAccessToken_service(
}

return ServiceOperationResult.failure(
"Failed to refresh user's Huggingface access token"
operationsResultsMessages.failedAccessTokenRefreshing
);
}
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();
});

0 comments on commit c6f8385

Please sign in to comment.