diff --git a/TWLight/resources/tests.py b/TWLight/resources/tests.py index e035cc078b..54828e0cce 100644 --- a/TWLight/resources/tests.py +++ b/TWLight/resources/tests.py @@ -1317,6 +1317,83 @@ def test_partner_suggestion_view_post(self): self.assertContains(response, new_suggested_partner["suggested_company_name"]) self.assertContains(response, new_suggested_partner["company_url"]) + def _post_valid_suggestion(self, editor): + factory = RequestFactory() + request = factory.post( + reverse("suggest"), + { + "suggested_company_name": "Eligibility Test Company", + "description": "A description", + "company_url": "https://www.testing123.com", + "next": reverse("suggest"), + }, + ) + request.user = editor.user + return PartnerSuggestionView.as_view()(request) + + def test_partner_suggestion_view_post_eligible(self): + """ + Tests that users meeting the edit count, account age, and block + criteria can add a suggestion, even without enough recent edits. + """ + editor = EditorFactory() + editor.wp_valid = True + editor.wp_enough_recent_edits = False + editor.save() + + response = self._post_valid_suggestion(editor) + + self.assertEqual(response.status_code, 302) + self.assertTrue( + Suggestion.objects.filter( + suggested_company_name="Eligibility Test Company" + ).exists() + ) + + def test_partner_suggestion_view_post_ineligible(self): + """ + Tests that users not meeting the eligibility criteria cannot add a + suggestion and are shown an error. + """ + editor = EditorFactory() + editor.wp_valid = False + editor.save() + + with patch("TWLight.resources.views.messages.add_message") as mock_message: + response = self._post_valid_suggestion(editor) + + self.assertEqual(response.status_code, 200) + mock_message.assert_called_once() + self.assertEqual( + mock_message.call_args.args[2], + "You must be eligible to use The Wikipedia Library to submit new partner suggestions.", + ) + self.assertFalse( + Suggestion.objects.filter( + suggested_company_name="Eligibility Test Company" + ).exists() + ) + + def test_partner_suggestion_view_post_ineligible_staff(self): + """ + Tests that staff can add a suggestion even if they do not meet the + eligibility criteria. + """ + editor = EditorFactory() + editor.wp_valid = False + editor.save() + editor.user.is_staff = True + editor.user.save() + + response = self._post_valid_suggestion(editor) + + self.assertEqual(response.status_code, 302) + self.assertTrue( + Suggestion.objects.filter( + suggested_company_name="Eligibility Test Company" + ).exists() + ) + def test_partner_suggestion_upvote_view(self): """ Tests that upvoting a partner suggestion works properly diff --git a/TWLight/resources/views.py b/TWLight/resources/views.py index 5e9fa8aa21..13ba5cc2dc 100755 --- a/TWLight/resources/views.py +++ b/TWLight/resources/views.py @@ -528,6 +528,20 @@ def form_valid(self, form): # Adding an extra check to ensure the user is a wikipedia editor. try: assert self.request.user.editor + # Users must meet the edit count, account age, and block criteria + # to post suggestions. Recent edits are not required. Staff can + # always post. + user = self.request.user + if not (user.editor.wp_valid or user.is_staff): + messages.add_message( + self.request, + messages.ERROR, + # fmt: off + # Translators: This message is shown to users who attempt to submit a partner suggestion without meeting the eligibility criteria. + _("You must be eligible to use The Wikipedia Library to submit new partner suggestions."), + # fmt: on + ) + return self.form_invalid(form) suggestion = Suggestion() suggestion.suggested_company_name = bleach.clean( form.cleaned_data["suggested_company_name"],