|
| 1 | +# The contents of this file are subject to the Common Public Attribution |
| 2 | +# License Version 1.0. (the "License"); you may not use this file except in |
| 3 | +# compliance with the License. You may obtain a copy of the License at |
| 4 | +# http://code.reddit.com/LICENSE. The License is based on the Mozilla Public |
| 5 | +# License Version 1.1, but Sections 14 and 15 have been added to cover use of |
| 6 | +# software over a computer network and provide for limited attribution for the |
| 7 | +# Original Developer. In addition, Exhibit A has been modified to be consistent |
| 8 | +# with Exhibit B. |
| 9 | +# |
| 10 | +# Software distributed under the License is distributed on an "AS IS" basis, |
| 11 | +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
| 12 | +# the specific language governing rights and limitations under the License. |
| 13 | +# |
| 14 | +# The Original Code is reddit. |
| 15 | +# |
| 16 | +# The Original Developer is the Initial Developer. The Initial Developer of |
| 17 | +# the Original Code is reddit Inc. |
| 18 | +# |
| 19 | +# All portions of the code written by reddit are Copyright (c) 2006-2015 reddit |
| 20 | +# Inc. All Rights Reserved. |
| 21 | +############################################################################### |
| 22 | +import contextlib |
| 23 | + |
| 24 | +from r2.tests import RedditControllerTestCase |
| 25 | +from mock import patch, MagicMock |
| 26 | +from r2.lib.validator import VByName, VUser, VModhash |
| 27 | + |
| 28 | +from r2.models import Link, Message, Account |
| 29 | + |
| 30 | +from pylons import app_globals as g |
| 31 | + |
| 32 | + |
| 33 | +class DelMsgTest(RedditControllerTestCase): |
| 34 | + CONTROLLER = "api" |
| 35 | + |
| 36 | + def setUp(self): |
| 37 | + super(DelMsgTest, self).setUp() |
| 38 | + |
| 39 | + self.id = 1 |
| 40 | + |
| 41 | + def test_del_msg_success(self): |
| 42 | + """Del_msg succeeds: Returns 200 and sets del_on_recipient.""" |
| 43 | + message = MagicMock(spec=Message) |
| 44 | + message.name = "msg_1" |
| 45 | + message.to_id = self.id |
| 46 | + message.del_on_recipient = False |
| 47 | + |
| 48 | + with self.mock_del_msg(message): |
| 49 | + res = self.do_del_msg(message.name) |
| 50 | + |
| 51 | + self.assertEqual(res.status, 200) |
| 52 | + self.assertTrue(message.del_on_recipient) |
| 53 | + |
| 54 | + def test_del_msg_failure_with_link(self): |
| 55 | + """Del_msg fails: Returns 200 and does not set del_on_recipient.""" |
| 56 | + link = MagicMock(spec=Link) |
| 57 | + link.del_on_recipient = False |
| 58 | + link.name = "msg_2" |
| 59 | + |
| 60 | + with self.mock_del_msg(link): |
| 61 | + res = self.do_del_msg(link.name) |
| 62 | + |
| 63 | + self.assertEqual(res.status, 200) |
| 64 | + self.assertFalse(link.del_on_recipient) |
| 65 | + |
| 66 | + def test_del_msg_failure_with_null_msg(self): |
| 67 | + """Del_msg fails: Returns 200 and does not set del_on_recipient.""" |
| 68 | + message = MagicMock(spec=Message) |
| 69 | + message.name = "msg_3" |
| 70 | + message.to_id = self.id |
| 71 | + message.del_on_recipient = False |
| 72 | + |
| 73 | + with self.mock_del_msg(message, False): |
| 74 | + res = self.do_del_msg(message.name) |
| 75 | + |
| 76 | + self.assertEqual(res.status, 200) |
| 77 | + self.assertFalse(message.del_on_recipient) |
| 78 | + |
| 79 | + def test_del_msg_failure_with_sender(self): |
| 80 | + """Del_msg fails: Returns 200 and does not set del_on_recipient.""" |
| 81 | + message = MagicMock(spec=Message) |
| 82 | + message.name = "msg_3" |
| 83 | + message.to_id = self.id + 1 |
| 84 | + message.del_on_recipient = False |
| 85 | + |
| 86 | + with self.mock_del_msg(message): |
| 87 | + res = self.do_del_msg(message.name) |
| 88 | + |
| 89 | + self.assertEqual(res.status, 200) |
| 90 | + self.assertFalse(message.del_on_recipient) |
| 91 | + |
| 92 | + def mock_del_msg(self, thing, ret=True): |
| 93 | + """Context manager for mocking del_msg.""" |
| 94 | + |
| 95 | + return contextlib.nested( |
| 96 | + patch.object(VByName, "run", return_value=thing if ret else None), |
| 97 | + patch.object(VModhash, "run", side_effect=None), |
| 98 | + patch.object(VUser, "run", side_effect=None), |
| 99 | + patch.object(thing, "_commit", side_effect=None), |
| 100 | + patch.object(Account, "_id", self.id, create=True), |
| 101 | + patch.object(g.events, "message_event", side_effect=None), |
| 102 | + ) |
| 103 | + |
| 104 | + def do_del_msg(self, name, **kw): |
| 105 | + return self.do_post("del_msg", {"id": name}, **kw) |
0 commit comments