diff --git a/src/lib/clients/artists.test.ts b/src/lib/clients/artists.test.ts index 6f15186..d427046 100644 --- a/src/lib/clients/artists.test.ts +++ b/src/lib/clients/artists.test.ts @@ -2,7 +2,7 @@ import { beforeEach, describe, it, expect, vi } from 'vitest'; import { Artist } from '@/lib/artists'; import { ArtistsHTTPClient } from './artists'; import { FakeHttpClient, HttpResponse, Method } from './http'; -import { FakeAuthHeaderBuilder } from './auth'; +import { AuthHeaderBuilderStub } from './auth'; describe('ArtistsHTTPClient', () => { let url: string; @@ -29,7 +29,7 @@ describe('ArtistsHTTPClient', () => { it('should call the client with the correct parameters', async () => { const headers = { something: 'value' }; - const httpAuthHeaderBuilder = new FakeAuthHeaderBuilder(headers); + const httpAuthHeaderBuilder = new AuthHeaderBuilderStub(headers); const client = new ArtistsHTTPClient( url, httpClient, @@ -48,7 +48,7 @@ describe('ArtistsHTTPClient', () => { }); it('should return the list of artists returned by the HTTP client', async () => { - const httpAuthHeaderBuilder = new FakeAuthHeaderBuilder(); + const httpAuthHeaderBuilder = new AuthHeaderBuilderStub(); const client = new ArtistsHTTPClient( url, httpClient, @@ -67,7 +67,7 @@ describe('ArtistsHTTPClient', () => { it('should throw an error if the HTTP client fails', async () => { const errorMessage = 'Request failed'; httpClient.setSendErrorMessage(errorMessage); - const httpAuthHeaderBuilder = new FakeAuthHeaderBuilder(); + const httpAuthHeaderBuilder = new AuthHeaderBuilderStub(); const client = new ArtistsHTTPClient( url, httpClient, @@ -80,7 +80,7 @@ describe('ArtistsHTTPClient', () => { }); it('should throw an error if the header builder fails', async () => { - const httpAuthHeaderBuilder = new FakeAuthHeaderBuilder(); + const httpAuthHeaderBuilder = new AuthHeaderBuilderStub(); vi.spyOn(httpAuthHeaderBuilder, 'buildHeader').mockImplementation(() => { throw new Error('test Error'); }); diff --git a/src/lib/clients/auth.test.ts b/src/lib/clients/auth.test.ts index c162fda..7b2b7e0 100644 --- a/src/lib/clients/auth.test.ts +++ b/src/lib/clients/auth.test.ts @@ -1,6 +1,6 @@ import { beforeEach, describe, it, expect, vi } from 'vitest'; import { - FakeAuthClient, + AuthClientStub, GCPAuthClient, BaseHTTPAuthHeaderBuilder, } from './auth'; @@ -56,7 +56,7 @@ describe('AuthClient', () => { function createAuthClient() { const authHeader = 'X-Serverless-Authorization'; const authToken = 'test-token'; - return new FakeAuthClient(authToken, authHeader); + return new AuthClientStub(authToken, authHeader); } it('should call getToken and getHeaderName on authClient', async () => { @@ -97,7 +97,9 @@ describe('AuthClient', () => { it('should throw an error if the auth client fails', async () => { const errorMessage = 'Auth request failed'; const authClient = createAuthClient(); - authClient.setGetTokenErrorMessage(errorMessage); + vi.spyOn(authClient, 'getToken').mockImplementation(() => { + throw new Error(errorMessage); + }); const client = new BaseHTTPAuthHeaderBuilder(authClient); const token = 'app-token'; diff --git a/src/lib/clients/auth.ts b/src/lib/clients/auth.ts index 8bde83b..3ab4552 100644 --- a/src/lib/clients/auth.ts +++ b/src/lib/clients/auth.ts @@ -36,10 +36,9 @@ export class GCPAuthClient implements AuthClient { } } -export class FakeAuthClient { +export class AuthClientStub implements AuthClient { private header: string; private token: string; - private getTokenErrorMessage: string | undefined = undefined; constructor(token: string, header: string) { this.token = token; @@ -50,14 +49,7 @@ export class FakeAuthClient { this.token = token; } - setGetTokenErrorMessage(message: string) { - this.getTokenErrorMessage = message; - } - async getToken(): Promise { - if (this.getTokenErrorMessage !== undefined) { - throw new Error(this.getTokenErrorMessage); - } return this.token; } @@ -91,7 +83,7 @@ export class BaseHTTPAuthHeaderBuilder implements HTTPAuthHeaderBuilder { } } -export class FakeAuthHeaderBuilder implements HTTPAuthHeaderBuilder { +export class AuthHeaderBuilderStub implements HTTPAuthHeaderBuilder { private headers: Record; constructor(headers: Record = {}) {