Ejemplo n.º 1
0
    def setUp(self):
        # Reset the validator state and errors before every test.
        self.validator = VSubmitParent(None)
        c.errors = ErrorSet()

        c.user_is_loggedin = True
        c.user_is_admin = False
        c.user = Account(id=100)

        Account.enemy_ids = MagicMock(return_value=[])
Ejemplo n.º 2
0
    def setUp(self):
        super(TestVSubmitParent, self).setUp()
        # Reset the validator state and errors before every test.
        self.validator = VSubmitParent(None)
        c.errors = ErrorSet()

        c.user_is_loggedin = True
        c.user_is_admin = False
        c.user = Account(id=100)

        self.autopatch(Account, "enemy_ids", return_value=[])
        self.autopatch(Subreddit, "_byID", return_value=None)
Ejemplo n.º 3
0
    def setUp(self):
        # Reset the validator state and errors before every test.
        self.validator = VSubmitParent(None)
        c.errors = ErrorSet()

        c.user_is_loggedin = True
        c.user_is_admin = False
        c.user = Account(id=100)

        Account.enemy_ids = MagicMock(return_value=[])
Ejemplo n.º 4
0
    def setUp(self):
        super(TestVSubmitParent, self).setUp()
        # Reset the validator state and errors before every test.
        self.validator = VSubmitParent(None)
        c.errors = ErrorSet()

        c.user_is_loggedin = True
        c.user_is_admin = False
        c.user = Account(id=100)

        self.autopatch(Account, "enemy_ids", return_value=[])
        self.autopatch(Subreddit, "_byID", return_value=None)
Ejemplo n.º 5
0
class TestVSubmitParent(ValidatorTests):
    def setUp(self):
        super(TestVSubmitParent, self).setUp()
        # Reset the validator state and errors before every test.
        self.validator = VSubmitParent(None)
        c.errors = ErrorSet()

        c.user_is_loggedin = True
        c.user_is_admin = False
        c.user = Account(id=100)

        self.autopatch(Account, "enemy_ids", return_value=[])
        self.autopatch(Subreddit, "_byID", return_value=None)

    def _mock_message(self, id=1, author_id=1, **kwargs):
        kwargs['id'] = id
        kwargs['author_id'] = author_id

        message = Message(**kwargs)
        self.autopatch(VByName, "run", return_value=message)

        return message

    def _mock_link(self,
                   id=1,
                   author_id=1,
                   sr_id=1,
                   can_comment=True,
                   can_view_promo=True,
                   **kwargs):
        kwargs['id'] = id
        kwargs['author_id'] = author_id
        kwargs['sr_id'] = sr_id

        link = Link(**kwargs)
        self.autopatch(VByName, "run", return_value=link)

        sr = Subreddit(id=sr_id)
        self.autopatch(Subreddit, "_byID", return_value=sr)
        self.autopatch(Subreddit, "can_comment", return_value=can_comment)
        self.autopatch(Link, "can_view_promo", return_value=can_view_promo)

        return link

    def _mock_comment(self,
                      id=1,
                      author_id=1,
                      link_id=1,
                      sr_id=1,
                      can_comment=True,
                      can_view_promo=True,
                      is_moderator=False,
                      **kwargs):
        kwargs['id'] = id
        kwargs['author_id'] = author_id
        kwargs['link_id'] = link_id
        kwargs['sr_id'] = sr_id

        comment = Comment(**kwargs)
        self.autopatch(VByName, "run", return_value=comment)

        link = Link(id=link_id, sr_id=sr_id)
        self.autopatch(Link, "_byID", return_value=link)

        sr = Subreddit(id=sr_id)
        self.autopatch(Subreddit, "_byID", return_value=sr)
        self.autopatch(Subreddit, "can_comment", return_value=can_comment)
        self.autopatch(Link, "can_view_promo", return_value=can_view_promo)
        self.autopatch(Subreddit, "is_moderator", return_value=is_moderator)

        return comment

    def test_no_fullname(self):
        with self.assertRaises(HTTPForbidden):
            self.validator.run('', None)

        self.assertFalse(self.validator.has_errors)

    def test_not_found(self):
        with self.assertRaises(HTTPForbidden):
            with patch.object(VByName, "run", return_value=None):
                self.validator.run('fullname', None)

        self.assertFalse(self.validator.has_errors)

    def test_invalid_thing(self):
        with self.assertRaises(HTTPForbidden):
            sr = Subreddit(id=1)
            with patch.object(VByName, "run", return_value=sr):
                self.validator.run('fullname', None)

        self.assertFalse(self.validator.has_errors)

    def test_not_loggedin(self):
        with self.assertRaises(HTTPForbidden):
            c.user_is_loggedin = False

            self._mock_comment()
            self.validator.run('fullname', None)

        self.assertFalse(self.validator.has_errors)

    def test_blocked_user(self):
        message = self._mock_message()
        with patch.object(Account,
                          "enemy_ids",
                          return_value=[message.author_id]):
            result = self.validator.run('fullname', None)

            self.assertEqual(result, message)
            self.assertTrue(self.validator.has_errors)
            self.assertIn((errors.USER_BLOCKED, None), c.errors)

    def test_valid_message(self):
        message = self._mock_message()
        result = self.validator.run('fullname', None)

        self.assertEqual(result, message)
        self.assertFalse(self.validator.has_errors)

    def test_valid_link(self):
        link = self._mock_link()
        result = self.validator.run('fullname', None)

        self.assertEqual(result, link)
        self.assertFalse(self.validator.has_errors)

    def test_removed_link(self):
        link = self._mock_link(_spam=True)
        result = self.validator.run('fullname', None)

        self.assertEqual(result, link)
        self.assertFalse(self.validator.has_errors)

    def test_archived_link(self):
        link = self._mock_link(date=dt.now(g.tz).replace(year=2000))
        result = self.validator.run('fullname', None)

        self.assertEqual(result, link)
        self.assertTrue(self.validator.has_errors)
        self.assertIn((errors.TOO_OLD, None), c.errors)

    def test_locked_link(self):
        link = self._mock_link(locked=True)
        with patch.object(Subreddit, "can_distinguish", return_value=False):
            result = self.validator.run('fullname', None)

            self.assertEqual(result, link)
            self.assertTrue(self.validator.has_errors)
            self.assertIn((errors.THREAD_LOCKED, None), c.errors)

    def test_locked_link_mod_reply(self):
        link = self._mock_link(locked=True)
        with patch.object(Subreddit, "can_distinguish", return_value=True):
            result = self.validator.run('fullname', None)

            self.assertEqual(result, link)
            self.assertFalse(self.validator.has_errors)

    def test_invalid_link(self):
        with self.assertRaises(HTTPForbidden):
            self._mock_link(can_comment=False)
            self.validator.run('fullname', None)

        self.assertFalse(self.validator.has_errors)

    def test_invalid_promo(self):
        with self.assertRaises(HTTPForbidden):
            self._mock_link(can_view_promo=False)
            self.validator.run('fullname', None)

        self.assertFalse(self.validator.has_errors)

    def test_valid_comment(self):
        comment = self._mock_comment()
        result = self.validator.run('fullname', None)

        self.assertEqual(result, comment)
        self.assertFalse(self.validator.has_errors)

    def test_deleted_comment(self):
        comment = self._mock_comment(_deleted=True)
        result = self.validator.run('fullname', None)

        self.assertEqual(result, comment)
        self.assertTrue(self.validator.has_errors)
        self.assertIn((errors.DELETED_COMMENT, None), c.errors)

    def test_removed_comment(self):
        comment = self._mock_comment(_spam=True)
        result = self.validator.run('fullname', None)

        self.assertEqual(result, comment)
        self.assertTrue(self.validator.has_errors)
        self.assertIn((errors.DELETED_COMMENT, None), c.errors)

    def test_removed_comment_self_reply(self):
        comment = self._mock_comment(author_id=c.user._id, _spam=True)
        result = self.validator.run('fullname', None)

        self.assertEqual(result, comment)
        self.assertFalse(self.validator.has_errors)

    def test_removed_comment_mod_reply(self):
        comment = self._mock_comment(_spam=True, is_moderator=True)
        result = self.validator.run('fullname', None)

        self.assertEqual(result, comment)
        self.assertFalse(self.validator.has_errors)

    def test_invalid_comment(self):
        with self.assertRaises(HTTPForbidden):
            comment = self._mock_comment(can_comment=False)
            self.validator.run('fullname', None)

        self.assertFalse(self.validator.has_errors)
Ejemplo n.º 6
0
class TestVSubmitParent(ValidatorTests):
    def setUp(self):
        # Reset the validator state and errors before every test.
        self.validator = VSubmitParent(None)
        c.errors = ErrorSet()

        c.user_is_loggedin = True
        c.user_is_admin = False
        c.user = Account(id=100)

        Account.enemy_ids = MagicMock(return_value=[])

    def _mock_message(id=1, author_id=1, **kwargs):
        kwargs["id"] = id
        kwargs["author_id"] = author_id

        message = Message(**kwargs)
        VByName.run = MagicMock(return_value=message)

        return message

    def _mock_link(id=1, author_id=1, sr_id=1, can_comment=True, can_view_promo=True, **kwargs):
        kwargs["id"] = id
        kwargs["author_id"] = author_id
        kwargs["sr_id"] = sr_id

        link = Link(**kwargs)
        VByName.run = MagicMock(return_value=link)

        sr = Subreddit(id=sr_id)
        link.subreddit_slow = sr

        Subreddit.can_comment = MagicMock(return_value=can_comment)
        Link.can_view_promo = MagicMock(return_value=can_view_promo)

        return link

    def _mock_comment(
        id=1, author_id=1, link_id=1, sr_id=1, can_comment=True, can_view_promo=True, is_moderator=False, **kwargs
    ):
        kwargs["id"] = id
        kwargs["author_id"] = author_id
        kwargs["link_id"] = link_id

        comment = Comment(**kwargs)
        VByName.run = MagicMock(return_value=comment)

        link = Link(id=link_id)
        Link._byID = MagicMock(return_value=link)

        sr = Subreddit(id=sr_id)
        comment.subreddit_slow = sr
        link.subreddit_slow = sr

        Subreddit.can_comment = MagicMock(return_value=can_comment)
        Link.can_view_promo = MagicMock(return_value=can_view_promo)
        Subreddit.is_moderator = MagicMock(return_value=is_moderator)

        return comment

    def test_no_fullname(self):
        with self.assertRaises(HTTPForbidden):
            self.validator.run("", None)

        self.assertFalse(self.validator.has_errors)

    def test_not_found(self):
        with self.assertRaises(HTTPForbidden):
            VByName.run = MagicMock(return_value=None)
            self.validator.run("fullname", None)

        self.assertFalse(self.validator.has_errors)

    def test_invalid_thing(self):
        with self.assertRaises(HTTPForbidden):
            sr = Subreddit(id=1)
            VByName.run = MagicMock(return_value=sr)
            self.validator.run("fullname", None)

        self.assertFalse(self.validator.has_errors)

    def test_not_loggedin(self):
        with self.assertRaises(HTTPForbidden):
            c.user_is_loggedin = False

            comment = self._mock_comment()
            self.validator.run("fullname", None)

        self.assertFalse(self.validator.has_errors)

    def test_blocked_user(self):
        message = self._mock_message()
        Account.enemy_ids = MagicMock(return_value=[message.author_id])

        result = self.validator.run("fullname", None)

        self.assertEqual(result, message)
        self.assertTrue(self.validator.has_errors)
        self.assertIn((errors.USER_BLOCKED, None), c.errors)

    def test_valid_message(self):
        message = self._mock_message()
        result = self.validator.run("fullname", None)

        self.assertEqual(result, message)
        self.assertFalse(self.validator.has_errors)

    def test_valid_link(self):
        link = self._mock_link()
        result = self.validator.run("fullname", None)

        self.assertEqual(result, link)
        self.assertFalse(self.validator.has_errors)

    def test_removed_link(self):
        link = self._mock_link(_spam=True)
        result = self.validator.run("fullname", None)

        self.assertEqual(result, link)
        self.assertFalse(self.validator.has_errors)

    def test_archived_link(self):
        link = self._mock_link(date=dt.now(g.tz).replace(year=2000))
        result = self.validator.run("fullname", None)

        self.assertEqual(result, link)
        self.assertTrue(self.validator.has_errors)
        self.assertIn((errors.TOO_OLD, None), c.errors)

    def test_locked_link(self):
        link = self._mock_link(locked=True)
        Subreddit.can_distinguish = MagicMock(return_value=False)
        result = self.validator.run("fullname", None)

        self.assertEqual(result, link)
        self.assertTrue(self.validator.has_errors)
        self.assertIn((errors.THREAD_LOCKED, None), c.errors)

    def test_locked_link_mod_reply(self):
        link = self._mock_link(locked=True)
        Subreddit.can_distinguish = MagicMock(return_value=True)
        result = self.validator.run("fullname", None)

        self.assertEqual(result, link)
        self.assertFalse(self.validator.has_errors)

    def test_invalid_link(self):
        with self.assertRaises(HTTPForbidden):
            self._mock_link(can_comment=False)
            self.validator.run("fullname", None)

        self.assertFalse(self.validator.has_errors)

    def test_invalid_promo(self):
        with self.assertRaises(HTTPForbidden):
            self._mock_link(can_view_promo=False)
            self.validator.run("fullname", None)

        self.assertFalse(self.validator.has_errors)

    def test_valid_comment(self):
        comment = self._mock_comment()
        result = self.validator.run("fullname", None)

        self.assertEqual(result, comment)
        self.assertFalse(self.validator.has_errors)

    def test_deleted_comment(self):
        comment = self._mock_comment(_deleted=True)
        result = self.validator.run("fullname", None)

        self.assertEqual(result, comment)
        self.assertTrue(self.validator.has_errors)
        self.assertIn((errors.DELETED_COMMENT, None), c.errors)

    def test_removed_comment(self):
        comment = self._mock_comment(_spam=True)
        result = self.validator.run("fullname", None)

        self.assertEqual(result, comment)
        self.assertTrue(self.validator.has_errors)
        self.assertIn((errors.DELETED_COMMENT, None), c.errors)

    def test_removed_comment_self_reply(self):
        comment = self._mock_comment(author_id=c.user._id, _spam=True)
        result = self.validator.run("fullname", None)

        self.assertEqual(result, comment)
        self.assertFalse(self.validator.has_errors)

    def test_removed_comment_mod_reply(self):
        comment = self._mock_comment(_spam=True, is_moderator=True)
        result = self.validator.run("fullname", None)

        self.assertEqual(result, comment)
        self.assertFalse(self.validator.has_errors)

    def test_invalid_comment(self):
        with self.assertRaises(HTTPForbidden):
            comment = self._mock_comment(can_comment=False)
            self.validator.run("fullname", None)

        self.assertFalse(self.validator.has_errors)
Ejemplo n.º 7
0
class TestVSubmitParent(ValidatorTests):
    def setUp(self):
        super(TestVSubmitParent, self).setUp()
        # Reset the validator state and errors before every test.
        self.validator = VSubmitParent(None)
        c.errors = ErrorSet()

        c.user_is_loggedin = True
        c.user_is_admin = False
        c.user = Account(id=100)

        self.autopatch(Account, "enemy_ids", return_value=[])
        self.autopatch(Subreddit, "_byID", return_value=None)

    def _mock_message(self, id=1, author_id=1, **kwargs):
        kwargs['id'] = id
        kwargs['author_id'] = author_id

        message = Message(**kwargs)
        self.autopatch(VByName, "run", return_value=message)

        return message

    def _mock_link(self, id=1, author_id=1, sr_id=1, can_comment=True,
                   can_view_promo=True, **kwargs):
        kwargs['id'] = id
        kwargs['author_id'] = author_id
        kwargs['sr_id'] = sr_id

        link = Link(**kwargs)
        self.autopatch(VByName, "run", return_value=link)

        sr = Subreddit(id=sr_id)
        self.autopatch(Subreddit, "_byID", return_value=sr)
        self.autopatch(Subreddit, "can_comment", return_value=can_comment)
        self.autopatch(Link, "can_view_promo", return_value=can_view_promo)

        return link

    def _mock_comment(self,
                      id=1, author_id=1, link_id=1, sr_id=1, can_comment=True,
                      can_view_promo=True, is_moderator=False, **kwargs):
        kwargs['id'] = id
        kwargs['author_id'] = author_id
        kwargs['link_id'] = link_id
        kwargs['sr_id'] = sr_id

        comment = Comment(**kwargs)
        self.autopatch(VByName, "run", return_value=comment)

        link = Link(id=link_id, sr_id=sr_id)
        self.autopatch(Link, "_byID", return_value=link)

        sr = Subreddit(id=sr_id)
        self.autopatch(Subreddit, "_byID", return_value=sr)
        self.autopatch(Subreddit, "can_comment", return_value=can_comment)
        self.autopatch(Link, "can_view_promo", return_value=can_view_promo)
        self.autopatch(Subreddit, "is_moderator", return_value=is_moderator)

        return comment

    def test_no_fullname(self):
        with self.assertRaises(HTTPForbidden):
            self.validator.run('', None)

        self.assertFalse(self.validator.has_errors)

    def test_not_found(self):
        with self.assertRaises(HTTPForbidden):
            with patch.object(VByName, "run", return_value=None):
                self.validator.run('fullname', None)

        self.assertFalse(self.validator.has_errors)

    def test_invalid_thing(self):
        with self.assertRaises(HTTPForbidden):
            sr = Subreddit(id=1)
            with patch.object(VByName, "run", return_value=sr):
                self.validator.run('fullname', None)

        self.assertFalse(self.validator.has_errors)

    def test_not_loggedin(self):
        with self.assertRaises(HTTPForbidden):
            c.user_is_loggedin = False

            self._mock_comment()
            self.validator.run('fullname', None)

        self.assertFalse(self.validator.has_errors)

    def test_blocked_user(self):
        message = self._mock_message()
        with patch.object(
            Account, "enemy_ids", return_value=[message.author_id]
        ):
            result = self.validator.run('fullname', None)

            self.assertEqual(result, message)
            self.assertTrue(self.validator.has_errors)
            self.assertIn((errors.USER_BLOCKED, None), c.errors)

    def test_valid_message(self):
        message = self._mock_message()
        result = self.validator.run('fullname', None)

        self.assertEqual(result, message)
        self.assertFalse(self.validator.has_errors)

    def test_valid_link(self):
        link = self._mock_link()
        result = self.validator.run('fullname', None)

        self.assertEqual(result, link)
        self.assertFalse(self.validator.has_errors)

    def test_removed_link(self):
        link = self._mock_link(_spam=True)
        result = self.validator.run('fullname', None)

        self.assertEqual(result, link)
        self.assertFalse(self.validator.has_errors)

    def test_archived_link(self):
        link = self._mock_link(date=dt.now(g.tz).replace(year=2000))
        result = self.validator.run('fullname', None)

        self.assertEqual(result, link)
        self.assertTrue(self.validator.has_errors)
        self.assertIn((errors.TOO_OLD, None), c.errors)

    def test_locked_link(self):
        link = self._mock_link(locked=True)
        with patch.object(Subreddit, "can_distinguish", return_value=False):
            result = self.validator.run('fullname', None)

            self.assertEqual(result, link)
            self.assertTrue(self.validator.has_errors)
            self.assertIn((errors.THREAD_LOCKED, None), c.errors)

    def test_locked_link_mod_reply(self):
        link = self._mock_link(locked=True)
        with patch.object(Subreddit, "can_distinguish", return_value=True):
            result = self.validator.run('fullname', None)

            self.assertEqual(result, link)
            self.assertFalse(self.validator.has_errors)

    def test_invalid_link(self):
        with self.assertRaises(HTTPForbidden):
            self._mock_link(can_comment=False)
            self.validator.run('fullname', None)

        self.assertFalse(self.validator.has_errors)

    def test_invalid_promo(self):
        with self.assertRaises(HTTPForbidden):
            self._mock_link(can_view_promo=False)
            self.validator.run('fullname', None)

        self.assertFalse(self.validator.has_errors)

    def test_valid_comment(self):
        comment = self._mock_comment()
        result = self.validator.run('fullname', None)

        self.assertEqual(result, comment)
        self.assertFalse(self.validator.has_errors)

    def test_deleted_comment(self):
        comment = self._mock_comment(_deleted=True)
        result = self.validator.run('fullname', None)

        self.assertEqual(result, comment)
        self.assertTrue(self.validator.has_errors)
        self.assertIn((errors.DELETED_COMMENT, None), c.errors)

    def test_removed_comment(self):
        comment = self._mock_comment(_spam=True)
        result = self.validator.run('fullname', None)

        self.assertEqual(result, comment)
        self.assertTrue(self.validator.has_errors)
        self.assertIn((errors.DELETED_COMMENT, None), c.errors)

    def test_removed_comment_self_reply(self):
        comment = self._mock_comment(author_id=c.user._id, _spam=True)
        result = self.validator.run('fullname', None)

        self.assertEqual(result, comment)
        self.assertFalse(self.validator.has_errors)

    def test_removed_comment_mod_reply(self):
        comment = self._mock_comment(_spam=True, is_moderator=True)
        result = self.validator.run('fullname', None)

        self.assertEqual(result, comment)
        self.assertFalse(self.validator.has_errors)

    def test_invalid_comment(self):
        with self.assertRaises(HTTPForbidden):
            comment = self._mock_comment(can_comment=False)
            self.validator.run('fullname', None)

        self.assertFalse(self.validator.has_errors)