Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Timo Ley
synapse
Commits
2451003f
Unverified
Commit
2451003f
authored
3 years ago
by
Sean Quah
Committed by
GitHub
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Test that `ClientIpStore` combines database and in-memory data correctly (#11179)
parent
29ffd680
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
changelog.d/11179.misc
+1
-0
1 addition, 0 deletions
changelog.d/11179.misc
tests/storage/test_client_ips.py
+206
-0
206 additions, 0 deletions
tests/storage/test_client_ips.py
with
207 additions
and
0 deletions
changelog.d/11179.misc
0 → 100644
+
1
−
0
View file @
2451003f
Add tests to check that `ClientIpStore.get_last_client_ip_by_device` and `get_user_ip_and_agents` combine database and in-memory data correctly.
This diff is collapsed.
Click to expand it.
tests/storage/test_client_ips.py
+
206
−
0
View file @
2451003f
...
@@ -20,6 +20,7 @@ from parameterized import parameterized
...
@@ -20,6 +20,7 @@ from parameterized import parameterized
import
synapse.rest.admin
import
synapse.rest.admin
from
synapse.http.site
import
XForwardedForRequest
from
synapse.http.site
import
XForwardedForRequest
from
synapse.rest.client
import
login
from
synapse.rest.client
import
login
from
synapse.storage.databases.main.client_ips
import
LAST_SEEN_GRANULARITY
from
synapse.types
import
UserID
from
synapse.types
import
UserID
from
tests
import
unittest
from
tests
import
unittest
...
@@ -171,6 +172,27 @@ class ClientIpStoreTestCase(unittest.HomeserverTestCase):
...
@@ -171,6 +172,27 @@ class ClientIpStoreTestCase(unittest.HomeserverTestCase):
if
after_persisting
:
if
after_persisting
:
# Trigger the storage loop
# Trigger the storage loop
self
.
reactor
.
advance
(
10
)
self
.
reactor
.
advance
(
10
)
else
:
# Check that the new IP and user agent has not been stored yet
db_result
=
self
.
get_success
(
self
.
store
.
db_pool
.
simple_select_list
(
table
=
"
devices
"
,
keyvalues
=
{},
retcols
=
(
"
user_id
"
,
"
ip
"
,
"
user_agent
"
,
"
device_id
"
,
"
last_seen
"
),
),
)
self
.
assertEqual
(
db_result
,
[
{
"
user_id
"
:
user_id
,
"
device_id
"
:
device_id
,
"
ip
"
:
None
,
"
user_agent
"
:
None
,
"
last_seen
"
:
None
,
},
],
)
result
=
self
.
get_success
(
result
=
self
.
get_success
(
self
.
store
.
get_last_client_ip_by_device
(
user_id
,
device_id
)
self
.
store
.
get_last_client_ip_by_device
(
user_id
,
device_id
)
...
@@ -189,6 +211,104 @@ class ClientIpStoreTestCase(unittest.HomeserverTestCase):
...
@@ -189,6 +211,104 @@ class ClientIpStoreTestCase(unittest.HomeserverTestCase):
},
},
)
)
def
test_get_last_client_ip_by_device_combined_data
(
self
):
"""
Test that `get_last_client_ip_by_device` combines persisted and unpersisted
data together correctly
"""
self
.
reactor
.
advance
(
12345678
)
user_id
=
"
@user:id
"
device_id_1
=
"
MY_DEVICE_1
"
device_id_2
=
"
MY_DEVICE_2
"
# Insert user IPs
self
.
get_success
(
self
.
store
.
store_device
(
user_id
,
device_id_1
,
"
display name
"
,
)
)
self
.
get_success
(
self
.
store
.
store_device
(
user_id
,
device_id_2
,
"
display name
"
,
)
)
self
.
get_success
(
self
.
store
.
insert_client_ip
(
user_id
,
"
access_token_1
"
,
"
ip_1
"
,
"
user_agent_1
"
,
device_id_1
)
)
self
.
get_success
(
self
.
store
.
insert_client_ip
(
user_id
,
"
access_token_2
"
,
"
ip_2
"
,
"
user_agent_2
"
,
device_id_2
)
)
# Trigger the storage loop and wait for the rate limiting period to be over
self
.
reactor
.
advance
(
10
+
LAST_SEEN_GRANULARITY
/
1000
)
# Update the user agent for the second device, without running the storage loop
self
.
get_success
(
self
.
store
.
insert_client_ip
(
user_id
,
"
access_token_2
"
,
"
ip_2
"
,
"
user_agent_3
"
,
device_id_2
)
)
# Check that the new IP and user agent has not been stored yet
db_result
=
self
.
get_success
(
self
.
store
.
db_pool
.
simple_select_list
(
table
=
"
devices
"
,
keyvalues
=
{},
retcols
=
(
"
user_id
"
,
"
ip
"
,
"
user_agent
"
,
"
device_id
"
,
"
last_seen
"
),
),
)
self
.
assertCountEqual
(
db_result
,
[
{
"
user_id
"
:
user_id
,
"
device_id
"
:
device_id_1
,
"
ip
"
:
"
ip_1
"
,
"
user_agent
"
:
"
user_agent_1
"
,
"
last_seen
"
:
12345678000
,
},
{
"
user_id
"
:
user_id
,
"
device_id
"
:
device_id_2
,
"
ip
"
:
"
ip_2
"
,
"
user_agent
"
:
"
user_agent_2
"
,
"
last_seen
"
:
12345678000
,
},
],
)
# Check that data from the database and memory are combined together correctly
result
=
self
.
get_success
(
self
.
store
.
get_last_client_ip_by_device
(
user_id
,
None
)
)
self
.
assertEqual
(
result
,
{
(
user_id
,
device_id_1
):
{
"
user_id
"
:
user_id
,
"
device_id
"
:
device_id_1
,
"
ip
"
:
"
ip_1
"
,
"
user_agent
"
:
"
user_agent_1
"
,
"
last_seen
"
:
12345678000
,
},
(
user_id
,
device_id_2
):
{
"
user_id
"
:
user_id
,
"
device_id
"
:
device_id_2
,
"
ip
"
:
"
ip_2
"
,
"
user_agent
"
:
"
user_agent_3
"
,
"
last_seen
"
:
12345688000
+
LAST_SEEN_GRANULARITY
,
},
},
)
@parameterized.expand
([(
False
,),
(
True
,)])
@parameterized.expand
([(
False
,),
(
True
,)])
def
test_get_user_ip_and_agents
(
self
,
after_persisting
:
bool
):
def
test_get_user_ip_and_agents
(
self
,
after_persisting
:
bool
):
"""
Test `get_user_ip_and_agents` for persisted and unpersisted data
"""
"""
Test `get_user_ip_and_agents` for persisted and unpersisted data
"""
...
@@ -207,6 +327,16 @@ class ClientIpStoreTestCase(unittest.HomeserverTestCase):
...
@@ -207,6 +327,16 @@ class ClientIpStoreTestCase(unittest.HomeserverTestCase):
if
after_persisting
:
if
after_persisting
:
# Trigger the storage loop
# Trigger the storage loop
self
.
reactor
.
advance
(
10
)
self
.
reactor
.
advance
(
10
)
else
:
# Check that the new IP and user agent has not been stored yet
db_result
=
self
.
get_success
(
self
.
store
.
db_pool
.
simple_select_list
(
table
=
"
user_ips
"
,
keyvalues
=
{},
retcols
=
(
"
access_token
"
,
"
ip
"
,
"
user_agent
"
,
"
last_seen
"
),
),
)
self
.
assertEqual
(
db_result
,
[])
self
.
assertEqual
(
self
.
assertEqual
(
self
.
get_success
(
self
.
store
.
get_user_ip_and_agents
(
user
)),
self
.
get_success
(
self
.
store
.
get_user_ip_and_agents
(
user
)),
...
@@ -220,6 +350,82 @@ class ClientIpStoreTestCase(unittest.HomeserverTestCase):
...
@@ -220,6 +350,82 @@ class ClientIpStoreTestCase(unittest.HomeserverTestCase):
],
],
)
)
def
test_get_user_ip_and_agents_combined_data
(
self
):
"""
Test that `get_user_ip_and_agents` combines persisted and unpersisted data
together correctly
"""
self
.
reactor
.
advance
(
12345678
)
user_id
=
"
@user:id
"
user
=
UserID
.
from_string
(
user_id
)
# Insert user IPs
self
.
get_success
(
self
.
store
.
insert_client_ip
(
user_id
,
"
access_token
"
,
"
ip_1
"
,
"
user_agent_1
"
,
"
MY_DEVICE_1
"
)
)
self
.
get_success
(
self
.
store
.
insert_client_ip
(
user_id
,
"
access_token
"
,
"
ip_2
"
,
"
user_agent_2
"
,
"
MY_DEVICE_2
"
)
)
# Trigger the storage loop and wait for the rate limiting period to be over
self
.
reactor
.
advance
(
10
+
LAST_SEEN_GRANULARITY
/
1000
)
# Update the user agent for the second device, without running the storage loop
self
.
get_success
(
self
.
store
.
insert_client_ip
(
user_id
,
"
access_token
"
,
"
ip_2
"
,
"
user_agent_3
"
,
"
MY_DEVICE_2
"
)
)
# Check that the new IP and user agent has not been stored yet
db_result
=
self
.
get_success
(
self
.
store
.
db_pool
.
simple_select_list
(
table
=
"
user_ips
"
,
keyvalues
=
{},
retcols
=
(
"
access_token
"
,
"
ip
"
,
"
user_agent
"
,
"
last_seen
"
),
),
)
self
.
assertEqual
(
db_result
,
[
{
"
access_token
"
:
"
access_token
"
,
"
ip
"
:
"
ip_1
"
,
"
user_agent
"
:
"
user_agent_1
"
,
"
last_seen
"
:
12345678000
,
},
{
"
access_token
"
:
"
access_token
"
,
"
ip
"
:
"
ip_2
"
,
"
user_agent
"
:
"
user_agent_2
"
,
"
last_seen
"
:
12345678000
,
},
],
)
# Check that data from the database and memory are combined together correctly
self
.
assertCountEqual
(
self
.
get_success
(
self
.
store
.
get_user_ip_and_agents
(
user
)),
[
{
"
access_token
"
:
"
access_token
"
,
"
ip
"
:
"
ip_1
"
,
"
user_agent
"
:
"
user_agent_1
"
,
"
last_seen
"
:
12345678000
,
},
{
"
access_token
"
:
"
access_token
"
,
"
ip
"
:
"
ip_2
"
,
"
user_agent
"
:
"
user_agent_3
"
,
"
last_seen
"
:
12345688000
+
LAST_SEEN_GRANULARITY
,
},
],
)
@override_config
({
"
limit_usage_by_mau
"
:
False
,
"
max_mau_value
"
:
50
})
@override_config
({
"
limit_usage_by_mau
"
:
False
,
"
max_mau_value
"
:
50
})
def
test_disabled_monthly_active_user
(
self
):
def
test_disabled_monthly_active_user
(
self
):
user_id
=
"
@user:server
"
user_id
=
"
@user:server
"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment