| 1 |
# Copyright (c) Ansible project |
| 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 |
| 10 |
|
| 11 |
from ansible_collections.community.general.plugins.modules.ipbase_info import IpbaseInfo |
| 12 |
|
| 13 |
IPBASE_DATA = { |
| 14 |
"response": b""" |
| 15 |
{ |
| 16 |
"data": { |
| 17 |
"ip": "1.1.1.1", |
| 18 |
"hostname": "one.one.one.one", |
| 19 |
"type": "v4", |
| 20 |
"range_type": { |
| 21 |
"type": "PUBLIC", |
| 22 |
"description": "Public address" |
| 23 |
}, |
| 24 |
"connection": { |
| 25 |
"asn": 13335, |
| 26 |
"organization": "Cloudflare, Inc.", |
| 27 |
"isp": "APNIC Research and Development", |
| 28 |
"range": "1.1.1.1/32" |
| 29 |
}, |
| 30 |
"location": { |
| 31 |
"geonames_id": 5332870, |
| 32 |
"latitude": 34.053611755371094, |
| 33 |
"longitude": -118.24549865722656, |
| 34 |
"zip": "90012", |
| 35 |
"continent": { |
| 36 |
"code": "NA", |
| 37 |
"name": "North America", |
| 38 |
"name_translated": "North America" |
| 39 |
}, |
| 40 |
"country": { |
| 41 |
"alpha2": "US", |
| 42 |
"alpha3": "USA", |
| 43 |
"calling_codes": [ |
| 44 |
"+1" |
| 45 |
], |
| 46 |
"currencies": [ |
| 47 |
{ |
| 48 |
"symbol": "$", |
| 49 |
"name": "US Dollar", |
| 50 |
"symbol_native": "$", |
| 51 |
"decimal_digits": 2, |
| 52 |
"rounding": 0, |
| 53 |
"code": "USD", |
| 54 |
"name_plural": "US dollars" |
| 55 |
} |
| 56 |
], |
| 57 |
"emoji": "...", |
| 58 |
"ioc": "USA", |
| 59 |
"languages": [ |
| 60 |
{ |
| 61 |
"name": "English", |
| 62 |
"name_native": "English" |
| 63 |
} |
| 64 |
], |
| 65 |
"name": "United States", |
| 66 |
"name_translated": "United States", |
| 67 |
"timezones": [ |
| 68 |
"America/New_York", |
| 69 |
"America/Detroit", |
| 70 |
"America/Kentucky/Louisville", |
| 71 |
"America/Kentucky/Monticello", |
| 72 |
"America/Indiana/Indianapolis", |
| 73 |
"America/Indiana/Vincennes", |
| 74 |
"America/Indiana/Winamac", |
| 75 |
"America/Indiana/Marengo", |
| 76 |
"America/Indiana/Petersburg", |
| 77 |
"America/Indiana/Vevay", |
| 78 |
"America/Chicago", |
| 79 |
"America/Indiana/Tell_City", |
| 80 |
"America/Indiana/Knox", |
| 81 |
"America/Menominee", |
| 82 |
"America/North_Dakota/Center", |
| 83 |
"America/North_Dakota/New_Salem", |
| 84 |
"America/North_Dakota/Beulah", |
| 85 |
"America/Denver", |
| 86 |
"America/Boise", |
| 87 |
"America/Phoenix", |
| 88 |
"America/Los_Angeles", |
| 89 |
"America/Anchorage", |
| 90 |
"America/Juneau", |
| 91 |
"America/Sitka", |
| 92 |
"America/Metlakatla", |
| 93 |
"America/Yakutat", |
| 94 |
"America/Nome", |
| 95 |
"America/Adak", |
| 96 |
"Pacific/Honolulu" |
| 97 |
], |
| 98 |
"is_in_european_union": false, |
| 99 |
"fips": "US", |
| 100 |
"geonames_id": 6252001, |
| 101 |
"hasc_id": "US", |
| 102 |
"wikidata_id": "Q30" |
| 103 |
}, |
| 104 |
"city": { |
| 105 |
"fips": "644000", |
| 106 |
"alpha2": null, |
| 107 |
"geonames_id": 5368753, |
| 108 |
"hasc_id": null, |
| 109 |
"wikidata_id": "Q65", |
| 110 |
"name": "Los Angeles", |
| 111 |
"name_translated": "Los Angeles" |
| 112 |
}, |
| 113 |
"region": { |
| 114 |
"fips": "US06", |
| 115 |
"alpha2": "US-CA", |
| 116 |
"geonames_id": 5332921, |
| 117 |
"hasc_id": "US.CA", |
| 118 |
"wikidata_id": "Q99", |
| 119 |
"name": "California", |
| 120 |
"name_translated": "California" |
| 121 |
} |
| 122 |
}, |
| 123 |
"tlds": [ |
| 124 |
".us" |
| 125 |
], |
| 126 |
"timezone": { |
| 127 |
"id": "America/Los_Angeles", |
| 128 |
"current_time": "2023-05-04T04:30:28-07:00", |
| 129 |
"code": "PDT", |
| 130 |
"is_daylight_saving": true, |
| 131 |
"gmt_offset": -25200 |
| 132 |
}, |
| 133 |
"security": { |
| 134 |
"is_anonymous": false, |
| 135 |
"is_datacenter": false, |
| 136 |
"is_vpn": false, |
| 137 |
"is_bot": false, |
| 138 |
"is_abuser": true, |
| 139 |
"is_known_attacker": true, |
| 140 |
"is_proxy": false, |
| 141 |
"is_spam": false, |
| 142 |
"is_tor": false, |
| 143 |
"is_icloud_relay": false, |
| 144 |
"threat_score": 100 |
| 145 |
}, |
| 146 |
"domains": { |
| 147 |
"count": 10943, |
| 148 |
"domains": [ |
| 149 |
"eliwise.academy", |
| 150 |
"accountingprose.academy", |
| 151 |
"pistola.academy", |
| 152 |
"1and1-test-ntlds-fr.accountant", |
| 153 |
"omnergy.africa" |
| 154 |
] |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
""" |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
class TestIpbase(unittest.TestCase): |
| 163 |
def test_info( |
| 164 |
self, |
| 165 |
): |
| 166 |
"test the json data extraction" |
| 167 |
|
| 168 |
params = { |
| 169 |
"ip": "1.1.1.1", |
| 170 |
"apikey": "aaa", |
| 171 |
"hostname": True, |
| 172 |
"language": "de", |
| 173 |
} |
| 174 |
module = Mock() |
| 175 |
module.params = params |
| 176 |
|
| 177 |
data = json.loads(IPBASE_DATA["response"].decode("utf-8")) |
| 178 |
|
| 179 |
IpbaseInfo._get_url_data = Mock() |
| 180 |
IpbaseInfo._get_url_data.return_value = data |
| 181 |
jenkins_plugin = IpbaseInfo(module) |
| 182 |
|
| 183 |
json_data = jenkins_plugin.info() |
| 184 |
|
| 185 |
self.maxDiff = None |
| 186 |
self.assertDictEqual(json_data, data) |