summaryrefslogtreecommitdiff
path: root/tests/unit/browser/test_pdfjs.py
blob: 3ccd255ce1dc39ffda2813c2b2233635a775b818 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2015 Daniel Schadt
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser.  If not, see <http://www.gnu.org/licenses/>.

import pytest
from PyQt5.QtCore import QUrl

from qutebrowser.browser import pdfjs
from qutebrowser.utils import usertypes, utils


@pytest.mark.parametrize('available, snippet', [
    pytest.param(True, '<title>PDF.js viewer</title>',
                 marks=pytest.mark.skipif(not pdfjs.is_available(),
                                          reason='PDF.js unavailable')),
    (False, '<h1>No pdf.js installation found</h1>'),
    ('force', 'fake PDF.js'),
])
def test_generate_pdfjs_page(available, snippet, monkeypatch):
    if available == 'force':
        monkeypatch.setattr(pdfjs, 'is_available', lambda: True)
        monkeypatch.setattr(pdfjs, 'get_pdfjs_res',
                            lambda filename: b'fake PDF.js')
    else:
        monkeypatch.setattr(pdfjs, 'is_available', lambda: available)

    content = pdfjs.generate_pdfjs_page('example.pdf', QUrl())
    print(content)
    assert snippet in content


# Note that we got double protection, once because we use QUrl.FullyEncoded and
# because we use qutebrowser.utils.javascript.to_js. Characters like " are
# already replaced by QUrl.
@pytest.mark.parametrize('filename, expected', [
    ('foo.bar', "foo.bar"),
    ('foo"bar', "foo%22bar"),
    ('foo\0bar', 'foo%00bar'),
    ('foobar");alert("attack!");',
     'foobar%22);alert(%22attack!%22);'),
])
def test_generate_pdfjs_script(filename, expected):
    expected_open = 'open("qute://pdfjs/file?filename={}");'.format(expected)
    actual = pdfjs._generate_pdfjs_script(filename)
    assert expected_open in actual
    assert 'PDFView' in actual


@pytest.mark.parametrize('qt, backend, expected', [
    ('new', usertypes.Backend.QtWebEngine, False),
    ('new', usertypes.Backend.QtWebKit, False),
    ('old', usertypes.Backend.QtWebEngine, True),
    ('old', usertypes.Backend.QtWebKit, False),
    ('5.7', usertypes.Backend.QtWebEngine, False),
    ('5.7', usertypes.Backend.QtWebKit, False),
])
def test_generate_pdfjs_script_disable_object_url(monkeypatch,
                                                  qt, backend, expected):
    if qt == 'new':
        monkeypatch.setattr(pdfjs.qtutils, 'version_check',
                            lambda version, exact=False, compiled=True:
                            False if version == '5.7.1' else True)
    elif qt == 'old':
        monkeypatch.setattr(pdfjs.qtutils, 'version_check',
                            lambda version, exact=False, compiled=True: False)
    elif qt == '5.7':
        monkeypatch.setattr(pdfjs.qtutils, 'version_check',
                            lambda version, exact=False, compiled=True:
                            True if version == '5.7.1' else False)
    else:
        raise utils.Unreachable

    monkeypatch.setattr(pdfjs.objects, 'backend', backend)

    script = pdfjs._generate_pdfjs_script('testfile')
    assert ('PDFJS.disableCreateObjectURL' in script) == expected


class TestResources:

    @pytest.fixture
    def read_system_mock(self, mocker):
        return mocker.patch.object(pdfjs, '_read_from_system', autospec=True)

    @pytest.fixture
    def read_file_mock(self, mocker):
        return mocker.patch.object(pdfjs.utils, 'read_file', autospec=True)

    def test_get_pdfjs_res_system(self, read_system_mock):
        read_system_mock.return_value = (b'content', 'path')

        assert pdfjs.get_pdfjs_res_and_path('web/test') == (b'content', 'path')
        assert pdfjs.get_pdfjs_res('web/test') == b'content'

        read_system_mock.assert_called_with('/usr/share/pdf.js/',
                                            ['web/test', 'test'])

    def test_get_pdfjs_res_bundled(self, read_system_mock, read_file_mock):
        read_system_mock.return_value = (None, None)

        read_file_mock.return_value = b'content'

        assert pdfjs.get_pdfjs_res_and_path('web/test') == (b'content', None)
        assert pdfjs.get_pdfjs_res('web/test') == b'content'

        for path in pdfjs.SYSTEM_PDFJS_PATHS:
            read_system_mock.assert_any_call(path, ['web/test', 'test'])

    def test_get_pdfjs_res_not_found(self, read_system_mock, read_file_mock):
        read_system_mock.return_value = (None, None)
        read_file_mock.side_effect = FileNotFoundError

        with pytest.raises(pdfjs.PDFJSNotFound,
                           match="Path 'web/test' not found"):
            pdfjs.get_pdfjs_res_and_path('web/test')


@pytest.mark.parametrize('path, expected', [
    ('web/viewer.js', 'viewer.js'),
    ('build/locale/foo.bar', 'locale/foo.bar'),
    ('viewer.js', 'viewer.js'),
    ('foo/viewer.css', 'foo/viewer.css'),
])
def test_remove_prefix(path, expected):
    assert pdfjs._remove_prefix(path) == expected


@pytest.mark.parametrize('names, expected_name', [
    (['one'], 'one'),
    (['doesnotexist', 'two'], 'two'),
    (['one', 'two'], 'one'),
    (['does', 'not', 'onexist'], None),
])
def test_read_from_system(names, expected_name, tmpdir):
    file1 = tmpdir / 'one'
    file1.write_text('text1', encoding='ascii')
    file2 = tmpdir / 'two'
    file2.write_text('text2', encoding='ascii')

    if expected_name == 'one':
        expected = (b'text1', str(file1))
    elif expected_name == 'two':
        expected = (b'text2', str(file2))
    elif expected_name is None:
        expected = (None, None)

    assert pdfjs._read_from_system(str(tmpdir), names) == expected


@pytest.mark.parametrize('available', [True, False])
def test_is_available(available, mocker):
    mock = mocker.patch.object(pdfjs, 'get_pdfjs_res', autospec=True)
    if available:
        mock.return_value = b'foo'
    else:
        mock.side_effect = pdfjs.PDFJSNotFound('build/pdf.js')

    assert pdfjs.is_available() == available


@pytest.mark.parametrize('mimetype, url, enabled, expected', [
    # PDF files
    ('application/pdf', 'http://www.example.com', True, True),
    ('application/x-pdf', 'http://www.example.com', True, True),
    # Not a PDF
    ('application/octet-stream', 'http://www.example.com', True, False),
    # PDF.js disabled
    ('application/pdf', 'http://www.example.com', False, False),
    # Download button in PDF.js
    ('application/pdf', 'blob:qute%3A///b45250b3', True, False),
])
def test_should_use_pdfjs(mimetype, url, enabled, expected, config_stub):
    config_stub.val.content.pdfjs = enabled
    assert pdfjs.should_use_pdfjs(mimetype, QUrl(url)) == expected


def test_get_main_url():
    expected = ('qute://pdfjs/web/viewer.html?filename='
                'hello?world.pdf&file=')
    assert pdfjs.get_main_url('hello?world.pdf') == QUrl(expected)