コード例 #1
0
        public void CredentialsItem_Equals_WithDifferentElemenName_ReturnsFalse()
        {
            var credentials1 = new CredentialsItem("source1", "user", "pass", isPasswordClearText: true, validAuthenticationTypes: "one,two,three");
            var credentials2 = new CredentialsItem("source2", "user", "pass", isPasswordClearText: true, validAuthenticationTypes: "one,two,three");

            credentials1.Equals(credentials2).Should().BeFalse();
        }
コード例 #2
0
        public void CredentialsItem_AsXNode_WithUsernameAndClearTextPassword_ReturnsCorrectElement()
        {
            // Arrange
            var credentialsItem = new CredentialsItem("name", "username", "password", isPasswordClearText: true, validAuthenticationTypes: null);

            // Act
            var xnode = credentialsItem.AsXNode();

            // Assert
            xnode.Should().BeOfType <XElement>();
            var xelement = xnode as XElement;

            xelement.Name.LocalName.Should().Be("name");
            var elements = xelement.Elements().ToList();

            elements.Count.Should().Be(2);
            elements[0].Name.LocalName.Should().Be("add");
            var elattr = elements[0].Attributes().ToList();

            elattr.Count.Should().Be(2);
            elattr[0].Value.Should().Be("Username");
            elattr[1].Value.Should().Be("username");

            elements[1].Name.LocalName.Should().Be("add");
            elattr = elements[1].Attributes().ToList();
            elattr.Count.Should().Be(2);
            elattr[0].Value.Should().Be("ClearTextPassword");
            elattr[1].Value.Should().Be("password");
        }
コード例 #3
0
        public void CredentialsItem_Equals_WithSameElementName_ReturnsTrue()
        {
            var credentials1 = new CredentialsItem("source", "user", "pass", isPasswordClearText: true, validAuthenticationTypes: "one,two,three");
            var credentials2 = new CredentialsItem("source", "user2", "pass", isPasswordClearText: false, validAuthenticationTypes: null);

            credentials1.Equals(credentials2).Should().BeTrue();
        }
コード例 #4
0
        public void CredentialsItem_Update_WhenOriginIsReadOnly_Throws()
        {
            // Arrange
            using (var mockBaseDirectory = TestDirectory.Create())
            {
                var origin = new SettingsFile(mockBaseDirectory, fileName: Settings.DefaultSettingsFileName, isMachineWide: false, isReadOnly: true);

                var xelement = new XElement("name",
                                            new XElement("add", new XAttribute("key", "Username"), new XAttribute("value", "user")),
                                            new XElement("add", new XAttribute("key", "Password"), new XAttribute("value", "pass")));

                var credentials = new CredentialsItem(xelement, origin);

                // Act
                var ex = Record.Exception(() =>
                                          credentials.Update(new CredentialsItem("name", "user", "notpass", isPasswordClearText: true, validAuthenticationTypes: null)));

                // Assert
                ex.Should().NotBeNull();
                ex.Should().BeOfType <InvalidOperationException>();
                ex.Message.Should().Be(Resources.CannotUpdateReadOnlyConfig);

                credentials.Password.Should().Be("pass");
            }
        }
コード例 #5
0
        public void CredentialsItem_Update_MakingPasswordEncrypted_UpdatesObjectAndXNode()
        {
            // Arrange
            using (var mockBaseDirectory = TestDirectory.Create())
            {
                var origin = new SettingsFile(mockBaseDirectory);

                var xelement = new XElement("name",
                                            new XElement("add", new XAttribute("key", "Username"), new XAttribute("value", "user")),
                                            new XElement("add", new XAttribute("key", "ClearTextPassword"), new XAttribute("value", "pass")));

                var credentials = new CredentialsItem(xelement, origin);

                // Act
                credentials.Update(new CredentialsItem("name", "user", "newpass", isPasswordClearText: false, validAuthenticationTypes: null));

                // Assert
                credentials.Password.Should().Be("newpass");

                var credentialElement = credentials.AsXNode() as XElement;
                var childElements     = credentialElement.Elements().ToList();

                childElements.Count.Should().Be(2);
                childElements[1].Name.LocalName.Should().Be("add");
                var elattr = childElements[1].Attributes().ToList();
                elattr.Count.Should().Be(2);
                elattr[0].Value.Should().Be("Password");
                elattr[1].Value.Should().Be("newpass");
            }
        }
コード例 #6
0
        public void AsCredentialsItem_WithAuthenticationTypes_WorksCorrectly()
        {
            var credentials = new PackageSourceCredential(
                "source",
                "user",
                "password",
                isPasswordClearText: false,
                validAuthenticationTypesText: "basic, negotiate");

            var expectedItem = new CredentialsItem("source", "user", "password", isPasswordClearText: false, validAuthenticationTypes: "basic, negotiate");

            SettingsTestUtils.DeepEquals(credentials.AsCredentialsItem(), expectedItem).Should().BeTrue();
        }
コード例 #7
0
        public void AsCredentialsItem_WithSpaceOnSourceName_WorksCorrectly()
        {
            var credentials = new PackageSourceCredential(
                "source name",
                "user",
                "password",
                isPasswordClearText: true,
                validAuthenticationTypesText: null);

            var expectedItem = new CredentialsItem("source name", "user", "password", isPasswordClearText: true, validAuthenticationTypes: null);

            SettingsTestUtils.DeepEquals(credentials.AsCredentialsItem(), expectedItem).Should().BeTrue();
        }
コード例 #8
0
        public void AsCredentialsItem_WithPassword_WorksCorrectly()
        {
            var credentials = new PackageSourceCredential(
                "source",
                "user",
                "password",
                isPasswordClearText: true,
                validAuthenticationTypesText: null);

            var expectedItem = new CredentialsItem("source", "user", "password", isPasswordClearText: true, validAuthenticationTypes: null);

            credentials.AsCredentialsItem().DeepEquals(expectedItem).Should().BeTrue();
        }
コード例 #9
0
        public void CredentialsItem_AsXNode_WithUrlInName_ReturnsCorrectElementName()
        {
            // Arrange
            var credentialsItem = new CredentialsItem("https://nuget.contoso.com/v3/index.json", "username", "password", isPasswordClearText: false, validAuthenticationTypes: null);

            // Act
            var xnode = credentialsItem.AsXNode();

            // Assert
            xnode.Should().BeOfType <XElement>();
            var xelement = xnode as XElement;

            xelement.Name.LocalName.Should().Be("https_x003A__x002F__x002F_nuget.contoso.com_x002F_v3_x002F_index.json");
        }
コード例 #10
0
        private static bool CredentialsItem_DeepEquals(CredentialsItem item1, CredentialsItem item2)
        {
            if (!ItemBase_DeepEquals(item1, item2))
            {
                return(false);
            }

            var validAutheticationTypesEquals = string.IsNullOrEmpty(item1.ValidAuthenticationTypes) ?
                                                string.IsNullOrEmpty(item2.ValidAuthenticationTypes) :
                                                string.Equals(item1.ValidAuthenticationTypes, item2.ValidAuthenticationTypes, StringComparison.Ordinal);

            return(string.Equals(item1.Username, item2.Username, StringComparison.Ordinal) &&
                   item1.IsPasswordClearText == item2.IsPasswordClearText &&
                   string.Equals(item1.Password, item2.Password, StringComparison.Ordinal) &&
                   validAutheticationTypesEquals);
        }
コード例 #11
0
        public void CredentialsItem_Update_WhenItemIsNotCredentialsItem_Throws()
        {
            // Arrange
            var credentials = new CredentialsItem("name", "user", "pass", isPasswordClearText: true, validAuthenticationTypes: null);

            // Act
            var ex = Record.Exception(() => credentials.Update(new AddItem("key", "value")));

            // Assert
            ex.Should().NotBeNull();
            ex.Should().BeOfType <InvalidOperationException>();
            ex.Message.Should().Be("The item passed to the Update method cannot refer to a different item than the one being updated.");

            credentials.ElementName.Should().Be("name");
            credentials.Username.Should().Be("user");
            credentials.Password.Should().Be("pass");
        }
コード例 #12
0
        public void CredentialsItem_Update_RemovingValidAuthenticationTypes_UpdatesObjectAndFile()
        {
            // Arrange
            using (var mockBaseDirectory = TestDirectory.Create())
            {
                var origin = new SettingsFile(mockBaseDirectory);

                var xelement = new XElement("name",
                                            new XElement("add", new XAttribute("key", "Username"), new XAttribute("value", "user")),
                                            new XElement("add", new XAttribute("key", "ClearTextPassword"), new XAttribute("value", "pass")),
                                            new XElement("add", new XAttribute("key", "ValidAuthenticationTypes"), new XAttribute("value", "one, two, three")));

                var credentials = new CredentialsItem(xelement, origin);

                // Act
                credentials.Update(new CredentialsItem("name", "user", "pass", isPasswordClearText: true, validAuthenticationTypes: null));

                // Assert
                credentials.ValidAuthenticationTypes.Should().BeNull();
                origin.IsDirty.Should().BeTrue();
                origin.SaveToDisk();

                var credentialElement = credentials.AsXNode() as XElement;
                var childElements     = credentialElement.Elements().ToList();

                childElements.Count.Should().Be(2);

                childElements[1].Name.LocalName.Should().Be("add");
                var elattr = childElements[0].Attributes().ToList();
                elattr.Count.Should().Be(2);
                elattr[0].Value.Should().Be("Username");

                childElements[1].Name.LocalName.Should().Be("add");
                elattr = childElements[1].Attributes().ToList();
                elattr.Count.Should().Be(2);
                elattr[0].Value.Should().Be("ClearTextPassword");
            }
        }
コード例 #13
0
        public void CredentialsItem_ElementName_IsCorrect()
        {
            var credentialsItem = new CredentialsItem("source", "user", "pass", isPasswordClearText: false, validAuthenticationTypes: null);

            credentialsItem.ElementName.Should().Be("source");
        }
コード例 #14
0
        public void CredentialsItem_Constructor_WithSpaceOnName_EncodesItCorrectly()
        {
            var item = new CredentialsItem("credentials name", "username", "password", isPasswordClearText: true, validAuthenticationTypes: null);

            item.ElementName.Should().Be("credentials name");
        }