summaryrefslogtreecommitdiff
path: root/testing/tests/blobs/test_fs_backend.py
blob: 0d7e978944ea4d61760e5610215c11add673ccf2 (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
# -*- coding: utf-8 -*-
# test_fs_backend.py
# Copyright (C) 2017 LEAP
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
"""
Tests for blobs backend on server side.
"""
from twisted.trial import unittest
from twisted.internet import defer
from twisted.web.test.test_web import DummyRequest
from leap.soledad.server import _blobs
from io import BytesIO
from mock import Mock
import mock
import os
import base64
import json
import pytest


class FilesystemBackendTestCase(unittest.TestCase):

    @mock.patch.object(_blobs, 'open')
    def test_tag_header(self, open_mock):
        open_mock.return_value = BytesIO('A' * 40 + 'B' * 16)
        expected_tag = base64.urlsafe_b64encode('B' * 16)
        expected_method = Mock()
        backend = _blobs.FilesystemBlobsBackend()
        request = Mock(responseHeaders=Mock(setRawHeaders=expected_method))
        backend.tag_header('user', 'blob_id', request)

        expected_method.assert_called_once_with('Tag', [expected_tag])

    @mock.patch.object(_blobs.static, 'File')
    def test_read_blob(self, file_mock):
        render_mock = Mock()
        file_mock.return_value = render_mock
        backend = _blobs.FilesystemBlobsBackend()
        request = DummyRequest([''])
        backend._get_path = Mock(return_value='path')
        backend.read_blob('user', 'blob_id', request)

        backend._get_path.assert_called_once_with('user', 'blob_id')
        ctype = 'application/octet-stream'
        _blobs.static.File.assert_called_once_with('path', defaultType=ctype)
        render_mock.render_GET.assert_called_once_with(request)

    @mock.patch.object(os.path, 'isfile')
    @defer.inlineCallbacks
    def test_cannot_overwrite(self, isfile):
        isfile.return_value = True
        backend = _blobs.FilesystemBlobsBackend()
        backend._get_path = Mock(return_value='path')
        request = DummyRequest([''])
        yield backend.write_blob('user', 'blob_id', request)
        self.assertEquals(request.written[0], "Blob already exists: blob_id")
        self.assertEquals(request.responseCode, 409)

    @pytest.mark.usefixtures("method_tmpdir")
    @mock.patch.object(os.path, 'isfile')
    @defer.inlineCallbacks
    def test_write_cannot_exceed_quota(self, isfile):
        isfile.return_value = False
        backend = _blobs.FilesystemBlobsBackend()
        backend._get_path = Mock(return_value=self.tempdir)
        request = Mock()

        backend.get_total_storage = lambda x: 100
        backend.quota = 90
        yield backend.write_blob('user', 'blob_id', request)

        request.setResponseCode.assert_called_once_with(507)
        request.write.assert_called_once_with('Quota Exceeded!')

    def test_get_path_partitioning(self):
        backend = _blobs.FilesystemBlobsBackend()
        backend.path = '/somewhere/'
        path = backend._get_path('user', 'blob_id')
        self.assertEquals(path, '/somewhere/user/b/blo/blob_i/blob_id')

    @pytest.mark.usefixtures("method_tmpdir")
    @mock.patch('leap.soledad.server._blobs.os.walk')
    def test_list_blobs(self, walk_mock):
        backend, _ = _blobs.FilesystemBlobsBackend(self.tempdir), None
        walk_mock.return_value = [(_, _, ['blob_0']), (_, _, ['blob_1'])]
        result = json.loads(backend.list_blobs('user', DummyRequest([''])))
        self.assertEquals(result, ['blob_0', 'blob_1'])

    @pytest.mark.usefixtures("method_tmpdir")
    def test_path_validation_for_subdirectories(self):
        blobs_path = self.tempdir
        backend = _blobs.FilesystemBlobsBackend(blobs_path)
        self.assertFalse(backend._valid_subdir('/'))
        self.assertFalse(backend._valid_subdir(blobs_path + '../../../../../'))
        self.assertTrue(backend._valid_subdir(os.path.join(blobs_path, 'x')))