Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions noipy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

import requests

HTTPBIN_URL = 'https://httpbin.org/ip'

try:
input = raw_input
except NameError:
Expand All @@ -21,10 +19,10 @@ def read_input(message):
return input(message)


def get_ip():
def get_ip(httpbin_url):
Copy link
Owner

@pv8 pv8 Jan 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this method is being used in other parts of the code (I need to improve the tests in general 😞), what about attributing a default value to the new function parameter?

Suggested change
def get_ip(httpbin_url):
def get_ip(req_ip_url=HTTPBIN_URL):

"""Return machine's origin IP address."""
try:
r = requests.get(HTTPBIN_URL)
r = requests.get(httpbin_url)
return r.json()['origin'] if r.status_code == 200 else None
except requests.exceptions.ConnectionError:
return None
Expand Down
6 changes: 2 additions & 4 deletions test/test_noipy.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ def tearDown(self):
shutil.rmtree(self.test_dir)

def test_get_ip(self):
ip = utils.get_ip()
ip = utils.get_ip('https://httpbin.org/ip')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, take a look at the above comment.

Suggested change
ip = utils.get_ip('https://httpbin.org/ip')
ip = utils.get_ip()


self.assertTrue(re.match(VALID_IP_REGEX, ip), 'get_ip() failed.')

# monkey patch for testing (forcing ConnectionError)
utils.HTTPBIN_URL = 'http://example.nothing'

ip = utils.get_ip()
ip = utils.get_ip('http://example.nothing')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, take a look at the above comment.

Suggested change
ip = utils.get_ip('http://example.nothing')
ip = utils.get_ip(req_ip_url='http://example.nothing')

self.assertTrue(ip is None, 'get_ip() should return None. IP={}'.format(ip))

def test_get_dns_ip(self):
Expand Down