|
| 1 | +import { createOctoClient, createGithubAuth } from './gh-auth'; |
| 2 | +import nock from 'nock'; |
| 3 | +import { createAppAuth } from '@octokit/auth-app'; |
| 4 | +import { StrategyOptions } from '@octokit/auth-app/dist-types/types'; |
| 5 | +import { decrypt } from './kms'; |
| 6 | +import { RequestInterface } from '@octokit/types'; |
| 7 | +import { mock, MockProxy } from 'jest-mock-extended'; |
| 8 | +import { request } from '@octokit/request'; |
| 9 | + |
| 10 | +jest.mock('./kms'); |
| 11 | +jest.mock('@octokit/auth-app'); |
| 12 | + |
| 13 | +const cleanEnv = process.env; |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + jest.resetModules(); |
| 17 | + jest.clearAllMocks(); |
| 18 | + process.env = { ...cleanEnv }; |
| 19 | + nock.disableNetConnect(); |
| 20 | +}); |
| 21 | + |
| 22 | +describe('Test createGithubAuth', () => { |
| 23 | + test('Creates app client to GitHub public', async () => { |
| 24 | + // Arrange |
| 25 | + const token = '123456'; |
| 26 | + |
| 27 | + // Act |
| 28 | + const result = await createOctoClient(token); |
| 29 | + |
| 30 | + // Assert |
| 31 | + expect(result.request.endpoint.DEFAULTS.baseUrl).toBe('https://api.github.com'); |
| 32 | + }); |
| 33 | + |
| 34 | + test('Creates app client to GitHub ES', async () => { |
| 35 | + // Arrange |
| 36 | + const enterpriseServer = 'https://github.enterprise.notgoingtowork'; |
| 37 | + const token = '123456'; |
| 38 | + |
| 39 | + // Act |
| 40 | + const result = await createOctoClient(token, enterpriseServer); |
| 41 | + |
| 42 | + // Assert |
| 43 | + expect(result.request.endpoint.DEFAULTS.baseUrl).toBe(enterpriseServer); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe('Test createGithubAuth', () => { |
| 48 | + const mockedDecrypt = (decrypt as unknown) as jest.Mock; |
| 49 | + const mockedCreatAppAuth = (createAppAuth as unknown) as jest.Mock; |
| 50 | + const mockedDefaults = jest.spyOn(request, 'defaults'); |
| 51 | + let mockedRequestInterface: MockProxy<RequestInterface>; |
| 52 | + |
| 53 | + const installationId = 1; |
| 54 | + const authType = 'app'; |
| 55 | + const token = '123456'; |
| 56 | + const decryptedValue = 'decryptedValue'; |
| 57 | + const b64 = Buffer.from(decryptedValue, 'binary').toString('base64'); |
| 58 | + |
| 59 | + beforeEach(() => { |
| 60 | + process.env.GITHUB_APP_ID = '1'; |
| 61 | + process.env.GITHUB_APP_CLIENT_SECRET = 'client_secret'; |
| 62 | + process.env.GITHUB_APP_KEY_BASE64 = 'base64'; |
| 63 | + process.env.KMS_KEY_ID = 'key_id'; |
| 64 | + process.env.ENVIRONMENT = 'dev'; |
| 65 | + process.env.GITHUB_APP_CLIENT_ID = '1'; |
| 66 | + }); |
| 67 | + |
| 68 | + test('Creates auth object for public GitHub', async () => { |
| 69 | + // Arrange |
| 70 | + const authOptions = { |
| 71 | + appId: parseInt(process.env.GITHUB_APP_ID as string), |
| 72 | + privateKey: 'decryptedValue', |
| 73 | + installationId, |
| 74 | + clientId: process.env.GITHUB_APP_CLIENT_ID, |
| 75 | + clientSecret: 'decryptedValue', |
| 76 | + }; |
| 77 | + |
| 78 | + mockedDecrypt.mockResolvedValueOnce(decryptedValue).mockResolvedValueOnce(b64); |
| 79 | + const mockedAuth = jest.fn(); |
| 80 | + mockedAuth.mockResolvedValue({ token }); |
| 81 | + mockedCreatAppAuth.mockImplementation((authOptions: StrategyOptions) => { |
| 82 | + return mockedAuth; |
| 83 | + }); |
| 84 | + |
| 85 | + // Act |
| 86 | + const result = await createGithubAuth(installationId, authType); |
| 87 | + |
| 88 | + // Assert |
| 89 | + expect(mockedDecrypt).toBeCalledWith( |
| 90 | + process.env.GITHUB_APP_CLIENT_SECRET, |
| 91 | + process.env.KMS_KEY_ID, |
| 92 | + process.env.ENVIRONMENT, |
| 93 | + ); |
| 94 | + expect(mockedDecrypt).toBeCalledWith( |
| 95 | + process.env.GITHUB_APP_KEY_BASE64, |
| 96 | + process.env.KMS_KEY_ID, |
| 97 | + process.env.ENVIRONMENT, |
| 98 | + ); |
| 99 | + expect(mockedCreatAppAuth).toBeCalledTimes(1); |
| 100 | + expect(mockedCreatAppAuth).toBeCalledWith(authOptions); |
| 101 | + expect(mockedAuth).toBeCalledWith({ type: authType }); |
| 102 | + expect(result.token).toBe(token); |
| 103 | + }); |
| 104 | + |
| 105 | + test('Creates auth object for Enterprise Server', async () => { |
| 106 | + // Arrange |
| 107 | + const githubServerUrl = 'https://github.enterprise.notgoingtowork'; |
| 108 | + |
| 109 | + mockedRequestInterface = mock<RequestInterface>(); |
| 110 | + mockedDefaults.mockImplementation(() => { |
| 111 | + return mockedRequestInterface.defaults({ baseUrl: githubServerUrl }); |
| 112 | + }); |
| 113 | + |
| 114 | + const authOptions = { |
| 115 | + appId: parseInt(process.env.GITHUB_APP_ID as string), |
| 116 | + privateKey: 'decryptedValue', |
| 117 | + installationId, |
| 118 | + clientId: process.env.GITHUB_APP_CLIENT_ID, |
| 119 | + clientSecret: 'decryptedValue', |
| 120 | + request: mockedRequestInterface.defaults({ baseUrl: githubServerUrl }), |
| 121 | + }; |
| 122 | + |
| 123 | + mockedDecrypt.mockResolvedValueOnce(decryptedValue).mockResolvedValueOnce(b64); |
| 124 | + const mockedAuth = jest.fn(); |
| 125 | + mockedAuth.mockResolvedValue({ token }); |
| 126 | + mockedCreatAppAuth.mockImplementation((authOptions: StrategyOptions) => { |
| 127 | + return mockedAuth; |
| 128 | + }); |
| 129 | + |
| 130 | + // Act |
| 131 | + const result = await createGithubAuth(installationId, authType, githubServerUrl); |
| 132 | + |
| 133 | + // Assert |
| 134 | + expect(mockedDecrypt).toBeCalledWith( |
| 135 | + process.env.GITHUB_APP_CLIENT_SECRET, |
| 136 | + process.env.KMS_KEY_ID, |
| 137 | + process.env.ENVIRONMENT, |
| 138 | + ); |
| 139 | + expect(mockedDecrypt).toBeCalledWith( |
| 140 | + process.env.GITHUB_APP_KEY_BASE64, |
| 141 | + process.env.KMS_KEY_ID, |
| 142 | + process.env.ENVIRONMENT, |
| 143 | + ); |
| 144 | + expect(mockedCreatAppAuth).toBeCalledTimes(1); |
| 145 | + expect(mockedCreatAppAuth).toBeCalledWith(authOptions); |
| 146 | + expect(mockedAuth).toBeCalledWith({ type: authType }); |
| 147 | + expect(result.token).toBe(token); |
| 148 | + }); |
| 149 | +}); |
0 commit comments