Skip to content

Commit

Permalink
feat: fail when provided credentials are incorrect (#43)
Browse files Browse the repository at this point in the history
* refactor: add and use restore env utils for testing

* feat: fail when provided credentials are incorrect
  • Loading branch information
byCedric authored May 8, 2020
1 parent 32b5f9c commit a7b6ce4
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 17 deletions.
12 changes: 9 additions & 3 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20413,9 +20413,15 @@ function authenticate(username, password) {
const bin = process.platform === 'win32'
? 'expo.cmd'
: 'expo';
yield cli.exec(bin, ['login', `--username=${username}`], {
env: Object.assign(Object.assign({}, process.env), { EXPO_CLI_PASSWORD: password }),
});
try {
yield cli.exec(bin, ['login', `--username=${username}`], {
env: Object.assign(Object.assign({}, process.env), { EXPO_CLI_PASSWORD: password }),
});
}
catch (error) {
core.setFailed(error);
throw error;
}
});
}
exports.authenticate = authenticate;
Expand Down
17 changes: 11 additions & 6 deletions src/expo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ export async function authenticate(username: string, password: string) {
? 'expo.cmd'
: 'expo';

await cli.exec(bin, ['login', `--username=${username}`], {
env: {
...process.env,
EXPO_CLI_PASSWORD: password,
},
});
try {
await cli.exec(bin, ['login', `--username=${username}`], {
env: {
...process.env,
EXPO_CLI_PASSWORD: password,
},
});
} catch (error) {
core.setFailed(error);
throw error;
}
}
17 changes: 14 additions & 3 deletions tests/expo.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import * as core from '@actions/core';
import * as cli from '@actions/exec';
import * as expo from '../src/expo';
import { setPlatform, resetPlatform } from './utils';
import { setPlatform, restorePlatform, setEnv, restoreEnv } from './utils';

describe('authenticate', () => {
const spy = {
exec: jest.spyOn(cli, 'exec').mockImplementation(),
info: jest.spyOn(core, 'info').mockImplementation(),
fail: jest.spyOn(core, 'setFailed').mockImplementation(),
};

it('skips authentication without credentials', async () => {
await expo.authenticate('', '');
expect(spy.exec).not.toBeCalled();
expect(spy.fail).not.toBeCalled();
expect(spy.info).toBeCalled();
});

it('skips authentication without password', async () => {
await expo.authenticate('bycedric', '');
expect(spy.exec).not.toBeCalled();
expect(spy.fail).not.toBeCalled();
expect(spy.info).toBeCalled();
});

it('executes login command with password through environment', async () => {
process.env['TEST_INCLUDED'] = 'hellyeah';
setEnv('TEST_INCLUDED', 'hellyeah');
await expo.authenticate('bycedric', 'mypassword');
expect(spy.exec).toBeCalled();
expect(spy.exec.mock.calls[0][0]).toBe('expo');
Expand All @@ -33,13 +36,21 @@ describe('authenticate', () => {
EXPO_CLI_PASSWORD: 'mypassword',
},
});
restoreEnv();
});

it('executes login command with `.cmd` suffix on windows', async () => {
setPlatform('win32');
await expo.authenticate('bycedric', 'mypassword');
expect(spy.exec).toBeCalled();
expect(spy.exec.mock.calls[0][0]).toBe('expo.cmd');
resetPlatform();
restorePlatform();
});

it('fails when credentials are incorrect', async () => {
const error = new Error('Invalid username/password. Please try again.');
spy.exec.mockRejectedValue(error);
await expect(expo.authenticate('bycedric', 'incorrect')).rejects.toBe(error);
expect(spy.fail).toBeCalledWith(error);
});
});
9 changes: 7 additions & 2 deletions tests/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jest.mock('libnpm', () => registry);
jest.mock('../src/cache', () => cache);

import * as install from '../src/install';
import { setEnv, restoreEnv } from './utils';

describe('resolve', () => {
it('fetches exact version of expo-cli', async () => {
Expand Down Expand Up @@ -41,19 +42,23 @@ describe('install', () => {
});

describe('fromPackager', () => {
afterEach(() => {
restoreEnv();
});

it('resolves tool path', async () => {
await install.fromPackager('3.0.10', 'npm');
expect(io.which).toBeCalledWith('npm');
});

it('creates temporary folder', async () => {
process.env['RUNNER_TEMP'] = '/temp/path';
setEnv('RUNNER_TEMP', '/temp/path');
await install.fromPackager('latest', 'yarn');
expect(io.mkdirP).toBeCalledWith('/temp/path');
});

it('installs expo with tool', async () => {
process.env['RUNNER_TEMP'] = '/temp/path';
setEnv('RUNNER_TEMP', '/temp/path');
io.which.mockResolvedValue('npm');
const expoPath = await install.fromPackager('beta', 'npm');
expect(expoPath).toBe('/temp/path');
Expand Down
4 changes: 2 additions & 2 deletions tests/system.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from '@actions/core';
import * as cli from '@actions/exec';
import * as system from '../src/system';
import { setPlatform, resetPlatform } from './utils';
import { setPlatform, restorePlatform } from './utils';

describe('patchWatchers', () => {
const spy = {
Expand All @@ -11,7 +11,7 @@ describe('patchWatchers', () => {
};

afterEach(() => {
resetPlatform();
restorePlatform();
});

it('increses fs inotify settings with sysctl', async () => {
Expand Down
19 changes: 18 additions & 1 deletion tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ export function setPlatform(platform: NodeJS.Platform) {
/**
* Revert the platform to the original one.
*/
export function resetPlatform() {
export function restorePlatform() {
setPlatform(originalPlatform);
}

// keep track of the original environment variables
const originalEnv = { ...process.env };

/**
* Change the environment variable for testing purposes.
*/
export function setEnv(name: string, value: string) {
process.env[name] = value;
}

/**
* Revert the environment variable changes.
*/
export function restoreEnv() {
process.env = originalEnv;
}

0 comments on commit a7b6ce4

Please sign in to comment.