Repository: Nitrate/Nitrate

Issue:
Fail to set default tester to selected cases in a plan page
```
AttributeError at /ajax/update/cases-default-tester/
'ObjectDoesNotExist' object has no attribute 'message'

Request Method: POST
Request URL: http://127.0.0.1:8000/ajax/update/cases-default-tester/
```


Discussion:

Resolving patch:
diff --git a/src/tcms/core/ajax.py b/src/tcms/core/ajax.py
--- a/src/tcms/core/ajax.py
+++ b/src/tcms/core/ajax.py
@@ -607,7 +607,7 @@ def update(self):
                 resp = action()
                 self._sendmail()
             except ObjectDoesNotExist as err:
-                return say_no(err.message)
+                return say_no(str(err))
             except Exception:
                 # TODO: besides this message to users, what happening should be
                 # recorded in the system log.
@@ -649,10 +649,14 @@ def _update_priority(self):
         self.get_update_targets().update(**{str(self.target_field): self.new_value})
 
     def _update_default_tester(self):
-        user_pk = User.objects.filter(username=self.new_value).values_list('pk', flat=True)
+        user_pk = User.objects.filter(
+            username=self.new_value).values_list('pk', flat=True)
         if not user_pk:
-            raise ObjectDoesNotExist('Your input is not found.')
-        self.get_update_targets().update(**{str(self.target_field): user_pk[0]})
+            raise ObjectDoesNotExist(
+                f'{self.new_value} cannot be set as a default tester, '
+                f'since this user does not exist.')
+        self.get_update_targets().update(
+            **{str(self.target_field): user_pk[0]})
 
     def _update_case_status(self):
         exists = TestCaseStatus.objects.filter(pk=self.new_value).exists()


Test patch:
diff --git a/src/tests/core/test_ajax.py b/src/tests/core/test_ajax.py
--- a/src/tests/core/test_ajax.py
+++ b/src/tests/core/test_ajax.py
@@ -2,7 +2,11 @@
 
 import unittest
 
+from django import test
+from django.urls import reverse
 from tcms.core.ajax import strip_parameters
+from tests import factories as f
+from tests import AuthMixin, HelperAssertions, user_should_have_perm
 
 
 class TestStripParameters(unittest.TestCase):
@@ -28,3 +32,58 @@ def test_remove_parameters_in_dict(self):
     def test_remove_parameters_not_in_dict(self):
         simplified_dict = strip_parameters(self.request_dict, ['non-existing-parameter'])
         self.assertEqual(self.request_dict, simplified_dict)
+
+
+class TestUpdateCasesDefaultTester(AuthMixin, HelperAssertions, test.TestCase):
+    """Test set default tester to selected cases"""
+
+    @classmethod
+    def setUpTestData(cls):
+        super().setUpTestData()
+
+        cls.plan = f.TestPlanFactory(owner=cls.tester, author=cls.tester)
+        cls.case_1 = f.TestCaseFactory(
+            author=cls.tester, reviewer=cls.tester, default_tester=None,
+            plan=[cls.plan])
+        cls.case_2 = f.TestCaseFactory(
+            author=cls.tester, reviewer=cls.tester, default_tester=None,
+            plan=[cls.plan])
+
+        user_should_have_perm(cls.tester, 'testcases.change_testcase')
+
+        cls.user_1 = f.UserFactory(username='user1')
+        cls.url = reverse('ajax-update-cases-default-tester')
+
+    def setUp(self):
+        self.login_tester()
+
+    def test_set_default_tester(self):
+        resp = self.client.post(self.url, data={
+            'from_plan': self.plan.pk,
+            'case': [self.case_1.pk, self.case_2.pk],
+            'target_field': 'default_tester',
+            'new_value': self.user_1.username,
+        })
+
+        self.assertJsonResponse(resp, {'rc': 0, 'response': 'ok'})
+
+        for case in [self.case_1, self.case_2]:
+            case.refresh_from_db()
+            self.assertEqual(self.user_1, case.default_tester)
+
+    def test_given_username_does_not_exist(self):
+        resp = self.client.post(self.url, data={
+            'from_plan': self.plan.pk,
+            'case': [self.case_1.pk, self.case_2.pk],
+            'target_field': 'default_tester',
+            'new_value': 'unknown',
+        })
+
+        self.assertJsonResponse(resp, {
+            'rc': 1,
+            'response': 'unknown cannot be set as a default tester, '
+                        'since this user does not exist.'})
+
+        for case in [self.case_1, self.case_2]:
+            case.refresh_from_db()
+            self.assertIsNone(case.default_tester)