Fixes API unit tests

Fix all unit tests, and use a `testcfg.ini` file to load test
configuration values (e.g. api_client, url, ...)
This commit is contained in:
2020-03-03 22:50:07 +01:00
parent 8d1e10a6eb
commit 91217cfa55
4 changed files with 164 additions and 96 deletions

2
.gitignore vendored
View File

@@ -6,3 +6,5 @@
*.json *.json
.tox .tox
build build
__pycache__/

6
wallabag_api/testcfg.ini Normal file
View File

@@ -0,0 +1,6 @@
[Test Configuration]
grant_type = password
client_id =
client_secret =
username =
password =

View File

@@ -380,7 +380,7 @@ class Wallabag(object):
Permanently remove one tag for an entry Permanently remove one tag for an entry
:param entry: \w+ an integer The Entry ID :param entry: \w+ an integer The Entry ID
:param tag: string The Tag :param tag: integer The Tag ID
:return data related to the ext :return data related to the ext
""" """
params = {'access_token': self.token} params = {'access_token': self.token}

View File

@@ -1,121 +1,181 @@
# coding: utf-8 # coding: utf-8
import asyncio
import configparser
import datetime import datetime
import os
import sys
import uuid
import unittest import unittest
from wallabag import Wallabag
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"]
def run_async(f):
def wrapper(*args, **kwargs):
coro = asyncio.coroutine(f)
future = coro(*args, **kwargs)
loop = asyncio.get_event_loop()
return loop.run_until_complete(future)
return wrapper
class TestWallabag(unittest.TestCase): class TestWallabag(unittest.TestCase):
host = 'http://localhost:8000' entry_data = {
client_id = '' "title": "foobar title",
client_secret = '' "url": "https://somwhere.over.the.raibow.com/",
token = '' "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",
}
def setUp(self): @classmethod
access_token = self.test_get_token() @run_async
self.format = 'json' async def setUpClass(cls):
self.w = Wallabag(host=self.host, cls._configuration = dict(load_test_config(CONFIG_PATH))
token=access_token, cls.access_token = await Wallabag.get_token(**cls._configuration)
client_id=self.client_id, # Open connection to wallabag
client_secret=self.client_secret) cls.w = Wallabag(
host=cls._configuration["host"],
token=cls.access_token,
client_id=cls._configuration["client_id"],
client_secret=cls._configuration["client_secret"],
aio_sess=aiohttp.ClientSession(),
)
# Create an entry for the tests
cls.entry = await cls.w.post_entries(**cls.entry_data)
def test_get_token(self): @classmethod
params = {"grant_type": "password", def tearDownClass(cls):
"client_id": asyncio.get_event_loop().run_until_complete(
'1_4wqe1riwt0qoks844kwc4go08koogkgk88go4cckkwg0408kcg', cls.w.delete_entries(cls.entry["id"])
"client_secret": '4mzw3qwi1xyc0cks4k80s4c8kco40wwkkkw0g40kwk4o4c44co', )
"username": 'wallabag', asyncio.get_event_loop().run_until_complete(cls.w.aio_sess.close())
"password": 'wallabag'}
print(self.host)
data = Wallabag.get_token(host=self.host, **params)
print(data)
self.assertTrue(isinstance(data, str), True)
return data
def create_entry(self): @run_async
title = 'foobar title' async def test_get_token(self):
url = 'https://somwhere.over.the.raibow.com/' data = await Wallabag.get_token(**self._configuration)
tags = ['foo', 'bar'] print(data, file=sys.stderr)
starred = 0 self.assertIsInstance(data, str)
archive = 0
content = '<p>Additional content</p>'
language = 'FR'
published_at = datetime.datetime.now()
authors = 'John Doe'
public = 0
original_url = 'http://localhost'
data = self.w.post_entries(url, title, tags, starred, archive, content, language, published_at, authors,
public, original_url)
return data @run_async
async def test_get_entries(self):
def test_get_entries(self): data = await self.w.get_entries(
params = {'delete': 0, **{
'sort': 'created', "delete": 0,
'order': 'desc', "sort": "created",
'page': 1, "order": "desc",
'perPage': 30, "page": 1,
'tags': []} "perPage": 30,
data = self.w.get_entries(**params) "tags": [],
}
)
self.assertIsInstance(data, dict) self.assertIsInstance(data, dict)
def test_get_entry(self): @run_async
entry = 1 async def test_get_entry(self):
self.assertTrue(isinstance(entry, int), True) entry_id = self.entry["id"]
data = self.w.get_entry(entry) self.assertIsInstance(entry_id, int)
self.assertTrue(data, str) data = await self.w.get_entry(entry_id)
self.assertIsInstance(data, dict)
def test_get_entry_tags(self): @run_async
entry = 1 async def test_get_entry_tags(self):
self.assertTrue(isinstance(entry, int), True) entry_id = self.entry["id"]
data = self.w.get_entry_tags(entry) self.assertIsInstance(entry_id, int)
data = await self.w.get_entry_tags(entry_id)
tag_names = [t["label"] for t in data]
self.assertIsInstance(data, list)
self.assertEquals(self.entry_data["tags"], tag_names)
@run_async
async def test_get_tags(self):
data = await self.w.get_tags()
self.assertIsInstance(data, list) self.assertIsInstance(data, list)
def test_get_tags(self): @run_async
data = self.w.get_tags() async def test_post_entries(self):
self.assertIsInstance(data, list) entry_data = self.entry_data.copy()
entry_data["url"] = "https://example.org"
data = await self.w.post_entries(**entry_data)
self.assertTrue(data, True)
self.assertIsInstance(data["id"], int)
await self.w.delete_entries(data["id"])
def test_post_entries(self): @run_async
data = self.create_entry() async def test_patch_entries(self):
entry_id = self.entry["id"]
self.assertIsInstance(entry_id, int)
data = await self.w.patch_entries(
entry_id,
**{
"title": "I change the title",
"archive": 0,
"tags": ["bimbo", "pipo"],
"order": "asc",
"star": 0,
"delete": 0,
},
)
self.assertTrue(data)
@run_async
async def test_delete_entries(self):
entry_id = await self._create_entry()
self.assertIsInstance(entry_id, int)
data = await self.w.delete_entries(entry_id)
self.assertTrue(data, True) self.assertTrue(data, True)
def test_patch_entries(self): @run_async
entry = 1 async def test_post_entry_tags(self):
params = {'title': 'I change the title', entry_id = self.entry["id"]
'archive': 0, self.assertIsInstance(entry_id, int)
'tags': ["bimbo", "pipo"], tags = ["foo", "bar"]
'order': 'asc', self.assertIsInstance(tags, list)
'star': 0, data = await self.w.post_entry_tags(entry_id, tags)
'delete': 0}
self.assertTrue(isinstance(entry, int), True)
self.assertTrue(isinstance(params, dict), True)
data = self.w.patch_entries(entry, **params)
self.assertTrue(data, True) self.assertTrue(data, True)
def test_delete_entries(self): @run_async
entry = self.create_entry() async def test_delete_entry_tag(self):
self.assertTrue(isinstance(entry['id'], int), True) entry_id = await self._create_entry()
data = self.w.delete_entries(entry['id']) tag_id = 1
self.assertTrue(data, True) self.assertIsInstance(entry_id, int)
self.assertIsInstance(tag_id, int)
resp = await self.w.delete_entry_tag(entry_id, tag_id)
self.assertIsInstance(resp["tags"], list)
resp_tags_id = [t["id"] for t in resp["tags"]]
self.assertEquals(1, len(resp_tags_id))
self.assertFalse(tag_id in resp_tags_id)
await self._delete_entry(entry_id)
def test_post_entry_tags(self): async def _create_entry(self):
entry = 1 data = self.entry_data.copy()
self.assertTrue(isinstance(entry, int), True) data["title"] = "Some title 2"
tags = ['foo', 'bar'] data["url"] = f"https://somwhere.over.the.raibow.com/{uuid.uuid4()}"
self.assertTrue(isinstance(tags, list), True) entry = await self.w.post_entries(**data)
data = self.w.post_entry_tags(entry, tags) self.assertIsInstance(entry["id"], int)
self.assertTrue(data, True) return entry["id"]
""" async def _delete_entry(self, entry_id):
def test_delete_entry_tag(self): await self.w.delete_entries(entry_id)
entry = self.create_entry()
tag = 'bar'
self.assertTrue(isinstance(entry['id'], int), True)
self.assertTrue(isinstance(tag, str), True)
resp = self.w.delete_entry_tag(entry['id'], tag)
self.assertTrue(resp, True)
"""
if __name__ == '__main__': if __name__ == "__main__":
unittest.main() unittest.main()