Files
wallabag-api-client/wallabag_api/test_wallabag.py
2026-03-02 18:19:00 +01:00

323 lines
10 KiB
Python

# coding: utf-8
import asyncio
import configparser
import datetime
import os
import sys
import uuid
import pytest
import aiohttp
from wallabag_api.wallabag import Wallabag
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_PATH = os.path.join(ROOT_DIR, "testcfg.ini")
def load_test_config(path):
config = configparser.ConfigParser()
config.read(path)
return config["Test Configuration"]
class TestWallabag:
entry_data = {
"title": "foobar title",
"url": "https://somwhere.over.the.raibow.com/",
"tags": ["foo", "bar"],
"starred": 0,
"archive": 0,
"content": "<p>Additional content</p>",
"language": "FR",
"published_at": datetime.datetime.now(),
"authors": "John Doe",
"public": 0,
"original_url": "http://localhost",
}
@pytest.fixture(scope="function")
async def configuration(self):
return load_test_config(CONFIG_PATH)
@pytest.fixture(scope="function")
async def setup_class(self, configuration):
access_token = await Wallabag.get_token(**configuration)
async with aiohttp.ClientSession() as session:
wallabag = Wallabag(
host=configuration["host"],
token=access_token,
client_id=configuration["client_id"],
client_secret=configuration["client_secret"],
aio_sess=session,
)
entry = await wallabag.post_entries(**self.entry_data)
yield wallabag, entry
await wallabag.delete_entries(entry["id"])
@pytest.mark.asyncio
async def test_get_token(self, configuration):
data = await Wallabag.get_token(**configuration)
print(data, file=sys.stderr)
assert isinstance(data, str)
@pytest.mark.asyncio
async def test_get_entries(self, setup_class):
wallabag, _ = setup_class
data = await wallabag.get_entries(
**{
"delete": 0,
"sort": "created",
"order": "desc",
"page": 1,
"perPage": 30,
"tags": [],
}
)
assert isinstance(data, dict)
@pytest.mark.asyncio
async def test_get_entries_non_existing_page(self, setup_class):
wallabag, _ = setup_class
data = await wallabag.get_entries(
**{
"delete": 0,
"sort": "created",
"order": "desc",
"page": 10,
"perPage": 30,
"tags": [],
}
)
assert isinstance(data, dict)
@pytest.mark.asyncio
async def test_get_entry(self, setup_class):
wallabag, entry = setup_class
entry_id = entry["id"]
assert isinstance(entry_id, int)
data = await wallabag.get_entry(entry_id)
assert isinstance(data, dict)
@pytest.mark.asyncio
async def test_get_entry_tags(self, setup_class):
wallabag, entry = setup_class
entry_id = entry["id"]
assert isinstance(entry_id, int)
data = await wallabag.get_entry_tags(entry_id)
tag_names = [t["label"] for t in data]
assert isinstance(data, list)
assert self.entry_data["tags"] == tag_names
@pytest.mark.asyncio
async def test_get_tags(self, setup_class):
wallabag, _ = setup_class
data = await wallabag.get_tags()
assert isinstance(data, list)
@pytest.mark.asyncio
async def test_post_entries(self, setup_class):
wallabag, _ = setup_class
entry_data = self.entry_data.copy()
entry_data["url"] = "https://example.org"
data = await wallabag.post_entries(**entry_data)
assert data
assert isinstance(data["id"], int)
await wallabag.delete_entries(data["id"])
@pytest.mark.asyncio
async def test_patch_entries(self, setup_class):
wallabag, entry = setup_class
entry_id = entry["id"]
assert isinstance(entry_id, int)
data = await wallabag.patch_entries(
entry_id,
**{
"title": "I change the title",
"archive": 0,
"tags": ["bimbo", "pipo"],
"order": "asc",
"star": 0,
"delete": 0,
},
)
assert data
@pytest.mark.asyncio
async def test_delete_entries(self, setup_class):
wallabag, _ = setup_class
entry_id = await self._create_entry(wallabag)
assert isinstance(entry_id, int)
data = await wallabag.delete_entries(entry_id)
assert data
@pytest.mark.asyncio
async def test_post_entry_tags(self, setup_class):
wallabag, entry = setup_class
entry_id = entry["id"]
assert isinstance(entry_id, int)
tags = ["foo", "bar"]
assert isinstance(tags, list)
data = await wallabag.post_entry_tags(entry_id, tags)
assert data
@pytest.mark.asyncio
async def test_delete_entry_tag(self, setup_class):
wallabag, _ = setup_class
entry_id = await self._create_entry(wallabag)
tag_id = 1
assert isinstance(entry_id, int)
assert isinstance(tag_id, int)
resp = await wallabag.delete_entry_tag(entry_id, tag_id)
assert isinstance(resp["tags"], list)
resp_tags_id = [t["id"] for t in resp["tags"]]
assert len(resp_tags_id) == 1
assert tag_id not in resp_tags_id
await self._delete_entry(wallabag, entry_id)
@pytest.mark.asyncio
async def test_put_annotations(self, setup_class):
wallabag, entry = setup_class
entry_id = entry["id"]
annotation = await wallabag.post_annotations(
entry_id,
ranges=[
{"start": "/p[1]", "startOffset": 0, "end": "/p[1]", "endOffset": 10}
],
quote="test quote",
text="test annotation",
)
annotation_id = annotation["id"]
data = await wallabag.put_annotations(annotation_id)
assert data is not None
@pytest.mark.asyncio
async def test_reload_entry(self, setup_class):
wallabag, entry = setup_class
entry_id = entry["id"]
assert isinstance(entry_id, int)
try:
data = await wallabag.patch_entry_reload(entry_id)
assert data
except Exception as e:
if "304" in str(e):
pass
else:
raise
@pytest.mark.asyncio
async def test_entries_exists(self, setup_class):
wallabag, entry = setup_class
entry_url = entry["url"]
data = await wallabag.entries_exists(url=entry_url)
if isinstance(data, dict) and "exists" in data:
assert data["exists"] is True
data = await wallabag.entries_exists(url="https://nonexistent.example.com")
if isinstance(data, dict) and "exists" in data:
assert data["exists"] is False
@pytest.mark.asyncio
async def test_delete_tag(self, setup_class):
wallabag, entry = setup_class
entry_id = entry["id"]
tags = ["tag_for_deletion"]
await wallabag.post_entry_tags(entry_id, tags)
all_tags = await wallabag.get_tags()
tag_to_delete = next(
(t for t in all_tags if t["label"] == "tag_for_deletion"), None
)
if tag_to_delete:
data = await wallabag.delete_tag(tag_to_delete["id"])
assert data
@pytest.mark.asyncio
async def test_delete_tag_label(self, setup_class):
wallabag, entry = setup_class
entry_id = entry["id"]
tags = ["label_for_deletion"]
await wallabag.post_entry_tags(entry_id, tags)
data = await wallabag.delete_tag_label("label_for_deletion")
assert data
@pytest.mark.asyncio
async def test_delete_tags_label(self, setup_class):
wallabag, entry = setup_class
entry_id = entry["id"]
tags = ["multi_delete_1", "multi_delete_2"]
await wallabag.post_entry_tags(entry_id, tags)
data = await wallabag.delete_tags_label(tags)
assert data is not None
@pytest.mark.asyncio
async def test_get_annotations(self, setup_class):
wallabag, entry = setup_class
entry_id = entry["id"]
data = await wallabag.get_annotations(entry_id)
assert isinstance(data, (dict, list))
if isinstance(data, dict):
assert "rows" in data or "total" in data
@pytest.mark.asyncio
async def test_post_annotations(self, setup_class):
wallabag, entry = setup_class
entry_id = entry["id"]
data = await wallabag.post_annotations(
entry_id,
ranges=[
{"start": "/p[1]", "startOffset": 0, "end": "/p[1]", "endOffset": 10}
],
quote="test quote",
text="test annotation",
)
assert data
assert "id" in data
return data["id"]
@pytest.mark.asyncio
async def test_put_annotations(self, setup_class):
wallabag, entry = setup_class
entry_id = entry["id"]
annotation = await wallabag.post_annotations(
entry_id,
ranges=[
{"start": "/p[1]", "startOffset": 0, "end": "/p[1]", "endOffset": 10}
],
quote="test quote",
text="test annotation",
)
annotation_id = annotation["id"]
data = await wallabag.put_annotations(annotation_id)
assert data is not None
@pytest.mark.asyncio
async def test_delete_annotations(self, setup_class):
wallabag, entry = setup_class
entry_id = entry["id"]
annotation = await wallabag.post_annotations(
entry_id,
ranges=[
{"start": "/p[1]", "startOffset": 0, "end": "/p[1]", "endOffset": 10}
],
quote="test quote",
text="test annotation",
)
annotation_id = annotation["id"]
data = await wallabag.delete_annotations(annotation_id)
assert data
@pytest.mark.asyncio
async def test_version(self, setup_class):
wallabag, _ = setup_class
data = await wallabag.version
assert data
async def _create_entry(self, wallabag):
data = self.entry_data.copy()
data["title"] = "Some title 2"
data["url"] = f"https://somwhere.over.the.raibow.com/{uuid.uuid4()}"
entry = await wallabag.post_entries(**data)
assert isinstance(entry["id"], int)
return entry["id"]
async def _delete_entry(self, wallabag, entry_id):
await wallabag.delete_entries(entry_id)