*: ensure unittests wait on tasks

This commit is contained in:
Jens Langhammer 2020-10-16 14:53:14 +02:00
parent 7154f19668
commit 3b9524cdfc
5 changed files with 7 additions and 7 deletions

View File

@ -14,5 +14,5 @@ class TestTasks(TestCase):
"""Test Token cleanup task"""
Token.objects.create(expires=now(), user=get_anonymous_user())
self.assertEqual(Token.objects.all().count(), 1)
clean_expired_models.delay()
clean_expired_models.delay().get()
self.assertEqual(Token.objects.all().count(), 0)

View File

@ -18,7 +18,7 @@ LOGGER = get_logger()
def save_ip_reputation(self: MonitoredTask):
"""Save currently cached reputation to database"""
objects_to_update = []
for key, score in cache.get_many(CACHE_KEY_IP_PREFIX + "*").items():
for key, score in cache.get_many(cache.keys(CACHE_KEY_IP_PREFIX + "*")).items():
remote_ip = key.replace(CACHE_KEY_IP_PREFIX, "")
rep, _ = IPReputation.objects.get_or_create(ip=remote_ip)
rep.score = score
@ -33,7 +33,7 @@ def save_ip_reputation(self: MonitoredTask):
def save_user_reputation(self: MonitoredTask):
"""Save currently cached reputation to database"""
objects_to_update = []
for key, score in cache.get_many(CACHE_KEY_USER_PREFIX + "*").items():
for key, score in cache.get_many(cache.keys(CACHE_KEY_USER_PREFIX + "*")).items():
username = key.replace(CACHE_KEY_USER_PREFIX, "")
users = User.objects.filter(username=username)
if not users.exists():

View File

@ -33,7 +33,7 @@ class TestReputationPolicy(TestCase):
# Test value in cache
self.assertEqual(cache.get(CACHE_KEY_IP_PREFIX + self.test_ip), -1)
# Save cache and check db values
save_ip_reputation.delay()
save_ip_reputation.delay().get()
self.assertEqual(IPReputation.objects.get(ip=self.test_ip).score, -1)
def test_user_reputation(self):
@ -43,7 +43,7 @@ class TestReputationPolicy(TestCase):
# Test value in cache
self.assertEqual(cache.get(CACHE_KEY_USER_PREFIX + self.test_username), -1)
# Save cache and check db values
save_user_reputation.delay()
save_user_reputation.delay().get()
self.assertEqual(UserReputation.objects.get(user=self.user).score, -1)
def test_policy(self):

View File

@ -48,4 +48,4 @@ class LDAPSyncTests(TestCase):
@patch("passbook.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
def test_tasks(self):
"""Test Scheduled tasks"""
ldap_sync_all.delay()
ldap_sync_all.delay().get()

View File

@ -127,7 +127,7 @@ class TestConsentStage(TestCase):
).exists()
)
sleep(1)
clean_expired_models.delay()
clean_expired_models.delay().get()
self.assertFalse(
UserConsent.objects.filter(
user=self.user, application=self.application