Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
vstarostin committed Apr 1, 2024
1 parent 24f410d commit 022d974
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 41 deletions.
30 changes: 21 additions & 9 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { type OctokitResponse } from '@octokit/types'
import * as config from '../src/config'
import * as execute from '../src/execute'
import * as github from '../src/github'
import { ENTERPRISE_DEFAULTS_FILENAME } from '../src/enterprise'
import {
ENTERPRISE_DEFAULTS_FILENAME,
ENTERPRISE_STAGE_CONFIG_FILENAME
} from '../src/enterprise'

jest.mock('@actions/exec')
jest.mock('@actions/tool-cache')
Expand Down Expand Up @@ -182,15 +185,24 @@ describe('Config', () => {
delete process.env.GITHUB_JOB
})
test('Download stage config', async () => {
const mockContent = 'testbase64string'
jest.spyOn(github, 'downloadFileFromGitHub').mockImplementationOnce(async () => {
const response = { data: { content: Buffer.from(mockContent).toString('base64') } }
return await Promise.resolve(response as unknown as OctokitResponse<any, number>)
})
// Mock implementation for 'Download stage config' test case
const mockGetReleaseAssetUrl = jest.spyOn(github, 'getReleaseAssetUrl');
mockGetReleaseAssetUrl.mockResolvedValue([`http://mock.test/asset/${ENTERPRISE_STAGE_CONFIG_FILENAME}`, 'v1.0.0']);

process.env.GITHUB_SERVER_URL = 'https://github.acme.com'
process.env.GITHUB_API_URL = 'https://github.acme.com/api/v3'

const server = 'https://github.anything.com'
const host = 'github.anything.com'
const sapStageConfigUrl = `http://mock.test/asset/${ENTERPRISE_STAGE_CONFIG_FILENAME}`
const expectedPiperFlags = ['--useV1', '--defaultsFile', `${sapStageConfigUrl}`, '--gitHubTokens', `${host}:blah-blah`]
const expectedWrittenFilepath = path.join(config.CONFIG_DIR, ENTERPRISE_STAGE_CONFIG_FILENAME)
piperExecResultMock = generatePiperGetDefaultsOutput([sapStageConfigUrl])

await config.downloadStageConfig('testToken', 'something', 'nothing')
await config.downloadStageConfig(server, 'https://dummy-api.test/', 'v1.0.0', 'blah-blah', 'something', 'nothing')

expect(fs.writeFileSync).toHaveBeenCalledWith(expect.stringContaining('.yml'), mockContent)
expect(execute.executePiper).toHaveBeenCalledWith('getDefaults', expectedPiperFlags)
expect(fs.writeFileSync).toHaveBeenCalledWith(expectedWrittenFilepath, expect.anything())
})

test('Check if step active', async () => {
Expand All @@ -211,7 +223,7 @@ describe('Config', () => {

process.env.GITHUB_JOB = 'Init'

await config.createCheckIfStepActiveMaps('testToken', 'something', 'nothing')
await config.createCheckIfStepActiveMaps('server','apiURL','version','testToken', 'something', 'nothing')

expect(config.downloadStageConfig).toHaveBeenCalled()
expect(config.checkIfStepActive).toHaveBeenCalled()
Expand Down
12 changes: 7 additions & 5 deletions test/enterprise.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
ENTERPRISE_STAGE_CONFIG_FILENAME,
getEnterpriseStageConfigUrl,
DEFAULT_CONFIG,
STAGE_CONFIG,
getEnterpriseConfigUrl,
isEnterpriseStep,
onGitHubEnterprise
} from '../src/enterprise'
Expand Down Expand Up @@ -42,13 +44,13 @@ describe('test enterprise.ts', () => {
})
})

describe('getEnterpriseStageConfigUrl', () => {
describe('getEnterpriseConfigUrl', () => {
test('with env var set', async () => {
// init
process.env.GITHUB_SERVER_URL = GITHUB_COM_SERVER_URL
// test
// assert
expect(getEnterpriseStageConfigUrl('anything', 'something')).toBe('')
expect(getEnterpriseConfigUrl('configType','apiURL','version', 'token','anything', 'something')).toBe('')
})

describe('with enterprise github', () => {
Expand All @@ -60,7 +62,7 @@ describe('test enterprise.ts', () => {
const repository = 'something'
// test
// assert
expect(getEnterpriseStageConfigUrl(owner, repository)).toBe(`${process.env.GITHUB_API_URL}/repos/${owner}/${repository}/contents/resources/${ENTERPRISE_STAGE_CONFIG_FILENAME}`)
expect(getEnterpriseConfigUrl(DEFAULT_CONFIG,'apiURL','version','token', owner, repository)).toBe(`${process.env.GITHUB_API_URL}/repos/${owner}/${repository}/contents/resources/${ENTERPRISE_STAGE_CONFIG_FILENAME}`)
})

test('with no repository', async () => {
Expand All @@ -69,7 +71,7 @@ describe('test enterprise.ts', () => {
const repository = ''
// test
// assert
expect(getEnterpriseStageConfigUrl(owner, repository)).toBe('')
expect(getEnterpriseConfigUrl(STAGE_CONFIG,'apiURL','version','token', owner, repository)).toBe('')
})
})
})
Expand Down
27 changes: 1 addition & 26 deletions test/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as toolCache from '@actions/tool-cache'
import * as octokit from '@octokit/core'
import * as core from '@actions/core'

import { downloadPiperBinary, buildPiperFromSource, downloadFileFromGitHub } from '../src/github'
import { downloadPiperBinary, buildPiperFromSource } from '../src/github'

jest.mock('@actions/core')
jest.mock('@actions/exec')
Expand Down Expand Up @@ -155,29 +155,4 @@ describe('GitHub package tests', () => {
await buildPiperFromSource(`devel:${owner}:${repository}:${commitISH}`)
).toBe(`${process.cwd()}/${owner}-${repository}-${shortCommitSHA}/piper`)
})

test('Download file from GitHub', async () => {
jest.spyOn(octokit, 'Octokit').mockImplementationOnce(() => {
return {
request: async (request: string) => {
return await Promise.resolve(
{
url: request.split(' ')[1],
type: 'file',
content: 'testString',
status: 200
}
)
}
} as unknown as octokit.Octokit
})
const testURL = 'https://github.acme.com/api/v3/repos/SAP/jenkins-library/contents/resources/piper-stage-config.yml'
const testToken = 'testToken'

const response = await downloadFileFromGitHub(testURL, testToken)

expect(response.status).toBe(200)
expect(response.url).toBe('/repos/SAP/jenkins-library/contents/resources/piper-stage-config.yml')
expect(octokit.Octokit).toHaveBeenCalledWith({ auth: testToken, baseUrl: 'https://github.acme.com/api/v3' })
})
})
8 changes: 7 additions & 1 deletion test/piper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ describe('Piper', () => {
inputs['sap-piper-repository']
)
expect(config.createCheckIfStepActiveMaps).toHaveBeenCalledWith(
inputs['github-enterprise-token'], inputs['sap-piper-owner'], inputs['sap-piper-repository'])
'https://githubenterprise.test.com/',
'https://api.githubenterprise.test.com/',
inputs['sap-piper-version'] = '1.2.3',
inputs['github-enterprise-token'],
inputs['sap-piper-owner'],
inputs['sap-piper-repository']
)
expect(docker.cleanupContainers).toHaveBeenCalled()
})

Expand Down

0 comments on commit 022d974

Please sign in to comment.