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