forked from retoor/devplacepy
81 lines
2.3 KiB
Python
81 lines
2.3 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
|
|
import pytest
|
|
|
|
from tests.conftest import run_async
|
|
|
|
from devplacepy.database import set_setting
|
|
from devplacepy.services.giphy import runtime
|
|
from devplacepy.services.giphy.client import GiphyError
|
|
from devplacepy.services.giphy.config import CONFIG_FIELDS, giphy_config
|
|
from devplacepy.services.giphy.fake import FakeGiphyClient
|
|
from devplacepy.services.giphy.service import GiphyService
|
|
|
|
|
|
@pytest.fixture
|
|
def settings(local_db):
|
|
yield
|
|
for key in ("giphy_api_key", "giphy_base_url", "giphy_rating"):
|
|
set_setting(key, "")
|
|
runtime.set_client(None)
|
|
|
|
|
|
def test_config_fields_declare_the_secret_key_and_rating_choices():
|
|
fields = {field.key: field for field in CONFIG_FIELDS}
|
|
assert fields["giphy_api_key"].secret is True
|
|
assert fields["giphy_api_key"].default == ""
|
|
assert fields["giphy_rating"].options == ["g", "pg", "pg-13", "r"]
|
|
assert GiphyService.config_fields is CONFIG_FIELDS
|
|
assert GiphyService.default_enabled is False
|
|
|
|
|
|
def test_config_reads_settings_and_applies_defaults(settings):
|
|
assert giphy_config().is_configured is False
|
|
set_setting("giphy_api_key", "k")
|
|
set_setting("giphy_rating", "")
|
|
config = giphy_config()
|
|
assert config.is_configured is True
|
|
assert config.rating == "pg"
|
|
assert config.base_url.startswith("https://api.giphy.com")
|
|
|
|
|
|
def test_run_once_is_quiet_without_a_key(settings):
|
|
async def body():
|
|
service = GiphyService()
|
|
runtime.set_client(FakeGiphyClient())
|
|
await service.run_once()
|
|
|
|
run_async(body())
|
|
|
|
|
|
def test_run_once_probes_trending_when_configured(settings):
|
|
async def body():
|
|
set_setting("giphy_api_key", "k")
|
|
calls = []
|
|
|
|
class Probe(FakeGiphyClient):
|
|
async def trending(self, limit=24):
|
|
calls.append(limit)
|
|
return await super().trending(limit)
|
|
|
|
runtime.set_client(Probe())
|
|
await GiphyService().run_once()
|
|
assert calls == [1]
|
|
|
|
run_async(body())
|
|
|
|
|
|
def test_run_once_survives_an_upstream_failure(settings):
|
|
async def body():
|
|
set_setting("giphy_api_key", "k")
|
|
|
|
class Broken(FakeGiphyClient):
|
|
async def trending(self, limit=24):
|
|
raise GiphyError("down", status=502)
|
|
|
|
runtime.set_client(Broken())
|
|
await GiphyService().run_once()
|
|
|
|
run_async(body())
|
|
|