2023-04-01 15:53:09 +08:00
|
|
|
import nock from 'nock';
|
|
|
|
|
|
|
|
import { mockConfig } from './mock.js';
|
|
|
|
|
2024-03-28 10:21:50 +08:00
|
|
|
const getConfig = vi.fn().mockResolvedValue(mockConfig);
|
2023-04-01 15:53:09 +08:00
|
|
|
|
|
|
|
const { default: createConnector } = await import('./index.js');
|
|
|
|
|
|
|
|
describe('getAuthorizationUri', () => {
|
|
|
|
afterEach(() => {
|
2024-03-28 10:21:50 +08:00
|
|
|
vi.clearAllMocks();
|
2023-04-01 15:53:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should get a valid uri by redirectUri and state', async () => {
|
|
|
|
const connector = await createConnector({ getConfig });
|
2024-03-28 10:21:50 +08:00
|
|
|
const setSession = vi.fn();
|
2023-04-01 15:53:09 +08:00
|
|
|
const authorizationUri = await connector.getAuthorizationUri(
|
|
|
|
{
|
|
|
|
state: 'some_state',
|
|
|
|
redirectUri: 'http://localhost:3001/callback',
|
|
|
|
connectorId: 'some_connector_id',
|
|
|
|
connectorFactoryId: 'some_connector_factory_id',
|
|
|
|
jti: 'some_jti',
|
|
|
|
headers: {},
|
|
|
|
},
|
|
|
|
setSession
|
|
|
|
);
|
|
|
|
|
|
|
|
const { origin, pathname, searchParams } = new URL(authorizationUri);
|
|
|
|
expect(origin + pathname).toEqual(mockConfig.authorizationEndpoint);
|
|
|
|
expect(searchParams.get('client_id')).toEqual(mockConfig.clientId);
|
|
|
|
expect(searchParams.get('redirect_uri')).toEqual('http://localhost:3001/callback');
|
|
|
|
expect(searchParams.get('state')).toEqual('some_state');
|
|
|
|
expect(searchParams.get('response_type')).toEqual('code');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getUserInfo', () => {
|
|
|
|
afterEach(() => {
|
|
|
|
nock.cleanAll();
|
2024-03-28 10:21:50 +08:00
|
|
|
vi.clearAllMocks();
|
2023-04-01 15:53:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should get valid userInfo', async () => {
|
|
|
|
const userId = 'userId';
|
|
|
|
const tokenEndpointUrl = new URL(mockConfig.tokenEndpoint);
|
|
|
|
nock(tokenEndpointUrl.origin)
|
|
|
|
.post(tokenEndpointUrl.pathname)
|
|
|
|
.query(true)
|
|
|
|
.reply(
|
|
|
|
200,
|
|
|
|
JSON.stringify({
|
|
|
|
access_token: 'access_token',
|
|
|
|
token_type: 'bearer',
|
|
|
|
})
|
|
|
|
);
|
|
|
|
const userInfoEndpointUrl = new URL(mockConfig.userInfoEndpoint);
|
|
|
|
nock(userInfoEndpointUrl.origin).get(userInfoEndpointUrl.pathname).query(true).reply(200, {
|
|
|
|
sub: userId,
|
2024-03-19 14:05:42 +08:00
|
|
|
foo: 'bar',
|
2023-04-01 15:53:09 +08:00
|
|
|
});
|
|
|
|
const connector = await createConnector({ getConfig });
|
|
|
|
const userInfo = await connector.getUserInfo(
|
|
|
|
{ code: 'code' },
|
2024-03-28 10:21:50 +08:00
|
|
|
vi.fn().mockImplementationOnce(() => {
|
2023-04-01 15:53:09 +08:00
|
|
|
return { redirectUri: 'http://localhost:3001/callback' };
|
|
|
|
})
|
|
|
|
);
|
2024-03-19 14:05:42 +08:00
|
|
|
expect(userInfo).toStrictEqual({ id: userId, rawData: { sub: userId, foo: 'bar' } });
|
2023-04-01 15:53:09 +08:00
|
|
|
});
|
|
|
|
});
|