Repository: hydroshare/hydroshare

Issue:
rewrite author order test
**Description of the bug**
This test fails occasionally. Rewrite it removing 2 assertions:
[https://github.com/hydroshare/hydroshare/blob/4372-communities-and-groups-2.0/hs_c[…]ore/tests/api/native/test_reorder_authors_management_command.py](https://github.com/hydroshare/hydroshare/blob/4372-communities-and-groups-2.0/hs_core/tests/api/native/test_reorder_authors_management_command.py#L180)

[https://github.com/hydroshare/hydroshare/blob/4372-communities-and-groups-2.0/hs_c[…]ore/tests/api/native/test_reorder_authors_management_command.py](https://github.com/hydroshare/hydroshare/blob/4372-communities-and-groups-2.0/hs_core/tests/api/native/test_reorder_authors_management_command.py#L152)

Also: rewrite this management command so that it takes a res ID as a param:
https://github.com/hydroshare/hydroshare/blob/4372-communities-and-groups-2.0/hs_core/management/commands/reorder_authors.py#L24

Steps to reproduce the bug:
http://ci.hydroshare.org:8080/job/hydroshare-pull-requests/5750/testReport/junit/hs_core.tests.api.native.test_reorder_authors_management_command/TestReorderAuthorsCommand/test_command_fixes_triplicate_authors/

**Expected behavior**
Test should not be dependent on django .get() order




Discussion:

Resolving patch:
diff --git a/hs_core/management/commands/reorder_authors.py b/hs_core/management/commands/reorder_authors.py
--- a/hs_core/management/commands/reorder_authors.py
+++ b/hs_core/management/commands/reorder_authors.py
@@ -6,7 +6,7 @@
 Related to https://github.com/hydroshare/hydroshare/issues/4695
 """
 
-from django.core.management.base import BaseCommand
+from django.core.management.base import BaseCommand, CommandError
 from hs_core.models import BaseResource
 from hs_core.hydroshare.utils import set_dirty_bag_flag
 
@@ -14,19 +14,30 @@
 class Command(BaseCommand):
     help = "Fix duplicate author 'order' values"
 
+    def add_arguments(self, parser):
+        # ID of a resource for which users should be re-ordered
+        parser.add_argument('--resource_id', type=str, help=('Required. The id (short_id) of'
+                                                             ' the resource'))
+
     def handle(self, *args, **options):
-        resources = BaseResource.objects.filter(raccess__published=False).only('object_id', 'short_id')
-        for res in resources:
-            if res.metadata is not None:
-                creators = res.metadata.creators.all()
-                is_dirty = False
-                for index, creator in enumerate(creators, start=1):
-                    if creator.order != index:
-                        print("*" * 100)
-                        print(f"Author out of order.\nR:{res.short_id}"
-                                f"\nExpected: {index}, got: {creator.order}")
-                        creator.order = index
-                        creator.save()
-                        is_dirty = True
-                if is_dirty:
-                    set_dirty_bag_flag(res)
+        if not options['resource_id']:
+            raise CommandError('resource_id argument is required')
+        res_id = options['resource_id']
+        res = BaseResource.objects.filter(short_id=res_id).first()
+        if not res:
+            raise CommandError('No resource found for the provided resource_id')
+        if res.raccess.published:
+            raise CommandError(f"Resource id: {res_id} is already published--can't update author order.")
+        if res.metadata is not None:
+            creators = res.metadata.creators.all()
+            is_dirty = False
+            for index, creator in enumerate(creators, start=1):
+                if creator.order != index:
+                    print("*" * 100)
+                    print(f"Author out of order.\nR:{res.short_id}"
+                            f"\nExpected: {index}, got: {creator.order}")
+                    creator.order = index
+                    creator.save()
+                    is_dirty = True
+            if is_dirty:
+                set_dirty_bag_flag(res)


Test patch:
diff --git a/hs_core/tests/api/native/test_reorder_authors_management_command.py b/hs_core/tests/api/native/test_reorder_authors_management_command.py
--- a/hs_core/tests/api/native/test_reorder_authors_management_command.py
+++ b/hs_core/tests/api/native/test_reorder_authors_management_command.py
@@ -2,6 +2,7 @@
 
 from django.contrib.auth.models import Group, User
 from django.core.management import call_command
+from django.core.management.base import CommandError
 
 from hs_core.models import GenericResource, Creator
 from hs_core import hydroshare
@@ -45,7 +46,8 @@ def setUp(self):
             self.res.short_id, "creator", name="Mark Miller"
         )
 
-        self.update_command = "reorder_authors"
+    def run_management_command(self):
+        call_command("reorder_authors", f"--resource_id={self.res.short_id}")
 
     def tearDown(self):
         super(TestReorderAuthorsCommand, self).tearDown()
@@ -65,7 +67,7 @@ def test_command_doesnt_alter_if_aready_correct(self):
             self.assertEqual(index, creator.order)
 
         # run  update command to fix author order
-        call_command(self.update_command)
+        self.run_management_command()
 
         self.assertEqual(self.res.get_citation(), citation_original)
         for index, creator in enumerate(self.res.metadata.creators.all(), start=1):
@@ -85,7 +87,7 @@ def test_command_alters_if_incorrect(self):
         self.assertEqual(john.order, lisa.order)
 
         # run  update command to fix author order
-        call_command(self.update_command)
+        self.run_management_command()
 
         for index, creator in enumerate(self.res.metadata.creators.all(), start=1):
             self.assertEqual(index, creator.order)
@@ -107,7 +109,7 @@ def test_command_alters_missing_first_author(self):
         self.assertIsNone(self.res.metadata.creators.filter(order=1).first())
 
         # run  update command to fix author order
-        call_command(self.update_command)
+        self.run_management_command()
 
         for index, creator in enumerate(self.res.metadata.creators.all(), start=1):
             self.assertEqual(index, creator.order)
@@ -141,16 +143,12 @@ def test_command_fixes_multiple_authors(self):
         lisa.save()
         last_author.save()
 
-        cit_original = self.res.get_citation()
-
         # run  update command to fix author order
-        call_command(self.update_command)
+        self.run_management_command()
 
         for index, creator in enumerate(self.res.metadata.creators.all(), start=1):
             self.assertEqual(index, creator.order)
 
-        self.assertEqual(self.res.get_citation(), cit_original)
-
     def test_command_fixes_triplicate_authors(self):
         """
         Testing author order is updated if there were 3 authors with the same order
@@ -169,16 +167,12 @@ def test_command_fixes_triplicate_authors(self):
         lisa.save()
         last_author.save()
 
-        cit_original = self.res.get_citation()
-
         # run  update command to fix author order
-        call_command(self.update_command)
+        self.run_management_command()
 
         for index, creator in enumerate(self.res.metadata.creators.all(), start=1):
             self.assertEqual(index, creator.order)
 
-        self.assertEqual(self.res.get_citation(), cit_original)
-
     def test_command_maintains_citations(self):
         """
         Testing citation is maintained after author_order management command
@@ -195,7 +189,7 @@ def test_command_maintains_citations(self):
         cit_original = self.res.get_citation()
 
         # run  update command to fix author order
-        call_command(self.update_command)
+        self.run_management_command()
 
         self.assertEqual(self.res.get_citation(), cit_original)
 
@@ -224,7 +218,8 @@ def test_author_order_command_doesnt_touch_published(self):
         cit_pub = self.res.get_citation()
 
         # run  update command to fix author order
-        call_command(self.update_command)
+        with self.assertRaises(CommandError):
+            self.run_management_command()
 
         self.assertEqual(self.res.get_citation(), cit_pub)