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