1
0
Files
login-action/__tests__/context.test.ts
T

71 lines
2.1 KiB
TypeScript
Raw Normal View History

2026-07-03 14:08:39 +02:00
import {afterEach, expect, test, vi} from 'vitest';
import * as path from 'path';
2023-09-08 09:35:48 +02:00
import {Buildx} from '@docker/actions-toolkit/lib/buildx/buildx.js';
import {getAuthList, getInputs} from '../src/context.js';
afterEach(() => {
2026-07-03 14:08:39 +02:00
vi.restoreAllMocks();
for (const key of Object.keys(process.env)) {
if (key.startsWith('INPUT_')) {
delete process.env[key];
}
}
});
2020-10-09 03:30:45 -07:00
2020-10-20 14:41:56 +02:00
test('with password and username getInputs does not throw error', async () => {
2020-10-16 18:34:48 +02:00
process.env['INPUT_USERNAME'] = 'dbowie';
2020-10-09 03:30:45 -07:00
process.env['INPUT_PASSWORD'] = 'groundcontrol';
2021-06-22 10:39:55 +02:00
process.env['INPUT_LOGOUT'] = 'true';
2020-10-09 03:30:45 -07:00
expect(() => {
getInputs();
2023-04-17 02:31:57 +02:00
}).not.toThrow();
2020-10-09 03:30:45 -07:00
});
test('getAuthList uses the default Docker Hub registry when computing scoped config dir', async () => {
process.env['INPUT_USERNAME'] = 'dbowie';
process.env['INPUT_PASSWORD'] = 'groundcontrol';
process.env['INPUT_SCOPE'] = 'myscope';
process.env['INPUT_LOGOUT'] = 'false';
const [auth] = getAuthList(getInputs());
expect(auth).toMatchObject({
registry: 'docker.io',
configDir: path.join(Buildx.configDir, 'config', 'registry-1.docker.io', 'myscope')
});
});
2026-07-03 14:08:39 +02:00
test('getAuthList skips secret masking when registry-auth password is absent', async () => {
const stdoutWriteSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
const [auth] = getAuthList({
registry: '',
username: '',
password: '',
scope: '',
ecr: '',
logout: true,
registryAuth: '- registry: public.ecr.aws\n'
});
expect(stdoutWriteSpy.mock.calls.map(call => call[0]).join('')).not.toContain('::add-mask::');
expect(auth).toMatchObject({
registry: 'public.ecr.aws',
ecr: 'auto'
});
});
test('getAuthList masks registry-auth password when present', async () => {
const stdoutWriteSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
getAuthList({
registry: '',
username: '',
password: '',
scope: '',
ecr: '',
logout: true,
registryAuth: '- registry: ghcr.io\n username: dbowie\n password: groundcontrol\n'
});
expect(stdoutWriteSpy.mock.calls.map(call => call[0]).join('')).toContain('::add-mask::groundcontrol');
});