Skip to content

Fix use of iconsistent timezones in tests #791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions collectives/api/event.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""API to get the event list in index page."""

import json
from datetime import datetime, timedelta
from datetime import timedelta

from flask import url_for, request, abort
from flask_login import current_user
Expand All @@ -15,7 +15,7 @@
from collectives.models import Question, QuestionAnswer, Configuration
from collectives.utils.url import slugify
from collectives.utils.access import valid_user
from collectives.utils.time import format_datetime_range, parse_api_date
from collectives.utils.time import format_datetime_range, parse_api_date, current_time


def photo_uri(event):
Expand Down Expand Up @@ -266,7 +266,7 @@ def events():

# Hide very old events to unauthenticated
if not current_user.is_authenticated:
min_date = datetime.now() - timedelta(
min_date = current_time() - timedelta(
days=Configuration.MAX_HISTORY_FOR_ANONYMOUS
)
query = query.filter(Event.end > min_date)
Expand Down
4 changes: 2 additions & 2 deletions collectives/forms/reservation.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Module for equipment reservation forms"""

from datetime import datetime
from wtforms import SubmitField, HiddenField

from flask_wtf.form import FlaskForm
from wtforms_alchemy import ModelForm

from collectives.models import Reservation
from collectives.utils.time import current_time


class ReservationForm(FlaskForm, ModelForm):
Expand All @@ -27,7 +27,7 @@ def __init__(self, *args, **kwargs):
self.event = kwargs["event"]
self.collect_date.data = self.event.start
else:
self.collect_date.data = datetime.now()
self.collect_date.data = current_time()


class LeaderReservationForm(ReservationForm):
Expand Down
4 changes: 1 addition & 3 deletions collectives/routes/auth/signup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Auth login to perform account creation and recover."""

import datetime

from flask import flash, render_template, redirect, url_for, request
from flask import current_app
from flask_login import current_user
Expand Down Expand Up @@ -60,7 +58,7 @@ def process_confirmation(token_uuid):
and token.existing_user.type == UserType.UnverifiedLocal
):
token.existing_user.type = UserType.Local
token.existing_user.legal_text_signature_date = datetime.datetime.now()
token.existing_user.legal_text_signature_date = current_time()
token.existing_user.legal_text_signed_version = (
Configuration.CURRENT_LEGAL_TEXT_VERSION
)
Expand Down
4 changes: 2 additions & 2 deletions collectives/routes/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# pylint: disable=too-many-lines
from typing import Tuple, List, Set
from datetime import datetime, timedelta
from datetime import timedelta

import builtins
from flask import flash, render_template, redirect, url_for, request, send_file
Expand Down Expand Up @@ -1213,7 +1213,7 @@ def preview(event_id):
if event is None:
abort(404)

min_date = datetime.now() - timedelta(days=Configuration.MAX_HISTORY_FOR_ANONYMOUS)
min_date = current_time() - timedelta(days=Configuration.MAX_HISTORY_FOR_ANONYMOUS)
if event.end < min_date:
abort(404)

Expand Down
5 changes: 3 additions & 2 deletions collectives/routes/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This modules contains the /profile Blueprint
"""

from datetime import date, datetime
from datetime import date
from os.path import exists
from io import BytesIO
import textwrap
Expand All @@ -29,6 +29,7 @@
from collectives.utils.access import valid_user
from collectives.utils.extranet import ExtranetError
from collectives.utils.misc import sanitize_file_name
from collectives.utils.time import current_time


images = Images()
Expand Down Expand Up @@ -205,7 +206,7 @@ def confidentiality_agreement():
request.method == "POST"
and current_user.confidentiality_agreement_signature_date == None
):
current_user.confidentiality_agreement_signature_date = datetime.now()
current_user.confidentiality_agreement_signature_date = current_time()
db.session.add(current_user)
db.session.commit()
flash("Merci d'avoir signé la charte RGPD", "success")
Expand Down
2 changes: 1 addition & 1 deletion collectives/utils/extranet.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def check_license(self, license_number: str) -> LicenseInfo:
if self.disabled():
# Dev mode, every license is valid
info.exists = True
info.renewal_date = datetime.now()
info.renewal_date = current_time()
return info

try:
Expand Down
6 changes: 3 additions & 3 deletions collectives/utils/init.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Module to initialise DB"""

import sqlite3
import datetime
import uuid
import sys

Expand All @@ -12,6 +11,7 @@

from collectives.models import ActivityType, EventType, User, Role, db, RoleIds
from collectives.models import Configuration, ConfigurationTypeEnum, ConfigurationItem
from collectives.utils.time import current_time


def catch_db_errors(fct, app, *args, **kwargs):
Expand Down Expand Up @@ -133,10 +133,10 @@ def init_admin(app):
user.license = str(uuid.uuid4())[:12]
user.first_name = "Compte"
user.last_name = "Administrateur"
user.confidentiality_agreement_signature_date = datetime.datetime.now()
user.confidentiality_agreement_signature_date = current_time()
version = Configuration.CURRENT_LEGAL_TEXT_VERSION
user.legal_text_signed_version = version
user.legal_text_signature_date = datetime.datetime.now()
user.legal_text_signature_date = current_time()
user.password = app.config["ADMINPWD"]
admin_role = Role(user=user, role_id=int(RoleIds.Administrator))
user.roles.append(admin_role)
Expand Down
5 changes: 3 additions & 2 deletions collectives/utils/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from collectives.models import RegistrationStatus, EventStatus, ActivityType
from collectives.models import User, EventTag
from collectives.utils.openpyxl import columns_best_fit
from collectives.utils.time import current_time

# Pylint does not understand that func.count IS callable.
# See https://github.com/pylint-dev/pylint/issues/1682
Expand Down Expand Up @@ -223,7 +224,7 @@ def __init__(self, **kwargs) -> None:
self.start = datetime(int(kwargs["year"]), 9, 1, 0, 0, 0)
self.end = datetime(int(kwargs["year"]) + 1, 8, 30, 23, 59)

self.creation_time = datetime.now()
self.creation_time = current_time()

def nb_days(self) -> int:
"""Returns number of days during the Engine interval.
Expand Down Expand Up @@ -482,7 +483,7 @@ def nb_unregistrations_inc_late_and_unjustified_absentees_per_week(self) -> dict
]
),
Event.start < func.now(),
Event.start >= datetime.now() - timedelta(weeks=52),
Event.start >= current_time() - timedelta(weeks=52),
)
.group_by("event_week", Registration.status)
)
Expand Down
7 changes: 4 additions & 3 deletions tests/events/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from collectives.models import db, EventStatus, ActivityType, Event, EventVisibility
from collectives.models import RoleIds, Question, QuestionType, RegistrationStatus
from collectives.utils.time import current_time
from tests import utils
from tests.fixtures.user import promote_user

Expand Down Expand Up @@ -195,7 +196,7 @@ def test_event_creation(leader_client):
response = leader_client.get("/collectives/add")
assert response.status_code == 200

now = datetime.now()
now = current_time()
alpinisme = ActivityType.query.filter_by(name="Alpinisme").first()
data = {
"update_activity": "0",
Expand Down Expand Up @@ -294,7 +295,7 @@ def test_event_duplication(leader_client, paying_event):
assert response.status_code == 200

data = utils.load_data_from_form(response.text, "form_edit_event")
now = datetime.now()
now = current_time()
data["start"] = (now + timedelta(days=33)).strftime("%Y-%m-%d %X")
data["end"] = (now + timedelta(days=33, hours=3)).strftime("%Y-%m-%d %X")
response = leader_client.post("/collectives/add", data=data)
Expand Down Expand Up @@ -433,7 +434,7 @@ def test_update_attendance_upcoming_event(
"""Test leader changing status of registrations for an event that starts soon"""

event = event1_with_reg_waiting_list
event.start = datetime.now() + timedelta(hours=1)
event.start = current_time() + timedelta(hours=1)
db.session.commit()

waiting_reg = event.waiting_registrations()[0]
Expand Down
5 changes: 3 additions & 2 deletions tests/events/test_registrations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Event actions tests related to registrations"""

from datetime import date, timedelta, datetime
from datetime import date, timedelta
from collectives.models import db, RegistrationStatus, BadgeIds
from collectives.utils.time import current_time


# pylint: disable=unused-argument
Expand Down Expand Up @@ -526,7 +527,7 @@ def test_register_for_valid_suspended_user(
assert user.has_a_valid_badge([BadgeIds.Suspended])
assert user.has_a_valid_suspended_badge()
assert len(event.registrations) == 6
assert not event.can_self_register(user, datetime.now())
assert not event.can_self_register(user, current_time())

response = user_client.post(
f"/collectives/{event.id}/self_register", follow_redirects=True
Expand Down
29 changes: 15 additions & 14 deletions tests/fixtures/event.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Module to create fixture events."""

from datetime import date, timedelta, datetime
from datetime import date, timedelta
from functools import wraps

import pytest
Expand All @@ -9,6 +9,7 @@
from collectives.models import EventType, Event, EventTag, EventStatus, EventVisibility
from collectives.models import Registration, RegistrationLevels, RegistrationStatus
from collectives.models import Question, QuestionType, QuestionAnswer
from collectives.utils.time import current_time

from tests.fixtures import payment

Expand Down Expand Up @@ -298,13 +299,13 @@ def event_in_less_than_x_hours_with_reg(
"""

prototype_event_in_less_than_x_hours.registration_open_time = (
datetime.now() - timedelta(hours=10)
current_time() - timedelta(hours=10)
)
prototype_event_in_less_than_x_hours.registration_close_time = (
datetime.now() + timedelta(hours=2)
current_time() + timedelta(hours=2)
)
prototype_event_in_less_than_x_hours.start = datetime.now() + timedelta(hours=3)
prototype_event_in_less_than_x_hours.end = datetime.now() + timedelta(hours=4)
prototype_event_in_less_than_x_hours.start = current_time() + timedelta(hours=3)
prototype_event_in_less_than_x_hours.end = current_time() + timedelta(hours=4)
prototype_event_in_less_than_x_hours.num_online_slots = 9

for user in [
Expand All @@ -321,7 +322,7 @@ def event_in_less_than_x_hours_with_reg(
status=RegistrationStatus.Active,
level=RegistrationLevels.Normal,
is_self=True,
registration_time=datetime.now() - timedelta(weeks=1),
registration_time=current_time() - timedelta(weeks=1),
)
)

Expand All @@ -336,13 +337,13 @@ def event_in_less_than_x_hours(prototype_event_in_less_than_x_hours):
"""Fixture for an event starting in less than 48 hours (parameterized)."""

prototype_event_in_less_than_x_hours.registration_open_time = (
datetime.now() - timedelta(hours=10)
current_time() - timedelta(hours=10)
)
prototype_event_in_less_than_x_hours.registration_close_time = (
datetime.now() + timedelta(hours=2)
current_time() + timedelta(hours=2)
)
prototype_event_in_less_than_x_hours.start = datetime.now() + timedelta(hours=3)
prototype_event_in_less_than_x_hours.end = datetime.now() + timedelta(hours=4)
prototype_event_in_less_than_x_hours.start = current_time() + timedelta(hours=3)
prototype_event_in_less_than_x_hours.end = current_time() + timedelta(hours=4)

prototype_event_in_less_than_x_hours.num_online_slots = 9

Expand All @@ -364,13 +365,13 @@ def event_with_no_activity_type_in_less_than_x_hours_with_reg(
"""

prototype_event_in_less_than_x_hours.registration_open_time = (
datetime.now() - timedelta(hours=10)
current_time() - timedelta(hours=10)
)
prototype_event_in_less_than_x_hours.registration_close_time = (
datetime.now() + timedelta(hours=2)
current_time() + timedelta(hours=2)
)
prototype_event_in_less_than_x_hours.start = datetime.now() + timedelta(hours=3)
prototype_event_in_less_than_x_hours.end = datetime.now() + timedelta(hours=4)
prototype_event_in_less_than_x_hours.start = current_time() + timedelta(hours=3)
prototype_event_in_less_than_x_hours.end = current_time() + timedelta(hours=4)
prototype_event_in_less_than_x_hours.num_online_slots = 9

prototype_event_in_less_than_x_hours.num_online_slots = 9
Expand Down
11 changes: 5 additions & 6 deletions tests/fixtures/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

These are not pytest fixtures."""

from datetime import datetime

from collectives.models.payment import ItemPrice, PaymentItem
from collectives.utils.time import current_time


def regular_price():
Expand All @@ -13,7 +12,7 @@ def regular_price():
price.amount = 12.5
price.title = "Normal"
price.enabled = True
price.update_time = datetime.now()
price.update_time = current_time()
return price


Expand All @@ -27,7 +26,7 @@ def leader_price(item):
# pylint: disable=protected-access
price._deprecated_leader_only = True # Test migration as well
# pylint: enable=protected-access
price.update_time = datetime.now()
price.update_time = current_time()
return price


Expand All @@ -37,7 +36,7 @@ def free_price():
price.amount = 0
price.title = "Gratuit"
price.enabled = True
price.update_time = datetime.now()
price.update_time = current_time()
return price


Expand All @@ -47,7 +46,7 @@ def disabled_price():
price.amount = 0.01
price.title = "Test"
price.enabled = False
price.update_time = datetime.now()
price.update_time = current_time()
return price


Expand Down
7 changes: 4 additions & 3 deletions tests/fixtures/user.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Module to create fixture users."""

from functools import wraps
from datetime import date, datetime, timedelta
from datetime import date, timedelta

import pytest

from collectives.models import User, Gender, db, Role, RoleIds, ActivityType
from collectives.models import Badge, BadgeIds, UserType
from collectives.utils.time import current_time

# pylint: disable=unused-argument,redefined-outer-name, unused-import

Expand Down Expand Up @@ -74,7 +75,7 @@ def user(app):
user.phone = f"0601020{identifier:03d}"
user.emergency_contact_name = f"Emergency {identifier}"
user.emergency_contact_phone = f"0699999{identifier:03}"
user.legal_text_signature_date = datetime.now()
user.legal_text_signature_date = current_time()
user.legal_text_signed_version = 1
user.gender = Gender(identifier % 2 + 1)

Expand Down Expand Up @@ -274,7 +275,7 @@ def promote_user(
activity_type = ActivityType.query.filter_by(name=activity_name).first()
role.activity_id = activity_type.id
if confidentiality_agreement_signature:
user.confidentiality_agreement_signature_date = datetime.now()
user.confidentiality_agreement_signature_date = current_time()
db.session.add(role)


Expand Down
Loading