Ejemplo n.º 1
0
    def test_getConverted_toStringlist_ShouldSkipInvalidProxies(self):
        # Given
        proxy1 = Proxy.make(endpoint="InvalidProxyMissingUsername",
                            port=2333,
                            username="",
                            pw="MyPass")
        proxy2 = Proxy.make(endpoint="186.230.153.100",
                            port=3526,
                            username="******",
                            pw="Some+-Pass")
        proxies = list((proxy1, proxy2))

        sut = StringlistProxiesConverter(source=proxies, target=tp.List[str])

        # When
        stringList = sut.getConverted()

        # Then
        self.assertEqual(1, len(stringList))
        self.assertEqual("186.230.153.100:3526:Some_User:Some+-Pass",
                         stringList[0])
Ejemplo n.º 2
0
    def test_buildForRequest(self):
        # 1. With authentication fields
        # Given
        sut = Proxy.make(scheme="http",
                         endpoint="build.this.com",
                         port=2738,
                         username="******",
                         pw="My_Password")
        # When
        buildString = sut.buildForRequest()
        # Then
        self.assertEqual("http://*****:*****@build.this.com:2738/",
                         buildString)

        # 2. Without authentication fields
        # Given
        sut = Proxy.make(endpoint="another.endpoint.com", port=9283)
        # When
        buildString = sut.buildForRequest()
        # Then
        self.assertEqual("http://another.endpoint.com:9283/", buildString)
Ejemplo n.º 3
0
    def test_insert(self):
        path = self.tempProxyRepoPath

        # Write some existing valid proxies before testing insert.
        existingProxies = [
            "243.172.183.94:8344:creepy-user:creepy_pass\n",
            "misoproponolpimpom:3344:jump-user:jump_pass"
        ]
        with open(str(path), "w", encoding="utf-8") as file:
            file.writelines(existingProxies)

        # Data to be inserted
        proxy1 = Proxy.make(endpoint="this.is.valid.com",
                            port=2938,
                            username="******",
                            pw="AndAPassword")
        proxy2 = Proxy.make(endpoint="this.too.com",
                            port=8493,
                            username="******",
                            pw="GreatPassword")

        with FileProxyDao(path) as sut:
            # When
            sut.insert(proxy1)
            sut.insert(proxy2)

            # Then
            with open(str(path), encoding="utf-8") as file:
                lines = [line.rstrip("\n") for line in file.readlines()]

            self.assertIsInstance(lines, list)
            self.assertEqual(4, len(lines))
            self.assertEqual("243.172.183.94:8344:creepy-user:creepy_pass",
                             lines[0])
            self.assertEqual("misoproponolpimpom:3344:jump-user:jump_pass",
                             lines[1])
            self.assertEqual(
                "this.is.valid.com:2938:SomeUsername:AndAPassword", lines[2])
            self.assertEqual("this.too.com:8493:Myself:GreatPassword",
                             lines[3])
Ejemplo n.º 4
0
    def test_addProxy_shouldRaiseOnInvalidProxy(self):
        # Given
        testfileProxyDao = FileProxyDao(filepath=self.tempProxyRepoPath)
        sut = ProxyRepo(dao=testfileProxyDao)

        proxy1 = Proxy.make(
            endpoint="Invalid endpoint, whitespace in string",
            port=2938,
        )

        # When / Then
        with self.assertRaises(ValueError):
            sut.addProxy(proxy1)
Ejemplo n.º 5
0
    def test_make_shouldSetCorrectProperties(self):
        # When
        sut = Proxy.make(scheme="https",
                         endpoint="this.is.a.test",
                         port=8273,
                         username="******",
                         pw="/Very*Special_")

        # Then
        self.assertEqual(sut.scheme, "https")
        self.assertEqual(sut.endpoint, "this.is.a.test")
        self.assertEqual(sut.port, 8273)
        self.assertEqual(sut.username, "Special")
        self.assertEqual(sut.pw, "/Very*Special_")
Ejemplo n.º 6
0
    def test_insert_shouldStripDuplicates(self):
        path = self.tempProxyRepoPath

        # Write existing valid proxy before testing insert.
        existingProxies = ["199.99.72.194:2837:creepy-user:creepy_pass\n"]
        with open(str(path), "w", encoding="utf-8") as file:
            file.writelines(existingProxies)

        # Data to be inserted
        proxy1 = Proxy.make(endpoint="this.is.valid.com",
                            port=2938,
                            username="******",
                            pw="AndAPassword")
        proxy2 = Proxy.make(endpoint="this.too.com",
                            port=8493,
                            username="******",
                            pw="GreatPassword")

        with FileProxyDao(path) as sut:
            sut.insert(proxy1)
            sut.insert(proxy2)

            # When
            sut.insert(
                proxy1
            )  # proxy1 would be a duplicate should be stripped by DAO method

        # Then
        with open(str(path), "r", encoding="utf-8") as file:
            allSaved = [line.rstrip("\n") for line in file.readlines()]

        self.assertEqual(3, len(allSaved))
        self.assertIn("199.99.72.194:2837:creepy-user:creepy_pass", allSaved)
        self.assertIn("this.too.com:8493:Myself:GreatPassword", allSaved)
        self.assertIn("this.is.valid.com:2938:SomeUsername:AndAPassword",
                      allSaved)
Ejemplo n.º 7
0
    def test_getConverted_toStringlist_shouldSetStringList(self):
        # Given
        proxy1 = Proxy.make(endpoint="abc.bar.de",
                            port=8021,
                            username="******",
                            pw="TestPW")
        proxy2 = Proxy.make(endpoint="123.234.217.1",
                            port=3526,
                            username="",
                            pw="")
        proxies = list((proxy1, proxy2))

        sut = StringlistProxiesConverter(source=proxies, target=tp.List[str])

        # When
        stringList = sut.getConverted()

        # Then
        self.assertIsInstance(stringList, list)
        self.assertEqual(2, len(stringList))
        idx = 0
        self.assertEqual("abc.bar.de:8021:Tester:TestPW", stringList[idx])
        idx = 1
        self.assertEqual("123.234.217.1:3526", stringList[idx])
Ejemplo n.º 8
0
    def test_getConverted_toString_shouldCallValidProxyCheck(self):
        # We won't test for invalid proxy strings here because there is a dedicated function
        # for this check which gets unit tested itself. Just test if this func is called.

        # Given
        proxy = Proxy.make(endpoint="foo.bar.baz", port=9031)
        sut = StringProxyConverter(source=proxy, target=str)

        # When
        with mock.patch("network.proxy.StringProxyConverter.isValidProxyString"
                        ) as check:
            sut.getConverted()

            # Then
            check.assert_called_once()
Ejemplo n.º 9
0
    def test_getConverted_toString_shouldReturnValidAuthProxy(self):
        # Given
        proxy = Proxy.make(endpoint="abc.bar.de",
                           port=8021,
                           username="******",
                           pw="TestPW")

        expectedString = "abc.bar.de:8021:Tester:TestPW"
        sut = StringProxyConverter(source=proxy, target=str)

        # When
        proxyStr = sut.getConverted()

        # Then
        self.assertEqual(expectedString, proxyStr)
Ejemplo n.º 10
0
    def test_isValid_shouldReturnFalseIfProxyIsConsideredInvalid(self):
        failedTestMsg = "Expected Proxy validation to return False, but got True."

        # Given
        sut = Proxy.make(endpoint="",
                         port=2983,
                         username="******",
                         pw="My_Password")
        # When
        result = sut.isValid()
        # Then
        self.assertFalse(result, failedTestMsg)

        # Given
        sut = Proxy.make(endpoint="MissingUsernameWhilePassIsGiven",
                         port=2983,
                         username="",
                         pw="My_Password")
        # When
        result = sut.isValid()
        # Then
        self.assertFalse(result, failedTestMsg)

        # Given
        sut = Proxy.make(endpoint="MissingPassWhileUsernameIsGiven",
                         port=2983,
                         username="******",
                         pw="")
        # When
        result = sut.isValid()
        # Then
        self.assertFalse(result, failedTestMsg)

        # Given
        sut = Proxy.make(scheme="", endpoint="MissingScheme", port=2950)
        # When
        result = sut.isValid()
        # Then
        self.assertFalse(result, failedTestMsg)

        # Given
        sut = Proxy.make(endpoint="Colon:InField", port=34)
        # When
        result = sut.isValid()
        # Then
        self.assertFalse(result, failedTestMsg)

        # Given
        sut = Proxy.make(endpoint="this.and.that.de",
                         port=4865,
                         username="******",
                         pw="CommentChar#InField")
        # When
        result = sut.isValid()
        # Then
        self.assertFalse(result, failedTestMsg)

        # Given
        sut = Proxy.make(endpoint="this.and.that.de",
                         port=4865,
                         username="******",
                         pw="MySpasswort")
        # When
        result = sut.isValid()
        # Then
        self.assertFalse(result, failedTestMsg)