-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d561938
commit 47c400c
Showing
6 changed files
with
98 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,44 @@ | ||
import { authenticate, revoke } from './index.js' | ||
|
||
const integrationClientId = process.env.VITE_TEST_INTEGRATION_CLIENT_ID | ||
const clientSecret = process.env.VITE_TEST_CLIENT_SECRET | ||
const domain = process.env.VITE_TEST_DOMAIN | ||
|
||
describe('Revoke', () => { | ||
it('should be able to revoke a valid integration accessToken', async () => { | ||
const authenticateResponse = await authenticate('client_credentials', { | ||
clientId: integrationClientId, | ||
clientSecret, | ||
domain | ||
}) | ||
|
||
expect(authenticateResponse).toHaveProperty('accessToken') | ||
|
||
const revokeResponse = await revoke({ | ||
clientId: integrationClientId, | ||
clientSecret, | ||
token: authenticateResponse.accessToken, | ||
domain | ||
}) | ||
|
||
expect(revokeResponse).toStrictEqual({}) | ||
}) | ||
|
||
it('should respond with error when something goes wrong', async () => { | ||
// @ts-expect-error I need to test this scenario | ||
const revokeResponse = await revoke({ | ||
clientSecret, | ||
token: '1234', | ||
domain | ||
}) | ||
|
||
expect(revokeResponse).toHaveProperty('errors') | ||
expect(revokeResponse.errors).toBeInstanceOf(Array) | ||
expect(revokeResponse.errors?.[0]).toMatchObject({ | ||
code: 'FORBIDDEN', | ||
detail: 'You are not authorized to revoke this token', | ||
status: 403, | ||
title: 'unauthorized_client' | ||
}) | ||
}) | ||
}) |
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 type { RevokeOptions, RevokeReturn } from '#types/index.js' | ||
|
||
import { camelCaseToSnakeCase } from '#utils/camelCaseToSnakeCase.js' | ||
import { mapKeys } from '#utils/mapKeys.js' | ||
|
||
export async function revoke({ | ||
domain = 'commercelayer.io', | ||
...options | ||
}: RevokeOptions): Promise<RevokeReturn> { | ||
const body = mapKeys(options, camelCaseToSnakeCase) | ||
|
||
const response = await fetch(`https://auth.${domain}/oauth/revoke`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json' | ||
}, | ||
body: JSON.stringify(body) | ||
}) | ||
|
||
return (await response.json()) as RevokeReturn | ||
} |
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