File: test_http_with_singlefile.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 (80 lines) | stat: -rw-r--r-- 2,386 bytes parent folder | download
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
# -*- coding: utf-8 -*-

import pytest

from requests import Response

import vdirsyncer.storage.http
from vdirsyncer.storage.base import Storage
from vdirsyncer.storage.singlefile import SingleFileStorage

from . import StorageTests


class CombinedStorage(Storage):
    '''A subclass of HttpStorage to make testing easier. It supports writes via
    SingleFileStorage.'''
    _repr_attributes = ('url', 'path')

    def __init__(self, url, path, **kwargs):
        if kwargs.get('collection', None) is not None:
            raise ValueError()

        super(CombinedStorage, self).__init__(**kwargs)
        self.url = url
        self.path = path
        self._reader = vdirsyncer.storage.http.HttpStorage(url=url)
        self._reader._ignore_uids = False
        self._writer = SingleFileStorage(path=path)

    def list(self, *a, **kw):
        return self._reader.list(*a, **kw)

    def get(self, *a, **kw):
        self.list()
        return self._reader.get(*a, **kw)

    def upload(self, *a, **kw):
        return self._writer.upload(*a, **kw)

    def update(self, *a, **kw):
        return self._writer.update(*a, **kw)

    def delete(self, *a, **kw):
        return self._writer.delete(*a, **kw)


class TestHttpStorage(StorageTests):
    storage_class = CombinedStorage
    supports_collections = False
    supports_metadata = False

    @pytest.fixture(autouse=True)
    def setup_tmpdir(self, tmpdir, monkeypatch):
        self.tmpfile = str(tmpdir.ensure('collection.txt'))

        def _request(method, url, *args, **kwargs):
            assert method == 'GET'
            assert url == 'http://localhost:123/collection.txt'
            assert 'vdirsyncer' in kwargs['headers']['User-Agent']
            r = Response()
            r.status_code = 200
            try:
                with open(self.tmpfile, 'rb') as f:
                    r._content = f.read()
            except IOError:
                r._content = b''

            r.headers['Content-Type'] = 'text/calendar'
            r.encoding = 'utf-8'
            return r

        monkeypatch.setattr(vdirsyncer.storage.http, 'request', _request)

    @pytest.fixture
    def get_storage_args(self):
        def inner(collection=None):
            assert collection is None
            return {'url': 'http://localhost:123/collection.txt',
                    'path': self.tmpfile}
        return inner