def test_get_new_ip_current_ip_failed(self, 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 'get_current_ip' keeps failing. """ mock_get_current_ip.side_effect = TorIpError tor_ip_changer = TorIpChanger() 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)
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)
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)