Skip to content

Commit

Permalink
Merge branch 'pr1403' into stable
Browse files Browse the repository at this point in the history
* pr1403:
  (GH-1301) Enable removal of API key via CLI
  • Loading branch information
ferventcoder committed Mar 6, 2019
2 parents db8bf5a + 2b1b5a6 commit 3540b3c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ public void should_add_short_version_of_apikey_to_the_option_set()
{
optionSet.Contains("k").ShouldBeTrue();
}

[Fact]
public void should_add_remove_to_the_option_set()
{
optionSet.Contains("remove").ShouldBeTrue();
}

[Fact]
public void should_add_short_version_of_remove_to_the_option_set()
{
optionSet.Contains("rem").ShouldBeTrue();
}
}

public class when_handling_validation : ChocolateyApiKeyCommandSpecsBase
Expand Down Expand Up @@ -154,6 +166,37 @@ public void should_continue_when_both_source_and_key_are_set()
configuration.Sources = "bob";
command.handle_validation(configuration);
}

[Fact]
public void should_throw_when_key_is_removed_without_a_source()
{
configuration.ApiKeyCommand.Remove = true;
configuration.Sources = "";
var errorred = false;
Exception error = null;

try
{
command.handle_validation(configuration);
}
catch (Exception ex)
{
errorred = true;
error = ex;
}

errorred.ShouldBeTrue();
error.ShouldNotBeNull();
error.ShouldBeType<ApplicationException>();
}

[Fact]
public void should_continue_when_removing_and_source_is_set()
{
configuration.ApiKeyCommand.Remove = true;
configuration.Sources = "bob";
command.handle_validation(configuration);
}
}

public class when_noop_is_called : ChocolateyApiKeyCommandSpecsBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ namespace chocolatey.infrastructure.app.commands
using logging;
using services;

[CommandFor("apikey", "retrieves or saves an apikey for a particular source")]
[CommandFor("setapikey", "retrieves or saves an apikey for a particular source (alias for apikey)")]
[CommandFor("apikey", "retrieves, saves or deletes an apikey for a particular source")]
[CommandFor("setapikey", "retrieves, saves or deletes an apikey for a particular source (alias for apikey)")]
public class ChocolateyApiKeyCommand : ICommand
{
private readonly IChocolateyConfigSettingsService _configSettingsService;
Expand All @@ -48,6 +48,9 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
.Add("k=|key=|apikey=|api-key=",
"ApiKey - The API key for the source. This is the authentication that identifies you and allows you to push to a source. With some sources this is either a key or it could be a user name and password specified as 'user:password'.",
option => configuration.ApiKeyCommand.Key = option.remove_surrounding_quotes())
.Add("rem|remove",
"Removes an API key from Chocolatey",
option => configuration.ApiKeyCommand.Remove = true)
;
}

Expand All @@ -58,9 +61,15 @@ public virtual void handle_additional_argument_parsing(IList<string> unparsedArg

public virtual void handle_validation(ChocolateyConfiguration configuration)
{
if (!string.IsNullOrWhiteSpace(configuration.ApiKeyCommand.Key) && string.IsNullOrWhiteSpace(configuration.Sources))
if (!configuration.ApiKeyCommand.Remove && !string.IsNullOrWhiteSpace(configuration.ApiKeyCommand.Key)
&& string.IsNullOrWhiteSpace(configuration.Sources))
{
throw new ApplicationException("You must specify both 'source' and 'key' to set an api key.");
throw new ApplicationException("You must specify both 'source' and 'key' to set an API key.");
}
if (configuration.ApiKeyCommand.Remove && string.IsNullOrWhiteSpace(configuration.Sources))
{
throw new ApplicationException("You must specify 'source' to remove an API key.");

}
}

Expand Down Expand Up @@ -125,7 +134,11 @@ public virtual void noop(ChocolateyConfiguration configuration)

public virtual void run(ChocolateyConfiguration configuration)
{
if (string.IsNullOrWhiteSpace(configuration.ApiKeyCommand.Key))
if (configuration.ApiKeyCommand.Remove)
{
_configSettingsService.remove_api_key(configuration);
}
else if (string.IsNullOrWhiteSpace(configuration.ApiKeyCommand.Key))
{
_configSettingsService.get_api_key(configuration, (key) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ public sealed class OutdatedCommandConfiguration
public sealed class ApiKeyCommandConfiguration
{
public string Key { get; set; }
public bool Remove { get; set; }
}

[Serializable]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,24 @@ public void set_api_key(ChocolateyConfiguration configuration)
}
}

public void remove_api_key(ChocolateyConfiguration configuration)
{
var apiKey = configFileSettings.ApiKeys.FirstOrDefault(p => p.Source.is_equal_to(configuration.Sources));
if (apiKey != null)
{
configFileSettings.ApiKeys.RemoveWhere(x => x.Source.is_equal_to(configuration.Sources));

_xmlService.serialize(configFileSettings, ApplicationParameters.GlobalConfigFileLocation);

this.Log().Info(() => "Removed ApiKey for {0}".format_with(configuration.Sources));
}
else
{
this.Log().Info(() => "ApiKey was not found for {0}".format_with(configuration.Sources));
}

}

public void config_list(ChocolateyConfiguration configuration)
{
this.Log().Info(ChocolateyLoggers.Important, "Settings");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public interface IChocolateyConfigSettingsService
void feature_enable(ChocolateyConfiguration configuration);
string get_api_key(ChocolateyConfiguration configuration, Action<ConfigFileApiKeySetting> keyAction);
void set_api_key(ChocolateyConfiguration configuration);
void remove_api_key(ChocolateyConfiguration configuration);
void config_list(ChocolateyConfiguration configuration);
void config_get(ChocolateyConfiguration configuration);
void config_set(ChocolateyConfiguration configuration);
Expand Down

0 comments on commit 3540b3c

Please sign in to comment.