Skip to content

Commit

Permalink
Broken safir.testing.slack implementation
Browse files Browse the repository at this point in the history
still broken but closer to functional
  • Loading branch information
Fireye04 committed Jun 9, 2023
1 parent 7ed2b8b commit b3ef91b
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 501 deletions.
1 change: 1 addition & 0 deletions requirements/dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pytest-httpx
pytest-xdist[psutil]
types-python-dateutil
types-PyYAML
respx

# Documentation
documenteer
Expand Down
564 changes: 117 additions & 447 deletions requirements/dev.txt

Large diffs are not rendered by default.

86 changes: 43 additions & 43 deletions requirements/main.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ cryptography==41.0.1 \
# via
# pyjwt
# safir
fastapi==0.95.2 \
--hash=sha256:4d9d3e8c71c73f11874bcf5e33626258d143252e329a01002f767306c64fb982 \
--hash=sha256:d374dbc4ef2ad9b803899bd3360d34c534adc574546e25314ab72c0c4411749f
fastapi==0.96.0 \
--hash=sha256:71232d47c2787446991c81c41c249f8a16238d52d779c0e6b43927d3773dbe3c \
--hash=sha256:b8e11fe81e81eab4e1504209917338e0b80f783878a42c2b99467e5e1019a1e9
# via
# -r requirements/main.in
# safir
Expand Down Expand Up @@ -400,43 +400,43 @@ pycparser==2.21 \
--hash=sha256:8ee45429555515e1f6b185e78100aea234072576aa43ab53aefcae078162fca9 \
--hash=sha256:e644fdec12f7872f86c58ff790da456218b10f863970249516d60a5eaca77206
# via cffi
pydantic==1.10.8 \
--hash=sha256:052d8654cb65174d6f9490cc9b9a200083a82cf5c3c5d3985db765757eb3b375 \
--hash=sha256:0c6fafa0965b539d7aab0a673a046466d23b86e4b0e8019d25fd53f4df62c277 \
--hash=sha256:1243d28e9b05003a89d72e7915fdb26ffd1d39bdd39b00b7dbe4afae4b557f9d \
--hash=sha256:12f7b0bf8553e310e530e9f3a2f5734c68699f42218bf3568ef49cd9b0e44df4 \
--hash=sha256:1410275520dfa70effadf4c21811d755e7ef9bb1f1d077a21958153a92c8d9ca \
--hash=sha256:16f8c3e33af1e9bb16c7a91fc7d5fa9fe27298e9f299cff6cb744d89d573d62c \
--hash=sha256:17aef11cc1b997f9d574b91909fed40761e13fac438d72b81f902226a69dac01 \
--hash=sha256:191ba419b605f897ede9892f6c56fb182f40a15d309ef0142212200a10af4c18 \
--hash=sha256:1952526ba40b220b912cdc43c1c32bcf4a58e3f192fa313ee665916b26befb68 \
--hash=sha256:1ced8375969673929809d7f36ad322934c35de4af3b5e5b09ec967c21f9f7887 \
--hash=sha256:2e4148e635994d57d834be1182a44bdb07dd867fa3c2d1b37002000646cc5459 \
--hash=sha256:34d327c81e68a1ecb52fe9c8d50c8a9b3e90d3c8ad991bfc8f953fb477d42fb4 \
--hash=sha256:35db5301b82e8661fa9c505c800d0990bc14e9f36f98932bb1d248c0ac5cada5 \
--hash=sha256:3e59417ba8a17265e632af99cc5f35ec309de5980c440c255ab1ca3ae96a3e0e \
--hash=sha256:42aa0c4b5c3025483240a25b09f3c09a189481ddda2ea3a831a9d25f444e03c1 \
--hash=sha256:666bdf6066bf6dbc107b30d034615d2627e2121506c555f73f90b54a463d1f33 \
--hash=sha256:66a703d1983c675a6e0fed8953b0971c44dba48a929a2000a493c3772eb61a5a \
--hash=sha256:6a82d6cda82258efca32b40040228ecf43a548671cb174a1e81477195ed3ed56 \
--hash=sha256:6f2e754d5566f050954727c77f094e01793bcb5725b663bf628fa6743a5a9108 \
--hash=sha256:7456eb22ed9aaa24ff3e7b4757da20d9e5ce2a81018c1b3ebd81a0b88a18f3b2 \
--hash=sha256:7b1f6cb446470b7ddf86c2e57cd119a24959af2b01e552f60705910663af09a4 \
--hash=sha256:7d5b8641c24886d764a74ec541d2fc2c7fb19f6da2a4001e6d580ba4a38f7878 \
--hash=sha256:84d80219c3f8d4cad44575e18404099c76851bc924ce5ab1c4c8bb5e2a2227d0 \
--hash=sha256:88f195f582851e8db960b4a94c3e3ad25692c1c1539e2552f3df7a9e972ef60e \
--hash=sha256:93e6bcfccbd831894a6a434b0aeb1947f9e70b7468f274154d03d71fabb1d7c6 \
--hash=sha256:93e766b4a8226e0708ef243e843105bf124e21331694367f95f4e3b4a92bbb3f \
--hash=sha256:ab523c31e22943713d80d8d342d23b6f6ac4b792a1e54064a8d0cf78fd64e800 \
--hash=sha256:bb14388ec45a7a0dc429e87def6396f9e73c8c77818c927b6a60706603d5f2ea \
--hash=sha256:c0ab53b609c11dfc0c060d94335993cc2b95b2150e25583bec37a49b2d6c6c3f \
--hash=sha256:c33b60054b2136aef8cf190cd4c52a3daa20b2263917c49adad20eaf381e823b \
--hash=sha256:ceb6a23bf1ba4b837d0cfe378329ad3f351b5897c8d4914ce95b85fba96da5a1 \
--hash=sha256:d532bf00f381bd6bc62cabc7d1372096b75a33bc197a312b03f5838b4fb84edd \
--hash=sha256:df7800cb1984d8f6e249351139667a8c50a379009271ee6236138a22a0c0f319 \
--hash=sha256:e82d4566fcd527eae8b244fa952d99f2ca3172b7e97add0b43e2d97ee77f81ab \
--hash=sha256:f90c1e29f447557e9e26afb1c4dbf8768a10cc676e3781b6a577841ade126b85 \
--hash=sha256:f9613fadad06b4f3bc5db2653ce2f22e0de84a7c6c293909b48f6ed37b83c61f
pydantic==1.10.9 \
--hash=sha256:07293ab08e7b4d3c9d7de4949a0ea571f11e4557d19ea24dd3ae0c524c0c334d \
--hash=sha256:0a2aabdc73c2a5960e87c3ffebca6ccde88665616d1fd6d3db3178ef427b267a \
--hash=sha256:0da48717dc9495d3a8f215e0d012599db6b8092db02acac5e0d58a65248ec5bc \
--hash=sha256:128d9453d92e6e81e881dd7e2484e08d8b164da5507f62d06ceecf84bf2e21d3 \
--hash=sha256:2196c06484da2b3fded1ab6dbe182bdabeb09f6318b7fdc412609ee2b564c49a \
--hash=sha256:2e9aec8627a1a6823fc62fb96480abe3eb10168fd0d859ee3d3b395105ae19a7 \
--hash=sha256:3283b574b01e8dbc982080d8287c968489d25329a463b29a90d4157de4f2baaf \
--hash=sha256:3c52eb595db83e189419bf337b59154bdcca642ee4b2a09e5d7797e41ace783f \
--hash=sha256:4b466a23009ff5cdd7076eb56aca537c745ca491293cc38e72bf1e0e00de5b91 \
--hash=sha256:517a681919bf880ce1dac7e5bc0c3af1e58ba118fd774da2ffcd93c5f96eaece \
--hash=sha256:5f8bbaf4013b9a50e8100333cc4e3fa2f81214033e05ac5aa44fa24a98670a29 \
--hash=sha256:6257bb45ad78abacda13f15bde5886efd6bf549dd71085e64b8dcf9919c38b60 \
--hash=sha256:67195274fd27780f15c4c372f4ba9a5c02dad6d50647b917b6a92bf00b3d301a \
--hash=sha256:6cafde02f6699ce4ff643417d1a9223716ec25e228ddc3b436fe7e2d25a1f305 \
--hash=sha256:73ef93e5e1d3c8e83f1ff2e7fdd026d9e063c7e089394869a6e2985696693766 \
--hash=sha256:7845b31959468bc5b78d7b95ec52fe5be32b55d0d09983a877cca6aedc51068f \
--hash=sha256:7847ca62e581e6088d9000f3c497267868ca2fa89432714e21a4fb33a04d52e8 \
--hash=sha256:7e1d5290044f620f80cf1c969c542a5468f3656de47b41aa78100c5baa2b8276 \
--hash=sha256:7ee829b86ce984261d99ff2fd6e88f2230068d96c2a582f29583ed602ef3fc2c \
--hash=sha256:83fcff3c7df7adff880622a98022626f4f6dbce6639a88a15a3ce0f96466cb60 \
--hash=sha256:939328fd539b8d0edf244327398a667b6b140afd3bf7e347cf9813c736211896 \
--hash=sha256:95c70da2cd3b6ddf3b9645ecaa8d98f3d80c606624b6d245558d202cd23ea3be \
--hash=sha256:963671eda0b6ba6926d8fc759e3e10335e1dc1b71ff2a43ed2efd6996634dafb \
--hash=sha256:970b1bdc6243ef663ba5c7e36ac9ab1f2bfecb8ad297c9824b542d41a750b298 \
--hash=sha256:9863b9420d99dfa9c064042304868e8ba08e89081428a1c471858aa2af6f57c4 \
--hash=sha256:ad428e92ab68798d9326bb3e5515bc927444a3d71a93b4a2ca02a8a5d795c572 \
--hash=sha256:b48d3d634bca23b172f47f2335c617d3fcb4b3ba18481c96b7943a4c634f5c8d \
--hash=sha256:b9cd67fb763248cbe38f0593cd8611bfe4b8ad82acb3bdf2b0898c23415a1f82 \
--hash=sha256:d111a21bbbfd85c17248130deac02bbd9b5e20b303338e0dbe0faa78330e37e0 \
--hash=sha256:e1aa5c2410769ca28aa9a7841b80d9d9a1c5f223928ca8bec7e7c9a34d26b1d4 \
--hash=sha256:e692dec4a40bfb40ca530e07805b1208c1de071a18d26af4a2a0d79015b352ca \
--hash=sha256:e7c9900b43ac14110efa977be3da28931ffc74c27e96ee89fbcaaf0b0fe338e1 \
--hash=sha256:eec39224b2b2e861259d6f3c8b6290d4e0fbdce147adb797484a42278a1a486f \
--hash=sha256:f0b7628fb8efe60fe66fd4adadd7ad2304014770cdc1f4934db41fe46cc8825f \
--hash=sha256:f50e1764ce9353be67267e7fd0da08349397c7db17a562ad036aa7c8f4adfdb6 \
--hash=sha256:fab81a92f42d6d525dd47ced310b0c3e10c416bbfae5d59523e63ea22f82b31e
# via
# -r requirements/main.in
# fastapi
Expand Down Expand Up @@ -581,9 +581,9 @@ structlog==23.1.0 \
# via
# -r requirements/main.in
# safir
typing-extensions==4.6.1 \
--hash=sha256:558bc0c4145f01e6405f4a5fdbd82050bd221b119f4bf72a961a1cfd471349d6 \
--hash=sha256:6bac751f4789b135c43228e72de18637e9a6c29d12777023a703fd1a6858469f
typing-extensions==4.6.3 \
--hash=sha256:88a4153d8505aabbb4e13aacb7c486c2b4a33ca3b3f807914a9b4c844c471c26 \
--hash=sha256:d91d5919357fe7f681a9f2b5b4cb2a5f1ef0a1e9f59c4d8ff0d3491e05c0ffd5
# via
# arq
# pydantic
Expand Down
2 changes: 1 addition & 1 deletion src/semaphore/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Config(BaseSettings):
For a list of environments, see https://github.com/lsst-sqre/phalanx.
"""

slack_webhook_url: Optional[SecretStr] = Field(
slack_webhook: Optional[SecretStr] = Field(
None, env="SEMAPHORE_SLACK_PRIVATE_KEY"
)
"""The Slack app private key."""
Expand Down
11 changes: 5 additions & 6 deletions src/semaphore/worker/functions/send_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
from safir.slack.blockkit import SlackMessage
from safir.slack.webhook import SlackWebhookClient

from semaphore.config import config


async def send_message(ctx: Dict[Any, Any], message: str) -> None:
async def send_message(
ctx: Dict[Any, Any], webhook: str, message: str
) -> SlackMessage:
logger = ctx["logger"].bind(task="send_message")
logger.info("Running send_message")

logger = structlog.get_logger(__name__)
client = SlackWebhookClient(
config.slack_webhook_url.get_secret_value(), "Semaphore", logger
)
client = SlackWebhookClient(webhook, "Semaphore", logger)

slack_message = SlackMessage(message=message)
await client.post(slack_message)
return slack_message
2 changes: 1 addition & 1 deletion src/semaphore/worker/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async def startup(ctx: Dict[Any, Any]) -> None:
ctx["http_client"] = http_client

# logger = structlog.get_logger(__name__)
# client = SlackWebhookClient(config.slack_webhook_url.get_secret_value(),
# client = SlackWebhookClient(config.slack_webhook.get_secret_value(),
# "Semaphore", logger)

# ctx["webhook_client", client]
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

import pytest
import pytest_asyncio
import respx
import structlog
from asgi_lifespan import LifespanManager
from httpx import AsyncClient
from safir.testing.slack import MockSlackWebhook, mock_slack_webhook

from semaphore import main
from semaphore.config import config

if TYPE_CHECKING:
from typing import AsyncIterator
Expand Down Expand Up @@ -53,3 +56,10 @@ def worker_context() -> dict[Any, Any]:
ctx["logger"] = logger

return ctx


@pytest.fixture
def mock_slack(respx_mock: respx.Router) -> MockSlackWebhook:
return mock_slack_webhook(
config.slack_webhook.get_secret_value(), respx_mock
)
23 changes: 21 additions & 2 deletions tests/worker/worker_functions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from typing import Any

import pytest
from httpx import AsyncClient
from safir.testing.slack import MockSlackWebhook

from semaphore.config import config
from semaphore.worker.functions.ping import ping
from semaphore.worker.functions.send_message import send_message

Expand All @@ -18,5 +21,21 @@ async def test_ping(worker_context: dict[Any, Any]) -> None:

@pytest.mark.asyncio
async def test_message(worker_context: dict[Any, Any]) -> None:
result = await send_message(worker_context, "test 1 2 3")
assert result is None
message = "test 1 2 3"
result = await send_message(
worker_context, config.slack_webhook.get_secret_value(), message
)
assert result is True


@pytest.mark.asyncio
async def test_something(
worker_context: dict[Any, Any],
client: AsyncClient,
mock_slack: MockSlackWebhook,
) -> None:
# Do something with client that generates Slack messages.
await send_message(worker_context, mock_slack, "test 1 2 3")
mock_slack.post_webhook()

assert mock_slack.messages == "hi"
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ setenv =
SEMAPHORE_LOG_LEVEL = debug
SEMAPHORE_GITHUB_WEBHOOK_SECRET = webhooksecret
SEMAPHORE_GITHUB_APP_PRIVATE_KEY = githubprivatekey
SEMAPHORE_SLACK_PRIVATE_KEY = slackwebhook
SEMAPHORE_SLACK_PRIVATE_KEY = https://hooks.slack.com/services/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
commands =
pytest -vv --cov=semaphore --cov-branch --cov-report= -n auto {posargs}

Expand Down

0 comments on commit b3ef91b

Please sign in to comment.