File: test_http.py

package info (click to toggle)
vdirsyncer 0.14.1-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 848 kB
  • sloc: python: 6,664; makefile: 243; sh: 59
file content (123 lines) | stat: -rw-r--r-- 3,671 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-

import pytest

from requests import Response

from tests import normalize_item

from vdirsyncer.exceptions import UserError
from vdirsyncer.storage.http import HttpStorage, prepare_auth


def test_list(monkeypatch):
    collection_url = 'http://127.0.0.1/calendar/collection.ics'

    items = [
        (u'BEGIN:VEVENT\n'
         u'SUMMARY:Eine Kurzinfo\n'
         u'DESCRIPTION:Beschreibung des Termines\n'
         u'END:VEVENT'),
        (u'BEGIN:VEVENT\n'
         u'SUMMARY:Eine zweite Küèrzinfo\n'
         u'DESCRIPTION:Beschreibung des anderen Termines\n'
         u'BEGIN:VALARM\n'
         u'ACTION:AUDIO\n'
         u'TRIGGER:19980403T120000\n'
         u'ATTACH;FMTTYPE=audio/basic:http://host.com/pub/ssbanner.aud\n'
         u'REPEAT:4\n'
         u'DURATION:PT1H\n'
         u'END:VALARM\n'
         u'END:VEVENT')
    ]

    responses = [
        u'\n'.join([u'BEGIN:VCALENDAR'] + items + [u'END:VCALENDAR'])
    ] * 2

    def get(self, method, url, *a, **kw):
        assert method == 'GET'
        assert url == collection_url
        r = Response()
        r.status_code = 200
        assert responses
        r._content = responses.pop().encode('utf-8')
        r.headers['Content-Type'] = 'text/calendar'
        r.encoding = 'ISO-8859-1'
        return r

    monkeypatch.setattr('requests.sessions.Session.request', get)

    s = HttpStorage(url=collection_url)

    found_items = {}

    for href, etag in s.list():
        item, etag2 = s.get(href)
        assert item.uid is not None
        assert etag2 == etag
        found_items[normalize_item(item)] = href

    expected = set(normalize_item(u'BEGIN:VCALENDAR\n' + x + '\nEND:VCALENDAR')
                   for x in items)

    assert set(found_items) == expected

    for href, etag in s.list():
        item, etag2 = s.get(href)
        assert item.uid is not None
        assert etag2 == etag
        assert found_items[normalize_item(item)] == href


def test_readonly_param():
    url = 'http://example.com/'
    with pytest.raises(ValueError):
        HttpStorage(url=url, read_only=False)

    a = HttpStorage(url=url, read_only=True).read_only
    b = HttpStorage(url=url, read_only=None).read_only
    assert a is b is True


def test_prepare_auth():
    assert prepare_auth(None, '', '') is None

    assert prepare_auth(None, 'user', 'pwd') == ('user', 'pwd')
    assert prepare_auth('basic', 'user', 'pwd') == ('user', 'pwd')

    with pytest.raises(ValueError) as excinfo:
        assert prepare_auth('basic', '', 'pwd')
    assert 'you need to specify username and password' in \
        str(excinfo.value).lower()

    from requests.auth import HTTPDigestAuth
    assert isinstance(prepare_auth('digest', 'user', 'pwd'),
                      HTTPDigestAuth)

    with pytest.raises(ValueError) as excinfo:
        prepare_auth('ladida', 'user', 'pwd')

    assert 'unknown authentication method' in str(excinfo.value).lower()


def test_prepare_auth_guess(monkeypatch):
    import requests_toolbelt.auth.guess

    assert isinstance(prepare_auth('guess', 'user', 'pwd'),
                      requests_toolbelt.auth.guess.GuessAuth)

    monkeypatch.delattr(requests_toolbelt.auth.guess, 'GuessAuth')

    with pytest.raises(UserError) as excinfo:
        prepare_auth('guess', 'user', 'pwd')

    assert 'requests_toolbelt is too old' in str(excinfo.value).lower()


def test_verify_false_disallowed():
    with pytest.raises(ValueError) as excinfo:
        HttpStorage(url='http://example.com', verify=False)

    assert 'forbidden' in str(excinfo.value).lower()
    assert 'consider setting verify_fingerprint' in str(excinfo.value).lower()