Skip to content
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

fix: do nothing if rfc has no draft; add test #6960

Merged
merged 3 commits into from
Jan 23, 2024
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
8 changes: 5 additions & 3 deletions ietf/doc/management/commands/reset_rfc_authors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def handle(self, *args, **options):
raise CommandError(
f"rfc{options['rfcnum']} does not exist in the Datatracker."
)

draft = rfc.came_from_draft()
if draft is None:
raise CommandError(f"{rfc.name} did not come from a draft. Can't reset.")

orig_authors = rfc.documentauthor_set.all()
if orig_authors.exists():
# Potentially dangerous, so refuse unless "--force" is specified
Expand All @@ -43,9 +48,6 @@ def handle(self, *args, **options):
f"Removed author(s): {', '.join(removed_auth_names)}"
)
)
draft = rfc.came_from_draft()
if draft is None:
raise CommandError(f"{rfc.name} did not come from a draft. Can't reset.")

for author in draft.documentauthor_set.all():
# Copy the author but point at the new doc.
Expand Down
72 changes: 72 additions & 0 deletions ietf/doc/management/commands/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright The IETF Trust 2024, All Rights Reserved
# -*- coding: utf-8 -*-

from io import StringIO

from django.core.management import call_command, CommandError

from ietf.doc.factories import DocumentAuthorFactory, WgDraftFactory, WgRfcFactory
from ietf.doc.models import Document, DocumentAuthor
from ietf.utils.test_utils import TestCase


class CommandTests(TestCase):
@staticmethod
def _call_command(command_name, *args, **options):
"""Call command, capturing (and suppressing) output"""
out = StringIO()
err = StringIO()
options["stdout"] = out
options["stderr"] = err
call_command(command_name, *args, **options)
return out.getvalue(), err.getvalue()

def test_reset_rfc_authors(self):
command_name = "reset_rfc_authors"

draft = WgDraftFactory()
DocumentAuthorFactory.create_batch(3, document=draft)
rfc = WgRfcFactory() # rfc does not yet have a draft
DocumentAuthorFactory.create_batch(3, document=rfc)
bad_rfc_num = (
1
+ Document.objects.filter(rfc_number__isnull=False)
.order_by("-rfc_number")
.first()
.rfc_number
)
docauthor_fields = [
field.name
for field in DocumentAuthor._meta.get_fields()
if field.name not in ["document", "id"]
]

with self.assertRaises(CommandError, msg="Cannot reset a bad RFC number"):
self._call_command(command_name, bad_rfc_num)

with self.assertRaises(CommandError, msg="Cannot reset an RFC with no draft"):
self._call_command(command_name, rfc.rfc_number)

with self.assertRaises(CommandError, msg="Cannot force-reset an RFC with no draft"):
self._call_command(command_name, rfc.rfc_number, "--force")

# Link the draft to the rfc
rfc.targets_related.create(relationship_id="became_rfc", source=draft)

with self.assertRaises(CommandError, msg="Cannot reset an RFC with authors"):
self._call_command(command_name, rfc.rfc_number)

# Calling with force should work
self._call_command(command_name, rfc.rfc_number, "--force")
self.assertCountEqual(
draft.documentauthor_set.values(*docauthor_fields),
rfc.documentauthor_set.values(*docauthor_fields),
)

# Calling on an RFC with no authors should also work
rfc.documentauthor_set.all().delete()
self._call_command(command_name, rfc.rfc_number)
self.assertCountEqual(
draft.documentauthor_set.values(*docauthor_fields),
rfc.documentauthor_set.values(*docauthor_fields),
)
Loading