diff --git a/__tests__/set-tokens.test.ts b/__tests__/set-tokens.test.ts index ea8a4a52f..e27662cf9 100644 --- a/__tests__/set-tokens.test.ts +++ b/__tests__/set-tokens.test.ts @@ -1,4 +1,27 @@ -import {getPublishRepo, setPersonalToken, setGithubToken} from '../src/set-tokens'; +import {getPublishRepo, setPersonalToken, setGithubToken, setSSHKey} from '../src/set-tokens'; +import {Inputs} from '../src/interfaces'; + +import fs from 'fs'; +import * as exec from '@actions/exec'; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import * as core from '@actions/core'; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import * as io from '@actions/io'; + +jest.mock('@actions/exec', () => ({ + exec: jest.fn(), + getExecOutput: jest.fn().mockReturnValue({ + stderr: '# hostname', + stdout: 'hostinfo', + exitCode: 0 + }) +})); +jest.mock('@actions/io', () => ({ + mkdirP: jest.fn() +})); +jest.mock('@actions/core'); +jest.mock('fs'); +jest.mock('child_process'); beforeEach(() => { jest.resetModules(); @@ -8,6 +31,64 @@ beforeEach(() => { // }); +describe('setSSHKey()', () => { + const createInputs = (): Inputs => ({ + DeployKey: 'DEPLOY_KEY', + GithubToken: '', + PersonalToken: '', + PublishBranch: 'gh-pages', + PublishDir: '', + DestinationDir: '', + ExternalRepository: '', + AllowEmptyCommit: false, + KeepFiles: false, + ForceOrphan: false, + UserName: '', + UserEmail: '', + CommitMessage: '', + FullCommitMessage: '', + TagName: '', + TagMessage: '', + DisableNoJekyll: false, + CNAME: '', + ExcludeAssets: '' + }); + + beforeEach(() => { + jest.resetModules(); + }); + + test('return correct repo address', async () => { + const inps: Inputs = createInputs(); + const test = await setSSHKey(inps, 'owner/repo'); + expect(test).toMatch('git@github.com:owner/repo.git'); + }); + + test('set known_hosts with the ssh-keyscan output if it succeeded', async () => { + const inps: Inputs = createInputs(); + await setSSHKey(inps, 'owner/repo'); + + const mockGetExecOutput = await exec.getExecOutput(''); + expect(fs.writeFileSync).toHaveBeenCalledWith( + expect.stringContaining('known_hosts'), + mockGetExecOutput.stderr + mockGetExecOutput.stdout + ); + }); + + test('SSH key fallbacks to default value if ssh-keyscan fails', async () => { + const inps: Inputs = createInputs(); + (exec.getExecOutput as jest.Mock).mockImplementation(() => { + throw new Error('error'); + }); + + await setSSHKey(inps, 'owner/repo'); + expect(fs.writeFileSync).toHaveBeenCalledWith( + expect.stringContaining('known_hosts'), + expect.stringContaining('# github.com:22 SSH-2.0-babeld-1f0633a6') + ); + }); +}); + describe('getPublishRepo()', () => { test('return repository name', () => { const test = getPublishRepo('', 'owner', 'repo'); diff --git a/src/set-tokens.ts b/src/set-tokens.ts index 277e7fc0f..b5f8b71c7 100644 --- a/src/set-tokens.ts +++ b/src/set-tokens.ts @@ -21,14 +21,25 @@ export async function setSSHKey(inps: Inputs, publishRepo: string): Promise> ~/.ssh/known_hosts on Ubuntu - const cmdSSHkeyscanOutput = `\ -# ${getServerUrl().host}.com:22 SSH-2.0-babeld-1f0633a6 -${ - getServerUrl().host -} ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ== + const foundKnownHostKey = await (async () => { + try { + const keyscanOutput = await exec.getExecOutput('ssh-keyscan', [ + '-t', + 'rsa', + getServerUrl().host + ]); + return keyscanOutput.stderr + keyscanOutput.stdout; + } catch (e) { + return `\ +# github.com:22 SSH-2.0-babeld-1f0633a6 +github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ== `; - fs.writeFileSync(knownHosts, cmdSSHkeyscanOutput + '\n'); + } + })(); + + fs.writeFileSync(knownHosts, foundKnownHostKey); core.info(`[INFO] wrote ${knownHosts}`); await exec.exec('chmod', ['600', knownHosts]);