Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_ip2location_info.py

raw
1 # Copyright (c) 2025, IP2Location <[email protected]>
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 import json
8 import unittest
9 from unittest.mock import Mock, patch
10
11 from ansible_collections.community.general.plugins.modules.ip2location_info import Ip2LocationInfo
12
13 IP2LOCATION_DATA = {
14 "ip": "8.8.8.8",
15 "country_code": "US",
16 "country_name": "United States of America",
17 "region_name": "California",
18 "city_name": "Mountain View",
19 "latitude": 37.386051,
20 "longitude": -122.083855,
21 "zip_code": "94035",
22 "time_zone": "-07:00",
23 "asn": "15169",
24 "as": "Google LLC",
25 "is_proxy": False,
26 }
27
28
29 class TestIp2LocationInfo(unittest.TestCase):
30 def test_get_geo_data_with_ip(self):
31 module = Mock()
32 module.params = {"timeout": 10, "ip": "8.8.8.8"}
33 module.from_json.side_effect = json.loads
34
35 with patch(
36 "ansible_collections.community.general.plugins.modules.ip2location_info.fetch_url"
37 ) as mock_fetch_url:
38 mock_response = Mock()
39 mock_response.read.return_value = json.dumps(IP2LOCATION_DATA)
40 mock_fetch_url.return_value = (mock_response, {"status": 200})
41
42 ip2location_info = Ip2LocationInfo(module)
43 result = ip2location_info.get_geo_data()
44
45 self.assertEqual(result["country_code"], "US")
46 mock_fetch_url.assert_called_once_with(module, "https://api.ip2location.io/?ip=8.8.8.8", timeout=10)