mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-02 17:48:36 -04:00
Scheduled tasks to clean up the database
- tasks run hourly - uses celery beat to schedule the tasks 4 new tasks - delete verify codes (after 1 day) - delete invitations (after 1 day) - delete successful notifications (after 1 day) - delete failed notifications (after 7 days) Delete methods in the DAO classes
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
from datetime import datetime, timedelta
|
||||
import uuid
|
||||
from app import db
|
||||
|
||||
from app.models import InvitedUser
|
||||
|
||||
@@ -6,7 +8,8 @@ from app.dao.invited_user_dao import (
|
||||
save_invited_user,
|
||||
get_invited_user,
|
||||
get_invited_users_for_service,
|
||||
get_invited_user_by_id
|
||||
get_invited_user_by_id,
|
||||
delete_invitations_older_created_more_than_a_day_ago
|
||||
)
|
||||
|
||||
|
||||
@@ -52,7 +55,6 @@ def test_get_unknown_invited_user_returns_none(notify_db, notify_db_session, sam
|
||||
|
||||
|
||||
def test_get_invited_users_for_service(notify_db, notify_db_session, sample_service):
|
||||
|
||||
from tests.app.conftest import sample_invited_user
|
||||
invites = []
|
||||
for i in range(0, 5):
|
||||
@@ -71,7 +73,6 @@ def test_get_invited_users_for_service(notify_db, notify_db_session, sample_serv
|
||||
|
||||
|
||||
def test_get_invited_users_for_service_that_has_no_invites(notify_db, notify_db_session, sample_service):
|
||||
|
||||
invites = get_invited_users_for_service(sample_service.id)
|
||||
assert len(invites) == 0
|
||||
|
||||
@@ -85,3 +86,40 @@ def test_save_invited_user_sets_status_to_cancelled(notify_db, notify_db_session
|
||||
assert InvitedUser.query.count() == 1
|
||||
cancelled_invited_user = InvitedUser.query.get(sample_invited_user.id)
|
||||
assert cancelled_invited_user.status == 'cancelled'
|
||||
|
||||
|
||||
def test_should_delete_all_invitations_more_than_one_day_old(
|
||||
sample_user,
|
||||
sample_service):
|
||||
make_invitation(sample_user, sample_service, age=timedelta(hours=24))
|
||||
make_invitation(sample_user, sample_service, age=timedelta(hours=24))
|
||||
assert len(InvitedUser.query.all()) == 2
|
||||
delete_invitations_older_created_more_than_a_day_ago()
|
||||
assert len(InvitedUser.query.all()) == 0
|
||||
|
||||
|
||||
def test_should_not_delete_invitations_less_than_one_day_old(
|
||||
sample_user,
|
||||
sample_service):
|
||||
make_invitation(sample_user, sample_service, age=timedelta(hours=23, minutes=59, seconds=59),
|
||||
email_address="valid@2.com")
|
||||
make_invitation(sample_user, sample_service, age=timedelta(hours=24),
|
||||
email_address="expired@1.com")
|
||||
|
||||
assert len(InvitedUser.query.all()) == 2
|
||||
delete_invitations_older_created_more_than_a_day_ago()
|
||||
assert len(InvitedUser.query.all()) == 1
|
||||
assert InvitedUser.query.first().email_address == "valid@2.com"
|
||||
|
||||
|
||||
def make_invitation(user, service, age=timedelta(hours=0), email_address="test@test.com"):
|
||||
verify_code = InvitedUser(
|
||||
email_address=email_address,
|
||||
from_user=user,
|
||||
service=service,
|
||||
status='pending',
|
||||
created_at=datetime.utcnow() - age,
|
||||
permissions='manage_settings'
|
||||
)
|
||||
db.session.add(verify_code)
|
||||
db.session.commit()
|
||||
|
||||
Reference in New Issue
Block a user