-
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 branch 'main' into feature/MOL-357/PICT-241
- Loading branch information
Showing
5 changed files
with
104 additions
and
3 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,22 @@ | ||
import { initMollieClient } from '../client/mollie.client'; | ||
import { MollieApiError, Profile } from '@mollie/api-client'; | ||
import { logger } from '../utils/logger.utils'; | ||
import CustomError from '../errors/custom.error'; | ||
|
||
export const getProfile = async (): Promise<Profile> => { | ||
try { | ||
return await initMollieClient().profiles.getCurrent(); | ||
} catch (error) { | ||
let errorMessage; | ||
if (error instanceof MollieApiError) { | ||
errorMessage = `SCTM - getProfile - error: ${error.message}, field: ${error.field}`; | ||
} else { | ||
errorMessage = `SCTM - getProfile - Failed to get Mollie profile with unknown errors`; | ||
} | ||
|
||
logger.error(errorMessage, { | ||
error, | ||
}); | ||
throw new CustomError(400, errorMessage); | ||
} | ||
}; |
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,59 @@ | ||
import { describe, it, expect, beforeEach, afterEach, jest } from '@jest/globals'; | ||
import { getProfile } from '../../src/mollie/profile.mollie'; | ||
import { MollieApiError, Profile } from '@mollie/api-client'; | ||
import { initMollieClient } from '../../src/client/mollie.client'; | ||
|
||
jest.mock('../../src/client/mollie.client', () => ({ | ||
initMollieClient: jest.fn(), | ||
})); | ||
|
||
describe('Test profile.mollie.ts', () => { | ||
let mockProfile: Profile; | ||
|
||
beforeEach(() => { | ||
mockProfile = { | ||
mode: 'test', | ||
name: 'Test Profile', | ||
website: 'https://example.com', | ||
status: 'verified', | ||
} as unknown as Profile; | ||
|
||
(initMollieClient as jest.Mock).mockReturnValue({ | ||
profiles: { | ||
getCurrent: jest.fn().mockReturnValue(mockProfile), | ||
}, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return the current profile', async () => { | ||
const profile = await getProfile(); | ||
expect(profile).toEqual(mockProfile); | ||
expect(initMollieClient).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should return Mollie API errors when fetching the profile', async () => { | ||
const errorMessage = `SCTM - getProfile - error: Missing authentication, or failed to authenticate, field: undefined"`; | ||
(initMollieClient as jest.Mock).mockReturnValue({ | ||
profiles: { | ||
getCurrent: jest.fn().mockReturnValue(new MollieApiError(errorMessage)), | ||
}, | ||
}); | ||
|
||
await expect(getProfile()).resolves.toThrow(errorMessage); | ||
}); | ||
|
||
it('should return unknown errors when fetching the profile', async () => { | ||
const errorMessage = 'SCTM - getProfile - Failed to get Mollie profile with unknown errors'; | ||
(initMollieClient as jest.Mock).mockReturnValue({ | ||
profiles: { | ||
getCurrent: jest.fn().mockReturnValue(new Error(errorMessage)), | ||
}, | ||
}); | ||
|
||
await expect(getProfile()).resolves.toThrow(errorMessage); | ||
}); | ||
}); |