aboutsummaryrefslogtreecommitdiff
path: root/tests/models/thl/test_user_iphistory.py
diff options
context:
space:
mode:
authorMax Nanis2026-03-06 16:49:46 -0500
committerMax Nanis2026-03-06 16:49:46 -0500
commit91d040211a4ed6e4157896256a762d3854777b5e (patch)
treecd95922ea4257dc8d3f4e4cbe8534474709a20dc /tests/models/thl/test_user_iphistory.py
downloadgeneralresearch-91d040211a4ed6e4157896256a762d3854777b5e.tar.gz
generalresearch-91d040211a4ed6e4157896256a762d3854777b5e.zip
Initial commitv3.3.4
Diffstat (limited to 'tests/models/thl/test_user_iphistory.py')
-rw-r--r--tests/models/thl/test_user_iphistory.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/models/thl/test_user_iphistory.py b/tests/models/thl/test_user_iphistory.py
new file mode 100644
index 0000000..46018e0
--- /dev/null
+++ b/tests/models/thl/test_user_iphistory.py
@@ -0,0 +1,45 @@
+from datetime import timezone, datetime, timedelta
+
+from generalresearch.models.thl.user_iphistory import (
+ UserIPHistory,
+ UserIPRecord,
+)
+
+
+def test_collapse_ip_records():
+ # This does not exist in a db, so we do not need fixtures/ real user ids, whatever
+ now = datetime.now(tz=timezone.utc) - timedelta(days=1)
+ # Gets stored most recent first. This is reversed, but the validator will order it
+ records = [
+ UserIPRecord(ip="1.2.3.5", created=now + timedelta(minutes=1)),
+ UserIPRecord(
+ ip="1e5c:de49:165a:6aa0:4f89:1433:9af7:aaaa",
+ created=now + timedelta(minutes=2),
+ ),
+ UserIPRecord(
+ ip="1e5c:de49:165a:6aa0:4f89:1433:9af7:bbbb",
+ created=now + timedelta(minutes=3),
+ ),
+ UserIPRecord(ip="1.2.3.5", created=now + timedelta(minutes=4)),
+ UserIPRecord(
+ ip="1e5c:de49:165a:6aa0:4f89:1433:9af7:cccc",
+ created=now + timedelta(minutes=5),
+ ),
+ UserIPRecord(
+ ip="6666:de49:165a:6aa0:4f89:1433:9af7:aaaa",
+ created=now + timedelta(minutes=6),
+ ),
+ UserIPRecord(ip="1.2.3.6", created=now + timedelta(minutes=7)),
+ ]
+ iph = UserIPHistory(user_id=1, ips=records)
+ res = iph.collapse_ip_records()
+
+ # We should be left with one of the 1.2.3.5 ipv4s,
+ # and only the 1e5c::cccc and the 6666 ipv6 addresses
+ assert len(res) == 4
+ assert [x.ip for x in res] == [
+ "1.2.3.6",
+ "6666:de49:165a:6aa0:4f89:1433:9af7:aaaa",
+ "1e5c:de49:165a:6aa0:4f89:1433:9af7:cccc",
+ "1.2.3.5",
+ ]