2020-06-20 14:53:44 +02:00
|
|
|
import pytest
|
2020-04-15 13:25:51 +02:00
|
|
|
import json
|
|
|
|
|
|
|
|
import util.location
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-06-20 14:53:44 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def urllib_req(mocker):
|
|
|
|
util.location.reset()
|
|
|
|
return mocker.patch("util.location.urllib.request")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-06-28 10:45:22 +02:00
|
|
|
def secondaryLocation():
|
2020-06-20 14:53:44 +02:00
|
|
|
return {
|
|
|
|
"country": "Middle Earth",
|
2022-11-26 12:05:42 +01:00
|
|
|
"lon": "10.0",
|
|
|
|
"lat": "20.5",
|
|
|
|
"query": "127.0.0.1",
|
2020-06-20 14:53:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-06-28 10:45:22 +02:00
|
|
|
def primaryLocation():
|
2020-06-20 14:53:44 +02:00
|
|
|
return {
|
2022-11-26 12:05:42 +01:00
|
|
|
"country": "Rivia",
|
2020-06-20 14:53:44 +02:00
|
|
|
"longitude": "-10.0",
|
|
|
|
"latitude": "-23",
|
|
|
|
"ip": "127.0.0.6",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def test_primary_provider(urllib_req, primaryLocation):
|
|
|
|
urllib_req.urlopen.return_value.read.return_value = json.dumps(primaryLocation)
|
|
|
|
|
2022-11-26 12:05:42 +01:00
|
|
|
assert util.location.country() == primaryLocation["country"]
|
2020-06-20 14:53:44 +02:00
|
|
|
assert util.location.coordinates() == (
|
|
|
|
primaryLocation["latitude"],
|
|
|
|
primaryLocation["longitude"],
|
|
|
|
)
|
|
|
|
assert util.location.public_ip() == primaryLocation["ip"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_secondary_provider(mocker, urllib_req, secondaryLocation):
|
|
|
|
urlopen = mocker.MagicMock()
|
|
|
|
urlopen.read.return_value = json.dumps(secondaryLocation)
|
|
|
|
urllib_req.urlopen.side_effect = [RuntimeError(), urlopen]
|
|
|
|
|
2020-06-28 10:45:22 +02:00
|
|
|
assert util.location.country() == secondaryLocation["country"]
|
2020-06-20 14:53:44 +02:00
|
|
|
assert util.location.coordinates() == (
|
2022-11-26 12:05:42 +01:00
|
|
|
secondaryLocation["lat"],
|
|
|
|
secondaryLocation["lon"],
|
2020-06-20 14:53:44 +02:00
|
|
|
)
|
2022-11-26 12:05:42 +01:00
|
|
|
assert util.location.public_ip() == secondaryLocation["query"]
|
2020-04-15 13:25:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|