def test_ip_is_usable_used_ip(self): """ Test that '_ip_is_usable' returns False for an already used IP. """ tor_ip_changer = TorIpChanger() tor_ip_changer._real_ip = "0.0.0.0" tor_ip_changer.used_ips = ["1.1.1.1"] ip_usable = tor_ip_changer._ip_is_usable("1.1.1.1") self.assertFalse(ip_usable)
def test_manage_usable_ips_releases_used_ip(self): """ Test that '_manage_used_ips' successfully releases an used IP. """ tor_ip_changer = TorIpChanger() tor_ip_changer.used_ips = ["1.1.1.1"] current_ip = "2.2.2.2" tor_ip_changer._manage_used_ips(current_ip) self.assertEqual([current_ip], tor_ip_changer.used_ips)
def test_manage_used_ips_releases_oldest_used_ip(self): """ Test that '_manage_used_ips' successfully releases oldest used IP. """ tor_ip_changer = TorIpChanger(3) tor_ip_changer._real_ip = "0.0.0.0" tor_ip_changer.used_ips = ["1.1.1.1", "2.2.2.2", "3.3.3.3"] current_ip = "4.4.4.4" expected_used_ips = ["2.2.2.2", "3.3.3.3", current_ip] tor_ip_changer._manage_used_ips(current_ip) self.assertEqual(tor_ip_changer.used_ips, expected_used_ips)
def test_get_new_ip_success(self, mock_obtain_new_ip, mock_get_current_ip): """ Test that 'get_new_ip' gets a new usable Tor IP address on the third attempt. """ mock_get_current_ip.side_effect = ["1.1.1.1", None, "2.2.2.2"] tor_ip_changer = TorIpChanger() tor_ip_changer._real_ip = "0.0.0.0" tor_ip_changer.used_ips = ["1.1.1.1"] new_ip = tor_ip_changer.get_new_ip() self.assertEqual(new_ip, "2.2.2.2") self.assertEqual([new_ip], tor_ip_changer.used_ips) self.assertEqual(mock_obtain_new_ip.call_count, 2) self.assertEqual(mock_get_current_ip.call_count, 3)
def test_get_new_ip_ip_is_usable_failed( self, mock_ip_is_usable, mock_obtain_new_ip, mock_get_current_ip ): """ Test that 'get_new_ip' attempts to get a new IP only the limited number of times when '_ip_is_usable' keeps returning the same IP. """ mock_get_current_ip.return_value = "1.1.1.1" mock_ip_is_usable.return_value = False tor_ip_changer = TorIpChanger() tor_ip_changer.used_ips = ["1.1.1.1"] with self.assertRaises(TorIpError): tor_ip_changer.get_new_ip() self.assertEqual(mock_obtain_new_ip.call_count, NEW_IP_MAX_ATTEMPTS) self.assertEqual(mock_get_current_ip.call_count, NEW_IP_MAX_ATTEMPTS) self.assertEqual(mock_ip_is_usable.call_count, NEW_IP_MAX_ATTEMPTS)