Skip to content

Commit

Permalink
(chocolatey#2219) Update tests for PushCommand
Browse files Browse the repository at this point in the history
The tests were written to expect the throwing from the argument parsing
rather than the Validate method where they now throw. Moved these tests
into the proper place and amended them where necessary.
  • Loading branch information
vexx32 committed Apr 14, 2023
1 parent a88aafc commit be9d1d2
Showing 1 changed file with 46 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public void Should_not_add_short_version_of_timeout_to_the_option_set()
public class When_handling_additional_argument_parsing : ChocolateyPushCommandSpecsBase
{
private readonly IList<string> _unparsedArgs = new List<string>();
private const string ApiKey = "bobdaf";
private Action _because;

public override void Because()
Expand All @@ -141,33 +140,46 @@ public void Should_allow_a_path_to_the_nupkg_to_be_passed_in()
_because();
Configuration.Input.ShouldEqual(nupkgPath);
}
}

public class When_validating : ChocolateyPushCommandSpecsBase
{
private Action _because;
private string _apiKey = "abcdef";

public override void Because()
{
_because = () => Command.Validate(Configuration);
}

[Fact]
public void Should_set_the_source_to_defaultpushsource_if_set_and_no_explicit_source()
public void Should_not_override_explicit_source_if_defaultpushsource_is_set()
{
Reset();
Configuration.Sources = "";
Configuration.Sources = "https://localhost/somewhere/out/there";
Configuration.PushCommand.Key = _apiKey;
Configuration.PushCommand.DefaultSource = "https://localhost/default/source";
_because();

Configuration.Sources.ShouldEqual("https://localhost/default/source");
Configuration.Sources.ShouldEqual("https://localhost/somewhere/out/there");
}


[Fact]
public void Should_not_override_explicit_source_if_defaultpushsource_is_set()
public void Should_set_the_source_to_defaultpushsource_if_set_and_no_explicit_source()
{
Reset();
Configuration.Sources = "https://localhost/somewhere/out/there";
Configuration.Sources = "";
Configuration.PushCommand.DefaultSource = "https://localhost/default/source";
ConfigSettingsService.Setup(s => s.GetApiKey(It.Is<ChocolateyConfiguration>(c => c.Sources == "https://localhost/default/source"), null))
.Returns(() => _apiKey);

_because();

Configuration.Sources.ShouldEqual("https://localhost/somewhere/out/there");
Configuration.Sources.ShouldEqual("https://localhost/default/source");
}

[Fact]
public void Should_throw_when_defaultpushsource_is_not_set_and_no_explicit_sources()
{
Reset();
Configuration.PushCommand.DefaultSource = "";
Configuration.Sources = "";

Expand All @@ -193,42 +205,53 @@ public void Should_throw_when_defaultpushsource_is_not_set_and_no_explicit_sourc
[Fact]
public void Should_continue_when_defaultpushsource_is_not_set_and_explicit_sources_passed()
{
Reset();
Configuration.Sources = "https://somewhere/out/there";
Configuration.PushCommand.Key = "bob";
Configuration.PushCommand.Key = _apiKey;
Configuration.PushCommand.DefaultSource = "";
_because();
}

[Fact]
public void Should_not_set_the_apiKey_if_source_is_not_found()
public void Should_throw_if_apikey_is_not_found_for_source()
{
Reset();
ConfigSettingsService.Setup(c => c.GetApiKey(Configuration, null)).Returns("");
Configuration.PushCommand.Key = "";
Configuration.Sources = "https://localhost/somewhere/out/there";
_because();

Configuration.PushCommand.Key.ShouldEqual("");
var errorred = false;
Exception error = null;

try
{
_because();
}
catch (Exception ex)
{
errorred = true;
error = ex;
}

errorred.ShouldBeTrue();
error.ShouldNotBeNull();
error.ShouldBeType<ApplicationException>();
error.Message.ShouldContain($"An API key was not found for '{Configuration.Sources}'");
}

[Fact]
public void Should_not_try_to_determine_the_key_if_passed_in_as_an_argument()
{
Reset();
ConfigSettingsService.Setup(c => c.GetApiKey(Configuration, null)).Returns("");
Configuration.PushCommand.Key = "bob";
Configuration.PushCommand.Key = _apiKey;
Configuration.Sources = "https://localhost/somewhere/out/there";
_because();

Configuration.PushCommand.Key.ShouldEqual("bob");
Configuration.PushCommand.Key.ShouldEqual(_apiKey);
ConfigSettingsService.Verify(c => c.GetApiKey(It.IsAny<ChocolateyConfiguration>(), It.IsAny<Action<ConfigFileApiKeySetting>>()), Times.Never);
}

[Fact]
public void Should_not_try_to_determine_the_key_if_source_is_set_for_a_local_source()
{
Reset();
Configuration.Sources = "c:\\packages";
Configuration.PushCommand.Key = "";
_because();
Expand All @@ -239,7 +262,6 @@ public void Should_not_try_to_determine_the_key_if_source_is_set_for_a_local_sou
[Fact]
public void Should_not_try_to_determine_the_key_if_source_is_set_for_an_unc_source()
{
Reset();
Configuration.Sources = "\\\\someserver\\packages";
Configuration.PushCommand.Key = "";
_because();
Expand All @@ -250,7 +272,6 @@ public void Should_not_try_to_determine_the_key_if_source_is_set_for_an_unc_sour
[Fact]
public void Should_throw_if_multiple_sources_are_passed()
{
Reset();
Configuration.Sources = "https://localhost/somewhere/out/there;https://localhost/somewhere/out/there";

Assert.Throws<ApplicationException>(() => _because(), "Multiple sources are not support by push command.");
Expand All @@ -259,7 +280,6 @@ public void Should_throw_if_multiple_sources_are_passed()
[Fact]
public void Should_update_source_if_alias_is_passed()
{
Reset();
Configuration.Sources = "chocolatey";
Configuration.MachineSources = new List<MachineSourceConfiguration>
{
Expand All @@ -277,7 +297,6 @@ public void Should_update_source_if_alias_is_passed()
[Fact]
public void Should_update_source_if_alias_is_passed_via_defaultpushsource()
{
Reset();
Configuration.Sources = "";
Configuration.PushCommand.DefaultSource = "myrepo";
Configuration.MachineSources = new List<MachineSourceConfiguration>
Expand All @@ -292,39 +311,6 @@ public void Should_update_source_if_alias_is_passed_via_defaultpushsource()

Configuration.Sources.ShouldEqual("https://localhost/somewhere/out/there");
}
}

public class When_validating : ChocolateyPushCommandSpecsBase
{
private Action _because;

public override void Because()
{
_because = () => Command.Validate(Configuration);
}

[Fact]
public void Should_throw_when_source_is_not_set()
{
Configuration.Sources = "";
var errored = false;
Exception error = null;

try
{
_because();
}
catch (Exception ex)
{
errored = true;
error = ex;
}

errored.ShouldBeTrue();
error.ShouldNotBeNull();
error.ShouldBeType<ApplicationException>();
error.Message.ShouldContain("Source is required.");
}

[Fact]
public void Should_throw_when_apiKey_has_not_been_set_or_determined_for_a_https_source()
Expand Down Expand Up @@ -354,7 +340,7 @@ public void Should_throw_when_apiKey_has_not_been_set_or_determined_for_a_https_
public void Should_continue_when_source_and_apikey_is_set_for_a_https_source()
{
Configuration.Sources = "https://somewhere/out/there";
Configuration.PushCommand.Key = "bob";
Configuration.PushCommand.Key = _apiKey;
_because();
}

Expand All @@ -378,7 +364,7 @@ public void Should_continue_when_source_is_set_for_an_unc_source()
public void Should_throw_when_source_is_http_and_not_secure()
{
Configuration.Sources = "http://somewhere/out/there";
Configuration.PushCommand.Key = "bob";
Configuration.PushCommand.Key = _apiKey;
Configuration.Force = false;
var errored = false;
Exception error = null;
Expand All @@ -403,7 +389,7 @@ public void Should_throw_when_source_is_http_and_not_secure()
public void Should_continue_when_source_is_http_and_not_secure_if_force_is_passed()
{
Configuration.Sources = "http://somewhere/out/there";
Configuration.PushCommand.Key = "bob";
Configuration.PushCommand.Key = _apiKey;
Configuration.Force = true;

_because();
Expand Down

0 comments on commit be9d1d2

Please sign in to comment.