Skip to content

Commit

Permalink
Readd mistakenly remoded test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tschuppi81 committed Jan 21, 2025
1 parent 18a1f89 commit f0392de
Showing 1 changed file with 160 additions and 0 deletions.
160 changes: 160 additions & 0 deletions tests/onegov/org/test_cronjobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from onegov.directory import (DirectoryEntryCollection,
DirectoryConfiguration,
DirectoryCollection)
from onegov.directory.collections.directory import EntryRecipientCollection
from onegov.event import EventCollection, OccurrenceCollection
from onegov.event.utils import as_rdates
from onegov.form import FormSubmissionCollection
Expand Down Expand Up @@ -1286,6 +1287,165 @@ def count_occurrences():
assert count_occurrences() == 0


def test_send_email_notification_for_recent_directory_entry_publications(
es_org_app,
handlers
):
org_app = es_org_app
register_echo_handler(handlers)
register_directory_handler(handlers)

client = Client(org_app)
job = get_cronjob_by_name(org_app, 'hourly_maintenance_tasks')
job.app = org_app
tz = ensure_timezone('Europe/Zurich')

assert len(os.listdir(client.app.maildir)) == 0

transaction.begin()

directories = DirectoryCollection(org_app.session(), type='extended')
planauflage = directories.add(
title='Öffentliche Planauflage',
structure="""
Gesuchsteller/in *= ___
Grundeigentümer/in *= ___
""",
configuration=DirectoryConfiguration(
title="[Gesuchsteller/in]",
order=('Gesuchsteller/in'),
searchable=['title'],
),
enable_update_notifications=True,
)
planauflage.add(values=dict(
gesuchsteller_in='Carmine Carminio',
grundeigentumer_in='Doris Dorinio',
publication_start=datetime(2025, 1, 6, 2, 0, tzinfo=tz),
publication_end=datetime(2025, 1, 30, 2, 0, tzinfo=tz),
))
planauflage.add(values=dict(
gesuchsteller_in='Emil Emilio',
grundeigentumer_in='Franco Francinio',
publication_start=datetime(2025, 1, 8, 6, 1, tzinfo=tz),
publication_end=datetime(2025, 1, 31, 2, 0, tzinfo=tz),
))

sport_clubs = directories.add(
title='Sport Clubs',
structure="""
Name *= ___
Category *= ___
""",
configuration=DirectoryConfiguration(
title="[Name]",
order=('Name'),
searchable=['title']
),
enable_update_notifications=False,
)
sport_clubs.add(values=dict(
name='Wanderfreunde',
category='Hiking',
publication_start=datetime(2025, 2, 1, 2, 0, tzinfo=tz),
publication_end=datetime(2025, 2, 22, 2, 0, tzinfo=tz),
))
sport_clubs.add(values=dict(
name='Pokerfreunde',
category='Games',
publication_start=datetime(2025, 2, 1, 2, 0, tzinfo=tz),
publication_end=datetime(2025, 2, 2, 2, 0, tzinfo=tz),
))

EntryRecipientCollection(org_app.session()).add(
directory_id=planauflage.id,
address='[email protected]',
confirmed=True
)
EntryRecipientCollection(org_app.session()).add(
directory_id=sport_clubs.id,
address='[email protected]',
confirmed=True
)

transaction.commit()
close_all_sessions()

def planauflagen():
return (DirectoryCollection(org_app.session(), type='extended')
.by_name('offentliche-planauflage'))

def sport_clubs():
return (DirectoryCollection(org_app.session(), type='extended')
.by_name('sport-clubs'))

def count_recipients():
return (EntryRecipientCollection(org_app.session()).query()
.filter_by(directory_id=planauflagen().id)
.filter_by(confirmed=True).count())

assert count_recipients() == 1
john = EntryRecipientCollection(org_app.session()).query().first()

assert org_app.org.meta.get('hourly_maintenance_tasks_last_run') is None

with freeze_time(datetime(2025, 1, 1, 4, 0, tzinfo=tz)):
client.get(get_cronjob_url(job))

assert len(os.listdir(client.app.maildir)) == 0
assert org_app.org.meta.get('hourly_maintenance_tasks_last_run')

with freeze_time(datetime(2025, 1, 6, 4, 0, tzinfo=tz)):
client.get(get_cronjob_url(job))

entry_1 = planauflagen().entries[0]

assert len(os.listdir(client.app.maildir)) == 1
message = client.get_email(0)
assert message['To'] == john.address
assert planauflagen().title in message['Subject']
assert entry_1.name in message['TextBody']

with freeze_time(datetime(2025, 1, 8, 10, 0, tzinfo=tz)):
client.get(get_cronjob_url(job))

entry_2 = planauflagen().entries[1]

assert len(os.listdir(client.app.maildir)) == 2
message = client.get_email(1)
assert message['To'] == john.address
assert planauflagen().title in message['Subject']
assert entry_2.name in message['TextBody']

# before enabling notifications for sport clubs after publication
with freeze_time(datetime(2025, 2, 1, 6, 0, tzinfo=tz)):
client.get(get_cronjob_url(job))

assert len(os.listdir(client.app.maildir)) == 2 # no additional mail

# enable notifications for sport clubs
sport_clubs().enable_update_notifications = True
transaction.commit()

with freeze_time(datetime(2025, 2, 1, 1, 0, tzinfo=tz)):
client.get(get_cronjob_url(job))

# no additional mail, because the entry is not published yet
assert len(os.listdir(client.app.maildir)) == 2

with freeze_time(datetime(2025, 2, 3, 10, 0, tzinfo=tz)):
client.get(get_cronjob_url(job))

entry_2 = sport_clubs().entries[1]

# only for still published sports club entry 'Wanderfreunde'
assert len(os.listdir(client.app.maildir)) == 3
message = client.get_email(2)
assert message['To'] == john.address
assert sport_clubs().title in message['Subject']
assert entry_2.name in message['TextBody']


def test_update_newsletter_email_bounce_statistics(org_app, handlers):
register_echo_handler(handlers)
register_directory_handler(handlers)
Expand Down

0 comments on commit f0392de

Please sign in to comment.