forked from chocolatey/choco
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(chocolatey#3565) PARTIAL: Add unit tests for ChocolateyNugetCredenti…
…alProvider
- Loading branch information
Showing
2 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
228 changes: 228 additions & 0 deletions
228
src/chocolatey.tests/infrastructure.app/nuget/ChocolateyNugetCredentialProviderSpecs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using chocolatey.infrastructure.app.configuration; | ||
using chocolatey.infrastructure.app.nuget; | ||
using FluentAssertions; | ||
using Moq; | ||
using NuGet.Configuration; | ||
|
||
namespace chocolatey.tests.infrastructure.app.nuget | ||
{ | ||
public class ChocolateyNugetCredentialProviderSpecs | ||
{ | ||
public abstract class ChocolateyNugetCredentialProviderSpecsBase : TinySpec | ||
{ | ||
protected ChocolateyConfiguration Configuration; | ||
protected ChocolateyNugetCredentialProvider Provider; | ||
|
||
protected const string Username = "user"; | ||
protected const string Password = "totally_secure_password!!!"; | ||
protected const string TargetSourceName = "testsource"; | ||
protected const string TargetSourceUrl = "https://testserver.org/repository/test-repository"; | ||
protected Uri TargetSourceUri = new Uri(TargetSourceUrl); | ||
|
||
protected NetworkCredential Result; | ||
|
||
private readonly CancellationTokenSource _tokenSource = new CancellationTokenSource(); | ||
protected CancellationToken CancellationToken | ||
{ | ||
get | ||
{ | ||
return _tokenSource.Token; | ||
} | ||
} | ||
|
||
public override void Context() | ||
{ | ||
Configuration = new ChocolateyConfiguration(); | ||
Provider = new ChocolateyNugetCredentialProvider(Configuration); | ||
|
||
Configuration.Information.IsInteractive = false; | ||
Configuration.MachineSources = new List<MachineSourceConfiguration> | ||
{ | ||
new MachineSourceConfiguration | ||
{ | ||
AllowSelfService = true, | ||
VisibleToAdminsOnly = false, | ||
EncryptedPassword = NugetEncryptionUtility.EncryptString("otherPassword"), | ||
Username = "otherUserName", | ||
Key = "https://someotherplace.com/repository/things/", | ||
Name = "not-this-one", | ||
Priority = 1, | ||
}, | ||
new MachineSourceConfiguration | ||
{ | ||
AllowSelfService = true, | ||
VisibleToAdminsOnly = false, | ||
EncryptedPassword = NugetEncryptionUtility.EncryptString(Password), | ||
Username = Username, | ||
Key = TargetSourceUrl, | ||
Name = TargetSourceName, | ||
Priority = 1, | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
public class When_using_explicit_credentials_and_source_param : ChocolateyNugetCredentialProviderSpecsBase | ||
{ | ||
public override void Because() | ||
{ | ||
Configuration.Sources = Configuration.ExplicitSources = TargetSourceUrl; | ||
Configuration.SourceCommand.Username = "user"; | ||
Configuration.SourceCommand.Password = "totally_secure_password!!!"; | ||
} | ||
|
||
[Fact] | ||
public async Task Returns_a_valid_credential() | ||
{ | ||
var result = await Provider.GetAsync(TargetSourceUri, proxy: null, CredentialRequestType.Unauthorized, message: string.Empty, isRetry: false, nonInteractive: true, CancellationToken); | ||
Result = result.Credentials as NetworkCredential; | ||
|
||
Result.Should().NotBeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Provides_the_correct_username() | ||
{ | ||
Result.UserName.Should().Be("user"); | ||
} | ||
|
||
[Fact] | ||
public void Provides_the_correct_password() | ||
{ | ||
Result.Password.Should().Be("totally_secure_password!!!"); | ||
} | ||
} | ||
|
||
public class When_a_source_name_is_provided : ChocolateyNugetCredentialProviderSpecsBase | ||
{ | ||
public override void Because() | ||
{ | ||
Configuration.Sources = TargetSourceUrl; | ||
Configuration.ExplicitSources = TargetSourceName; | ||
} | ||
|
||
[Fact] | ||
public async Task Finds_the_saved_source_and_returns_the_credential() | ||
{ | ||
var result = await Provider.GetAsync(TargetSourceUri, proxy: null, CredentialRequestType.Unauthorized, message: string.Empty, isRetry: false, nonInteractive: true, CancellationToken); | ||
Result = result.Credentials as NetworkCredential; | ||
|
||
Result.Should().NotBeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Provides_the_correct_username() | ||
{ | ||
Result.UserName.Should().Be(Username); | ||
} | ||
|
||
[Fact] | ||
public void Provides_the_correct_password() | ||
{ | ||
Result.Password.Should().Be(Password); | ||
} | ||
} | ||
|
||
public class When_a_source_url_matching_a_saved_source_is_provided : ChocolateyNugetCredentialProviderSpecsBase | ||
{ | ||
public override void Because() | ||
{ | ||
Configuration.Sources = Configuration.ExplicitSources = TargetSourceUrl; | ||
} | ||
|
||
[Fact] | ||
public async Task Finds_the_saved_source_and_returns_the_credential() | ||
{ | ||
var result = await Provider.GetAsync(TargetSourceUri, proxy: null, CredentialRequestType.Unauthorized, message: string.Empty, isRetry: false, nonInteractive: true, CancellationToken); | ||
Result = result.Credentials as NetworkCredential; | ||
|
||
Result.Should().NotBeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Provides_the_correct_username() | ||
{ | ||
Result.UserName.Should().Be(Username); | ||
} | ||
|
||
[Fact] | ||
public void Provides_the_correct_password() | ||
{ | ||
Result.Password.Should().Be(Password); | ||
} | ||
} | ||
|
||
public class Looks_up_source_url_when_name_and_credentials_is_provided : ChocolateyNugetCredentialProviderSpecsBase | ||
{ | ||
public override void Because() | ||
{ | ||
Configuration.Sources = TargetSourceUrl; | ||
Configuration.ExplicitSources = TargetSourceName; | ||
|
||
Configuration.SourceCommand.Username = "user"; | ||
Configuration.SourceCommand.Password = "totally_secure_password!!!"; | ||
} | ||
|
||
[Fact] | ||
public async Task Finds_the_saved_source_and_returns_the_credential() | ||
{ | ||
var result = await Provider.GetAsync(TargetSourceUri, proxy: null, CredentialRequestType.Unauthorized, message: string.Empty, isRetry: false, nonInteractive: true, CancellationToken); | ||
Result = result.Credentials as NetworkCredential; | ||
|
||
Result.Should().NotBeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Provides_the_correct_username() | ||
{ | ||
Result.UserName.Should().Be(Username); | ||
} | ||
|
||
[Fact] | ||
public void Provides_the_correct_password() | ||
{ | ||
Result.Password.Should().Be(Password); | ||
} | ||
} | ||
|
||
public class When_no_matching_source_is_found_by_url : ChocolateyNugetCredentialProviderSpecsBase | ||
{ | ||
public override void Because() | ||
{ | ||
Configuration.Sources = Configuration.ExplicitSources = "https://unknownurl.com/api/v2/"; | ||
} | ||
|
||
[Fact] | ||
public async Task Returns_the_default_network_credential() | ||
{ | ||
var result = await Provider.GetAsync(new Uri("https://unknownurl.com/api/v2/"), proxy: null, CredentialRequestType.Unauthorized, message: string.Empty, isRetry: false, nonInteractive: true, CancellationToken); | ||
result.Should().NotBeNull(); | ||
|
||
result.Credentials.Should().Be(CredentialCache.DefaultNetworkCredentials); | ||
} | ||
} | ||
|
||
public class When_multiple_named_sources_are_provided : ChocolateyNugetCredentialProviderSpecsBase | ||
{ | ||
|
||
} | ||
|
||
public class When_multiple_source_urls_are_provided : ChocolateyNugetCredentialProviderSpecsBase | ||
{ | ||
|
||
} | ||
|
||
public class When_a_mix_of_named_and_url_sources_are_provided : ChocolateyNugetCredentialProviderSpecsBase | ||
{ | ||
|
||
} | ||
} | ||
} |