Skip to content

Commit

Permalink
refactor: all domains used in the unit test suite are now .test
Browse files Browse the repository at this point in the history
this ensures they will never be valid, and will never generate real
world requests
  • Loading branch information
azmeuk committed Nov 20, 2024
1 parent 0af3682 commit 77667c6
Show file tree
Hide file tree
Showing 43 changed files with 446 additions and 441 deletions.
1 change: 1 addition & 0 deletions canaille/app/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def email_validator(form, field):
except ImportError:
pass

email_validator.TEST_ENVIRONMENT = current_app.config.get("TESTING", False)
wtforms.validators.Email()(form, field)


Expand Down
8 changes: 4 additions & 4 deletions tests/app/commands/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def test_create(testclient, backend, foo_group):
"--formatted-name",
"Johnny",
"--emails",
"foo@example.org",
"foo@example.test",
"--emails",
"bar@example.org",
"bar@example.test",
"--given-name",
"John",
"--family-name",
Expand All @@ -40,8 +40,8 @@ def test_create(testclient, backend, foo_group):
"created": mock.ANY,
"last_modified": mock.ANY,
"emails": [
"foo@example.org",
"bar@example.org",
"foo@example.test",
"bar@example.test",
],
"family_name": "Doe",
"given_name": "John",
Expand Down
4 changes: 2 additions & 2 deletions tests/app/commands/test_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_delete_by_id(testclient, backend):
user = models.User(
formatted_name="Foo bar",
family_name="Bar",
emails=["foobar@example.org"],
emails=["foobar@example.test"],
user_name="foobar",
)
backend.save(user)
Expand All @@ -24,7 +24,7 @@ def test_delete_by_identifier(testclient, backend):
user = models.User(
formatted_name="Foo bar",
family_name="Bar",
emails=["foobar@example.org"],
emails=["foobar@example.test"],
user_name="foobar",
)
backend.save(user)
Expand Down
12 changes: 6 additions & 6 deletions tests/app/commands/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_get(testclient, backend, user):
"created": mock.ANY,
"display_name": "Johnny",
"emails": [
"john@doe.com",
"john@doe.test",
],
"family_name": "Doe",
"formatted_address": "1235, somewhere",
Expand All @@ -39,7 +39,7 @@ def test_get(testclient, backend, user):
"555-000-000",
],
"preferred_language": "en",
"profile_url": "https://john.example",
"profile_url": "https://john.test",
"user_name": "user",
},
]
Expand All @@ -56,7 +56,7 @@ def test_get_model_filter(testclient, backend, user, admin, foo_group):
"created": mock.ANY,
"display_name": "Johnny",
"emails": [
"john@doe.com",
"john@doe.test",
],
"family_name": "Doe",
"formatted_address": "1235, somewhere",
Expand All @@ -69,7 +69,7 @@ def test_get_model_filter(testclient, backend, user, admin, foo_group):
"555-000-000",
],
"preferred_language": "en",
"profile_url": "https://john.example",
"profile_url": "https://john.test",
"user_name": "user",
"groups": [foo_group.id],
},
Expand All @@ -87,7 +87,7 @@ def test_get_datetime_filter(testclient, backend, user):
"created": mock.ANY,
"display_name": "Johnny",
"emails": [
"john@doe.com",
"john@doe.test",
],
"family_name": "Doe",
"formatted_address": "1235, somewhere",
Expand All @@ -100,7 +100,7 @@ def test_get_datetime_filter(testclient, backend, user):
"555-000-000",
],
"preferred_language": "en",
"profile_url": "https://john.example",
"profile_url": "https://john.test",
"user_name": "user",
},
]
30 changes: 15 additions & 15 deletions tests/app/commands/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_set_string_by_id(testclient, backend, user):
"created": mock.ANY,
"display_name": "Johnny",
"emails": [
"john@doe.com",
"john@doe.test",
],
"family_name": "Doe",
"formatted_address": "1235, somewhere",
Expand All @@ -27,7 +27,7 @@ def test_set_string_by_id(testclient, backend, user):
"555-000-000",
],
"preferred_language": "en",
"profile_url": "https://john.example",
"profile_url": "https://john.test",
"user_name": "user",
}
backend.reload(user)
Expand All @@ -44,7 +44,7 @@ def test_set_string_by_identifier(testclient, backend, user):
"created": mock.ANY,
"display_name": "Johnny",
"emails": [
"john@doe.com",
"john@doe.test",
],
"family_name": "Doe",
"formatted_address": "1235, somewhere",
Expand All @@ -57,7 +57,7 @@ def test_set_string_by_identifier(testclient, backend, user):
"555-000-000",
],
"preferred_language": "en",
"profile_url": "https://john.example",
"profile_url": "https://john.test",
"user_name": "user",
}
backend.reload(user)
Expand All @@ -75,18 +75,18 @@ def test_set_multiple(testclient, backend, user):
"user",
user.id,
"--emails",
"foo@example.org",
"foo@example.test",
"--emails",
"bar@example.org",
"bar@example.test",
],
)
assert res.exit_code == 0, res.stdout
assert json.loads(res.stdout) == {
"created": mock.ANY,
"display_name": "Johnny",
"emails": [
"foo@example.org",
"bar@example.org",
"foo@example.test",
"bar@example.test",
],
"family_name": "Doe",
"formatted_address": "1235, somewhere",
Expand All @@ -99,13 +99,13 @@ def test_set_multiple(testclient, backend, user):
"555-000-000",
],
"preferred_language": "en",
"profile_url": "https://john.example",
"profile_url": "https://john.test",
"user_name": "user",
}
backend.reload(user)
assert user.emails == [
"foo@example.org",
"bar@example.org",
"foo@example.test",
"bar@example.test",
]


Expand All @@ -129,7 +129,7 @@ def test_set_remove_simple_attribute(testclient, backend, user, admin):
"created": mock.ANY,
"display_name": "Johnny",
"emails": [
"john@doe.com",
"john@doe.test",
],
"family_name": "Doe",
"formatted_name": "John (johnny) Doe",
Expand All @@ -141,7 +141,7 @@ def test_set_remove_simple_attribute(testclient, backend, user, admin):
"555-000-000",
],
"preferred_language": "en",
"profile_url": "https://john.example",
"profile_url": "https://john.test",
"user_name": "user",
}
backend.reload(user)
Expand All @@ -161,7 +161,7 @@ def test_set_remove_multiple_attribute(testclient, backend, user, admin, foo_gro
"created": mock.ANY,
"display_name": "Johnny",
"emails": [
"john@doe.com",
"john@doe.test",
],
"family_name": "Doe",
"formatted_name": "John (johnny) Doe",
Expand All @@ -174,7 +174,7 @@ def test_set_remove_multiple_attribute(testclient, backend, user, admin, foo_gro
"555-000-000",
],
"preferred_language": "en",
"profile_url": "https://john.example",
"profile_url": "https://john.test",
"user_name": "user",
}
backend.reload(user)
Expand Down
4 changes: 2 additions & 2 deletions tests/app/test_apputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


def test_validate_uri():
assert validate_uri("https://canaille.tld")
assert validate_uri("scheme.with.dots://canaille.tld")
assert validate_uri("https://canaille.test")
assert validate_uri("scheme.with.dots://canaille.test")
assert validate_uri("scheme.with.dots://localhost")
assert validate_uri("scheme.with.dots://oauth")
assert not validate_uri("invalid")
16 changes: 8 additions & 8 deletions tests/app/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ def test_configuration_nestedsecrets_directory(tmp_path, backend, configuration)
def test_configuration_from_environment_vars():
"""Canaille should read configuration from environment vars."""
os.environ["SECRET_KEY"] = "very-very-secret"
os.environ["CANAILLE__SMTP__FROM_ADDR"] = "user@mydomain.tld"
os.environ["CANAILLE__SMTP__FROM_ADDR"] = "user@mydomain.test"
os.environ["CANAILLE_SQL__DATABASE_URI"] = "sqlite:///anything.db"

conf = settings_factory({"TIMEZONE": "UTC"})
assert conf.SECRET_KEY == "very-very-secret"
assert conf.CANAILLE.SMTP.FROM_ADDR == "user@mydomain.tld"
assert conf.CANAILLE.SMTP.FROM_ADDR == "user@mydomain.test"
assert conf.CANAILLE_SQL.DATABASE_URI == "sqlite:///anything.db"

app = create_app({"TIMEZONE": "UTC"})
assert app.config["SECRET_KEY"] == "very-very-secret"
assert app.config["CANAILLE"]["SMTP"]["FROM_ADDR"] == "user@mydomain.tld"
assert app.config["CANAILLE"]["SMTP"]["FROM_ADDR"] == "user@mydomain.test"
assert app.config["CANAILLE_SQL"]["DATABASE_URI"] == "sqlite:///anything.db"

del os.environ["SECRET_KEY"]
Expand All @@ -65,14 +65,14 @@ def test_disable_env_var_loading(tmp_path, configuration):
"""Canaille should not read configuration from environment vars when
env_prefix is False."""
del configuration["SERVER_NAME"]
os.environ["SERVER_NAME"] = "example.com"
os.environ["FOOBAR_SERVER_NAME"] = "foobar.example.com"
os.environ["SERVER_NAME"] = "example.test"
os.environ["FOOBAR_SERVER_NAME"] = "foobar.example.test"

app = create_app(configuration, env_prefix="")
assert app.config["SERVER_NAME"] == "example.com"
assert app.config["SERVER_NAME"] == "example.test"

app = create_app(configuration, env_prefix="FOOBAR_")
assert app.config["SERVER_NAME"] == "foobar.example.com"
assert app.config["SERVER_NAME"] == "foobar.example.test"

del os.environ["SERVER_NAME"]

Expand Down Expand Up @@ -215,7 +215,7 @@ def test_enable_password_compromission_check_with_and_without_admin_email(
validate(config_dict, validate_remote=False)

configuration["CANAILLE"]["ENABLE_PASSWORD_COMPROMISSION_CHECK"] = True
configuration["CANAILLE"]["ADMIN_EMAIL"] = "admin_default_mail@mymail.com"
configuration["CANAILLE"]["ADMIN_EMAIL"] = "admin_default_mail@mydomain.test"
config_obj = settings_factory(configuration)
config_dict = config_obj.model_dump()
validate(config_dict, validate_remote=False)
Expand Down
14 changes: 7 additions & 7 deletions tests/app/test_flaskutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

def test_set_parameter_in_url_query():
assert (
set_parameter_in_url_query("https://auth.mydomain.tld", foo="bar")
== "https://auth.mydomain.tld?foo=bar"
set_parameter_in_url_query("https://auth.mydomain.test", foo="bar")
== "https://auth.mydomain.test?foo=bar"
)

assert (
set_parameter_in_url_query("https://auth.mydomain.tld?foo=baz", foo="bar")
== "https://auth.mydomain.tld?foo=bar"
set_parameter_in_url_query("https://auth.mydomain.test?foo=baz", foo="bar")
== "https://auth.mydomain.test?foo=bar"
)

assert (
set_parameter_in_url_query("https://auth.mydomain.tld?foo=baz", hello="world")
== "https://auth.mydomain.tld?foo=baz&hello=world"
set_parameter_in_url_query("https://auth.mydomain.test?foo=baz", hello="world")
== "https://auth.mydomain.test?foo=baz&hello=world"
)


Expand All @@ -30,7 +30,7 @@ def test_environment_configuration(configuration, tmp_path):

os.environ["CONFIG"] = config_path
app = create_app()
assert app.config["CANAILLE"]["SMTP"]["FROM_ADDR"] == "admin@mydomain.tld"
assert app.config["CANAILLE"]["SMTP"]["FROM_ADDR"] == "admin@mydomain.test"

del os.environ["CONFIG"]
os.remove(config_path)
2 changes: 1 addition & 1 deletion tests/app/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def test_inline_validation_invalid_field(testclient, logged_admin, user):
"/profile",
{
"csrf_token": res.form["csrf_token"].value,
"email": "john@doe.com",
"email": "john@doe.test",
},
headers={
"HX-Request": "true",
Expand Down
10 changes: 6 additions & 4 deletions tests/app/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@


def test_file_log_config(configuration, backend, tmp_path, smtpd, admin):
configuration["TESTING"] = True
assert len(smtpd.messages) == 0
log_path = os.path.join(tmp_path, "canaille-by-file.log")

Expand All @@ -49,7 +50,7 @@ def test_file_log_config(configuration, backend, tmp_path, smtpd, admin):
sess["user_id"] = [admin.id]

res = testclient.get("/admin/mail")
res.form["email"] = "test@test.com"
res.form["email"] = "test@test.test"
res = res.form.submit()

assert len(smtpd.messages) == 1
Expand All @@ -58,10 +59,11 @@ def test_file_log_config(configuration, backend, tmp_path, smtpd, admin):
with open(log_path) as fd:
log_content = fd.read()

assert "Sending a mail to test@test.com: Test email from" in log_content
assert "Sending a mail to test@test.test: Test email from" in log_content


def test_dict_log_config(configuration, backend, tmp_path, smtpd, admin):
configuration["TESTING"] = True
assert len(smtpd.messages) == 0
log_path = os.path.join(tmp_path, "canaille-by-dict.log")
configuration["CANAILLE"]["LOGGING"] = {
Expand Down Expand Up @@ -91,7 +93,7 @@ def test_dict_log_config(configuration, backend, tmp_path, smtpd, admin):
sess["user_id"] = [admin.id]

res = testclient.get("/admin/mail")
res.form["email"] = "test@test.com"
res.form["email"] = "test@test.test"
res = res.form.submit()

assert len(smtpd.messages) == 1
Expand All @@ -100,7 +102,7 @@ def test_dict_log_config(configuration, backend, tmp_path, smtpd, admin):
with open(log_path) as fd:
log_content = fd.read()

assert "Sending a mail to test@test.com: Test email from" in log_content
assert "Sending a mail to test@test.test: Test email from" in log_content


def test_custom_root_logger(caplog):
Expand Down
Loading

0 comments on commit 77667c6

Please sign in to comment.