Skip to content

Commit

Permalink
chore: CLI Profile Remove - Check and Use config file before checking…
Browse files Browse the repository at this point in the history
… system keychain. (twilio#126)

* chore: CLI Profile Remove - Check and Use config file before checking system keychain

* Remves the check removeProfile while updating the project.
  • Loading branch information
ravali-rimmalapudi authored Jun 23, 2021
1 parent 9fb9dd3 commit e326a31
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 33 deletions.
10 changes: 7 additions & 3 deletions src/services/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,13 @@ class ConfigData {
}

removeProfile(profileToRemove) {
this.projects = this.projects.filter((profile) => {
return profile.id !== profileToRemove.id;
});
if (this.profiles[profileToRemove.id]) {
delete this.profiles[profileToRemove.id];
} else {
this.projects = this.projects.filter((profile) => {
return profile.id !== profileToRemove.id;
});
}
if (profileToRemove.id === this.activeProfile) {
this.activeProfile = null;
}
Expand Down
129 changes: 99 additions & 30 deletions test/services/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,46 +210,115 @@ describe('services', () => {
describe('ConfigData.removeProfile', () => {
test.it('remove a profile that does not exist', () => {
const configData = new ConfigData();
configData.addProfile('firstProfile', constants.FAKE_ACCOUNT_SID);
configData.addProfile('secondProfile', 'new_account_SID');
configData.addProfile('thirdProfile', 'newest_account_SID');
configData.addProject('firstProfile', constants.FAKE_ACCOUNT_SID);
configData.addProfile(
'secondProfile',
'new_account_SID',
'',
constants.FAKE_API_KEY,
constants.FAKE_API_SECRET,
);
configData.addProfile(
'thirdProfile',
'newest_account_SID',
'',
constants.FAKE_API_KEY,
constants.FAKE_API_SECRET,
);
const fakeProfile = {
id: 'DOES_NOT_EXIST',
accountSid: 'fake_SID',
};
const originalLength = configData.projects.length;
const originalProfilesLength = Object.keys(configData.profiles).length;
configData.removeProfile(fakeProfile);

expect(configData.projects.length).to.equal(originalLength);
expect(Object.keys(configData.profiles).length).to.equal(originalProfilesLength);
});

test.it('removes profile from projects', () => {
const configData = new ConfigData();
configData.addProject('firstProfile', constants.FAKE_ACCOUNT_SID);
configData.addProject('secondProfile', 'new_account_SID');
configData.addProject('thirdProfile', 'newest_account_SID');
configData.addProfile(
'fourthProfile',
'fourth_account_SID',
'',
constants.FAKE_API_KEY,
constants.FAKE_API_SECRET,
);
const profile = configData.getProfileById('secondProfile');
const originalLengthProfiles = Object.keys(configData.profiles).length;
configData.removeProfile(profile);

expect(configData.projects[1].id).to.equal('thirdProfile');
expect(configData.projects[1].accountSid).to.equal('newest_account_SID');
expect(Object.keys(configData.profiles).length).to.equal(originalLengthProfiles);
});
test.it('removes profile from profiles', () => {
const configData = new ConfigData();
configData.addProject('firstProfile', constants.FAKE_ACCOUNT_SID);
configData.addProject('secondProfile', 'new_account_SID');
configData.addProfile('thirdProfile', 'newest_account_SID', '', 'third_api_key', 'third_api_secret');
configData.addProfile(
'fourthProfile',
'fourth_account_SID',
'',
constants.FAKE_API_KEY,
constants.FAKE_API_SECRET,
);
const profile = configData.getProfileById('thirdProfile');
const originalLengthProjects = configData.projects.length;
configData.removeProfile(profile);

expect(configData.projects.length).to.equal(originalLengthProjects);
expect(configData.profiles[profile.id]).to.be.undefined;
});

/*
* TODO: To be fixed with profiles:remove functionality
* test.it('removes profile', () => {
* const configData = new ConfigData();
* configData.addProfile('firstProfile', constants.FAKE_ACCOUNT_SID);
* configData.addProfile('secondProfile', 'new_account_SID');
* configData.addProfile('thirdProfile', 'newest_account_SID');
* const profile = configData.getProfileById('secondProfile');
* configData.removeProfile(profile);
*
* expect(configData.projects[1].id).to.equal('thirdProfile');
* expect(configData.projects[1].accountSid).to.equal('newest_account_SID');
* });
*
* test.it('removes active profile', () => {
* const configData = new ConfigData();
* configData.addProfile('firstProfile', constants.FAKE_ACCOUNT_SID);
* configData.addProfile('secondProfile', 'new_account_SID');
* configData.addProfile('thirdProfile', 'newest_account_SID');
* const profile = configData.setActiveProfile('firstProfile');
* configData.removeProfile(profile);
*
* expect(configData.projects[1].id).to.equal('thirdProfile');
* expect(configData.projects[1].accountSid).to.equal('newest_account_SID');
* expect(configData.activeProfile).to.equal(null);
* });
*/
test.it('removes active profile of projects', () => {
const configData = new ConfigData();
configData.addProject('firstProfile', constants.FAKE_ACCOUNT_SID);
configData.addProject('secondProfile', 'new_account_SID');
configData.addProject('thirdProfile', 'newest_account_SID');
configData.addProfile(
'fourthProfile',
'fourth_account_SID',
'',
constants.FAKE_API_KEY,
constants.FAKE_API_SECRET,
);
const profile = configData.setActiveProfile('firstProfile');
const originalLengthProfiles = Object.keys(configData.profiles).length;
configData.removeProfile(profile);

expect(configData.projects[1].id).to.equal('thirdProfile');
expect(configData.projects[1].accountSid).to.equal('newest_account_SID');
expect(configData.activeProfile).to.equal(null);
expect(Object.keys(configData.profiles).length).to.equal(originalLengthProfiles);
});

test.it('removes active profile of profiles', () => {
const configData = new ConfigData();
configData.addProject('firstProfile', constants.FAKE_ACCOUNT_SID);
configData.addProject('secondProfile', 'new_account_SID');
configData.addProfile('thirdProfile', 'newest_account_SID', '', 'third_api_key', 'third_api_secret');
configData.addProfile(
'fourthProfile',
'fourth_account_SID',
'',
constants.FAKE_API_KEY,
constants.FAKE_API_SECRET,
);
const profile = configData.setActiveProfile('thirdProfile');
const originalLengthProjects = configData.projects.length;
configData.removeProfile(profile);

expect(configData.projects.length).to.equal(originalLengthProjects);
expect(configData.profiles[profile.id]).to.be.undefined;
expect(configData.activeProfile).to.equal(null);
});
});
describe('ConfigData.prompts', () => {
test.it('should store prompt acks', () => {
Expand Down

0 comments on commit e326a31

Please sign in to comment.