0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

fix(github): fix the way of parsing github getAccessToken response (#1094)

* fix(github): fix the way of parsing Github getAccessToken response

* fix(github): fix getAccessToken response mock
This commit is contained in:
Darcy Ye 2022-06-10 12:12:58 +08:00 committed by GitHub
parent 3925424316
commit 5516e18fe1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 12 deletions

View file

@ -29,6 +29,7 @@
"@logto/schemas": "^0.1.0",
"@silverhand/essentials": "^1.1.0",
"got": "^11.8.2",
"query-string": "^7.0.1",
"zod": "^3.14.3"
},
"devDependencies": {

View file

@ -1,5 +1,6 @@
import { ConnectorError, ConnectorErrorCodes, GetConnectorConfig } from '@logto/connector-types';
import nock from 'nock';
import * as qs from 'query-string';
import GithubConnector from '.';
import { accessTokenEndpoint, authorizationEndpoint, userInfoEndpoint } from './constant';
@ -37,11 +38,16 @@ describe('getAccessToken', () => {
});
it('should get an accessToken by exchanging with code', async () => {
nock(accessTokenEndpoint).post('').reply(200, {
access_token: 'access_token',
scope: 'scope',
token_type: 'token_type',
});
nock(accessTokenEndpoint)
.post('')
.reply(
200,
qs.stringify({
access_token: 'access_token',
scope: 'scope',
token_type: 'token_type',
})
);
const { accessToken } = await githubMethods.getAccessToken('code');
expect(accessToken).toEqual('access_token');
});
@ -49,7 +55,7 @@ describe('getAccessToken', () => {
it('throws SocialAuthCodeInvalid error if accessToken not found in response', async () => {
nock(accessTokenEndpoint)
.post('')
.reply(200, { access_token: '', scope: 'scope', token_type: 'token_type' });
.reply(200, qs.stringify({ access_token: '', scope: 'scope', token_type: 'token_type' }));
await expect(githubMethods.getAccessToken('code')).rejects.toMatchError(
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid)
);
@ -78,11 +84,16 @@ describe('validateConfig', () => {
describe('getUserInfo', () => {
beforeEach(() => {
nock(accessTokenEndpoint).post('').reply(200, {
access_token: 'access_token',
scope: 'scope',
token_type: 'token_type',
});
nock(accessTokenEndpoint)
.post('')
.reply(
200,
qs.stringify({
access_token: 'access_token',
scope: 'scope',
token_type: 'token_type',
})
);
});
afterEach(() => {

View file

@ -11,6 +11,7 @@ import {
} from '@logto/connector-types';
import { assert, conditional } from '@silverhand/essentials';
import got, { RequestError as GotRequestError } from 'got';
import * as qs from 'query-string';
import {
authorizationEndpoint,
@ -68,7 +69,7 @@ export default class GithubConnector implements SocialConnector {
timeout: defaultTimeout,
});
const result = accessTokenResponseGuard.safeParse(JSON.parse(httpResponse.body));
const result = accessTokenResponseGuard.safeParse(qs.parse(httpResponse.body));
if (!result.success) {
throw new ConnectorError(ConnectorErrorCodes.InvalidResponse, result.error.message);
@ -98,6 +99,7 @@ export default class GithubConnector implements SocialConnector {
if (!result.success) {
throw new ConnectorError(ConnectorErrorCodes.InvalidResponse, result.error.message);
}
const { id, avatar_url: avatar, email, name } = result.data;
return {

View file

@ -333,6 +333,7 @@ importers:
lint-staged: ^13.0.0
nock: ^13.2.2
prettier: ^2.3.2
query-string: ^7.0.1
ts-jest: ^27.1.1
tsc-watch: ^5.0.0
typescript: ^4.6.2
@ -344,6 +345,7 @@ importers:
'@silverhand/essentials': 1.1.7
'@silverhand/jest-config': 0.14.0_tgc6da2oqazvrn56dzwolsqo5i
got: 11.8.3
query-string: 7.0.1
zod: 3.14.3
devDependencies:
'@jest/types': 27.5.1