1+ from unittest import mock
2+
3+ from django .contrib .gis .geos import Point
4+ from django .urls import reverse
15from swapper import load_model
26
37from ..tests .utils import CreateConfigMixin
@@ -21,6 +25,7 @@ def _create_whois_info(self, **kwargs):
2125 isp = "Google LLC" ,
2226 timezone = "America/Los_Angeles" ,
2327 cidr = "172.217.22.0/24" ,
28+ coordinates = Point (150 , 50 , srid = 4326 ),
2429 )
2530
2631 options .update (kwargs )
@@ -34,3 +39,104 @@ def setUp(self):
3439 OrganizationConfigSettings .objects .create (
3540 organization = self ._get_org (), whois_enabled = True
3641 )
42+
43+
44+ class WHOISTransactionMixin :
45+ @staticmethod
46+ def _mocked_client_response ():
47+ mock_response = mock .MagicMock ()
48+ mock_response .city .name = "Mountain View"
49+ mock_response .country .name = "United States"
50+ mock_response .continent .name = "North America"
51+ mock_response .postal .code = "94043"
52+ mock_response .traits .autonomous_system_organization = "Google LLC"
53+ mock_response .traits .autonomous_system_number = 15169
54+ mock_response .traits .network = "172.217.22.0/24"
55+ mock_response .location .time_zone = "America/Los_Angeles"
56+ mock_response .location .latitude = 50
57+ mock_response .location .longitude = 150
58+ return mock_response
59+
60+ def _task_called (self , mocked_task , task_name = "WHOIS lookup" ):
61+ org = self ._get_org ()
62+
63+ with self .subTest (f"{ task_name } task called when last_ip is public" ):
64+ with mock .patch ("django.core.cache.cache.set" ) as mocked_set :
65+ device = self ._create_device (last_ip = "172.217.22.14" )
66+ mocked_task .assert_called ()
67+ mocked_set .assert_called_once ()
68+ mocked_task .reset_mock ()
69+
70+ with self .subTest (
71+ f"{ task_name } task called when last_ip is changed and is public"
72+ ):
73+ with mock .patch ("django.core.cache.cache.get" ) as mocked_get :
74+ device .last_ip = "172.217.22.10"
75+ device .save ()
76+ mocked_task .assert_called ()
77+ # The cache `get` is called twice, once for `whois_enabled` and
78+ # once for `estimated_location_enabled`
79+ mocked_get .assert_called ()
80+ mocked_task .reset_mock ()
81+
82+ with self .subTest (f"{ task_name } task not called when last_ip is private" ):
83+ device .last_ip = "10.0.0.1"
84+ device .save ()
85+ mocked_task .assert_not_called ()
86+ mocked_task .reset_mock ()
87+
88+ with self .subTest (f"{ task_name } task not called when WHOIS is disabled" ):
89+ Device .objects .all ().delete ()
90+ org .config_settings .whois_enabled = False
91+ # Invalidates old org config settings cache
92+ org .config_settings .save (update_fields = ["whois_enabled" ])
93+ device = self ._create_device (last_ip = "172.217.22.14" )
94+ mocked_task .assert_not_called ()
95+ mocked_task .reset_mock ()
96+
97+ with self .subTest (
98+ f"{ task_name } task called via DeviceChecksumView when WHOIS is enabled"
99+ ):
100+ org .config_settings .whois_enabled = True
101+ # Invalidates old org config settings cache
102+ org .config_settings .save (update_fields = ["whois_enabled" ])
103+ # config is required for checksum view to work
104+ self ._create_config (device = device )
105+ # setting remote address field to a public IP to trigger WHOIS task
106+ # since the view uses this header for tracking the device's IP
107+ response = self .client .get (
108+ reverse ("controller:device_checksum" , args = [device .pk ]),
109+ {"key" : device .key },
110+ REMOTE_ADDR = "172.217.22.10" ,
111+ )
112+ self .assertEqual (response .status_code , 200 )
113+ mocked_task .assert_called ()
114+ mocked_task .reset_mock ()
115+
116+ with self .subTest (
117+ f"{ task_name } task called via DeviceChecksumView for no WHOIS record"
118+ ):
119+ WHOISInfo .objects .all ().delete ()
120+ response = self .client .get (
121+ reverse ("controller:device_checksum" , args = [device .pk ]),
122+ {"key" : device .key },
123+ REMOTE_ADDR = device .last_ip ,
124+ )
125+ self .assertEqual (response .status_code , 200 )
126+ mocked_task .assert_called ()
127+ mocked_task .reset_mock ()
128+
129+ with self .subTest (
130+ f"{ task_name } task not called via DeviceChecksumView when WHOIS is disabled"
131+ ):
132+ WHOISInfo .objects .all ().delete ()
133+ org .config_settings .whois_enabled = False
134+ org .config_settings .save (update_fields = ["whois_enabled" ])
135+ response = self .client .get (
136+ reverse ("controller:device_checksum" , args = [device .pk ]),
137+ {"key" : device .key },
138+ REMOTE_ADDR = device .last_ip ,
139+ )
140+ self .assertEqual (response .status_code , 200 )
141+ mocked_task .assert_not_called ()
142+ mocked_task .reset_mock ()
0 commit comments