Skip to content

Commit

Permalink
Merge pull request #18 from crazy-max/username-required
Browse files Browse the repository at this point in the history
Username required
  • Loading branch information
tonistiigi authored Oct 16, 2020
2 parents 4b15841 + 1c402b7 commit 39ef12f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 44 deletions.
10 changes: 9 additions & 1 deletion __tests__/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ import osm = require('os');

import {getInputs} from '../src/context';

test('without username getInputs throws errors', async () => {
expect(() => {
getInputs();
}).toThrowError('Input required and not supplied: username');
});

test('without password getInputs throws errors', async () => {
process.env['INPUT_USERNAME'] = 'dbowie';
expect(() => {
getInputs();
}).toThrowError('Input required and not supplied: password');
});

test('with password getInputs does not error', async () => {
test('with password and username getInputs does not error', async () => {
process.env['INPUT_USERNAME'] = 'dbowie';
process.env['INPUT_PASSWORD'] = 'groundcontrol';
expect(() => {
getInputs();
Expand Down
53 changes: 35 additions & 18 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,51 @@ test('errors when not run on linux platform', async () => {
expect(coreSpy).toHaveBeenCalledWith('Only supported on linux platform');
});

test('errors without username', async () => {
const platSpy = jest.spyOn(osm, 'platform');
platSpy.mockImplementation(() => 'linux');

const coreSpy: jest.SpyInstance = jest.spyOn(core, 'setFailed');

await run();

expect(coreSpy).toHaveBeenCalledWith('Input required and not supplied: username');
});

test('errors without password', async () => {
const platSpy = jest.spyOn(osm, 'platform');
platSpy.mockImplementation(() => 'linux');

const coreSpy: jest.SpyInstance = jest.spyOn(core, 'setFailed');

const username: string = 'dbowie';
process.env[`INPUT_USERNAME`] = username;

await run();

expect(coreSpy).toHaveBeenCalledWith('Input required and not supplied: password');
});

test('successful with only password', async () => {
const platSpy = jest.spyOn(osm, 'platform');
platSpy.mockImplementation(() => 'linux');

const setRegistrySpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setRegistry');
const setLogoutSpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setLogout');
const dockerSpy: jest.SpyInstance = jest.spyOn(docker, 'login');
dockerSpy.mockImplementation(() => {});

const password: string = 'groundcontrol';
process.env[`INPUT_PASSWORD`] = password;

await run();

expect(setRegistrySpy).toHaveBeenCalledWith('');
expect(setLogoutSpy).toHaveBeenCalledWith('');
expect(dockerSpy).toHaveBeenCalledWith('', '', password);
test('successful with username and password', async () => {
const platSpy = jest.spyOn(osm, 'platform');
platSpy.mockImplementation(() => 'linux');

const setRegistrySpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setRegistry');
const setLogoutSpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setLogout');
const dockerSpy: jest.SpyInstance = jest.spyOn(docker, 'login');
dockerSpy.mockImplementation(() => {});

const username: string = 'dbowie';
process.env[`INPUT_USERNAME`] = username;

const password: string = 'groundcontrol';
process.env[`INPUT_PASSWORD`] = password;

await run();

expect(setRegistrySpy).toHaveBeenCalledWith('');
expect(setLogoutSpy).toHaveBeenCalledWith('');
expect(dockerSpy).toHaveBeenCalledWith('', username, password);
});

test('calls docker login', async () => {
Expand All @@ -66,7 +83,7 @@ test('calls docker login', async () => {
process.env[`INPUT_REGISTRY`] = registry;

const logout: string = 'true';
process.env['INPUT_LOGOUT'] = logout
process.env['INPUT_LOGOUT'] = logout;

await run();

Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ inputs:
required: false
username:
description: 'Username used to log against the Docker registry'
required: false
required: true
password:
description: 'Password or personal access token used to log against the Docker registry'
required: true
Expand Down
36 changes: 13 additions & 23 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface Inputs {
export function getInputs(): Inputs {
return {
registry: core.getInput('registry'),
username: core.getInput('username'),
username: core.getInput('username', {required: true}),
password: core.getInput('password', {required: true}),
logout: core.getInput('logout')
};
Expand Down

0 comments on commit 39ef12f

Please sign in to comment.