1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- encoding: utf-8 -*- |
3
|
|
|
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8: |
4
|
|
|
# Author: Binux<[email protected]> |
5
|
|
|
# http://binux.me |
6
|
|
|
# Created on 2015-06-03 21:15 |
7
|
|
|
|
8
|
|
|
import os |
9
|
|
|
import sys |
10
|
|
|
import six |
11
|
|
|
import time |
12
|
|
|
import shutil |
13
|
|
|
import inspect |
14
|
|
|
import unittest2 as unittest |
15
|
|
|
|
16
|
|
|
from six import BytesIO |
17
|
|
|
from pyspider import run |
18
|
|
|
from pyspider.libs import utils |
19
|
|
|
from tests import data_sample_handler, data_handler |
20
|
|
|
|
21
|
|
|
@unittest.skipIf(sys.version_info >= (3, 6), "easywebdav doesn't support python 3.6") |
22
|
|
|
class TestWebDav(unittest.TestCase): |
23
|
|
View Code Duplication |
@classmethod |
|
|
|
|
24
|
|
|
def setUpClass(self): |
25
|
|
|
import easywebdav |
26
|
|
|
|
27
|
|
|
shutil.rmtree('./data/tests', ignore_errors=True) |
28
|
|
|
os.makedirs('./data/tests') |
29
|
|
|
|
30
|
|
|
ctx = run.cli.make_context('test', [ |
31
|
|
|
'--taskdb', 'sqlite+taskdb:///data/tests/task.db', |
32
|
|
|
'--projectdb', 'sqlite+projectdb:///data/tests/projectdb.db', |
33
|
|
|
'--resultdb', 'sqlite+resultdb:///data/tests/resultdb.db', |
34
|
|
|
], None, obj=utils.ObjectDict(testing_mode=True)) |
35
|
|
|
self.ctx = run.cli.invoke(ctx) |
36
|
|
|
|
37
|
|
|
ctx = run.webui.make_context('webui', [ |
38
|
|
|
'--username', 'binux', |
39
|
|
|
'--password', '4321', |
40
|
|
|
], self.ctx) |
41
|
|
|
self.app = run.webui.invoke(ctx) |
42
|
|
|
self.app_thread = utils.run_in_thread(self.app.run) |
43
|
|
|
time.sleep(5) |
44
|
|
|
|
45
|
|
|
self.webdav = easywebdav.connect('localhost', port=5000, path='dav') |
46
|
|
|
self.webdav_up = easywebdav.connect('localhost', port=5000, path='dav', |
47
|
|
|
username='binux', password='4321') |
48
|
|
|
|
49
|
|
View Code Duplication |
@classmethod |
|
|
|
|
50
|
|
|
def tearDownClass(self): |
51
|
|
|
for each in self.ctx.obj.instances: |
52
|
|
|
each.quit() |
53
|
|
|
self.app_thread.join() |
54
|
|
|
time.sleep(1) |
55
|
|
|
|
56
|
|
|
assert not utils.check_port_open(5000) |
57
|
|
|
assert not utils.check_port_open(23333) |
58
|
|
|
assert not utils.check_port_open(24444) |
59
|
|
|
assert not utils.check_port_open(25555) |
60
|
|
|
assert not utils.check_port_open(14887) |
61
|
|
|
|
62
|
|
|
shutil.rmtree('./data/tests', ignore_errors=True) |
63
|
|
|
|
64
|
|
|
def test_10_ls(self): |
65
|
|
|
self.assertEqual(len(self.webdav.ls()), 1) |
66
|
|
|
|
67
|
|
|
def test_20_create_error(self): |
68
|
|
|
import easywebdav |
69
|
|
|
with self.assertRaises(easywebdav.OperationFailed): |
70
|
|
|
self.webdav.upload(inspect.getsourcefile(data_sample_handler), |
71
|
|
|
'bad_file_name') |
72
|
|
|
with self.assertRaises(easywebdav.OperationFailed): |
73
|
|
|
self.webdav.upload(inspect.getsourcefile(data_sample_handler), |
74
|
|
|
'bad.file.name') |
75
|
|
|
|
76
|
|
|
def test_30_create_ok(self): |
77
|
|
|
self.webdav.upload(inspect.getsourcefile(data_handler), 'handler.py') |
78
|
|
|
self.webdav.upload(inspect.getsourcefile(data_sample_handler), 'sample_handler.py') |
79
|
|
|
self.assertEqual(len(self.webdav.ls()), 3) |
80
|
|
|
|
81
|
|
|
def test_40_get_404(self): |
82
|
|
|
io = BytesIO() |
83
|
|
|
import easywebdav |
84
|
|
|
with self.assertRaises(easywebdav.OperationFailed): |
85
|
|
|
self.webdav.download('not_exitst', io) |
86
|
|
|
io.close() |
87
|
|
|
|
88
|
|
|
def test_50_get(self): |
89
|
|
|
io = BytesIO() |
90
|
|
|
self.webdav.download('handler.py', io) |
91
|
|
|
self.assertEqual(utils.text(inspect.getsource(data_handler)), utils.text(io.getvalue())) |
92
|
|
|
io.close() |
93
|
|
|
|
94
|
|
|
io = BytesIO() |
95
|
|
|
self.webdav.download('sample_handler.py', io) |
96
|
|
|
self.assertEqual(utils.text(inspect.getsource(data_sample_handler)), utils.text(io.getvalue())) |
97
|
|
|
io.close() |
98
|
|
|
|
99
|
|
|
def test_60_edit(self): |
100
|
|
|
self.webdav.upload(inspect.getsourcefile(data_handler), 'sample_handler.py') |
101
|
|
|
|
102
|
|
|
def test_70_get(self): |
103
|
|
|
io = BytesIO() |
104
|
|
|
self.webdav.download('sample_handler.py', io) |
105
|
|
|
self.assertEqual(utils.text(inspect.getsource(data_handler)), utils.text(io.getvalue())) |
106
|
|
|
io.close() |
107
|
|
|
|
108
|
|
|
def test_80_password(self): |
109
|
|
|
import requests |
110
|
|
|
rv = requests.post('http://localhost:5000/update', data={ |
111
|
|
|
'name': 'group', |
112
|
|
|
'value': 'lock', |
113
|
|
|
'pk': 'sample_handler', |
114
|
|
|
}) |
115
|
|
|
self.assertEqual(rv.status_code, 200) |
116
|
|
|
|
117
|
|
|
import easywebdav |
118
|
|
|
with self.assertRaises(easywebdav.OperationFailed): |
119
|
|
|
self.webdav.upload(inspect.getsourcefile(data_sample_handler), 'sample_handler.py') |
120
|
|
|
self.webdav_up.upload(inspect.getsourcefile(data_sample_handler), 'sample_handler.py') |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
@unittest.skipIf(sys.version_info >= (3, 6), "easywebdav doesn't support python 3.6") |
124
|
|
|
class TestWebDavNeedAuth(unittest.TestCase): |
125
|
|
View Code Duplication |
@classmethod |
|
|
|
|
126
|
|
|
def setUpClass(self): |
127
|
|
|
import easywebdav |
128
|
|
|
|
129
|
|
|
shutil.rmtree('./data/tests', ignore_errors=True) |
130
|
|
|
os.makedirs('./data/tests') |
131
|
|
|
|
132
|
|
|
ctx = run.cli.make_context('test', [ |
133
|
|
|
'--taskdb', 'sqlite+taskdb:///data/tests/task.db', |
134
|
|
|
'--projectdb', 'sqlite+projectdb:///data/tests/projectdb.db', |
135
|
|
|
'--resultdb', 'sqlite+resultdb:///data/tests/resultdb.db', |
136
|
|
|
], None, obj=utils.ObjectDict(testing_mode=True)) |
137
|
|
|
self.ctx = run.cli.invoke(ctx) |
138
|
|
|
|
139
|
|
|
ctx = run.webui.make_context('webui', [ |
140
|
|
|
'--username', 'binux', |
141
|
|
|
'--password', '4321', |
142
|
|
|
'--need-auth', |
143
|
|
|
], self.ctx) |
144
|
|
|
self.app = run.webui.invoke(ctx) |
145
|
|
|
self.app_thread = utils.run_in_thread(self.app.run) |
146
|
|
|
time.sleep(5) |
147
|
|
|
|
148
|
|
|
self.webdav = easywebdav.connect('localhost', port=5000, path='dav') |
149
|
|
|
self.webdav_up = easywebdav.connect('localhost', port=5000, path='dav', |
150
|
|
|
username='binux', password='4321') |
151
|
|
|
|
152
|
|
View Code Duplication |
@classmethod |
|
|
|
|
153
|
|
|
def tearDownClass(self): |
154
|
|
|
for each in self.ctx.obj.instances: |
155
|
|
|
each.quit() |
156
|
|
|
self.app_thread.join() |
157
|
|
|
time.sleep(1) |
158
|
|
|
|
159
|
|
|
assert not utils.check_port_open(5000) |
160
|
|
|
assert not utils.check_port_open(23333) |
161
|
|
|
assert not utils.check_port_open(24444) |
162
|
|
|
assert not utils.check_port_open(25555) |
163
|
|
|
assert not utils.check_port_open(14887) |
164
|
|
|
|
165
|
|
|
shutil.rmtree('./data/tests', ignore_errors=True) |
166
|
|
|
|
167
|
|
|
def test_10_ls(self): |
168
|
|
|
import easywebdav |
169
|
|
|
with self.assertRaises(easywebdav.OperationFailed): |
170
|
|
|
self.assertEqual(len(self.webdav.ls()), 1) |
171
|
|
|
self.assertEqual(len(self.webdav_up.ls()), 1) |
172
|
|
|
|
173
|
|
|
def test_30_create_ok(self): |
174
|
|
|
self.webdav_up.upload(inspect.getsourcefile(data_handler), 'handler.py') |
175
|
|
|
self.assertEqual(len(self.webdav_up.ls()), 2) |
176
|
|
|
|
177
|
|
|
def test_50_get(self): |
178
|
|
|
import easywebdav |
179
|
|
|
with self.assertRaises(easywebdav.OperationFailed): |
180
|
|
|
io = BytesIO() |
181
|
|
|
self.webdav.download('handler.py', io) |
182
|
|
|
io.close() |
183
|
|
|
|
184
|
|
|
io = BytesIO() |
185
|
|
|
self.webdav_up.download('handler.py', io) |
186
|
|
|
self.assertEqual(utils.text(inspect.getsource(data_handler)), utils.text(io.getvalue())) |
187
|
|
|
io.close() |
188
|
|
|
|