summaryrefslogtreecommitdiffstats
path: root/deluge/tests/test_webserver.py
blob: 9503f506e30ebbe0e401f8f70a186b6073f94088 (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
#
# Copyright (C) 2016 bendikro <bro.devel+deluge@gmail.com>
#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#

import json as json_lib
from io import BytesIO

import pytest
import twisted.web.client
from twisted.internet import reactor
from twisted.web.client import Agent, FileBodyProducer
from twisted.web.http_headers import Headers

from . import common
from .common import get_test_data_file
from .common_web import WebServerMockBase, WebServerTestBase

common.disable_new_release_check()


class TestWebServer(WebServerTestBase, WebServerMockBase):
    async def test_get_torrent_info(self):
        agent = Agent(reactor)

        self.mock_authentication_ignore(self.deluge_web.auth)

        # This torrent file contains an uncommon field 'filehash' which must be hex
        # encoded to allow dumping the torrent info to json. Otherwise it will fail with:
        # UnicodeDecodeError: 'utf8' codec can't decode byte 0xe5 in position 0: invalid continuation byte
        filename = get_test_data_file('filehash_field.torrent')
        input_file = (
            '{"params": ["%s"], "method": "web.get_torrent_info", "id": 22}'
            % filename.replace('\\', '\\\\')
        )
        headers = {
            b'User-Agent': ['Twisted Web Client Example'],
            b'Content-Type': ['application/json'],
        }
        url = 'http://127.0.0.1:%s/json' % self.deluge_web.port

        response = await agent.request(
            b'POST',
            url.encode(),
            Headers(headers),
            FileBodyProducer(BytesIO(input_file.encode())),
        )
        body = await twisted.web.client.readBody(response)

        try:
            json = json_lib.loads(body.decode())
        except Exception:
            print('aoeu')
        assert json['error'] is None
        assert 'torrent_filehash' == json['result']['name']

    @pytest.mark.parametrize('base', ['', '/', 'deluge'])
    async def test_base_with_config(self, base):
        agent = Agent(reactor)
        root_url = f'http://127.0.0.1:{self.deluge_web.port}'
        base_url = f'{root_url}/{base}'

        self.deluge_web.base = base

        response = await agent.request(b'GET', root_url.encode())
        assert response.code == 200
        body = await twisted.web.client.readBody(response)
        assert 'Deluge WebUI' in body.decode()

        response = await agent.request(b'GET', base_url.encode())
        assert response.code == 200

    @pytest.mark.parametrize('base', ['/', 'deluge'])
    async def test_base_with_config_recurring_basepath(self, base):
        agent = Agent(reactor)
        base_url = f'http://127.0.0.1:{self.deluge_web.port}/{base}'

        self.deluge_web.base = base

        response = await agent.request(b'GET', base_url.encode())
        assert response.code == 200

        recursive_url = f'{base_url}/{base}'
        response = await agent.request(b'GET', recursive_url.encode())
        assert response.code == 404 if base.strip('/') else 200

        recursive_url = f'{recursive_url}/{base}'
        response = await agent.request(b'GET', recursive_url.encode())
        assert response.code == 404 if base.strip('/') else 200

    async def test_base_with_deluge_header(self):
        """Ensure base path is set and HTML contains path"""
        agent = Agent(reactor)
        base = 'deluge'
        url = f'http://127.0.0.1:{self.deluge_web.port}'
        headers = Headers({'X-Deluge-Base': [base]})

        response = await agent.request(b'GET', url.encode(), headers)
        body = await twisted.web.client.readBody(response)
        assert f'href="/{base}/' in body.decode()

        # Header only changes HTML base path so ensure no resource at server path
        url = f'{url}/{base}'
        response = await agent.request(b'GET', url.encode(), headers)
        assert response.code == 404