示例#1
0
 def setUp(self):
     """Setup the client."""
     self.dav = CoreWebDAVClient("127.0.0.1", 80)
     self.dav.setbasicauth(b"test", b"passwd")
     self.con = Mock.HTTPConnection()
     self.dav._getconnection = lambda: self.con
     response = Mock.Response()
     response.content = LOCKDISCOVERY
     response.status = 200
     self.lock = WebDAVLockResponse(self.dav, "/", response)
示例#2
0
 def test_preparecopymove_illegal_depth(self):
     """Test CoreWebDAVClient._preparecopymove with illegal depth value."""
     source = "/foo bar/baz"
     dest = "/dest/in/ation"
     headers = {"X-Test": "Hello"}
     query = {"foo": "bär"}
     http = CoreWebDAVClient("127.0.0.1", 80)
     http.setbasicauth("me", "secret")
     self.assertRaises(
         ValueError,
         http._preparecopymove,
         source, dest, "1", False, headers
     )
示例#3
0
 def setUp(self):
     """Setup the tests"""
     self.client = CoreWebDAVClient("localhost")
     response = Mock.Response()
     response.content = LOCKDISCOVERY
     response.status = 200
     self.lock = WebDAVLockResponse(self.client, "/", response)
示例#4
0
 def test_preparecopymove(self):
     """Test CoreWebDAVClient._preparecopymove."""
     source = "/foo%20bar/baz"
     dest = "/dest/in/ation"
     headers = {"X-Test": "Hello"}
     http = CoreWebDAVClient("127.0.0.1", 80)
     http.setbasicauth(b"me", b"secret")
     (source, headers) = http._preparecopymove(source, dest, 0, False,
                                               headers)
     assert source == "/foo%20bar/baz"
     exp_headers = {
         "Destination": "http://127.0.0.1:80/dest/in/ation",
         "Overwrite": "F",
         "Authorization": "Basic bWU6c2VjcmV0",
         "X-Test": "Hello",
     }
     assert headers == exp_headers
示例#5
0
 def test_init_409(self):
     """Test WebDAVLockResponse.__init__ with 409 status."""
     client = CoreWebDAVClient("localhost")
     response = Mock.Response()
     response.content = MULTISTATUS
     response.status = 409
     lock = WebDAVLockResponse(client, "/", response)
     assert lock._etree.find("/{DAV:}response") is not None
     assert lock.is_multistatus
示例#6
0
 def test_preparecopymove(self):
     """Test CoreWebDAVClient._preparecopymove."""
     source = "/foo bar/baz"
     dest = "/dest/in/ation"
     headers = {"X-Test": "Hello"}
     query = {"foo": "bär"}
     http = CoreWebDAVClient("127.0.0.1", 80)
     http.setbasicauth("me", "secret")
     (source, headers) = http._preparecopymove(source, dest, 0,
                                               False, headers)
     self.assertEqual(source, "/foo%20bar/baz")
     exp_headers = {
         "Destination": "http://127.0.0.1:80/dest/in/ation",
         "Overwrite": "F",
         "Authorization": "Basic bWU6c2VjcmV0",
         "X-Test": "=?utf-8?q?Hello?=",
     }
     self.assertEqual(headers, exp_headers)
示例#7
0
 def setUp(self):
     """Setup the client."""
     self.dav = CoreWebDAVClient("127.0.0.1", 80)
     self.dav.setbasicauth("test", "passwd")
     self.con = Mock.HTTPConnection()
     self.dav._getconnection = lambda: self.con
     response = Mock.Response()
     response.content = LOCKDISCOVERY
     response.status = 200
     self.lock = WebDAVLockResponse(self.dav, "/", response)
 def test_preparecopymove_col(self):
     """Test CoreWebDAVClient._preparecopymove with collection as source."""
     source = "/foo bar/baz/"
     dest = "/dest/in/ation"
     headers = {"X-Test": "Hello", "X-Test-2": "Umlaut ä"}
     query = {"foo": "bär"}
     http = CoreWebDAVClient("127.0.0.1", 80)
     http.setbasicauth("me", "secret")
     (source, headers) = http._preparecopymove(source, dest, 0,
                                               True, headers)
     self.assertEqual(source, "/foo%20bar/baz/")
     exp_headers = {
         "Destination": "http://127.0.0.1:80/dest/in/ation",
         "Depth": "0",
         "Overwrite": "T",
         "Authorization": "Basic bWU6c2VjcmV0",
         "X-Test": "Hello",
         "X-Test-2": "=?utf-8?b?VW1sYXV0IMOk?=",
     }
     self.assertEqual(headers, exp_headers)
示例#9
0
 def test_preparecopymove_illegal_depth(self):
     """Test CoreWebDAVClient._preparecopymove with illegal depth value."""
     source = "/foo bar/baz"
     dest = "/dest/in/ation"
     headers = {"X-Test": "Hello"}
     http = CoreWebDAVClient("127.0.0.1", 80)
     http.setbasicauth(b"me", b"secret")
     with pytest.raises(ValueError):
         http._preparecopymove(source, dest, "1", False, headers)
示例#10
0
class CoreWebDAVClientTestCase(unittest.TestCase):
    """Test the CoreWebDAVClient class."""
    def setUp(self):
        """Setup the client."""
        self.dav = CoreWebDAVClient("127.0.0.1", 80)
        self.dav.setbasicauth(b"test", b"passwd")
        self.con = Mock.HTTPConnection()
        self.dav._getconnection = lambda: self.con
        response = Mock.Response()
        response.content = LOCKDISCOVERY
        response.status = 200
        self.lock = WebDAVLockResponse(self.dav, "/", response)

    def test_preparecopymove(self):
        """Test CoreWebDAVClient._preparecopymove."""
        source = "/foo%20bar/baz"
        dest = "/dest/in/ation"
        headers = {"X-Test": "Hello"}
        http = CoreWebDAVClient("127.0.0.1", 80)
        http.setbasicauth(b"me", b"secret")
        (source, headers) = http._preparecopymove(source, dest, 0, False,
                                                  headers)
        assert source == "/foo%20bar/baz"
        exp_headers = {
            "Destination": "http://127.0.0.1:80/dest/in/ation",
            "Overwrite": "F",
            "Authorization": "Basic bWU6c2VjcmV0",
            "X-Test": "Hello",
        }
        assert headers == exp_headers

    def test_preparecopymove_col(self):
        """Test CoreWebDAVClient._preparecopymove with collection as source."""
        source = "/foo%20bar/baz/"
        dest = "/dest/in/ation"
        headers = {"X-Test": "Hello"}
        http = CoreWebDAVClient("127.0.0.1", 80)
        http.setbasicauth(b"me", b"secret")
        (source, headers) = http._preparecopymove(source, dest, 0, True,
                                                  headers)
        assert source == "/foo%20bar/baz/"
        exp_headers = {
            "Destination": "http://127.0.0.1:80/dest/in/ation",
            "Depth": "0",
            "Overwrite": "T",
            "Authorization": "Basic bWU6c2VjcmV0",
            "X-Test": "Hello",
        }
        assert headers == exp_headers

    def test_preparecopymove_illegal_depth(self):
        """Test CoreWebDAVClient._preparecopymove with illegal depth value."""
        source = "/foo bar/baz"
        dest = "/dest/in/ation"
        headers = {"X-Test": "Hello"}
        http = CoreWebDAVClient("127.0.0.1", 80)
        http.setbasicauth(b"me", b"secret")
        with pytest.raises(ValueError):
            http._preparecopymove(source, dest, "1", False, headers)

    def test_mkcol(self):
        """Test CoreWebDAVClient.mkcol."""
        # prepare mock connection
        self.con.response.status = 201
        assert self.dav.mkcol("/foobar") == 201
        assert self.con.method == "MKCOL"
        assert self.con.path == "/foobar"
        assert self.con.closed
        assert "Authorization" in self.con.headers

    def test_propfind(self):
        """Test CoreWebDAVClient.propfind."""
        # prepare mock connection
        self.con.response.status = 207
        self.con.response.content = MULTISTATUS
        assert self.dav.propfind("/foobar") == 207
        assert self.con.method == "PROPFIND"
        assert self.con.path == "/foobar"
        assert self.con.headers["Depth"] == "0"
        assert self.con.closed
        assert "Authorization" in self.con.headers

    def test_propfind_depth_1(self):
        """Test CoreWebDAVClient.propfind with depth 1."""
        # prepare mock connection
        self.con.response.status = 207
        self.con.response.content = MULTISTATUS
        assert self.dav.propfind("/foobar", "1") == 207
        assert self.con.method == "PROPFIND"
        assert self.con.path == "/foobar"
        assert self.con.headers["Depth"] == "1"
        assert self.con.closed
        assert "Authorization" in self.con.headers

    def test_propfind_illegal_depth(self):
        """Test CoreWebDAVClient.propfind with illegal depth."""
        # prepare mock connection
        with pytest.raises(ValueError):
            self.dav.propfind("/foobar", "ABC")

    def test_propfind_illegal_args(self):
        """Test CoreWebDAVClient.propfind with illegal args."""
        # prepare mock connection
        with pytest.raises(ValueError):
            self.dav.propfind("/foobar",
                              1,
                              properties=["foo"],
                              include=["bar"])

    def test_put(self):
        """Test CoreWebDAVClient.put."""
        # prepare mock connection
        self.con.response.status = 201
        self.con.response.content = "Test content."
        assert self.dav.put("/foobar", self.con.response) == 201
        assert self.con.method == "PUT"
        assert self.con.path == "/foobar"
        body = getattr(self.con.body, 'content', self.con.body)
        assert body == self.con.response.content
        assert self.con.closed
        assert "Authorization" in self.con.headers

    def test_proppatch(self):
        """Test CoreWebDAVClient.proppatch."""
        self.con.response.status = 207
        self.con.response.content = MULTISTATUS
        props = {"CADN:author": "me", "CADN:created": "2009-09-09 13:31"}
        ns = {"CADN": "CADN:"}
        assert 207 == self.dav.proppatch("/foobar", props, None, ns)

    def test_proppatch_noprops(self):
        """Test CoreWebDAVClient.proppatch with no defined properties."""
        ns = {"CADN": "CADN:"}
        with pytest.raises(ValueError):
            self.dav.proppatch("/foobar", None, None, ns)

    def test_delete(self):
        """Test CoreWebDAVClient.delete."""
        self.con.response.status = 200
        assert 200 == self.dav.delete("/foobar", None)

    def test_delete_collection(self):
        """Test CoreWebDAVClient.delete on collection."""
        self.con.response.status = 200
        assert 200 == self.dav.delete("/foobar/", None)

    def test_copy(self):
        """Test CoreWebDAVClient.copy."""
        self.con.response.status = 200
        source = "/foo bar/baz"
        dest = "/dest/in/ation"
        headers = {"X-Test": "Hello"}
        resp = self.dav.copy(source, dest, 0, False, headers)
        assert resp == 200

    def test_move(self):
        """Test CoreWebDAVClient.move."""
        self.con.response.status = 200
        source = "/foo bar/baz"
        dest = "/dest/in/ation"
        headers = {"X-Test": "Hello"}
        resp = self.dav.move(source, dest, 0, False, headers)
        assert resp == 200

    def test_move_collection_illegal_depth(self):
        """Test CoreWebDAVClient.move on collections with illegal depth."""
        self.con.response.status = 200
        source = "/foo bar/baz/"
        dest = "/dest/in/ation"
        with pytest.raises(ValueError):
            self.dav.move(source, dest, 0)

    def test_lock(self):
        """Test CoreWebDAVClient.lock."""
        self.con.response.status = 200
        resp = self.dav.lock("/foo")
        assert isinstance(resp, WebDAVLockResponse)
        assert resp == 200

    def test_lock_timeout(self):
        """Test CoreWebDAVClient.lock with timeout."""
        self.con.response.status = 200
        resp = self.dav.lock("/foo", timeout=12345)
        assert resp == 200

    def test_lock_timeout_inf(self):
        """Test CoreWebDAVClient.lock with infinite timeout."""
        self.con.response.status = 200
        resp = self.dav.lock("/foo", timeout="infinite")
        assert resp == 200

    def test_lock_timeout_toolong(self):
        """Test CoreWebDAVClient.lock with too long timeout."""
        with pytest.raises(ValueError):
            self.dav.lock("/foo", timeout=4294967296)

    def test_lock_timeout_err(self):
        """Test CoreWebDAVClient.lock with wrong timeout."""
        with pytest.raises(ValueError):
            self.dav.lock("/foo", timeout="abc")

    def test_lock_depth(self):
        """Test CoreWebDAVClient.lock with given depth."""
        self.con.response.status = 200
        resp = self.dav.lock("/foo", depth=0)
        assert resp == 200
        assert self.con.headers["Depth"] == "0"

    def test_lock_illegaldepth(self):
        """Test CoreWebDAVClient.lock with given illegal depth."""
        with pytest.raises(ValueError):
            self.dav.lock("/foo", depth=1)

    def test_unlock_lock(self):
        """Test CoreWebDAVClient.unlock with lock object."""
        self.dav.locks[self.lock._tag] = self.lock
        self.con.response.status = 204
        self.dav.unlock(self.lock)
        assert self.con.method == "UNLOCK"
        assert self.con.headers["Lock-Token"] == \
            "<%s>" % self.lock.locktokens[0]
        assert self.lock._tag not in self.dav.locks

    def test_unlock_uri(self):
        """Test CoreWebDAVClient.unlock with uri."""
        self.dav.locks[self.lock._tag] = self.lock
        self.con.response.status = 204
        self.dav.unlock("/")
        assert self.con.method == "UNLOCK"
        assert self.con.headers["Lock-Token"] == \
            "<%s>" % self.lock.locktokens[0]
        assert self.lock._tag not in self.dav.locks

    def test_unlock_uri_no_token(self):
        """Test CoreWebDAVClient.unlock with uri."""
        self.con.response.status = 204
        with pytest.raises(ValueError):
            self.dav.unlock("/")

    def test_unlock_lock_no_token(self):
        """Test CoreWebDAVClient.unlock with lock object and no token."""
        self.con.response.status = 204
        self.dav.unlock(self.lock)
        assert self.con.method == "UNLOCK"
        assert self.con.headers["Lock-Token"] == \
            "<%s>" % self.lock.locktokens[0]
        assert self.lock._tag not in self.dav.locks
示例#11
0
class CoreWebDAVClientTestCase(unittest.TestCase):
    """Test the CoreWebDAVClient class."""
    def setUp(self):
        """Setup the client."""
        self.dav = CoreWebDAVClient("127.0.0.1", 80)
        self.dav.setbasicauth("test", "passwd")
        self.con = Mock.HTTPConnection()
        self.dav._getconnection = lambda: self.con
        response = Mock.Response()
        response.content = LOCKDISCOVERY
        response.status = 200
        self.lock = WebDAVLockResponse(self.dav, "/", response)

    def test_preparecopymove(self):
        """Test CoreWebDAVClient._preparecopymove."""
        source = "/foo bar/baz"
        dest = "/dest/in/ation"
        headers = {"X-Test": "Hello"}
        query = {"foo": "bär"}
        http = CoreWebDAVClient("127.0.0.1", 80)
        http.setbasicauth("me", "secret")
        (source, headers) = http._preparecopymove(source, dest, 0,
                                                  False, headers)
        self.assertEqual(source, "/foo%20bar/baz")
        exp_headers = {
            "Destination": "http://127.0.0.1:80/dest/in/ation",
            "Overwrite": "F",
            "Authorization": "Basic bWU6c2VjcmV0",
            "X-Test": "=?utf-8?q?Hello?=",
        }
        self.assertEqual(headers, exp_headers)

    def test_preparecopymove_col(self):
        """Test CoreWebDAVClient._preparecopymove with collection as source."""
        source = "/foo bar/baz/"
        dest = "/dest/in/ation"
        headers = {"X-Test": "Hello"}
        query = {"foo": "bär"}
        http = CoreWebDAVClient("127.0.0.1", 80)
        http.setbasicauth("me", "secret")
        (source, headers) = http._preparecopymove(source, dest, 0,
                                                  True, headers)
        self.assertEqual(source, "/foo%20bar/baz/")
        exp_headers = {
            "Destination": "http://127.0.0.1:80/dest/in/ation",
            "Depth": "0",
            "Overwrite": "T",
            "Authorization": "Basic bWU6c2VjcmV0",
            "X-Test": "=?utf-8?q?Hello?=",
        }
        self.assertEqual(headers, exp_headers)

    def test_preparecopymove_illegal_depth(self):
        """Test CoreWebDAVClient._preparecopymove with illegal depth value."""
        source = "/foo bar/baz"
        dest = "/dest/in/ation"
        headers = {"X-Test": "Hello"}
        query = {"foo": "bär"}
        http = CoreWebDAVClient("127.0.0.1", 80)
        http.setbasicauth("me", "secret")
        self.assertRaises(
            ValueError,
            http._preparecopymove,
            source, dest, "1", False, headers
        )

    def test_mkcol(self):
        """Test CoreWebDAVClient.mkcol."""
        # prepare mock connection
        self.con.response.status = 201
        self.assertEqual(self.dav.mkcol("/foobar"), 201)
        self.assertEqual(self.con.method, "MKCOL")
        self.assertEqual(self.con.path, "/foobar")
        self.assertTrue(self.con.closed)
        self.assertTrue("Authorization" in self.con.headers)

    def test_propfind(self):
        """Test CoreWebDAVClient.propfind."""
        # prepare mock connection
        self.con.response.status = 207
        self.con.response.content = MULTISTATUS
        self.assertEqual(self.dav.propfind("/foobar"), 207)
        self.assertEqual(self.con.method, "PROPFIND")
        self.assertEqual(self.con.path, "/foobar")
        self.assertEqual(self.con.headers["Depth"], "0")
        self.assertTrue(self.con.closed)
        self.assertTrue("Authorization" in self.con.headers)

    def test_propfind_depth_1(self):
        """Test CoreWebDAVClient.propfind with depth 1."""
        # prepare mock connection
        self.con.response.status = 207
        self.con.response.content = MULTISTATUS
        self.assertEqual(self.dav.propfind("/foobar", "1"), 207)
        self.assertEqual(self.con.method, "PROPFIND")
        self.assertEqual(self.con.path, "/foobar")
        self.assertEqual(self.con.headers["Depth"], "1")
        self.assertTrue(self.con.closed)
        self.assertTrue("Authorization" in self.con.headers)

    def test_propfind_illegal_depth(self):
        """Test CoreWebDAVClient.propfind with illegal depth."""
        # prepare mock connection
        self.assertRaises(ValueError, self.dav.propfind, "/foobar", "ABC")

    def test_propfind_illegal_args(self):
        """Test CoreWebDAVClient.propfind with illegal args."""
        # prepare mock connection
        self.assertRaises(ValueError,
                          self.dav.propfind, "/foobar", 1,
                          properties=["foo"], include=["bar"])

    def test_put(self):
        """Test CoreWebDAVClient.put."""
        # prepare mock connection
        self.con.response.status = 201
        self.con.response.content = "Test content."
        self.assertEqual(self.dav.put("/foobar", self.con.response), 201)
        self.assertEqual(self.con.method, "PUT")
        self.assertEqual(self.con.path, "/foobar")
        if PYTHONVERSION == (2, 5):
            self.assertEqual(self.con.body, "Test content.")
        else:
            self.assertEqual(self.con.body, self.con.response)
        self.assertTrue(self.con.closed)
        self.assertTrue("Authorization" in self.con.headers)

    def test_proppatch(self):
        """Test CoreWebDAVClient.proppatch."""
        self.con.response.status = 207
        self.con.response.content = MULTISTATUS
        props = {"CADN:author": "me", "CADN:created": "2009-09-09 13:31"}
        ns = {"CADN": "CADN:"}
        self.assertEqual(207, self.dav.proppatch("/foobar", props, None, ns))

    def test_proppatch_noprops(self):
        """Test CoreWebDAVClient.proppatch with no defined properties."""
        ns = {"CADN": "CADN:"}
        self.assertRaises(ValueError,
                          self.dav.proppatch, "/foobar", None, None, ns)

    def test_delete(self):
        """Test CoreWebDAVClient.delete."""
        self.con.response.status = 200
        self.assertEqual(200, self.dav.delete("/foobar", None))

    def test_delete_collection(self):
        """Test CoreWebDAVClient.delete on collection."""
        self.con.response.status = 200
        self.assertEqual(200, self.dav.delete("/foobar/", None))

    def test_copy(self):
        """Test CoreWebDAVClient.copy."""
        self.con.response.status = 200
        source = "/foo bar/baz"
        dest = "/dest/in/ation"
        headers = {"X-Test": "Hello"}
        resp = self.dav.copy(source, dest, 0, False, headers)
        self.assertEqual(resp, 200)

    def test_move(self):
        """Test CoreWebDAVClient.move."""
        self.con.response.status = 200
        source = "/foo bar/baz"
        dest = "/dest/in/ation"
        headers = {"X-Test": "Hello"}
        resp = self.dav.move(source, dest, 0, False, headers)
        self.assertEqual(resp, 200)

    def test_move_collection_illegal_depth(self):
        """Test CoreWebDAVClient.move on collections with illegal depth."""
        self.con.response.status = 200
        source = "/foo bar/baz/"
        dest = "/dest/in/ation"
        headers = {"X-Test": "Hello"}
        self.assertRaises(
            ValueError,
            self.dav.move,
            source, dest, 0
        )

    def test_lock(self):
        """Test CoreWebDAVClient.lock."""
        self.con.response.status = 200
        resp = self.dav.lock("/foo")
        self.assertTrue(isinstance(resp, WebDAVLockResponse))
        self.assertEqual(resp, 200)

    def test_lock_timeout(self):
        """Test CoreWebDAVClient.lock with timeout."""
        self.con.response.status = 200
        resp = self.dav.lock("/foo", timeout=12345)
        self.assertEqual(resp, 200)

    def test_lock_timeout_inf(self):
        """Test CoreWebDAVClient.lock with infinite timeout."""
        self.con.response.status = 200
        resp = self.dav.lock("/foo", timeout="infinite")
        self.assertEqual(resp, 200)

    def test_lock_timeout_toolong(self):
        """Test CoreWebDAVClient.lock with too long timeout."""
        self.assertRaises(
            ValueError,
            self.dav.lock,
            "/foo",
            timeout=4294967296
        )

    def test_lock_timeout_err(self):
        """Test CoreWebDAVClient.lock with wrong timeout."""
        self.assertRaises(
            ValueError,
            self.dav.lock,
            "/foo",
            timeout="abc"
        )

    def test_lock_depth(self):
        """Test CoreWebDAVClient.lock with given depth."""
        self.con.response.status = 200
        resp = self.dav.lock("/foo", depth=0)
        self.assertEqual(resp, 200)
        self.assertEqual(self.con.headers["Depth"], "0")

    def test_lock_illegaldepth(self):
        """Test CoreWebDAVClient.lock with given illegal depth."""
        self.assertRaises(
            ValueError,
            self.dav.lock,
            "/foo",
            depth=1
        )

    def test_unlock_lock(self):
        """Test CoreWebDAVClient.unlock with lock object."""
        self.dav.locks[self.lock._tag] = self.lock
        self.con.response.status = 204
        resp = self.dav.unlock(self.lock)
        self.assertEqual(self.con.method, "UNLOCK")
        self.assertEqual(self.con.headers["Lock-Token"],
                         "<%s>" % self.lock.locktokens[0])
        self.assertTrue(self.lock._tag not in self.dav.locks)

    def test_unlock_uri(self):
        """Test CoreWebDAVClient.unlock with uri."""
        self.dav.locks[self.lock._tag] = self.lock
        self.con.response.status = 204
        resp = self.dav.unlock("/")
        self.assertEqual(self.con.method, "UNLOCK")
        self.assertEqual(self.con.headers["Lock-Token"],
                         "<%s>" % self.lock.locktokens[0])
        self.assertTrue(self.lock._tag not in self.dav.locks)

    def test_unlock_uri_no_token(self):
        """Test CoreWebDAVClient.unlock with uri."""
        self.con.response.status = 204
        self.assertRaises(ValueError, self.dav.unlock, "/")

    def test_unlock_lock_no_token(self):
        """Test CoreWebDAVClient.unlock with lock object and no token."""
        self.con.response.status = 204
        resp = self.dav.unlock(self.lock)
        self.assertEqual(self.con.method, "UNLOCK")
        self.assertEqual(self.con.headers["Lock-Token"],
                         "<%s>" % self.lock.locktokens[0])
        self.assertTrue(self.lock._tag not in self.dav.locks)