Skip to content

Commit

Permalink
tests: use time-travel instead of freezegun
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed May 2, 2024
1 parent a3a8281 commit 4ce9f77
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 79 deletions.
163 changes: 111 additions & 52 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@ sphinx-issues = "^4.0.0"
coverage = {version = "*", extras=["toml"]}
faker = "*"
flask-webtest = "*"
freezegun = "*"
pre-commit = "*"
pyquery = "*"
pytest = "^8.0.0"
pytest-coverage = "*"
pytest-httpserver = "*"
pytest-lazy-fixtures = "^1.0.7"
pytest-smtpd = "^0.1.0"
pytest-xdist = "^3.3.1"
slapd = "*"
time-machine = "^2.14.1"
toml = "^0.10.0"
pytest-lazy-fixtures = "^1.0.7"

[tool.poetry.group.demo]
optional = true
Expand Down
6 changes: 3 additions & 3 deletions tests/backends/test_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime

import freezegun
import pytest
import time_machine

from canaille.app import models

Expand Down Expand Up @@ -195,7 +195,7 @@ def test_model_creation_edition_datetime(testclient, backend):
if "ldap" in backend.__class__.__module__:
pytest.skip()

with freezegun.freeze_time("2020-01-01 02:00:00"):
with time_machine.travel("2020-01-01 02:00:00+00:00", tick=False):
user = models.User(
user_name="foo",
family_name="foo",
Expand All @@ -209,7 +209,7 @@ def test_model_creation_edition_datetime(testclient, backend):
2020, 1, 1, 2, tzinfo=datetime.timezone.utc
)

with freezegun.freeze_time("2021-01-01 02:00:00"):
with time_machine.travel("2021-01-01 02:00:00+00:00", tick=False):
user.family_name = "bar"
user.save()
assert user.created == datetime.datetime(
Expand Down
32 changes: 16 additions & 16 deletions tests/core/test_email_confirmation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
from unittest import mock

import freezegun
import time_machine
from flask import url_for

from canaille.core.endpoints.account import EmailConfirmationPayload
Expand Down Expand Up @@ -126,19 +126,19 @@ def test_confirmation_unset_smtp_enabled_email_user_validation(
then users emails should be validated by sending a confirmation email."""
testclient.app.config["CANAILLE"]["EMAIL_CONFIRMATION"] = None

with freezegun.freeze_time("2020-01-01 01:00:00"):
with time_machine.travel("2020-01-01 01:00:00+00:00", tick=False):
res = testclient.get("/login")
res.form["login"] = "user"
res = res.form.submit().follow()
res.form["password"] = "correct horse battery staple"
res = res.form.submit()

with freezegun.freeze_time("2020-01-01 02:00:00"):
with time_machine.travel("2020-01-01 02:00:00+00:00", tick=False):
res = testclient.get("/profile/user")

assert "readonly" in res.forms["emailconfirmationform"]["old_emails-0"].attrs

with freezegun.freeze_time("2020-01-01 02:00:00"):
with time_machine.travel("2020-01-01 02:00:00+00:00", tick=False):
res.forms["emailconfirmationform"]["new_email"] = "[email protected]"
res = res.forms["emailconfirmationform"].submit(
name="action", value="add_email"
Expand Down Expand Up @@ -168,7 +168,7 @@ def test_confirmation_unset_smtp_enabled_email_user_validation(
email_content = str(smtpd.messages[0].get_payload()[0]).replace("=\n", "")
assert email_confirmation_url in email_content

with freezegun.freeze_time("2020-01-01 03:00:00"):
with time_machine.travel("2020-01-01 03:00:00+00:00", tick=False):
res = testclient.get(email_confirmation_url)

assert ("success", "Your email address have been confirmed.") in res.flashes
Expand All @@ -187,19 +187,19 @@ def test_confirmation_invalid_link(testclient, backend, user):

def test_confirmation_mail_form_failed(testclient, backend, user):
"""Tests when an error happens during the mail sending."""
with freezegun.freeze_time("2020-01-01 01:00:00"):
with time_machine.travel("2020-01-01 01:00:00+00:00", tick=False):
res = testclient.get("/login")
res.form["login"] = "user"
res = res.form.submit().follow()
res.form["password"] = "correct horse battery staple"
res = res.form.submit()

with freezegun.freeze_time("2020-01-01 02:00:00"):
with time_machine.travel("2020-01-01 02:00:00+00:00", tick=False):
res = testclient.get("/profile/user")

assert "readonly" in res.forms["emailconfirmationform"]["old_emails-0"].attrs

with freezegun.freeze_time("2020-01-01 02:00:00"):
with time_machine.travel("2020-01-01 02:00:00+00:00", tick=False):
res.forms["emailconfirmationform"]["new_email"] = "invalid"
res = res.forms["emailconfirmationform"].submit(
name="action", value="add_email"
Expand All @@ -214,19 +214,19 @@ def test_confirmation_mail_form_failed(testclient, backend, user):
def test_confirmation_mail_send_failed(SMTP, smtpd, testclient, backend, user):
"""Tests when an error happens during the mail sending."""
SMTP.side_effect = mock.Mock(side_effect=OSError("unit test mail error"))
with freezegun.freeze_time("2020-01-01 01:00:00"):
with time_machine.travel("2020-01-01 01:00:00+00:00", tick=False):
res = testclient.get("/login")
res.form["login"] = "user"
res = res.form.submit().follow()
res.form["password"] = "correct horse battery staple"
res = res.form.submit()

with freezegun.freeze_time("2020-01-01 02:00:00"):
with time_machine.travel("2020-01-01 02:00:00+00:00", tick=False):
res = testclient.get("/profile/user")

assert "readonly" in res.forms["emailconfirmationform"]["old_emails-0"].attrs

with freezegun.freeze_time("2020-01-01 02:00:00"):
with time_machine.travel("2020-01-01 02:00:00+00:00", tick=False):
res.forms["emailconfirmationform"]["new_email"] = "[email protected]"
res = res.forms["emailconfirmationform"].submit(
name="action", value="add_email", expect_errors=True
Expand All @@ -251,7 +251,7 @@ def test_confirmation_expired_link(testclient, backend, user):
_external=True,
)

with freezegun.freeze_time("2021-01-01 01:00:00"):
with time_machine.travel("2021-01-01 01:00:00+00:00", tick=False):
res = testclient.get(email_confirmation_url)

assert (
Expand All @@ -276,7 +276,7 @@ def test_confirmation_invalid_hash_link(testclient, backend, user):
_external=True,
)

with freezegun.freeze_time("2020-01-01 01:00:00"):
with time_machine.travel("2020-01-01 01:00:00+00:00", tick=False):
res = testclient.get(email_confirmation_url)

assert (
Expand Down Expand Up @@ -305,7 +305,7 @@ def test_confirmation_invalid_user_link(testclient, backend, user):
_external=True,
)

with freezegun.freeze_time("2020-01-01 01:00:00"):
with time_machine.travel("2020-01-01 01:00:00+00:00", tick=False):
res = testclient.get(email_confirmation_url)

assert (
Expand All @@ -330,7 +330,7 @@ def test_confirmation_email_already_confirmed_link(testclient, backend, user, ad
_external=True,
)

with freezegun.freeze_time("2020-01-01 01:00:00"):
with time_machine.travel("2020-01-01 01:00:00+00:00", tick=False):
res = testclient.get(email_confirmation_url)

assert (
Expand Down Expand Up @@ -360,7 +360,7 @@ def test_confirmation_email_already_used_link(testclient, backend, user, admin):
_external=True,
)

with freezegun.freeze_time("2020-01-01 01:00:00"):
with time_machine.travel("2020-01-01 01:00:00+00:00", tick=False):
res = testclient.get(email_confirmation_url)

assert (
Expand Down
6 changes: 3 additions & 3 deletions tests/core/test_registration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import mock

import freezegun
import time_machine
from flask import url_for

from canaille.app import models
Expand Down Expand Up @@ -31,7 +31,7 @@ def test_registration_with_email_validation(testclient, backend, smtpd, foo_grou
"""Tests a nominal registration with email validation."""
testclient.app.config["CANAILLE"]["ENABLE_REGISTRATION"] = True

with freezegun.freeze_time("2020-01-01 02:00:00"):
with time_machine.travel("2020-01-01 02:00:00+00:00", tick=False):
res = testclient.get(url_for("core.account.join"))
res.form["email"] = "[email protected]"
res = res.form.submit()
Expand Down Expand Up @@ -61,7 +61,7 @@ def test_registration_with_email_validation(testclient, backend, smtpd, foo_grou
assert registration_url in text_mail

assert not models.User.query(user_name="newuser")
with freezegun.freeze_time("2020-01-01 02:01:00"):
with time_machine.travel("2020-01-01 02:01:00+00:00", tick=False):
res = testclient.get(registration_url, status=200)
res.form["user_name"] = "newuser"
res.form["password1"] = "password"
Expand Down
6 changes: 3 additions & 3 deletions tests/oidc/test_authorization_code_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from urllib.parse import parse_qs
from urllib.parse import urlsplit

import freezegun
import time_machine
from authlib.jose import jwt
from authlib.oauth2.rfc7636 import create_s256_code_challenge
from flask import g
Expand Down Expand Up @@ -626,7 +626,7 @@ def test_request_scope_too_large(testclient, logged_user, keypair, client):


def test_code_expired(testclient, user, client):
with freezegun.freeze_time("2020-01-01 01:00:00"):
with time_machine.travel("2020-01-01 01:00:00+00:00", tick=False):
res = testclient.get(
"/oauth/authorize",
params=dict(
Expand All @@ -647,7 +647,7 @@ def test_code_expired(testclient, user, client):
params = parse_qs(urlsplit(res.location).query)
code = params["code"][0]

with freezegun.freeze_time("2021-01-01 01:00:00"):
with time_machine.travel("2021-01-01 01:00:00+00:00", tick=False):
res = testclient.post(
"/oauth/token",
params=dict(
Expand Down

0 comments on commit 4ce9f77

Please sign in to comment.