Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add tests for the background update job
Browse files Browse the repository at this point in the history
  • Loading branch information
reivilibre committed Jan 17, 2022
1 parent 288b0e8 commit 2690bd6
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions tests/handlers/test_deactivate_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,124 @@ def test_account_data_deleted_upon_deactivation(self) -> None:
)
),
)

def _rerun_retroactive_account_data_deletion_job(self) -> None:
# Reset the 'all done' flag
self._store.db_pool.updates._all_done = False

self.get_success(
self._store.db_pool.simple_insert(
"background_updates",
{
"update_name": "delete_account_data_for_deactivated_users",
"progress_json": "{}",
},
)
)

self.wait_for_background_updates()

def test_account_data_deleted_retroactively_by_background_job_if_deactivated(
self,
) -> None:
"""
Tests that a user, who deactivated their account before account data was
deleted automatically upon deactivation, has their account data retroactively
scrubbed by the background job.
"""

# Request the deactivation of our account
req = self.get_success(
self.make_request(
"POST",
"account/deactivate",
{
"auth": {
"type": "m.login.password",
"user": self.user,
"password": "pass",
},
"erase": True,
},
access_token=self.token,
)
)
self.assertEqual(req.code, 200, req)

# Add some account data
# (we do this after the deactivation so that the act of deactivating doesn't
# clear it out. This emulates a user that was deactivated before this was cleared
# upon deactivation.)
self.get_success(
self._store.add_account_data_for_user(
self.user,
AccountDataTypes.DIRECT,
{"@someone:remote": ["!somewhere:remote"]},
)
)

# Check that the account data is there.
self.assertIsNotNone(
self.get_success(
self._store.get_global_account_data_by_type_for_user(
AccountDataTypes.DIRECT,
self.user,
)
),
)

# Re-run the retroactive deletion job
self._rerun_retroactive_account_data_deletion_job()

# Check that the account data was cleared.
self._store.get_global_account_data_by_type_for_user.invalidate_all()
self.assertIsNone(
self.get_success(
self._store.get_global_account_data_by_type_for_user(
AccountDataTypes.DIRECT,
self.user,
)
),
)

def test_account_data_preserved_by_background_job_if_not_deactivated(self) -> None:
"""
Tests that the background job does not scrub account data for users that have
not been deactivated.
"""

# Add some account data
# (we do this after the deactivation so that the act of deactivating doesn't
# clear it out. This emulates a user that was deactivated before this was cleared
# upon deactivation.)
self.get_success(
self._store.add_account_data_for_user(
self.user,
AccountDataTypes.DIRECT,
{"@someone:remote": ["!somewhere:remote"]},
)
)

# Check that the account data is there.
self.assertIsNotNone(
self.get_success(
self._store.get_global_account_data_by_type_for_user(
AccountDataTypes.DIRECT,
self.user,
)
),
)

# Re-run the retroactive deletion job
self._rerun_retroactive_account_data_deletion_job()

# Check that the account data was NOT cleared.
self._store.get_global_account_data_by_type_for_user.invalidate_all()
self.assertIsNotNone(
self.get_success(
self._store.get_global_account_data_by_type_for_user(
AccountDataTypes.DIRECT,
self.user,
)
),
)

0 comments on commit 2690bd6

Please sign in to comment.