Files
login-action/__tests__/docker.test.ts
T

95 lines
2.6 KiB
TypeScript
Raw Normal View History

import {afterEach, expect, test, vi} from 'vitest';
2023-09-08 09:35:48 +02:00
2026-03-01 11:26:40 +01:00
import {Docker} from '@docker/actions-toolkit/lib/docker/docker.js';
2024-07-22 10:41:37 +02:00
import {login, loginStandard, logout} from '../src/docker.js';
import * as dockerhub from '../src/dockerhub.js';
afterEach(() => {
vi.restoreAllMocks();
delete process.env.DOCKERHUB_OIDC_CONNECTIONID;
});
2020-10-09 03:30:45 -07:00
test('loginStandard calls exec', async () => {
2026-03-01 11:22:24 +01:00
const execSpy = vi.spyOn(Docker, 'getExecOutput').mockImplementation(async () => {
2022-02-28 08:28:55 +01:00
return {
2021-06-22 11:09:26 +02:00
exitCode: expect.any(Number),
stdout: expect.any(Function),
stderr: expect.any(Function)
2022-02-28 08:28:55 +01:00
};
});
2020-10-09 03:30:45 -07:00
2022-03-21 10:57:36 +01:00
const username = 'dbowie';
const password = 'groundcontrol';
const registry = 'https://ghcr.io';
2020-10-09 03:30:45 -07:00
await loginStandard(registry, username, password);
2024-07-22 10:41:37 +02:00
expect(execSpy).toHaveBeenCalledTimes(1);
const callfunc = execSpy.mock.calls[0];
if (callfunc && callfunc[1]) {
// we don't want to check env opt
callfunc[1].env = undefined;
}
expect(execSpy).toHaveBeenCalledWith(['login', '--password-stdin', '--username', username, registry], {
2020-10-09 03:30:45 -07:00
input: Buffer.from(password),
silent: true,
2021-06-22 11:09:26 +02:00
ignoreReturnCode: true
2020-10-09 03:30:45 -07:00
});
});
test('login exchanges Docker Hub OIDC token for password-less auth', async () => {
process.env.DOCKERHUB_OIDC_CONNECTIONID = '123e4567-e89b-42d3-a456-426614174000';
const execSpy = vi.spyOn(Docker, 'getExecOutput').mockImplementation(async () => {
return {
exitCode: 0,
stdout: '',
stderr: ''
};
});
const oidcSpy = vi.spyOn(dockerhub, 'getOIDCToken').mockResolvedValue({
username: 'docker-org',
token: 'hub-token'
});
await login({
registry: 'docker.io',
username: 'docker-org',
password: '',
scope: '',
ecr: 'auto',
configDir: ''
});
expect(oidcSpy).toHaveBeenCalledWith('docker.io', 'docker-org');
expect(execSpy).toHaveBeenCalledWith(['login', '--password-stdin', '--username', 'docker-org', 'docker.io'], {
input: Buffer.from('hub-token'),
silent: true,
ignoreReturnCode: true
});
});
2020-10-09 03:30:45 -07:00
test('logout calls exec', async () => {
2026-03-01 11:22:24 +01:00
const execSpy = vi.spyOn(Docker, 'getExecOutput').mockImplementation(async () => {
2022-02-28 08:28:55 +01:00
return {
2021-06-22 11:09:26 +02:00
exitCode: expect.any(Number),
stdout: expect.any(Function),
stderr: expect.any(Function)
2022-02-28 08:28:55 +01:00
};
});
2020-10-09 03:30:45 -07:00
2022-03-21 10:57:36 +01:00
const registry = 'https://ghcr.io';
2020-10-09 03:30:45 -07:00
await logout(registry, '');
2020-10-09 03:30:45 -07:00
2024-07-22 10:41:37 +02:00
expect(execSpy).toHaveBeenCalledTimes(1);
const callfunc = execSpy.mock.calls[0];
if (callfunc && callfunc[1]) {
// we don't want to check env opt
callfunc[1].env = undefined;
}
expect(execSpy).toHaveBeenCalledWith(['logout', registry], {
2021-06-22 11:09:26 +02:00
ignoreReturnCode: true
2020-10-09 03:30:45 -07:00
});
});