2020-04-15 13:25:51 +02:00
|
|
|
import unittest
|
|
|
|
import json
|
|
|
|
import urllib.request
|
|
|
|
|
|
|
|
import util.location
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-15 13:25:51 +02:00
|
|
|
class location(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
patcher = unittest.mock.patch("util.location.urllib.request")
|
2020-04-15 13:25:51 +02:00
|
|
|
self.addCleanup(patcher.stop)
|
|
|
|
self.request = patcher.start()
|
|
|
|
util.location.reset()
|
|
|
|
|
|
|
|
self.primary = {
|
2020-05-03 11:15:52 +02:00
|
|
|
"country": "Middle Earth",
|
|
|
|
"longitude": "10.0",
|
|
|
|
"latitude": "20.5",
|
|
|
|
"ip": "127.0.0.1",
|
2020-04-15 13:25:51 +02:00
|
|
|
}
|
|
|
|
self.secondary = {
|
2020-05-03 11:15:52 +02:00
|
|
|
"country_name": "Rivia",
|
|
|
|
"longitude": "-10.0",
|
|
|
|
"latitude": "-23",
|
|
|
|
"ip": "127.0.0.6",
|
2020-04-15 13:25:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def test_primary_provider(self):
|
|
|
|
self.request.urlopen.return_value.read.return_value = json.dumps(self.primary)
|
|
|
|
util.location.country()
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(self.primary["country"], util.location.country())
|
|
|
|
self.assertEqual(
|
|
|
|
(self.primary["latitude"], self.primary["longitude"]),
|
|
|
|
util.location.coordinates(),
|
|
|
|
)
|
|
|
|
self.assertEqual(self.primary["ip"], util.location.public_ip())
|
2020-04-15 13:25:51 +02:00
|
|
|
|
|
|
|
def test_secondary_provider(self):
|
|
|
|
urlopen = unittest.mock.MagicMock()
|
|
|
|
urlopen.read.return_value = json.dumps(self.secondary)
|
2020-05-03 11:15:52 +02:00
|
|
|
self.request.urlopen.side_effect = [RuntimeError(), urlopen]
|
|
|
|
|
|
|
|
self.assertEqual(self.secondary["country_name"], util.location.country())
|
|
|
|
self.assertEqual(
|
|
|
|
(self.secondary["latitude"], self.secondary["longitude"]),
|
|
|
|
util.location.coordinates(),
|
|
|
|
)
|
|
|
|
self.assertEqual(self.secondary["ip"], util.location.public_ip())
|
2020-04-15 13:25:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|