-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_plugin_downloadfileproperty.py
78 lines (67 loc) · 2.69 KB
/
test_plugin_downloadfileproperty.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import hashlib
from unittest.mock import MagicMock, patch
from n2y.notion import Client
from n2y.notion_mocks import mock_page, mock_property_value, mock_user
from n2y.page import Page
from n2y.plugins.downloadfileproperty import DownloadFilePropertyValue
from n2y.user import User
from n2y.utils import slugify
@patch("n2y.notion.Client.wrap_notion_user")
def mock_page_with_file_property(tmp_dir: str, mk_wrap_notion_user: MagicMock) -> Page:
client = Client(
"",
plugins=["n2y.plugins.downloadfileproperty"],
media_root=tmp_dir,
media_url="http://foo.com/",
)
mk_wrap_notion_user.return_value = User(client, mock_user())
file_prop = mock_property_value(
"files",
[
{
"name": "Notion Developer Image",
"type": "external",
"external": {
"url": "https://files.readme.io/a267aac-notion-devs-logo.svg"
},
}
],
)
notion_page = mock_page(extra_properties={"Files": file_prop})
page = Page(client, notion_page)
page._children = []
return page
def test_downloadfileproperty_class_is_used(tmpdir):
page = mock_page_with_file_property(tmpdir)
file_prop = page.properties["Files"]
assert isinstance(file_prop, DownloadFilePropertyValue)
def test_file_is_downloaded(tmpdir):
page = mock_page_with_file_property(tmpdir)
file_prop: DownloadFilePropertyValue = page.properties["Files"]
urls: list[str] = file_prop.to_value(None, None)
assert len(urls) == 1
assert urls[0].startswith("http://foo.com/")
assert urls[0].endswith(".svg")
assert len(tmpdir.listdir()) == 1
assert tmpdir.listdir()[0].ext == ".svg"
assert tmpdir.listdir()[0].size() > 0
def test_filename_format(tmpdir):
page = mock_page_with_file_property(tmpdir)
file_prop: DownloadFilePropertyValue = page.properties["Files"]
urls: list[str] = file_prop.to_value(None, None)
assert len(urls) == 1
url_path = urls[0]
file_path = str(tmpdir.listdir()[0])
page_title_slug = slugify(page.title.to_plain_text())
with open(file_path, "rb") as f:
file_content = f.read()
file_hash = file_hash = hashlib.sha256(file_content).hexdigest()[:10]
assert url_path.endswith(f"{page_title_slug}-{file_hash}.svg")
assert file_path.endswith(f"{page_title_slug}-{file_hash}.svg")
def test_file_naming_is_consistent(tmpdir):
page = mock_page_with_file_property(tmpdir)
file_prop: DownloadFilePropertyValue = page.properties["Files"]
urls: list[str] = file_prop.to_value(None, None)
urls_2: list[str] = file_prop.to_value(None, None)
assert urls == urls_2
assert len(tmpdir.listdir()) == 1