def test_addProxy(self): # Given testfileProxyDao = FileProxyDao(filepath=self.tempProxyRepoPath) sut = ProxyRepo(dao=testfileProxyDao) proxy1 = Proxy.make(endpoint="this.is.valid", port=9384, username="******", pw="AndAPassword") proxy2 = Proxy.make(endpoint="a.better.world.for.com", port=2837, username="******", pw="GreatPassword") # When sut.addProxy(proxy1) sut.addProxy(proxy2) # Then with open(str(self.tempProxyRepoPath), encoding="utf-8") as file: lines = [line.rstrip("\n") for line in file.readlines()] self.assertIsInstance(lines, list) self.assertEqual(2, len(lines)) self.assertEqual("this.is.valid:9384:SomeUsername:AndAPassword", lines[0]) self.assertEqual("a.better.world.for.com:2837:Myself:GreatPassword", lines[1])
def test_getConverted_toString_shouldReturnNoneOnEmptyProxy(self): # Given proxy = Proxy() sut = StringProxyConverter(source=proxy, target=str) # When proxyStr = sut.getConverted() # Then self.assertEqual(None, proxyStr)
def test_buildForRequest_shouldRaiseIfProxyHasInvalidValues(self): # Given sut = Proxy.make(scheme="invalidScheme", endpoint="have.a.niceday", port=2983, username="******", pw="My_Password") # When / Then with self.assertRaises(ValueError): sut.buildForRequest()
def test_init_shouldSetDefaultValues(self): # When sut = Proxy() # Then self.assertEqual(sut.scheme, "http") self.assertEqual(sut.endpoint, "") self.assertEqual(sut.port, 0) self.assertEqual(sut.username, "") self.assertEqual(sut.pw, "")
def test_isValid_shouldReturnTrueIfAllIsValid(self): # Given sut = Proxy.make(endpoint="have.a.niceday", port=2983, username="******", pw="My_Password") # When result = sut.isValid() # Then self.assertTrue(result)
def test_getConverted_toString_shouldReturnValidSimpleProxy(self): # Given proxy = Proxy.make(endpoint="foo.bar.baz", port=9031) expectedString = "foo.bar.baz:9031" sut = StringProxyConverter(source=proxy, target=str) # When proxyStr = sut.getConverted() # Then self.assertEqual(expectedString, proxyStr)
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)
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])
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])
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)
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_")
def addProxy(self, proxy: Proxy) -> None: if proxy.isValid(): with self._dao as dao: dao.insert(data=proxy) # raises logger.debug( "Added proxy to repo. Proxy: ENDPOINT: %s, PORT: %d, " "USER: %s", proxy.endpoint, proxy.port, proxy.username) else: raise ValueError( "Proxy not added to repo because of one ore more invalid fields. " f"Values are: {proxy.scheme}, {proxy.username}, <PW is private>, " f"{proxy.endpoint}, {proxy.port}")
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)
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)
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()
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])
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)