You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
stacosys/tests/test_api.py

65 lines
1.6 KiB
Python

3 years ago
#!/usr/bin/python
# -*- coding: UTF-8 -*-
3 years ago
import json
3 years ago
import logging
import pytest
3 years ago
from stacosys.db import database, dao
3 years ago
from stacosys.interface import api
from stacosys.interface import app
3 years ago
def init_test_db():
c1 = dao.create_comment("/site1", "Bob", "/bob.site", "", "comment 1")
c2 = dao.create_comment("/site2", "Bill", "/bill.site", "", "comment 2")
c3 = dao.create_comment("/site3", "Jack", "/jack.site", "", "comment 3")
dao.publish_comment(c1)
dao.publish_comment(c3)
3 years ago
@pytest.fixture
def client():
logger = logging.getLogger(__name__)
database.setup(":memory:")
3 years ago
init_test_db()
3 years ago
app.config.update(SITE_TOKEN="ETC")
logger.info(f"start interface {api}")
return app.test_client()
def test_api_ping(client):
resp = client.get('/api/ping')
3 years ago
assert resp.data == b"OK"
def test_api_count_global(client):
resp = client.get('/api/comments/count')
3 years ago
d = json.loads(resp.data)
assert d and d['count'] == 2
def test_api_count_url(client):
resp = client.get('/api/comments/count?url=/site1')
3 years ago
d = json.loads(resp.data)
assert d and d['count'] == 1
resp = client.get('/api/comments/count?url=/site2')
3 years ago
d = json.loads(resp.data)
assert d and d['count'] == 0
def test_api_comment(client):
resp = client.get('/api/comments?url=/site1')
3 years ago
d = json.loads(resp.data)
assert d and len(d['data']) == 1
comment = d['data'][0]
assert comment['author'] == 'Bob'
assert comment['content'] == 'comment 1'
3 years ago
3 years ago
def test_api_comment_not_found(client):
resp = client.get('/api/comments?url=/site2')
3 years ago
d = json.loads(resp.data)
assert d and d['data'] == []