<commit_before>using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Moq;
using NUnit.Framework;

namespace LastPass.Test
{
    [TestFixture]
    class FetcherTest
    {
        private const string UnknownEmailMessage = "Unknown email address.";
        private const string InvalidPasswordMessage = "Invalid password!";

        private const string Url = "https://lastpass.com/login.php";
        private const string Username = "username";
        private const string Password = "password";
        private readonly NameValueCollection _expectedValues = new NameValueCollection
            {
                {"method", "mobile"},
                {"web", "1"},
                {"xml", "1"},
                {"username", Username},
                {"hash", "e379d972c3eb59579abe3864d850b5f54911544adfa2daf9fb53c05d30cdc985"},
                {"iterations", "1"}
            };

        [Test]
        [ExpectedException(typeof(LoginException), ExpectedMessage = UnknownEmailMessage)]
        public void Login_failed_because_of_unknown_email()
        {
            var response = Encoding.UTF8.GetBytes(string.Format(
                "<response><error message=\"{0}\" cause=\"unknownemail\" /></response>",
                UnknownEmailMessage));

            var webClient = new Mock<IWebClient>();
            webClient
                .Setup(x => x.UploadValues(It.Is<string>(s => s == Url),
                                           It.Is<NameValueCollection>(v => AreEqual(v, _expectedValues))))
                .Returns(response);

            new Fetcher(Username, Password).Login(webClient.Object);
        }

        [Test]
        [ExpectedException(typeof(LoginException), ExpectedMessage = InvalidPasswordMessage)]
        public void Login_failed_because_of_invalid_password()
        {
            var response = Encoding.UTF8.GetBytes(string.Format(
                "<response><error message=\"{0}\" cause=\"unknownpassword\" /></response>",
                InvalidPasswordMessage));

            var webClient = new Mock<IWebClient>();
            webClient
                .Setup(x => x.UploadValues(It.Is<string>(s => s == Url),
                                           It.Is<NameValueCollection>(v => AreEqual(v, _expectedValues))))
                .Returns(response);

            new Fetcher(Username, Password).Login(webClient.Object);
        }

        [Test]
        public void Login_rerequests_with_given_iterations()
        {
            var response1 = Encoding.UTF8.GetBytes("<response><error iterations=\"5000\" /></response>");
            var response2 = Encoding.UTF8.GetBytes("<ok />");

            var expectedValues2 = new NameValueCollection(_expectedValues);
            expectedValues2.Set("hash", "7880a04588cfab954aa1a2da98fd9c0d2c6eba4c53e36a94510e6dbf30759256");
            expectedValues2.Set("iterations", "5000");

            var webClient = new Mock<IWebClient>();
            webClient
                .Setup(x => x.UploadValues(It.Is<string>(s => s == Url),
                                           It.Is<NameValueCollection>(v => AreEqual(v, _expectedValues))))
                .Returns(response1);

            webClient
                .Setup(x => x.UploadValues(It.Is<string>(s => s == Url),
                                           It.Is<NameValueCollection>(v => AreEqual(v, expectedValues2))))
                .Returns(response2);

            new Fetcher(Username, Password).Login(webClient.Object);
        }

        private static bool AreEqual(NameValueCollection a, NameValueCollection b)
        {
            return a.AllKeys.OrderBy(s => s).SequenceEqual(b.AllKeys.OrderBy(s => s)) &&
                   a.AllKeys.All(s => a[s] == b[s]);
        }
    }
}<commit_msg>Test login failure for unknown reason<commit_after>using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Moq;
using NUnit.Framework;

namespace LastPass.Test
{
    [TestFixture]
    class FetcherTest
    {
        private const string UnknownEmailMessage = "Unknown email address.";
        private const string InvalidPasswordMessage = "Invalid password!";
        private const string UnknownReasonMessage = "Unknown reason";

        private const string Url = "https://lastpass.com/login.php";
        private const string Username = "username";
        private const string Password = "password";
        private const string Iterations1 = "1";
        private const string Iterations2 = "5000";

        private static readonly NameValueCollection SharedExpectedValues = new NameValueCollection
            {
                {"method", "mobile"},
                {"web", "1"},
                {"xml", "1"},
                {"username", Username}
            };

        private static readonly NameValueCollection ExpectedValues1 = new NameValueCollection(SharedExpectedValues)
            {
                {"hash", "e379d972c3eb59579abe3864d850b5f54911544adfa2daf9fb53c05d30cdc985"},
                {"iterations", Iterations1}
            };

        private static readonly NameValueCollection ExpectedValues2 = new NameValueCollection(SharedExpectedValues)
            {
                {"hash", "7880a04588cfab954aa1a2da98fd9c0d2c6eba4c53e36a94510e6dbf30759256"},
                {"iterations", Iterations2}
            };

        [Test]
        [ExpectedException(typeof(LoginException), ExpectedMessage = UnknownEmailMessage)]
        public void Login_failed_because_of_unknown_email()
        {
            var response = Encoding.UTF8.GetBytes(string.Format(
                "<response><error message=\"{0}\" cause=\"unknownemail\" /></response>",
                UnknownEmailMessage));

            var webClient = new Mock<IWebClient>();
            webClient
                .Setup(x => x.UploadValues(It.Is<string>(s => s == Url),
                                           It.Is<NameValueCollection>(v => AreEqual(v, ExpectedValues1))))
                .Returns(response);

            new Fetcher(Username, Password).Login(webClient.Object);
        }

        [Test]
        [ExpectedException(typeof(LoginException), ExpectedMessage = InvalidPasswordMessage)]
        public void Login_failed_because_of_invalid_password()
        {
            var response = Encoding.UTF8.GetBytes(string.Format(
                "<response><error message=\"{0}\" cause=\"unknownpassword\" /></response>",
                InvalidPasswordMessage));

            var webClient = new Mock<IWebClient>();
            webClient
                .Setup(x => x.UploadValues(It.Is<string>(s => s == Url),
                                           It.Is<NameValueCollection>(v => AreEqual(v, ExpectedValues1))))
                .Returns(response);

            new Fetcher(Username, Password).Login(webClient.Object);
        }

        [Test]
        [ExpectedException(typeof(LoginException), ExpectedMessage = UnknownReasonMessage)]
        public void Login_failed_for_unknown_reason_with_error_element()
        {
            var response = Encoding.UTF8.GetBytes("<response><error /></response>");

            var webClient = new Mock<IWebClient>();
            webClient
                .Setup(x => x.UploadValues(It.Is<string>(s => s == Url),
                                           It.Is<NameValueCollection>(v => AreEqual(v, ExpectedValues1))))
                .Returns(response);

            new Fetcher(Username, Password).Login(webClient.Object);
        }

        [Test]
        [ExpectedException(typeof(LoginException), ExpectedMessage = UnknownReasonMessage)]
        public void Login_failed_for_unknown_reason_without_error_element()
        {
            var response = Encoding.UTF8.GetBytes("<response />");

            var webClient = new Mock<IWebClient>();
            webClient
                .Setup(x => x.UploadValues(It.Is<string>(s => s == Url),
                                           It.Is<NameValueCollection>(v => AreEqual(v, ExpectedValues1))))
                .Returns(response);

            new Fetcher(Username, Password).Login(webClient.Object);
        }

        [Test]
        public void Login_rerequests_with_given_iterations()
        {
            var response1 = Encoding.UTF8.GetBytes(string.Format("<response><error iterations=\"{0}\" /></response>", Iterations2));
            var response2 = Encoding.UTF8.GetBytes("<ok />");

            var webClient = new Mock<IWebClient>();
            webClient
                .Setup(x => x.UploadValues(It.Is<string>(s => s == Url),
                                           It.Is<NameValueCollection>(v => AreEqual(v, ExpectedValues1))))
                .Returns(response1);

            webClient
                .Setup(x => x.UploadValues(It.Is<string>(s => s == Url),
                                           It.Is<NameValueCollection>(v => AreEqual(v, ExpectedValues2))))
                .Returns(response2);

            new Fetcher(Username, Password).Login(webClient.Object);
        }

        private static bool AreEqual(NameValueCollection a, NameValueCollection b)
        {
            return a.AllKeys.OrderBy(s => s).SequenceEqual(b.AllKeys.OrderBy(s => s)) &&
                   a.AllKeys.All(s => a[s] == b[s]);
        }
    }
}
{'fraction_non_alphanumeric': 0.115899762367682, 'fraction_numerical': 0.023115143659537696, 'mean_word_length': 2.053759894459103, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 91, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2182773', 'n_tokens_mistral': 2542, 'n_tokens_neox': 2350, 'n_words': 450}