| 1 |
"""Tests for the resource module.""" |
| 2 |
|
| 3 |
import io |
| 4 |
from pathlib import Path |
| 5 |
from typing import Any |
| 6 |
|
| 7 |
from aioresponses import aioresponses |
| 8 |
|
| 9 |
from aiowebdav2.client import Client |
| 10 |
from aiowebdav2.models import PropertyRequest |
| 11 |
|
| 12 |
from . import load_responses |
| 13 |
|
| 14 |
|
| 15 |
async def test_resource_is_dir_and_info( |
| 16 |
client: Client, responses: aioresponses |
| 17 |
) -> None: |
| 18 |
"""Test resource is dir and info.""" |
| 19 |
responses.add( |
| 20 |
"https://webdav.example.com/test_dir/", |
| 21 |
"PROPFIND", |
| 22 |
headers={"Accept": "*/*", "Depth": "0"}, |
| 23 |
content_type="application/xml", |
| 24 |
status=200, |
| 25 |
body=load_responses("is_dir_directory.xml"), |
| 26 |
) |
| 27 |
responses.add( |
| 28 |
"https://webdav.example.com/test_dir/test.txt", |
| 29 |
"PROPFIND", |
| 30 |
headers={"Accept": "*/*", "Depth": "1"}, |
| 31 |
content_type="application/xml", |
| 32 |
status=200, |
| 33 |
body=load_responses("get_info.xml"), |
| 34 |
) |
| 35 |
|
| 36 |
dir_resource = client.resource("/test_dir/") |
| 37 |
assert await dir_resource.is_dir() |
| 38 |
|
| 39 |
file_resource = client.resource("/test_dir/test.txt") |
| 40 |
info = await file_resource.info(params={"name": ""}) |
| 41 |
assert info == {"name": "test.txt"} |
| 42 |
|
| 43 |
|
| 44 |
async def test_resource_rename_move_copy( |
| 45 |
client: Client, responses: aioresponses |
| 46 |
) -> None: |
| 47 |
"""Test resource rename move copy.""" |
| 48 |
responses.add( |
| 49 |
"https://webdav.example.com/test_dir/test.txt", |
| 50 |
"MOVE", |
| 51 |
headers={ |
| 52 |
"Accept": "*/*", |
| 53 |
"Destination": "https://webdav.example.com/test_dir/test2.txt", |
| 54 |
"Overwrite": "F", |
| 55 |
}, |
| 56 |
status=201, |
| 57 |
) |
| 58 |
responses.add( |
| 59 |
"https://webdav.example.com/test_dir/test2.txt", |
| 60 |
"MOVE", |
| 61 |
headers={ |
| 62 |
"Accept": "*/*", |
| 63 |
"Destination": "https://webdav.example.com/test_dir/test3.txt", |
| 64 |
"Overwrite": "F", |
| 65 |
}, |
| 66 |
status=201, |
| 67 |
) |
| 68 |
responses.add( |
| 69 |
"https://webdav.example.com/test_dir/test3.txt", |
| 70 |
"COPY", |
| 71 |
headers={ |
| 72 |
"Accept": "*/*", |
| 73 |
"Destination": "https://webdav.example.com/test_dir/test4.txt", |
| 74 |
}, |
| 75 |
status=201, |
| 76 |
) |
| 77 |
responses.add( |
| 78 |
"https://webdav.example.com/test_dir/test3.txt", |
| 79 |
"PROPFIND", |
| 80 |
headers={"Accept": "*/*", "Depth": "0"}, |
| 81 |
content_type="application/xml", |
| 82 |
status=200, |
| 83 |
body=load_responses("is_dir_file.xml").replace( |
| 84 |
"/test_dir/test.txt", "/test_dir/test3.txt" |
| 85 |
), |
| 86 |
) |
| 87 |
|
| 88 |
resource = client.resource("/test_dir/test.txt") |
| 89 |
await resource.rename("test2.txt") |
| 90 |
assert resource.urn.path() == "/test_dir/test2.txt" |
| 91 |
|
| 92 |
await resource.move("/test_dir/test3.txt") |
| 93 |
assert resource.urn.path() == "/test_dir/test3.txt" |
| 94 |
|
| 95 |
new_resource = await resource.copy("/test_dir/test4.txt") |
| 96 |
assert new_resource.urn.path() == "/test_dir/test4.txt" |
| 97 |
|
| 98 |
|
| 99 |
async def test_resource_clean_check_publish_unpublish( |
| 100 |
client: Client, responses: aioresponses |
| 101 |
) -> None: |
| 102 |
"""Test resource clean check publish unpublish.""" |
| 103 |
responses.add( |
| 104 |
"https://webdav.example.com/test_dir/test.txt", |
| 105 |
"DELETE", |
| 106 |
status=204, |
| 107 |
) |
| 108 |
responses.add( |
| 109 |
"https://webdav.example.com/test_dir/test.txt", |
| 110 |
"PROPFIND", |
| 111 |
headers={"Accept": "*/*", "Depth": "0"}, |
| 112 |
content_type="application/xml", |
| 113 |
status=200, |
| 114 |
body=load_responses("is_dir_file.xml"), |
| 115 |
) |
| 116 |
responses.add( |
| 117 |
"https://webdav.example.com/test_dir/test.txt", |
| 118 |
"PROPPATCH", |
| 119 |
headers={"Accept": "*/*", "Depth": "0"}, |
| 120 |
status=207, |
| 121 |
) |
| 122 |
responses.add( |
| 123 |
"https://webdav.example.com/test_dir/test.txt", |
| 124 |
"PROPPATCH", |
| 125 |
headers={"Accept": "*/*", "Depth": "0"}, |
| 126 |
status=207, |
| 127 |
) |
| 128 |
|
| 129 |
resource = client.resource("/test_dir/test.txt") |
| 130 |
await resource.clean() |
| 131 |
assert await resource.check() |
| 132 |
await resource.publish() |
| 133 |
await resource.unpublish() |
| 134 |
|
| 135 |
|
| 136 |
async def test_resource_read_write_paths( |
| 137 |
client: Client, responses: aioresponses, tmp_path: Any |
| 138 |
) -> None: |
| 139 |
"""Test resource read write paths.""" |
| 140 |
responses.add( |
| 141 |
"https://webdav.example.com/test_dir/test.txt", |
| 142 |
"GET", |
| 143 |
status=200, |
| 144 |
body=b"file content", |
| 145 |
headers={"content-length": "12"}, |
| 146 |
) |
| 147 |
responses.add( |
| 148 |
"https://webdav.example.com/test_dir/test.txt", |
| 149 |
"PUT", |
| 150 |
headers={"Accept": "*/*"}, |
| 151 |
status=201, |
| 152 |
) |
| 153 |
|
| 154 |
local_path = Path(tmp_path) / "test.txt" |
| 155 |
resource = client.resource("/test_dir/test.txt") |
| 156 |
await resource.read(local_path=local_path) |
| 157 |
assert (local_path.read_bytes()) == b"file content" |
| 158 |
|
| 159 |
local_path.write_bytes(b"upload") |
| 160 |
await resource.write(local_path=local_path) |
| 161 |
|
| 162 |
|
| 163 |
async def test_resource_read_write_buffers( |
| 164 |
client: Client, responses: aioresponses |
| 165 |
) -> None: |
| 166 |
"""Test resource read write buffers.""" |
| 167 |
responses.add( |
| 168 |
"https://webdav.example.com/test_dir/test.txt", |
| 169 |
"GET", |
| 170 |
status=200, |
| 171 |
body=b"buffer content", |
| 172 |
headers={"content-length": "14"}, |
| 173 |
) |
| 174 |
responses.add( |
| 175 |
"https://webdav.example.com/test_dir/test.txt", |
| 176 |
"PUT", |
| 177 |
headers={"Accept": "*/*"}, |
| 178 |
status=201, |
| 179 |
) |
| 180 |
|
| 181 |
resource = client.resource("/test_dir/test.txt") |
| 182 |
buff = io.BytesIO() |
| 183 |
await resource.read_from(buff) |
| 184 |
assert buff.getvalue() == b"buffer content" |
| 185 |
|
| 186 |
await resource.write_to(buff=io.BytesIO(b"upload")) |
| 187 |
|
| 188 |
|
| 189 |
async def test_resource_get_set_property( |
| 190 |
client: Client, responses: aioresponses |
| 191 |
) -> None: |
| 192 |
"""Test resource get set property.""" |
| 193 |
responses.add( |
| 194 |
"https://webdav.example.com/test_dir/test.txt", |
| 195 |
"PROPFIND", |
| 196 |
headers={"Accept": "*/*", "Depth": "0"}, |
| 197 |
content_type="application/xml", |
| 198 |
status=200, |
| 199 |
body=load_responses("get_property.xml"), |
| 200 |
) |
| 201 |
responses.add( |
| 202 |
"https://webdav.example.com/test_dir/test.txt", |
| 203 |
"PROPPATCH", |
| 204 |
headers={"Accept": "*/*"}, |
| 205 |
status=207, |
| 206 |
) |
| 207 |
|
| 208 |
resource = client.resource("/test_dir/test.txt") |
| 209 |
prop = await resource.get_property( |
| 210 |
PropertyRequest(namespace="test", name="aProperty") |
| 211 |
) |
| 212 |
assert prop is not None |
| 213 |
await resource.set_property(name="aProperty", value="aValue", namespace="test") |