def wrapped_queue_json_publish(*args: Any, **kwargs: Any) -> None:
            # Mock the network request result so the test can be fast without Internet
            response = MockPythonResponse(self.open_graph_html, 200)
            mocked_response_original = mock.Mock(
                side_effect=lambda k: {original_url: response}.get(k, MockPythonResponse('', 404)))
            mocked_response_edited = mock.Mock(
                side_effect=lambda k: {edited_url: response}.get(k, MockPythonResponse('', 404)))
            with self.settings(TEST_SUITE=False, CACHES=TEST_CACHES):
                with mock.patch('requests.get', mocked_response_original):
                    # Run the queue processor. This will simulate the event for original_url being
                    # processed after the message has been edited.
                    FetchLinksEmbedData().consume(event)
            msg = Message.objects.select_related("sender").get(id=msg_id)
            # The content of the message has changed since the event for original_url has been created,
            # it should not be rendered. Another, up-to-date event will have been sent (edited_url).
            self.assertNotIn('<a href="{0}" target="_blank" title="The Rock">The Rock</a>'.format(original_url),
                             msg.rendered_content)
            mocked_response_edited.assert_not_called()

            with self.settings(TEST_SUITE=False, CACHES=TEST_CACHES):
                with mock.patch('requests.get', mocked_response_edited):
                    # Now proceed with the original queue_json_publish and call the
                    # up-to-date event for edited_url.
                    queue_json_publish(*args, **kwargs)
                    msg = Message.objects.select_related("sender").get(id=msg_id)
                    self.assertIn('<a href="{0}" target="_blank" title="The Rock">The Rock</a>'.format(edited_url),
                                  msg.rendered_content)
    def test_edit_message_history(self) -> None:
        email = self.example_email('hamlet')
        self.login(email)
        msg_id = self.send_stream_message(email, "Scotland",
                                          topic_name="editing", content="original")

        url = 'http://test.org/'
        response = MockPythonResponse(self.open_graph_html, 200)
        mocked_response = mock.Mock(
            side_effect=lambda k: {url: response}.get(k, MockPythonResponse('', 404)))

        with mock.patch('zerver.views.messages.queue_json_publish') as patched:
            result = self.client_patch("/json/messages/" + str(msg_id), {
                'message_id': msg_id, 'content': url,
            })
            self.assert_json_success(result)
            patched.assert_called_once()
            queue = patched.call_args[0][0]
            self.assertEqual(queue, "embed_links")
            event = patched.call_args[0][1]

        with self.settings(TEST_SUITE=False, CACHES=TEST_CACHES):
            with mock.patch('requests.get', mocked_response):
                FetchLinksEmbedData().consume(event)

        embedded_link = '<a href="{0}" target="_blank" title="The Rock">The Rock</a>'.format(url)
        msg = Message.objects.select_related("sender").get(id=msg_id)
        self.assertIn(embedded_link, msg.rendered_content)
Beispiel #3
0
 def create_mock_response(cls, url: str, relative_url: bool=False,
                          headers: Optional[Dict[str, str]]=None) -> Callable[..., MockPythonResponse]:
     html = cls.open_graph_html
     if relative_url is True:
         html = html.replace('http://ia.media-imdb.com', '')
     response = MockPythonResponse(html, 200, headers)
     return lambda k, **kwargs: {url: response}.get(k, MockPythonResponse('', 404, headers))
Beispiel #4
0
 def create_mock_response(
         cls,
         url: str,
         relative_url: bool = False) -> Callable[..., MockPythonResponse]:
     html = cls.open_graph_html
     if relative_url is True:
         html = html.replace('http://ia.media-imdb.com', '')
     response = MockPythonResponse(html, 200)
     return lambda k: {url: response}.get(k, MockPythonResponse('', 404))
Beispiel #5
0
    def _send_message_with_test_org_url(self,
                                        sender_email,
                                        queue_should_run=True,
                                        relative_url=False):
        # type: (str, bool, bool) -> Message
        url = 'http://test.org/'
        with mock.patch('zerver.lib.actions.queue_json_publish') as patched:
            msg_id = self.send_personal_message(
                sender_email,
                self.example_email('cordelia'),
                content=url,
            )
            if queue_should_run:
                patched.assert_called_once()
                queue = patched.call_args[0][0]
                self.assertEqual(queue, "embed_links")
                event = patched.call_args[0][1]
            else:
                patched.assert_not_called()
                # If we nothing was put in the queue, we don't need to
                # run the queue processor or any of the following code
                return Message.objects.select_related("sender").get(id=msg_id)

        # Verify the initial message doesn't have the embedded links rendered
        msg = Message.objects.select_related("sender").get(id=msg_id)
        self.assertNotIn(
            '<a href="{0}" target="_blank" title="The Rock">The Rock</a>'.
            format(url), msg.rendered_content)

        # Mock the network request result so the test can be fast without Internet
        response = MockPythonResponse(self.open_graph_html, 200)
        if relative_url is True:
            response = MockPythonResponse(
                self.open_graph_html.replace('http://ia.media-imdb.com', ''),
                200)
        mocked_response = mock.Mock(side_effect=lambda k: {url: response}.get(
            k, MockPythonResponse('', 404)))

        # Run the queue processor to potentially rerender things
        with self.settings(TEST_SUITE=False, CACHES=TEST_CACHES):
            with mock.patch('requests.get', mocked_response):
                FetchLinksEmbedData().consume(event)
        msg = Message.objects.select_related("sender").get(id=msg_id)
        return msg