|
| 1 | +import { GithubClient, GithubError } from '../githubClient.js'; |
| 2 | + |
| 3 | +describe('GithubClient.validateCredentials', () => { |
| 4 | + it('returns not configured when credentials are missing', async () => { |
| 5 | + const githubClient = new GithubClient({ |
| 6 | + clientId: '', |
| 7 | + clientSecret: '', |
| 8 | + }); |
| 9 | + |
| 10 | + githubClient.httpClient.get = jest.fn(); |
| 11 | + |
| 12 | + const result = await githubClient.validateCredentials(); |
| 13 | + |
| 14 | + expect(result).toEqual({ |
| 15 | + configured: false, |
| 16 | + valid: false, |
| 17 | + }); |
| 18 | + expect(githubClient.httpClient.get).not.toHaveBeenCalled(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('returns success when GitHub accepts the credentials', async () => { |
| 22 | + const githubClient = new GithubClient({ |
| 23 | + clientId: 'client-id', |
| 24 | + clientSecret: 'client-secret', |
| 25 | + }); |
| 26 | + |
| 27 | + githubClient.httpClient.get = jest.fn().mockResolvedValue({ status: 200 }); |
| 28 | + |
| 29 | + const result = await githubClient.validateCredentials({ timeout: 1234 }); |
| 30 | + |
| 31 | + expect(result).toEqual({ |
| 32 | + configured: true, |
| 33 | + valid: true, |
| 34 | + status: 200, |
| 35 | + }); |
| 36 | + expect(githubClient.httpClient.get).toHaveBeenCalledWith('/rate_limit', { |
| 37 | + timeout: 1234, |
| 38 | + auth: { |
| 39 | + username: 'client-id', |
| 40 | + password: 'client-secret', |
| 41 | + }, |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns failure details when GitHub rejects the credentials', async () => { |
| 46 | + const githubClient = new GithubClient({ |
| 47 | + clientId: 'client-id', |
| 48 | + clientSecret: 'client-secret', |
| 49 | + }); |
| 50 | + |
| 51 | + githubClient.httpClient.get = jest.fn().mockRejectedValue({ |
| 52 | + response: { |
| 53 | + status: 401, |
| 54 | + statusText: 'Unauthorized', |
| 55 | + headers: {}, |
| 56 | + data: { message: 'Bad credentials' }, |
| 57 | + }, |
| 58 | + }); |
| 59 | + |
| 60 | + const result = await githubClient.validateCredentials(); |
| 61 | + |
| 62 | + expect(result.configured).toBe(true); |
| 63 | + expect(result.valid).toBe(false); |
| 64 | + expect(result.status).toBe(401); |
| 65 | + expect(result.error).toBeInstanceOf(GithubError); |
| 66 | + }); |
| 67 | +}); |
0 commit comments