def setUp(self): self.blank_collection = MockCollection( path='/blank_collection/', get_descendants=Mock(return_value=[]), get_parent=lambda: self.top_collection ) self.sub_object = MockObject( path='/collection/sub_object', getcontentlength=42, get_descendants=Mock(return_value=[]), get_parent=lambda: self.top_collection ) self.missing_sub_object = MissingMockObject( path='/collection/missing_sub_object', getcontentlength=42, get_descendants=Mock(return_value=[]), get_parent=lambda: self.top_collection ) self.missing_sub_collection = MissingMockCollection( path='/collection/missing_sub_collection', get_descendants=Mock(return_value=[]), get_parent=lambda: self.top_collection ) self.sub_collection = MockCollection( path='/collection/sub_colection/', get_descendants=Mock(return_value=[]), get_parent=lambda: self.top_collection ) self.top_collection = MockCollection( path='/collection/', get_descendants=Mock(return_value=[self.sub_object, self.sub_collection]) )
def test_get_object_redirect(self): actual_path = '/object.mp4' wrong_path = '/object.mp4/' r = DavView(path=wrong_path, acl_class=FullAcl) r.__dict__['resource'] = MockObject(actual_path) request = Mock(META={'SERVERNANE': 'testserver'}, build_absolute_uri=Mock(return_value=wrong_path)) resp = r.get(request, wrong_path, 'xbody') self.assertEqual(resp.status_code, 302) self.assertEqual(resp['Location'], actual_path)
def test_head_object(self): path = '/object.txt' v = DavView(path=path, base_url='/base', _allowed_methods=Mock(return_value=['ALL']), acl_class=FullAcl) v.__dict__['resource'] = MockObject(path) resp = v.head(None, path) self.assertEqual("text/plain", resp['Content-Type']) self.assertEqual("Wed, 24 Dec 2014 06:00:00 +0000", resp['Last-Modified']) self.assertEqual(b"", resp.content) self.assertEqual("0", resp['Content-Length'])
def test_get_obj(self): path = '/obj.txt' v = DavView(path=path, _allowed_methods=Mock(return_value=['ALL']), acl_class=FullAcl) v.__dict__['resource'] = MockObject(path, read=Mock(return_value="C" * 42)) resp = v.get(None, path, acl_class=FullAcl) self.assertEqual(resp['Etag'], "0" * 40) self.assertEqual(resp['Content-Type'], "text/plain") self.assertEqual(resp['Last-Modified'], "Wed, 24 Dec 2014 06:00:00 +0000") self.assertEqual(resp.content, b"C" * 42)
def test_options_root(self): path = '/' v = DavView(path=path, acl_class=FullAcl) v.__dict__['resource'] = MockObject(path) resp = v.options(None, path) self.assertEqual(sorted(resp.items()), [ ('Content-Length', '0'), ('Content-Type', 'text/xml; charset="utf-8"'), ('DAV', '1,2'), ])
def test_copy_collection_depth_0(self): child = MockObject('/path/to/src/child', copy=Mock()) src = MockCollection('/path/to/src/', get_children=Mock(return_value=[child]), delete=Mock()) dst = MissingMockCollection('/path/to/dst/', create_collection=Mock()) src.copy(dst, 0) dst.create_collection.assert_called_with() self.assertEqual(child.copy.call_count, 0)
def test_options_obj(self): path = '/obj' v = DavView(path=path, _allowed_methods=Mock(return_value=['ALL']), acl_class=FullAcl) v.__dict__['resource'] = MockObject(path) resp = v.options(None, path) self.assertEqual(sorted(resp.items()), [ ('Allow', 'ALL'), ('Allow-Ranges', 'bytes'), ('Content-Length', '0'), ('Content-Type', 'text/xml; charset="utf-8"'), ('DAV', '1,2'), ])
def test_copy_collection_collision(self): child = MockObject('/path/to/src/child', copy=Mock()) src = MockCollection('/path/to/src/', get_children=Mock(return_value=[child]), delete=Mock()) dst = MockCollection('/path/to/dst/', create_collection=Mock()) src.copy_collection(dst) self.assertEqual(dst.create_collection.call_count, 0) self.assertEqual(child.copy.call_args[0][0].path, ['path', 'to', 'dst', 'child'])
def test_move_collection(self): child = MockObject('/path/to/src/child', move=Mock()) src = MockCollection('/path/to/src/', get_children=Mock(return_value=[child]), delete=Mock()) dst = MissingMockCollection('/path/to/dst/', create_collection=Mock()) src.move(dst) src.delete.assert_called_with() dst.create_collection.assert_called_with() self.assertEqual(child.move.call_args[0][0].path, ['path', 'to', 'dst', 'child'])