|
| 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 | +@unittest.skipIf(six.PY3, 'webdav not support python3') |
| 21 | +class TestWebDav(unittest.TestCase): |
| 22 | + @classmethod |
| 23 | + def setUpClass(self): |
| 24 | + import easywebdav |
| 25 | + |
| 26 | + shutil.rmtree('./data/tests', ignore_errors=True) |
| 27 | + os.makedirs('./data/tests') |
| 28 | + |
| 29 | + ctx = run.cli.make_context('test', [ |
| 30 | + '--taskdb', 'sqlite+taskdb:///data/tests/task.db', |
| 31 | + '--projectdb', 'sqlite+projectdb:///data/tests/projectdb.db', |
| 32 | + '--resultdb', 'sqlite+resultdb:///data/tests/resultdb.db', |
| 33 | + ], None, obj=utils.ObjectDict(testing_mode=True)) |
| 34 | + self.ctx = run.cli.invoke(ctx) |
| 35 | + |
| 36 | + ctx = run.webui.make_context('webui', [ |
| 37 | + '--username', 'binux', |
| 38 | + '--password', '4321', |
| 39 | + ], self.ctx) |
| 40 | + self.app = run.webui.invoke(ctx) |
| 41 | + utils.run_in_thread(self.app.run) |
| 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 | + @classmethod |
| 48 | + def tearDownClass(self): |
| 49 | + for each in self.ctx.obj.instances: |
| 50 | + each.quit() |
| 51 | + time.sleep(1) |
| 52 | + |
| 53 | + shutil.rmtree('./data/tests', ignore_errors=True) |
| 54 | + |
| 55 | + def test_10_ls(self): |
| 56 | + self.assertEqual(len(self.webdav.ls()), 1) |
| 57 | + |
| 58 | + def test_20_create_error(self): |
| 59 | + import easywebdav |
| 60 | + with self.assertRaises(easywebdav.OperationFailed): |
| 61 | + self.webdav.upload(inspect.getsourcefile(data_sample_handler), |
| 62 | + 'bad_file_name') |
| 63 | + with self.assertRaises(easywebdav.OperationFailed): |
| 64 | + self.webdav.upload(inspect.getsourcefile(data_sample_handler), |
| 65 | + 'bad.file.name') |
| 66 | + |
| 67 | + def test_30_create_ok(self): |
| 68 | + self.webdav.upload(inspect.getsourcefile(data_handler), 'handler.py') |
| 69 | + self.webdav.upload(inspect.getsourcefile(data_sample_handler), 'sample_handler.py') |
| 70 | + self.assertEqual(len(self.webdav.ls()), 3) |
| 71 | + |
| 72 | + def test_40_get_404(self): |
| 73 | + io = BytesIO() |
| 74 | + import easywebdav |
| 75 | + with self.assertRaises(easywebdav.OperationFailed): |
| 76 | + self.webdav.download('not_exitst', io) |
| 77 | + io.close() |
| 78 | + |
| 79 | + def test_50_get(self): |
| 80 | + io = BytesIO() |
| 81 | + self.webdav.download('handler.py', io) |
| 82 | + self.assertEqual(inspect.getsource(data_handler), io.getvalue()) |
| 83 | + io.close() |
| 84 | + |
| 85 | + io = BytesIO() |
| 86 | + self.webdav.download('sample_handler.py', io) |
| 87 | + self.assertEqual(inspect.getsource(data_sample_handler), io.getvalue()) |
| 88 | + io.close() |
| 89 | + |
| 90 | + def test_60_edit(self): |
| 91 | + self.webdav.upload(inspect.getsourcefile(data_handler), 'sample_handler.py') |
| 92 | + |
| 93 | + def test_70_get(self): |
| 94 | + io = BytesIO() |
| 95 | + self.webdav.download('sample_handler.py', io) |
| 96 | + self.assertEqual(inspect.getsource(data_handler), io.getvalue()) |
| 97 | + io.close() |
| 98 | + |
| 99 | + def test_80_password(self): |
| 100 | + import requests |
| 101 | + rv = requests.post('http://localhost:5000/update', data={ |
| 102 | + 'name': 'group', |
| 103 | + 'value': 'lock', |
| 104 | + 'pk': 'sample_handler', |
| 105 | + }) |
| 106 | + self.assertEqual(rv.status_code, 200) |
| 107 | + |
| 108 | + import easywebdav |
| 109 | + with self.assertRaises(easywebdav.OperationFailed): |
| 110 | + self.webdav.upload(inspect.getsourcefile(data_sample_handler), 'sample_handler.py') |
| 111 | + self.webdav_up.upload(inspect.getsourcefile(data_sample_handler), 'sample_handler.py') |
| 112 | + |
0 commit comments