File: test_ftp.py

package info (click to toggle)
python-django-storages 1.13.2-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 860 kB
  • sloc: python: 3,990; makefile: 120; sh: 6
file content (241 lines) | stat: -rw-r--r-- 9,227 bytes parent folder | download | duplicates (3)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import io
from datetime import datetime
from unittest.mock import patch

from django.core.exceptions import ImproperlyConfigured
from django.core.files.base import File
from django.test import TestCase

from storages.backends import ftp

USER = 'foo'
PASSWORD = 'b@r'
HOST = 'localhost'
PORT = 2121
URL = "ftp://{user}:{passwd}@{host}:{port}/".format(user=USER, passwd=PASSWORD,
                                                    host=HOST, port=PORT)

LIST_FIXTURE = """drwxr-xr-x   2 ftp      nogroup      4096 Jul 27 09:46 dir
-rw-r--r--   1 ftp      nogroup      1024 Jul 27 09:45 fi
-rw-r--r--   1 ftp      nogroup      2048 Jul 27 09:50 fi2"""


def list_retrlines(cmd, func):
    for line in LIST_FIXTURE.splitlines():
        func(line)


class FTPTest(TestCase):
    def setUp(self):
        self.storage = ftp.FTPStorage(location=URL)

    def test_init_no_location(self):
        with self.assertRaises(ImproperlyConfigured):
            ftp.FTPStorage()

    @patch('storages.backends.ftp.setting', return_value=URL)
    def test_init_location_from_setting(self, mock_setting):
        storage = ftp.FTPStorage()
        self.assertTrue(mock_setting.called)
        self.assertEqual(storage.location, URL)

    def test_decode_location(self):
        config = self.storage._decode_location(URL)
        wanted_config = {
            'passwd': 'b@r',
            'host': 'localhost',
            'user': 'foo',
            'active': False,
            'path': '/',
            'port': 2121,
        }
        self.assertEqual(config, wanted_config)
        # Test active FTP
        config = self.storage._decode_location('a'+URL)
        wanted_config = {
            'passwd': 'b@r',
            'host': 'localhost',
            'user': 'foo',
            'active': True,
            'path': '/',
            'port': 2121,
        }
        self.assertEqual(config, wanted_config)

    def test_decode_location_error(self):
        with self.assertRaises(ImproperlyConfigured):
            self.storage._decode_location('foo')
        with self.assertRaises(ImproperlyConfigured):
            self.storage._decode_location('http://foo.pt')
        # TODO: Cannot not provide a port
        # with self.assertRaises(ImproperlyConfigured):
        #     self.storage._decode_location('ftp://')

    @patch('ftplib.FTP')
    def test_start_connection(self, mock_ftp):
        self.storage._start_connection()
        self.assertIsNotNone(self.storage._connection)
        # Start active
        storage = ftp.FTPStorage(location='a'+URL)
        storage._start_connection()

    @patch('ftplib.FTP', **{'return_value.pwd.side_effect': IOError()})
    def test_start_connection_timeout(self, mock_ftp):
        self.storage._start_connection()
        self.assertIsNotNone(self.storage._connection)

    @patch('ftplib.FTP', **{'return_value.connect.side_effect': IOError()})
    def test_start_connection_error(self, mock_ftp):
        with self.assertRaises(ftp.FTPStorageException):
            self.storage._start_connection()

    @patch('ftplib.FTP', **{'return_value.quit.return_value': None})
    def test_disconnect(self, mock_ftp_quit):
        self.storage._start_connection()
        self.storage.disconnect()
        self.assertIsNone(self.storage._connection)

    @patch('ftplib.FTP', **{'return_value.pwd.return_value': 'foo'})
    def test_mkremdirs(self, mock_ftp):
        self.storage._start_connection()
        self.storage._mkremdirs('foo/bar')

    @patch('ftplib.FTP', **{'return_value.pwd.return_value': 'foo'})
    def test_mkremdirs_n_subdirectories(self, mock_ftp):
        self.storage._start_connection()
        self.storage._mkremdirs('foo/bar/null')

    @patch('ftplib.FTP', **{
        'return_value.pwd.return_value': 'foo',
        'return_value.storbinary.return_value': None
    })
    def test_put_file(self, mock_ftp):
        self.storage._start_connection()
        self.storage._put_file('foo', File(io.BytesIO(b'foo'), 'foo'))

    @patch('ftplib.FTP', **{
        'return_value.pwd.return_value': 'foo',
        'return_value.storbinary.side_effect': IOError()
    })
    def test_put_file_error(self, mock_ftp):
        self.storage._start_connection()
        with self.assertRaises(ftp.FTPStorageException):
            self.storage._put_file('foo', File(io.BytesIO(b'foo'), 'foo'))

    def test_open(self):
        remote_file = self.storage._open('foo')
        self.assertIsInstance(remote_file, ftp.FTPStorageFile)

    @patch('ftplib.FTP', **{'return_value.pwd.return_value': 'foo'})
    def test_read(self, mock_ftp):
        self.storage._start_connection()
        self.storage._read('foo')

    @patch('ftplib.FTP', **{'return_value.pwd.side_effect': IOError()})
    def test_read2(self, mock_ftp):
        self.storage._start_connection()
        with self.assertRaises(ftp.FTPStorageException):
            self.storage._read('foo')

    @patch('ftplib.FTP', **{
        'return_value.pwd.return_value': 'foo',
        'return_value.storbinary.return_value': None
    })
    def test_save(self, mock_ftp):
        self.storage._save('foo', File(io.BytesIO(b'foo'), 'foo'))

    @patch('ftplib.FTP', **{'return_value.sendcmd.return_value': '213 20160727094506'})
    def test_modified_time(self, mock_ftp):
        self.storage._start_connection()
        modif_date = self.storage.modified_time('foo')
        self.assertEqual(modif_date, datetime(2016, 7, 27, 9, 45, 6))

    @patch('ftplib.FTP', **{'return_value.sendcmd.return_value': '500'})
    def test_modified_time_error(self, mock_ftp):
        self.storage._start_connection()
        with self.assertRaises(ftp.FTPStorageException):
            self.storage.modified_time('foo')

    @patch('ftplib.FTP', **{'return_value.retrlines': list_retrlines})
    def test_listdir(self, mock_retrlines):
        dirs, files = self.storage.listdir('/')
        self.assertEqual(len(dirs), 1)
        self.assertEqual(dirs, ['dir'])
        self.assertEqual(len(files), 2)
        self.assertEqual(sorted(files), sorted(['fi', 'fi2']))

    @patch('ftplib.FTP', **{'return_value.retrlines.side_effect': IOError()})
    def test_listdir_error(self, mock_ftp):
        with self.assertRaises(ftp.FTPStorageException):
            self.storage.listdir('/')

    @patch('ftplib.FTP', **{'return_value.nlst.return_value': ['foo', 'foo2']})
    def test_exists(self, mock_ftp):
        self.assertTrue(self.storage.exists('foo'))
        self.assertFalse(self.storage.exists('bar'))

    @patch('ftplib.FTP', **{'return_value.nlst.side_effect': IOError()})
    def test_exists_error(self, mock_ftp):
        with self.assertRaises(ftp.FTPStorageException):
            self.storage.exists('foo')

    @patch('ftplib.FTP', **{
        'return_value.delete.return_value': None,
        'return_value.nlst.return_value': ['foo', 'foo2']
    })
    def test_delete(self, mock_ftp):
        self.storage.delete('foo')
        self.assertTrue(mock_ftp.return_value.delete.called)

    @patch('ftplib.FTP', **{'return_value.retrlines': list_retrlines})
    def test_size(self, mock_ftp):
        self.assertEqual(1024, self.storage.size('fi'))
        self.assertEqual(2048, self.storage.size('fi2'))
        self.assertEqual(0, self.storage.size('bar'))

    @patch('ftplib.FTP', **{'return_value.retrlines.side_effect': IOError()})
    def test_size_error(self, mock_ftp):
        self.assertEqual(0, self.storage.size('foo'))

    def test_url(self):
        with self.assertRaises(ValueError):
            self.storage._base_url = None
            self.storage.url('foo')
        self.storage = ftp.FTPStorage(location=URL, base_url='http://foo.bar/')
        self.assertEqual('http://foo.bar/foo', self.storage.url('foo'))


class FTPStorageFileTest(TestCase):
    def setUp(self):
        self.storage = ftp.FTPStorage(location=URL)

    @patch('ftplib.FTP', **{'return_value.retrlines': list_retrlines})
    def test_size(self, mock_ftp):
        file_ = ftp.FTPStorageFile('fi', self.storage, 'wb')
        self.assertEqual(file_.size, 1024)

    @patch('ftplib.FTP', **{'return_value.pwd.return_value': 'foo'})
    @patch('storages.backends.ftp.FTPStorage._read', return_value=io.BytesIO(b'foo'))
    def test_readlines(self, mock_ftp, mock_storage):
        file_ = ftp.FTPStorageFile('fi', self.storage, 'wb')
        self.assertEqual([b'foo'], file_.readlines())

    @patch('ftplib.FTP', **{'return_value.pwd.return_value': 'foo'})
    @patch('storages.backends.ftp.FTPStorage._read', return_value=io.BytesIO(b'foo'))
    def test_read(self, mock_ftp, mock_storage):
        file_ = ftp.FTPStorageFile('fi', self.storage, 'wb')
        self.assertEqual(b'foo', file_.read())

    def test_write(self):
        file_ = ftp.FTPStorageFile('fi', self.storage, 'wb')
        file_.write(b'foo')
        file_.seek(0)
        self.assertEqual(file_.file.read(), b'foo')

    @patch('ftplib.FTP', **{'return_value.pwd.return_value': 'foo'})
    @patch('storages.backends.ftp.FTPStorage._read', return_value=io.BytesIO(b'foo'))
    def test_close(self, mock_ftp, mock_storage):
        file_ = ftp.FTPStorageFile('fi', self.storage, 'wb')
        file_.is_dirty = True
        file_.read()
        file_.close()