public void Remove_IgnoresAnyUnexistantTrustedSigner() { // Arrange var config = @" <configuration> <trustedSigners> <author name=""author1""> <certificate fingerprint=""abc"" hashAlgorithm=""SHA256"" allowUntrustedRoot=""false"" /> </author> <author name=""author2""> <certificate fingerprint=""def"" hashAlgorithm=""SHA256"" allowUntrustedRoot=""false"" /> </author> <repository name=""repo1"" serviceIndex=""https://serviceIndex.test/v3/api.json""> <certificate fingerprint=""ghi"" hashAlgorithm=""SHA256"" allowUntrustedRoot=""false"" /> </repository> </trustedSigners> </configuration>"; var nugetConfigPath = "NuGet.Config"; using (var mockBaseDirectory = TestDirectory.Create()) { SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config); var expectedTrustedSigners = new List <TrustedSignerItem>() { new AuthorItem("author2", new CertificateItem("def", HashAlgorithmName.SHA256)), new RepositoryItem("repo1", "https://serviceIndex.test/v3/api.json", new CertificateItem("ghi", HashAlgorithmName.SHA256)) }; // Act and Assert var settings = new Settings(mockBaseDirectory); settings.Should().NotBeNull(); var trustedSignerProvider = new TrustedSignersProvider(settings); var trustedSigners = trustedSignerProvider.GetTrustedSigners(); trustedSignerProvider.Remove(new[] { trustedSigners.First(), new AuthorItem("DontExist", new CertificateItem("abc", HashAlgorithmName.SHA256)) }); trustedSigners = trustedSignerProvider.GetTrustedSigners(); trustedSigners.Should().NotBeNull(); trustedSigners.Count.Should().Be(2); trustedSigners.Should().BeEquivalentTo( expectedTrustedSigners, options => options .Excluding(o => o.Path == "[0].Origin") .Excluding(o => o.Path == "[1].Origin")); } }
public void GetTrustedSigner_WithEmptyTrustedSignersSection_ReturnsEmptyList() { // Arrange var config = @" <configuration> <trustedSigners> </trustedSigners> </configuration>"; var nugetConfigPath = "NuGet.Config"; using (var mockBaseDirectory = TestDirectory.Create()) { SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config); // Act and Assert var settings = new Settings(mockBaseDirectory); settings.Should().NotBeNull(); var trustedSignerProvider = new TrustedSignersProvider(settings); var trustedSigners = trustedSignerProvider.GetTrustedSigners(); trustedSigners.Should().NotBeNull(); trustedSigners.Should().BeEmpty(); } }
public void GetTrustedSigner_WithItemsDifferentThanTrustedSigners_IgnoresThemAndOnlyReturnsTrustedSigners() { // Arrange var config = @" <configuration> <trustedSigners> <add key=""oneKey"" value=""val"" /> <author name=""author1""> <certificate fingerprint=""abc"" hashAlgorithm=""SHA256"" allowUntrustedRoot=""false"" /> </author> <author name=""author2""> <certificate fingerprint=""def"" hashAlgorithm=""SHA256"" allowUntrustedRoot=""false"" /> </author> <certificate fingerprint=""ghi"" hashAlgorithm=""SHA256"" allowUntrustedRoot=""false"" /> <repository name=""repo1"" serviceIndex=""https://serviceIndex.test/v3/api.json""> <certificate fingerprint=""ghi"" hashAlgorithm=""SHA256"" allowUntrustedRoot=""false"" /> </repository> <add key=""otherKey"" value=""val"" /> </trustedSigners> </configuration>"; var nugetConfigPath = "NuGet.Config"; using (var mockBaseDirectory = TestDirectory.Create()) { SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config); var expectedTrustedSigners = new List <TrustedSignerItem>() { new AuthorItem("author1", new CertificateItem("abc", HashAlgorithmName.SHA256)), new AuthorItem("author2", new CertificateItem("def", HashAlgorithmName.SHA256)), new RepositoryItem("repo1", "https://serviceIndex.test/v3/api.json", new CertificateItem("ghi", HashAlgorithmName.SHA256)) }; // Act and Assert var settings = new Settings(mockBaseDirectory); settings.Should().NotBeNull(); var trustedSignerProvider = new TrustedSignersProvider(settings); var trustedSigners = trustedSignerProvider.GetTrustedSigners(); trustedSigners.Should().NotBeNull(); trustedSigners.Count.Should().Be(3); trustedSigners.Should().BeEquivalentTo( expectedTrustedSigners, options => options .Excluding(o => o.SelectedMemberPath == "[0].Origin") .Excluding(o => o.SelectedMemberPath == "[1].Origin") .Excluding(o => o.SelectedMemberPath == "[2].Origin")); } }
public void AddOrUpdateTrustedSigner_WithNewTrustedSigner_AddsItSuccesfully() { // Arrange var config = @" <configuration> <trustedSigners> <author name=""author1""> <certificate fingerprint=""abc"" hashAlgorithm=""SHA256"" allowUntrustedRoot=""false"" /> </author> <author name=""author2""> <certificate fingerprint=""def"" hashAlgorithm=""SHA256"" allowUntrustedRoot=""false"" /> </author> <repository name=""repo1"" serviceIndex=""https://serviceIndex.test/v3/api.json""> <certificate fingerprint=""ghi"" hashAlgorithm=""SHA256"" allowUntrustedRoot=""false"" /> </repository> </trustedSigners> </configuration>"; var nugetConfigPath = "NuGet.Config"; using (var mockBaseDirectory = TestDirectory.Create()) { SettingsTestUtils.CreateConfigurationFile(nugetConfigPath, mockBaseDirectory, config); var expectedTrustedSigners = new List <TrustedSignerItem>() { new AuthorItem("author1", new CertificateItem("abc", HashAlgorithmName.SHA256)), new AuthorItem("author2", new CertificateItem("def", HashAlgorithmName.SHA256)), new RepositoryItem("repo1", "https://serviceIndex.test/v3/api.json", new CertificateItem("ghi", HashAlgorithmName.SHA256)), new AuthorItem("author3", new CertificateItem("jkl", HashAlgorithmName.SHA256)), }; // Act and Assert var settings = new Settings(mockBaseDirectory); settings.Should().NotBeNull(); var trustedSignerProvider = new TrustedSignersProvider(settings); trustedSignerProvider.AddOrUpdateTrustedSigner(new AuthorItem("author3", new CertificateItem("jkl", HashAlgorithmName.SHA256))); var trustedSigners = trustedSignerProvider.GetTrustedSigners(); trustedSigners.Should().NotBeNull(); trustedSigners.Count.Should().Be(4); trustedSigners.Should().BeEquivalentTo(expectedTrustedSigners); } }