187 lines
5.9 KiB
Python
187 lines
5.9 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)
|
|
|
|
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)
|