Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 1 | # Copyright 2016 The Chromium Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Unittest for the tracker helpers module.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | from mock import Mock, patch |
| 11 | import unittest |
| 12 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 13 | from mrproto import tracker_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 14 | from tracker import attachment_helpers |
| 15 | |
| 16 | |
| 17 | class AttachmentHelpersFunctionsTest(unittest.TestCase): |
| 18 | |
| 19 | def testIsViewableImage(self): |
| 20 | self.assertTrue(attachment_helpers.IsViewableImage('image/gif', 123)) |
| 21 | self.assertTrue(attachment_helpers.IsViewableImage( |
| 22 | 'image/gif; charset=binary', 123)) |
| 23 | self.assertTrue(attachment_helpers.IsViewableImage('image/png', 123)) |
| 24 | self.assertTrue(attachment_helpers.IsViewableImage( |
| 25 | 'image/png; charset=binary', 123)) |
| 26 | self.assertTrue(attachment_helpers.IsViewableImage('image/x-png', 123)) |
| 27 | self.assertTrue(attachment_helpers.IsViewableImage('image/jpeg', 123)) |
| 28 | self.assertTrue(attachment_helpers.IsViewableImage( |
| 29 | 'image/jpeg; charset=binary', 123)) |
| 30 | self.assertTrue(attachment_helpers.IsViewableImage( |
| 31 | 'image/jpeg', 14 * 1024 * 1024)) |
| 32 | |
| 33 | self.assertFalse(attachment_helpers.IsViewableImage('junk/bits', 123)) |
| 34 | self.assertFalse(attachment_helpers.IsViewableImage( |
| 35 | 'junk/bits; charset=binary', 123)) |
| 36 | self.assertFalse(attachment_helpers.IsViewableImage( |
| 37 | 'image/jpeg', 16 * 1024 * 1024)) |
| 38 | |
| 39 | def testIsViewableVideo(self): |
| 40 | self.assertTrue(attachment_helpers.IsViewableVideo('video/ogg', 123)) |
| 41 | self.assertTrue(attachment_helpers.IsViewableVideo( |
| 42 | 'video/ogg; charset=binary', 123)) |
| 43 | self.assertTrue(attachment_helpers.IsViewableVideo('video/mp4', 123)) |
| 44 | self.assertTrue(attachment_helpers.IsViewableVideo( |
| 45 | 'video/mp4; charset=binary', 123)) |
| 46 | self.assertTrue(attachment_helpers.IsViewableVideo('video/mpg', 123)) |
| 47 | self.assertTrue(attachment_helpers.IsViewableVideo( |
| 48 | 'video/mpg; charset=binary', 123)) |
| 49 | self.assertTrue(attachment_helpers.IsViewableVideo('video/mpeg', 123)) |
| 50 | self.assertTrue(attachment_helpers.IsViewableVideo( |
| 51 | 'video/mpeg; charset=binary', 123)) |
| 52 | self.assertTrue(attachment_helpers.IsViewableVideo( |
| 53 | 'video/mpeg', 14 * 1024 * 1024)) |
| 54 | |
| 55 | self.assertFalse(attachment_helpers.IsViewableVideo('junk/bits', 123)) |
| 56 | self.assertFalse(attachment_helpers.IsViewableVideo( |
| 57 | 'junk/bits; charset=binary', 123)) |
| 58 | self.assertFalse(attachment_helpers.IsViewableVideo( |
| 59 | 'video/mp4', 16 * 1024 * 1024)) |
| 60 | |
| 61 | def testIsViewableText(self): |
| 62 | self.assertTrue(attachment_helpers.IsViewableText('text/plain', 0)) |
| 63 | self.assertTrue(attachment_helpers.IsViewableText('text/plain', 1000)) |
| 64 | self.assertTrue(attachment_helpers.IsViewableText('text/html', 1000)) |
| 65 | self.assertFalse( |
| 66 | attachment_helpers.IsViewableText('text/plain', 200 * 1024 * 1024)) |
| 67 | self.assertFalse(attachment_helpers.IsViewableText('image/jpeg', 200)) |
| 68 | self.assertFalse( |
| 69 | attachment_helpers.IsViewableText('image/jpeg', 200 * 1024 * 1024)) |
| 70 | |
| 71 | def testSignAttachmentID(self): |
| 72 | pass # TODO(jrobbins): write tests |
| 73 | |
| 74 | @patch('tracker.attachment_helpers.SignAttachmentID') |
| 75 | def testGetDownloadURL(self, mock_SignAttachmentID): |
| 76 | """The download URL is always our to attachment servlet.""" |
| 77 | mock_SignAttachmentID.return_value = 67890 |
| 78 | self.assertEqual( |
| 79 | 'attachment?aid=12345&signed_aid=67890', |
| 80 | attachment_helpers.GetDownloadURL(12345)) |
| 81 | |
| 82 | def testGetViewURL(self): |
| 83 | """The view URL may add &inline=1, or use our text attachment servlet.""" |
| 84 | attach = tracker_pb2.Attachment( |
| 85 | attachment_id=1, mimetype='see below', filesize=1000) |
| 86 | download_url = 'attachment?aid=1&signed_aid=2' |
| 87 | |
| 88 | # Viewable image. |
| 89 | attach.mimetype = 'image/jpeg' |
| 90 | self.assertEqual( |
| 91 | download_url + '&inline=1', |
| 92 | attachment_helpers.GetViewURL(attach, download_url, 'proj')) |
| 93 | |
| 94 | # Viewable video. |
| 95 | attach.mimetype = 'video/mpeg' |
| 96 | self.assertEqual( |
| 97 | download_url + '&inline=1', |
| 98 | attachment_helpers.GetViewURL(attach, download_url, 'proj')) |
| 99 | |
| 100 | # Viewable text file. |
| 101 | attach.mimetype = 'text/html' |
| 102 | self.assertEqual( |
| 103 | '/p/proj/issues/attachmentText?aid=1', |
| 104 | attachment_helpers.GetViewURL(attach, download_url, 'proj')) |
| 105 | |
| 106 | # Something we don't support. |
| 107 | attach.mimetype = 'audio/mp3' |
| 108 | self.assertIsNone( |
| 109 | attachment_helpers.GetViewURL(attach, download_url, 'proj')) |
| 110 | |
| 111 | def testGetThumbnailURL(self): |
| 112 | """The thumbnail URL may add param thumb=1 or not.""" |
| 113 | attach = tracker_pb2.Attachment( |
| 114 | attachment_id=1, mimetype='see below', filesize=1000) |
| 115 | download_url = 'attachment?aid=1&signed_aid=2' |
| 116 | |
| 117 | # Viewable image. |
| 118 | attach.mimetype = 'image/jpeg' |
| 119 | self.assertEqual( |
| 120 | download_url + '&inline=1&thumb=1', |
| 121 | attachment_helpers.GetThumbnailURL(attach, download_url)) |
| 122 | |
| 123 | # Viewable video. |
| 124 | attach.mimetype = 'video/mpeg' |
| 125 | self.assertIsNone( |
| 126 | # Video thumbs are displayed via GetVideoURL rather than this. |
| 127 | attachment_helpers.GetThumbnailURL(attach, download_url)) |
| 128 | |
| 129 | # Something that we don't thumbnail. |
| 130 | attach.mimetype = 'audio/mp3' |
| 131 | self.assertIsNone(attachment_helpers.GetThumbnailURL(attach, download_url)) |
| 132 | |
| 133 | def testGetVideoURL(self): |
| 134 | """The video URL is the same as the view URL for actual videos.""" |
| 135 | attach = tracker_pb2.Attachment( |
| 136 | attachment_id=1, mimetype='see below', filesize=1000) |
| 137 | download_url = 'attachment?aid=1&signed_aid=2' |
| 138 | |
| 139 | # Viewable video. |
| 140 | attach.mimetype = 'video/mpeg' |
| 141 | self.assertEqual( |
| 142 | download_url + '&inline=1', |
| 143 | attachment_helpers.GetVideoURL(attach, download_url)) |
| 144 | |
| 145 | # Anything that is not a video. |
| 146 | attach.mimetype = 'audio/mp3' |
| 147 | self.assertIsNone(attachment_helpers.GetVideoURL(attach, download_url)) |