Skip to content

Commit 312c930

Browse files
committed
add tests
1 parent 2df791f commit 312c930

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

__tests__/main.test.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {Updater} from '../src/updater'
1313
import {ImageService, MetricReporter} from '../src/image-service'
1414
import {updaterImageName} from '../src/docker-tags'
1515
import * as inputs from '../src/inputs'
16-
import {run} from '../src/main'
16+
import {run, credentialsFromEnv} from '../src/main'
1717

1818
import {eventFixturePath} from './helpers'
1919

@@ -792,3 +792,50 @@ describe('run', () => {
792792
})
793793
})
794794
})
795+
796+
describe('credentialsFromEnv', () => {
797+
const originalEnv = process.env.GITHUB_REGISTRIES_PROXY
798+
afterEach(() => {
799+
process.env.GITHUB_REGISTRIES_PROXY = originalEnv
800+
jest.clearAllMocks()
801+
})
802+
803+
it('returns an empty array if GITHUB_REGISTRIES_PROXY is not set', () => {
804+
delete process.env.GITHUB_REGISTRIES_PROXY
805+
expect(credentialsFromEnv()).toEqual([])
806+
})
807+
808+
it('returns an empty array if GITHUB_REGISTRIES_PROXY is not valid base64', () => {
809+
process.env.GITHUB_REGISTRIES_PROXY = 'not-base64!'
810+
expect(credentialsFromEnv()).toEqual([])
811+
})
812+
813+
it('returns an empty array if GITHUB_REGISTRIES_PROXY is not valid JSON', () => {
814+
process.env.GITHUB_REGISTRIES_PROXY =
815+
Buffer.from('not-json').toString('base64')
816+
expect(credentialsFromEnv()).toEqual([])
817+
})
818+
819+
it('returns parsed credentials and masks secrets', () => {
820+
const creds = [
821+
{
822+
url: 'https://foo',
823+
username: 'bar',
824+
password: 'baz',
825+
token: 'tok',
826+
host: 'h',
827+
'replaces-base': false
828+
}
829+
]
830+
process.env.GITHUB_REGISTRIES_PROXY = Buffer.from(
831+
JSON.stringify(creds)
832+
).toString('base64')
833+
const setSecretSpy = jest.spyOn(core, 'setSecret')
834+
const result = credentialsFromEnv()
835+
expect(result).toEqual(creds)
836+
expect(setSecretSpy).toHaveBeenCalledWith('baz')
837+
expect(setSecretSpy).toHaveBeenCalledWith('tok')
838+
expect(setSecretSpy).not.toHaveBeenCalledWith('bar')
839+
expect(setSecretSpy).not.toHaveBeenCalledWith('https://foo')
840+
})
841+
})

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ function dependabotJobUrl(id: number): string {
219219
return url_parts.filter(Boolean).join('/')
220220
}
221221

222-
function credentialsFromEnv(): Credential[] {
222+
export function credentialsFromEnv(): Credential[] {
223223
const registriesProxyStr = process.env.GITHUB_REGISTRIES_PROXY
224224
let credentialsStr: string
225225
if (registriesProxyStr !== undefined) {

0 commit comments

Comments
 (0)