Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/add_forge_now/test_migration.py
# Copyright (C) 2022 The Software Heritage developers | # Copyright (C) 2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from datetime import datetime, timezone | from datetime import datetime, timezone | ||||
import pytest | import pytest | ||||
from django.core.exceptions import ValidationError | |||||
from swh.web.add_forge_now.apps import APP_LABEL | from swh.web.add_forge_now.apps import APP_LABEL | ||||
MIGRATION_0001 = "0001_initial" | MIGRATION_0001 = "0001_initial" | ||||
MIGRATION_0002 = "0002_authorized_null_comment" | MIGRATION_0002 = "0002_authorized_null_comment" | ||||
MIGRATION_0003 = "0003_request_submitter_forward_username" | MIGRATION_0003 = "0003_request_submitter_forward_username" | ||||
MIGRATION_0005 = "0005_prepare_inbound_email" | MIGRATION_0005 = "0005_prepare_inbound_email" | ||||
MIGRATION_0006 = "0006_request_add_new_fields" | MIGRATION_0006 = "0006_request_add_new_fields" | ||||
MIGRATION_0007 = "0007_turn_request_forge_url_into_url_field" | |||||
def now() -> datetime: | def now() -> datetime: | ||||
return datetime.now(tz=timezone.utc) | return datetime.now(tz=timezone.utc) | ||||
def test_add_forge_now_initial_migration(migrator): | def test_add_forge_now_initial_migration(migrator): | ||||
"""Basic migration test to check the model is fine""" | """Basic migration test to check the model is fine""" | ||||
▲ Show 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | def test_add_forge_now_add_new_fields_to_request(migrator): | ||||
assert hasattr(Request, "last_moderator") | assert hasattr(Request, "last_moderator") | ||||
assert hasattr(Request, "last_modified_date") | assert hasattr(Request, "last_modified_date") | ||||
for request in Request.objects.all(): | for request in Request.objects.all(): | ||||
history = RequestHistory.objects.filter(request=request) | history = RequestHistory.objects.filter(request=request) | ||||
history = history.order_by("id") | history = history.order_by("id") | ||||
assert request.last_modified_date == history.last().date | assert request.last_modified_date == history.last().date | ||||
assert request.last_moderator == history.last().actor | assert request.last_moderator == history.last().actor | ||||
def test_add_forge_now_url_validation(migrator): | |||||
state = migrator.apply_tested_migration((APP_LABEL, MIGRATION_0006)) | |||||
Request = state.apps.get_model(APP_LABEL, "Request") | |||||
from swh.web.add_forge_now.models import RequestStatus | |||||
request = Request( | |||||
status=RequestStatus.PENDING.name, | |||||
submitter_name="dudess", | |||||
submitter_email="dudess@orga.org", | |||||
forge_type="cgit", | |||||
forge_url="foo", | |||||
forge_contact_email="forge@example.org", | |||||
forge_contact_name="forge", | |||||
forge_contact_comment="bar", | |||||
last_modified_date=datetime.now(timezone.utc), | |||||
) | |||||
request.clean_fields() | |||||
state = migrator.apply_tested_migration((APP_LABEL, MIGRATION_0007)) | |||||
Request = state.apps.get_model(APP_LABEL, "Request") | |||||
request = Request( | |||||
status=RequestStatus.PENDING.name, | |||||
submitter_name="johndoe", | |||||
submitter_email="johndoe@example.org", | |||||
forge_type="cgit", | |||||
forge_url="foobar", | |||||
forge_contact_email="forge@example.org", | |||||
forge_contact_name="forge", | |||||
forge_contact_comment="bar", | |||||
last_modified_date=datetime.now(timezone.utc), | |||||
) | |||||
with pytest.raises(ValidationError, match="Enter a valid URL."): | |||||
request.clean_fields() |