Skip to content

Commit

Permalink
Adding tests for injecting the GitHub URL
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-murray committed Sep 15, 2022
1 parent 385d3aa commit 092f9bd
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions __test__/git-auth-helper.test.ts
Expand Up @@ -5,8 +5,8 @@ import * as io from '@actions/io'
import * as os from 'os'
import * as path from 'path'
import * as stateHelper from '../lib/state-helper'
import {IGitCommandManager} from '../lib/git-command-manager'
import {IGitSourceSettings} from '../lib/git-source-settings'
import { IGitCommandManager } from '../lib/git-command-manager'
import { IGitSourceSettings } from '../lib/git-source-settings'

const isWindows = process.platform === 'win32'
const testWorkspace = path.join(__dirname, '_temp', 'git-auth-helper')
Expand All @@ -17,9 +17,10 @@ let localGitConfigPath: string
let globalGitConfigPath: string
let runnerTemp: string
let tempHomedir: string
let git: IGitCommandManager & {env: {[key: string]: string}}
let git: IGitCommandManager & { env: { [key: string]: string } }
let settings: IGitSourceSettings
let sshPath: string
let githubServerUrl: string

describe('git-auth-helper tests', () => {
beforeAll(async () => {
Expand All @@ -32,7 +33,7 @@ describe('git-auth-helper tests', () => {

beforeEach(() => {
// Mock setSecret
jest.spyOn(core, 'setSecret').mockImplementation((secret: string) => {})
jest.spyOn(core, 'setSecret').mockImplementation((secret: string) => { })

// Mock error/warning/info/debug
jest.spyOn(core, 'error').mockImplementation(jest.fn())
Expand Down Expand Up @@ -67,11 +68,15 @@ describe('git-auth-helper tests', () => {
}
})

const configureAuth_configuresAuthHeader =
'configureAuth configures auth header'
it(configureAuth_configuresAuthHeader, async () => {
async function testAuthHeader(testName: string, serverUrl: string | undefined = undefined) {
// Arrange
await setup(configureAuth_configuresAuthHeader)
let expectedServerUrl = 'https://github.com'
if (serverUrl) {
githubServerUrl = serverUrl
expectedServerUrl = githubServerUrl
}

await setup(testName)
expect(settings.authToken).toBeTruthy() // sanity check
const authHelper = gitAuthHelper.createAuthHelper(git, settings)

Expand All @@ -88,9 +93,25 @@ describe('git-auth-helper tests', () => {
).toString('base64')
expect(
configContent.indexOf(
`http.https://github.com/.extraheader AUTHORIZATION: basic ${basicCredential}`
`http.${expectedServerUrl}/.extraheader AUTHORIZATION: basic ${basicCredential}`
)
).toBeGreaterThanOrEqual(0)
}

const configureAuth_configuresAuthHeader =
'configureAuth configures auth header'
it(configureAuth_configuresAuthHeader, async () => {
await testAuthHeader(configureAuth_configuresAuthHeader);
})

const configureAuth_AcceptsGitHubServerUrl = 'inject https://my-ghes-server.com as github server url'
it(configureAuth_AcceptsGitHubServerUrl, async () => {
await testAuthHeader(configureAuth_AcceptsGitHubServerUrl, 'https://my-ghes-server.com');
})

const configureAuth_AcceptsGitHubServerUrlSetToGHEC = 'inject https://github.com as github server url'
it(configureAuth_AcceptsGitHubServerUrlSetToGHEC, async () => {
await testAuthHeader(configureAuth_AcceptsGitHubServerUrl, 'https://github.com');
})

const configureAuth_configuresAuthHeaderEvenWhenPersistCredentialsFalse =
Expand Down Expand Up @@ -678,17 +699,17 @@ async function setup(testName: string): Promise<void> {
workspace = path.join(testWorkspace, testName, 'workspace')
runnerTemp = path.join(testWorkspace, testName, 'runner-temp')
tempHomedir = path.join(testWorkspace, testName, 'home-dir')
await fs.promises.mkdir(workspace, {recursive: true})
await fs.promises.mkdir(runnerTemp, {recursive: true})
await fs.promises.mkdir(tempHomedir, {recursive: true})
await fs.promises.mkdir(workspace, { recursive: true })
await fs.promises.mkdir(runnerTemp, { recursive: true })
await fs.promises.mkdir(tempHomedir, { recursive: true })
process.env['RUNNER_TEMP'] = runnerTemp
process.env['HOME'] = tempHomedir

// Create git config
globalGitConfigPath = path.join(tempHomedir, '.gitconfig')
await fs.promises.writeFile(globalGitConfigPath, '')
localGitConfigPath = path.join(workspace, '.git', 'config')
await fs.promises.mkdir(path.dirname(localGitConfigPath), {recursive: true})
await fs.promises.mkdir(path.dirname(localGitConfigPath), { recursive: true })
await fs.promises.writeFile(localGitConfigPath, '')

git = {
Expand Down Expand Up @@ -779,7 +800,7 @@ async function setup(testName: string): Promise<void> {
sshStrict: true,
workflowOrganizationId: 123456,
setSafeDirectory: true,
githubServerUrl: undefined
githubServerUrl: githubServerUrl
}
}

Expand Down

0 comments on commit 092f9bd

Please sign in to comment.