|
| 1 | +import nock from 'nock'; |
| 2 | + |
| 3 | +import { ConnectorError, ConnectorErrorCodes, type SocialUserInfo } from '@logto/connector-kit'; |
| 4 | + |
| 5 | +import { authorizationEndpoint, tokenEndpoint, userInfoEndpoint } from './constant.js'; |
| 6 | +import createConnector from './index.js'; |
| 7 | + |
| 8 | +const getConfig = vi.fn().mockResolvedValue({ |
| 9 | + clientId: '<client-id>', |
| 10 | + clientSecret: '<client-secret>', |
| 11 | + scope: 'profile email', |
| 12 | +}); |
| 13 | + |
| 14 | +const getSessionMock = vi.fn().mockResolvedValue({ redirectUri: 'http://localhost:3000/callback' }); |
| 15 | + |
| 16 | +describe('GitLab connector', () => { |
| 17 | + beforeEach(() => { |
| 18 | + nock(tokenEndpoint).post('').reply(200, { |
| 19 | + access_token: 'access_token', |
| 20 | + scope: 'scope', |
| 21 | + token_type: 'token_type', |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + nock.cleanAll(); |
| 27 | + vi.clearAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should get a valid uri by redirectUri and state', async () => { |
| 31 | + const connector = await createConnector({ getConfig }); |
| 32 | + const authorizationUri = await connector.getAuthorizationUri( |
| 33 | + { |
| 34 | + state: 'some_state', |
| 35 | + redirectUri: 'http://localhost:3000/callback', |
| 36 | + connectorId: 'some_connector_id', |
| 37 | + connectorFactoryId: 'some_connector_factory_id', |
| 38 | + jti: 'some_jti', |
| 39 | + headers: {}, |
| 40 | + }, |
| 41 | + vi.fn() |
| 42 | + ); |
| 43 | + |
| 44 | + expect(authorizationUri).toEqual( |
| 45 | + `${authorizationEndpoint}?${new URLSearchParams({ |
| 46 | + response_type: 'code', |
| 47 | + client_id: '<client-id>', |
| 48 | + scope: 'profile email openid', // We add openid to the scopes if not present always |
| 49 | + redirect_uri: 'http://localhost:3000/callback', |
| 50 | + state: 'some_state', |
| 51 | + }).toString()}` |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should get valid SocialUserInfo', async () => { |
| 56 | + nock(userInfoEndpoint) |
| 57 | + .get('') |
| 58 | + .reply(200, { |
| 59 | + sub: '1234567', |
| 60 | + sub_legacy: 'abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890', |
| 61 | + name: 'John Doe', |
| 62 | + nickname: 'john.doe', |
| 63 | + preferred_username: 'john.doe', |
| 64 | + email: 'john.doe@example.com', |
| 65 | + email_verified: true, |
| 66 | + profile: 'https://gitlab.com/john.doe', |
| 67 | + picture: 'https://gitlab.com/uploads/-/system/user/avatar/1234567/avatar.png', |
| 68 | + groups: ['group1', 'group2', 'group3'], |
| 69 | + }); |
| 70 | + |
| 71 | + const connector = await createConnector({ getConfig }); |
| 72 | + const socialUserInfo = await connector.getUserInfo({ code: 'code' }, getSessionMock); |
| 73 | + const expectedSocialUserInfo: SocialUserInfo = { |
| 74 | + id: '1234567', |
| 75 | + avatar: 'https://gitlab.com/uploads/-/system/user/avatar/1234567/avatar.png', |
| 76 | + name: 'John Doe', |
| 77 | + email: 'john.doe@example.com', |
| 78 | + rawData: { |
| 79 | + sub: '1234567', |
| 80 | + sub_legacy: 'abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890', |
| 81 | + name: 'John Doe', |
| 82 | + nickname: 'john.doe', |
| 83 | + preferred_username: 'john.doe', |
| 84 | + email: 'john.doe@example.com', |
| 85 | + email_verified: true, |
| 86 | + profile: 'https://gitlab.com/john.doe', |
| 87 | + picture: 'https://gitlab.com/uploads/-/system/user/avatar/1234567/avatar.png', |
| 88 | + groups: ['group1', 'group2', 'group3'], |
| 89 | + }, |
| 90 | + }; |
| 91 | + |
| 92 | + expect(socialUserInfo).toStrictEqual(expectedSocialUserInfo); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should not return email when email_verified is false on SocialUserInfo', async () => { |
| 96 | + nock(userInfoEndpoint) |
| 97 | + .get('') |
| 98 | + .reply(200, { |
| 99 | + sub: '1234567', |
| 100 | + sub_legacy: 'abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890', |
| 101 | + name: 'John Doe', |
| 102 | + nickname: 'john.doe', |
| 103 | + preferred_username: 'john.doe', |
| 104 | + email: 'john.doe@example.com', |
| 105 | + email_verified: false, |
| 106 | + profile: 'https://gitlab.com/john.doe', |
| 107 | + picture: 'https://gitlab.com/uploads/-/system/user/avatar/1234567/avatar.png', |
| 108 | + groups: ['group1', 'group2', 'group3'], |
| 109 | + }); |
| 110 | + |
| 111 | + const connector = await createConnector({ getConfig }); |
| 112 | + const socialUserInfo = await connector.getUserInfo({ code: 'code' }, getSessionMock); |
| 113 | + const expectedSocialUserInfo: SocialUserInfo = { |
| 114 | + id: '1234567', |
| 115 | + avatar: 'https://gitlab.com/uploads/-/system/user/avatar/1234567/avatar.png', |
| 116 | + name: 'John Doe', |
| 117 | + rawData: { |
| 118 | + sub: '1234567', |
| 119 | + sub_legacy: 'abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890', |
| 120 | + name: 'John Doe', |
| 121 | + nickname: 'john.doe', |
| 122 | + preferred_username: 'john.doe', |
| 123 | + email: 'john.doe@example.com', |
| 124 | + email_verified: false, |
| 125 | + profile: 'https://gitlab.com/john.doe', |
| 126 | + picture: 'https://gitlab.com/uploads/-/system/user/avatar/1234567/avatar.png', |
| 127 | + groups: ['group1', 'group2', 'group3'], |
| 128 | + }, |
| 129 | + }; |
| 130 | + |
| 131 | + expect(socialUserInfo).toStrictEqual(expectedSocialUserInfo); |
| 132 | + }); |
| 133 | + |
| 134 | + it('throws AuthorizationFailed error if authentication failed', async () => { |
| 135 | + const connector = await createConnector({ getConfig }); |
| 136 | + await expect( |
| 137 | + connector.getUserInfo({ error: 'some error' }, getSessionMock) |
| 138 | + ).rejects.toStrictEqual( |
| 139 | + new ConnectorError(ConnectorErrorCodes.AuthorizationFailed, { error: 'some error' }) |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + it('throws InvalidResponse error if token response is invalid', async () => { |
| 144 | + // Clear token response mock |
| 145 | + nock.cleanAll(); |
| 146 | + |
| 147 | + nock(tokenEndpoint).post('').reply(200, { |
| 148 | + invalid_field: true, |
| 149 | + }); |
| 150 | + |
| 151 | + const connector = await createConnector({ getConfig }); |
| 152 | + await expect(connector.getUserInfo({ code: 'code' }, getSessionMock)).rejects.toSatisfy( |
| 153 | + (connectorError) => |
| 154 | + (connectorError as ConnectorError).code === ConnectorErrorCodes.InvalidResponse |
| 155 | + ); |
| 156 | + }); |
| 157 | + |
| 158 | + it('throws InvalidResponse error if userinfo response is invalid', async () => { |
| 159 | + nock(userInfoEndpoint).get('').reply(200, { |
| 160 | + id: 'id', |
| 161 | + }); |
| 162 | + |
| 163 | + const connector = await createConnector({ getConfig }); |
| 164 | + await expect(connector.getUserInfo({ code: 'code' }, getSessionMock)).rejects.toSatisfy( |
| 165 | + (connectorError) => |
| 166 | + (connectorError as ConnectorError).code === ConnectorErrorCodes.InvalidResponse |
| 167 | + ); |
| 168 | + }); |
| 169 | + |
| 170 | + it('throws SocialAccessTokenInvalid error if user info responded with 401', async () => { |
| 171 | + nock(userInfoEndpoint).get('').reply(401); |
| 172 | + |
| 173 | + const connector = await createConnector({ getConfig }); |
| 174 | + await expect(connector.getUserInfo({ code: 'code' }, getSessionMock)).rejects.toStrictEqual( |
| 175 | + new ConnectorError(ConnectorErrorCodes.SocialAccessTokenInvalid) |
| 176 | + ); |
| 177 | + }); |
| 178 | + |
| 179 | + it('throws General error if user info responded with a non-401 error', async () => { |
| 180 | + nock(userInfoEndpoint).get('').reply(422); |
| 181 | + |
| 182 | + const connector = await createConnector({ getConfig }); |
| 183 | + await expect(connector.getUserInfo({ code: 'code' }, getSessionMock)).rejects.toStrictEqual( |
| 184 | + new ConnectorError(ConnectorErrorCodes.General) |
| 185 | + ); |
| 186 | + }); |
| 187 | +}); |
0 commit comments