Menu

[r34]: / tests / client_test.py  Maximize  Restore  History

Download this file

176 lines (149 with data), 6.3 kB

  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
import unittest
import os
import mock
import python_webdav.client as webdav_client
class TestClient(unittest.TestCase):
""" Test case for the client
"""
def setUp(self):
""" setUp
"""
self.client = webdav_client.Client('http://localhost/webdav',
webdav_path = '.',
realm = 'test-realm',
port = 80)
self.client.set_connection(username='wibble', password='fish')
def tearDown(self):
""" tearDown
"""
pass
def test_download_file(self):
""" Download a simple file
"""
file_path = 'webdav/test_file1.txt'
dest_path = os.path.join(os.path.dirname(__file__), 'test_data')
resp, content = self.client.download_file(file_path,
dest_path=dest_path)
written_file = os.path.join(dest_path, 'test_file1.txt')
self.assertTrue(os.path.exists(written_file))
file_fd = open(written_file, 'r')
data = file_fd.read()
file_fd.close
os.remove(written_file)
self.assertEqual(data, 'Test file\n')
def test_chdir(self):
""" test_chdir
"""
path1 = ''
path2 = '/'
path3 = '/hello'
path4 = 'hello/'
self.client.connection.path = path1
self.client.chdir('..')
self.assertEqual(self.client.connection.path, '/')
self.client.connection.path = path2
self.client.chdir('..')
self.assertEqual(self.client.connection.path, '/')
self.client.connection.path = path3
self.client.chdir('..')
self.assertEqual(self.client.connection.path, '/')
self.client.connection.path = path4
self.client.chdir('..')
self.assertEqual(self.client.connection.path, '/')
self.client.connection.path = path1
self.client.chdir('wibble')
self.assertEqual(self.client.connection.path, '/wibble')
self.client.chdir('wibble')
self.assertEqual(self.client.connection.path, '/wibble/wibble')
self.client.chdir('/foo/bar')
self.assertEqual(self.client.connection.path, '/foo/bar')
def test_ls(self):
self.client.connection.path = 'webdav'
self.client.connection.host = 'http://localhost/'
result = sorted(self.client.ls())
expected_result = sorted([
['/webdav/', 'httpd/unix-directory'],
['/webdav/newpath/', 'httpd/unix-directory'],
['/webdav/test_dir1/', 'httpd/unix-directory'],
['/webdav/test_file1.txt', 'text/plain'],
['/webdav/test_file2.txt', 'text/plain'],
['/webdav/test_file_post.txt', 'text/plain']])
self.assertEqual(result[0][0:2], expected_result[0])
self.assertEqual(result[1][0:2], expected_result[1])
self.assertEqual(result[2][0:2], expected_result[2])
self.assertEqual(result[3][0:2], expected_result[3])
self.assertEqual(result[4][0:2], expected_result[4])
self.assertEqual(result[5][0:2], expected_result[5])
self.assertTrue(result[0][-1])
self.assertTrue(result[1][-1])
self.assertTrue(result[2][-1])
self.assertTrue(result[3][-1])
self.assertTrue(result[4][-1])
self.assertTrue(result[5][-1])
def test_mkdir(self):
""" test_mkdir
Test for making directories (collections)
"""
self.client.connection.send_mkcol = mock.Mock()
self.client.connection.path = 'myWebDAV'
self.client.mkdir('newpath')
self.assertEqual(self.client.connection.send_mkcol.call_args_list,
[(('myWebDAV/newpath',), {})])
def test_rmdir(self):
""" test_rmdir
Test for removing directories
"""
self.client.connection.send_rmcol = mock.Mock()
self.client.connection.path = 'myWebDAV'
self.client.rmdir('new_dir')
self.assertEqual([(('myWebDAV/new_dir',), {})],
self.client.connection.send_rmcol.call_args_list)
def test_pwd(self):
""" test_pwd
Test that the pwd method returns the current path stored in the
connection object
"""
self.client.connection.path = "/myWebDAV/monty"
result = self.client.pwd()
self.assertEqual(result, "/myWebDAV/monty")
def test_pwd_none(self):
""" test_pwd
Test that the pwd method returns None when a connection has not been
set up yet.
"""
self.client.connection= None
result = self.client.pwd()
self.assertEqual(result, None)
def test_upload_file_filename(self):
""" Test that a file can be sent to a server
"""
file_name = os.path.join(os.path.dirname(__file__), 'test_data',
"some_file.txt")
if not os.path.exists(file_name):
open(file_name, 'wb').close()
self.client.connection.send_put = mock.Mock()
self.client.connection.send_put.return_value = (200, '')
self.client.connection.path = 'myWebDAV'
resp, cont = self.client.upload_file(file_name)
os.remove(file_name)
self.assertEqual(file_name,
self.client.connection.send_put.call_args[0][0])
def test_upload_file_fileobj(self):
""" Test that a file can be sent to a server
"""
file_name = os.path.join(os.path.dirname(__file__), 'test_data',
"some_file.txt")
if not os.path.exists(file_name):
open(file_name, 'wb').close()
self.client.connection.send_put = mock.Mock()
self.client.connection.send_put.return_value = (200, '')
self.client.connection.path = 'myWebDAV'
fd1 = open(file_name, 'r')
try:
resp, cont = self.client.upload_file(fd1)
finally:
if not fd1.closed:
fd1.close()
os.remove(file_name)
self.assertEqual(file_name,
self.client.connection.send_put.call_args[0][0])