Skip to content

Commit

Permalink
fix: ashx new AHS -> fails with credential problem (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chih-Ying authored Apr 1, 2020
1 parent 975c3e2 commit 2da4326
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
18 changes: 18 additions & 0 deletions lib/clients/git-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ module.exports = class GitClient {
* @param {string} credentialHelperPath the path of git credential helper
*/
configureCredentialHelper(credentialHelperPath) {
const commands = [
'git config --local credential.helper ""',
`git config --local --add credential.helper "!${credentialHelperPath}"`,
'git config --local credential.UseHttpPath true'];
const options = {
showStdOut: this.verbosityOptions.showOutput,
showStdErr: true,
showCmd: this.verbosityOptions.showCommand,
workingDir: this.projectPath
};
this._execCommands(commands, options);
}

/**
* Replace local git credential helper with credentialHelperPath
* @param {string} credentialHelperPath the path of git credential helper
*/
updateCredentialHelper(credentialHelperPath) {
const commands = [
`git config --local --replace-all credential.helper "!${credentialHelperPath}"`,
'git config --local credential.UseHttpPath true'];
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/util/upgrade-project/hosted-skill-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function postUpgradeGitSetup(profile, doDebug, gitClient, callback) {

function _setGitCredentialHelper(profile, gitClient) {
const credentialHelperPath = `askx util git-credentials-helper --profile ${profile}`;
gitClient.configureCredentialHelper(credentialHelperPath);
gitClient.updateCredentialHelper(credentialHelperPath);
}

function _updateGitIgnoreWithAskResourcesJson(gitClient) {
Expand Down
37 changes: 35 additions & 2 deletions test/unit/clients/git-client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ describe('Clients test - cli git client', () => {
describe('# test configureCredentialHelper', () => {
const TEST_CREDENTIAL_HELPER_PATH = 'TEST_CREDENTIAL_HELPER_PATH';
const TEST_COMMAND = [
`git config --local --replace-all credential.helper "!${TEST_CREDENTIAL_HELPER_PATH}"`,
'git config --local credential.helper ""',
`git config --local --add credential.helper "!${TEST_CREDENTIAL_HELPER_PATH}"`,
'git config --local credential.UseHttpPath true'];

afterEach(() => {
Expand All @@ -78,9 +79,10 @@ describe('Clients test - cli git client', () => {
// call
gitClient.configureCredentialHelper(TEST_CREDENTIAL_HELPER_PATH);
// verify
expect(gitClient._execChildProcessSync.callCount).eq(2);
expect(gitClient._execChildProcessSync.callCount).eq(3);
expect(gitClient._execChildProcessSync.args[0][0]).eq(TEST_COMMAND[0]);
expect(gitClient._execChildProcessSync.args[1][0]).eq(TEST_COMMAND[1]);
expect(gitClient._execChildProcessSync.args[2][0]).eq(TEST_COMMAND[2]);
});

it('| test git configureCredentialHelper fails', () => {
Expand All @@ -92,6 +94,37 @@ describe('Clients test - cli git client', () => {
});
});

describe('# test updateCredentialHelper', () => {
const TEST_CREDENTIAL_HELPER_PATH = 'TEST_CREDENTIAL_HELPER_PATH';
const TEST_COMMAND = [
`git config --local --replace-all credential.helper "!${TEST_CREDENTIAL_HELPER_PATH}"`,
'git config --local credential.UseHttpPath true'];

afterEach(() => {
sinon.restore();
});

it('| test git updateCredentialHelper execute commands correctly ', () => {
// setup
const gitClient = new GitClient(TEST_PROJECT_PATH, TEST_VERBOSITY_OPTIONS);
sinon.stub(gitClient, '_execChildProcessSync');
// call
gitClient.updateCredentialHelper(TEST_CREDENTIAL_HELPER_PATH);
// verify
expect(gitClient._execChildProcessSync.callCount).eq(2);
expect(gitClient._execChildProcessSync.args[0][0]).eq(TEST_COMMAND[0]);
expect(gitClient._execChildProcessSync.args[1][0]).eq(TEST_COMMAND[1]);
});

it('| test git updateCredentialHelper fails', () => {
// setup
const gitClient = new GitClient(TEST_PROJECT_PATH, TEST_VERBOSITY_OPTIONS_DEBUG);
sinon.stub(gitClient, '_execChildProcessSync').throws(new CLiError(`${TEST_ERROR}`));
// call & verify
expect(() => gitClient.updateCredentialHelper(TEST_CREDENTIAL_HELPER_PATH)).throw(CLiError, `CliError: ${TEST_ERROR}`);
});
});

describe('# test addOrigin', () => {
const TEST_REPO_URL = 'TEST_REPO_URL';
const TEST_COMMAND = [`git remote add origin ${TEST_REPO_URL}`];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ describe('Commands upgrade-project test - hosted skill helper test', () => {

it('| set Git Credential Helper fails, expect error thrown', (done) => {
// setup
sinon.stub(gitClient, 'configureCredentialHelper').throws(new CliError(TEST_ERROR));
sinon.stub(gitClient, 'updateCredentialHelper').throws(new CliError(TEST_ERROR));
// call
hostedSkillHelper.postUpgradeGitSetup(TEST_PROFILE, TEST_DO_DEBUG, gitClient, (err) => {
// verify
Expand All @@ -194,7 +194,7 @@ describe('Commands upgrade-project test - hosted skill helper test', () => {

it('| set Master As Default fails, expect error thrown', (done) => {
// setup
sinon.stub(gitClient, 'configureCredentialHelper');
sinon.stub(gitClient, 'updateCredentialHelper');
sinon.stub(gitClient, 'checkoutBranch');
sinon.stub(gitClient, 'merge');
sinon.stub(gitClient, 'deleteBranch').throws(new CliError(TEST_ERROR));
Expand All @@ -208,7 +208,7 @@ describe('Commands upgrade-project test - hosted skill helper test', () => {

it('| update git ignore file, expect error thrown', (done) => {
// setup
sinon.stub(gitClient, 'configureCredentialHelper');
sinon.stub(gitClient, 'updateCredentialHelper');
sinon.stub(gitClient, 'checkoutBranch');
sinon.stub(gitClient, 'merge');
sinon.stub(gitClient, 'deleteBranch');
Expand All @@ -224,7 +224,7 @@ describe('Commands upgrade-project test - hosted skill helper test', () => {

it('| set Pre PushHook Template fails, expect error thrown', (done) => {
// setup
sinon.stub(gitClient, 'configureCredentialHelper');
sinon.stub(gitClient, 'updateCredentialHelper');
sinon.stub(gitClient, 'checkoutBranch');
sinon.stub(gitClient, 'merge');
sinon.stub(gitClient, 'deleteBranch');
Expand All @@ -240,7 +240,7 @@ describe('Commands upgrade-project test - hosted skill helper test', () => {

it('| post Upgrade Git Setup succeeds, expect no error thrown', (done) => {
// setup
sinon.stub(gitClient, 'configureCredentialHelper');
sinon.stub(gitClient, 'updateCredentialHelper');
sinon.stub(gitClient, 'checkoutBranch');
sinon.stub(gitClient, 'merge');
sinon.stub(gitClient, 'deleteBranch');
Expand Down

0 comments on commit 2da4326

Please sign in to comment.