Skip to content

Commit

Permalink
Assert bodies in sync tests (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
amh4r authored Sep 24, 2024
1 parent 971e066 commit 1d1002c
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 11 deletions.
8 changes: 4 additions & 4 deletions tests/test_registration/cases/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
from . import (
base,
cloud_branch_env,
in_sync_invalid_sig,
in_sync_missing_sig,
in_band_invalid_sig,
in_band_missing_sig,
out_of_band,
server_kind_mismatch,
)

_modules = (
cloud_branch_env,
in_sync_invalid_sig,
in_sync_missing_sig,
in_band_invalid_sig,
in_band_missing_sig,
out_of_band,
server_kind_mismatch,
)
Expand Down
61 changes: 60 additions & 1 deletion tests/test_registration/cases/cloud_branch_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import inngest
import inngest.fast_api
from inngest._internal import net, server_lib
from inngest._internal import const, net, server_lib

from . import base

Expand Down Expand Up @@ -70,6 +70,65 @@ def fn(
str,
)

assert json.loads(res.body.decode("utf-8")) == {
"app_id": client.app_id,
"env": "my-env",
"framework": framework.value,
"functions": [
{
"batchEvents": None,
"cancel": None,
"concurrency": None,
"debounce": None,
"id": fn.id,
"idempotency": None,
"name": "foo",
"priority": None,
"rateLimit": None,
"steps": {
"step": {
"id": "step",
"name": "step",
"retries": {"attempts": 0},
"runtime": {
"type": "http",
"url": f"http://test.local?fnId={fn.id}&stepId=step",
},
}
},
"throttle": None,
"triggers": [{"event": "app/foo", "expression": None}],
}
],
"inspection": {
"schema_version": "2024-05-24",
"api_origin": "https://api.inngest.com/",
"app_id": client.app_id,
"authentication_succeeded": True,
"capabilities": {"in_band_sync": "v1", "trust_probe": "v1"},
"env": "my-env",
"event_api_origin": "https://inn.gs/",
"event_key_hash": None,
"framework": framework.value,
"function_count": 1,
"has_event_key": False,
"has_signing_key": True,
"has_signing_key_fallback": False,
"mode": "cloud",
"sdk_language": "py",
"sdk_version": const.VERSION,
"serve_origin": None,
"serve_path": None,
"signing_key_fallback_hash": None,
"signing_key_hash": "709e80c88487a2411e1ee4dfb9f22a861492d20c4765150c0c794abd70f8147c",
},
"platform": None,
"sdk_author": "inngest",
"sdk_language": "py",
"sdk_version": const.VERSION,
"url": "http://test.local",
}

return base.Case(
name=_TEST_NAME,
run_test=run_test,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ def fn(
assert res.headers["x-inngest-expected-server-kind"] == "cloud"
assert "x-inngest-sync-kind" not in res.headers

assert json.loads(res.body.decode("utf-8")) == {
"code": "sig_verification_failed",
"message": "",
"name": "SigVerificationFailedError",
}

return base.Case(
name=_TEST_NAME,
run_test=run_test,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def fn(
assert res.headers["x-inngest-expected-server-kind"] == "cloud"
assert "x-inngest-sync-kind" not in res.headers

assert json.loads(res.body.decode("utf-8")) == {
"code": "header_missing",
"message": "cannot validate signature in production mode without a x-inngest-signature header",
"name": "HeaderMissingError",
}

return base.Case(
name=_TEST_NAME,
run_test=run_test,
Expand Down
46 changes: 45 additions & 1 deletion tests/test_registration/cases/out_of_band.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ def run_test(self: base.TestCase) -> None:

@dataclasses.dataclass
class State:
body: typing.Optional[bytes]
headers: dict[str, list[str]]

state = State(headers={})
state = State(
body=None,
headers={},
)

def on_request(
*,
Expand All @@ -38,6 +42,9 @@ def on_request(
for k, v in headers.items():
state.headers[k] = v

if body is not None:
state.body = body

return http_proxy.Response(
body=json.dumps({}).encode("utf-8"),
headers={},
Expand Down Expand Up @@ -68,13 +75,50 @@ def fn(
self.serve(client, [fn])
res = self.put(body={})
assert res.status_code == 200
assert json.loads(res.body.decode("utf-8")) == {}

assert state.headers.get("authorization") is not None
assert state.headers.get("x-inngest-env") == ["my-env"]
assert state.headers.get("x-inngest-framework") == [framework.value]
assert state.headers.get("x-inngest-sdk") == [
f"inngest-py:v{const.VERSION}"
]

host: str
if framework == server_lib.Framework.FAST_API:
host = "http://testserver"
elif framework == server_lib.Framework.FLASK:
host = "http://localhost"

assert state.body is not None
assert json.loads(state.body.decode("utf-8")) == {
"appname": client.app_id,
"capabilities": {"in_band_sync": "v1", "trust_probe": "v1"},
"deploy_type": "ping",
"framework": framework.value,
"functions": [
{
"id": fn.id,
"name": "foo",
"steps": {
"step": {
"id": "step",
"name": "step",
"retries": {"attempts": 0},
"runtime": {
"type": "http",
"url": f"{host}/api/inngest?fnId={fn.id}&stepId=step",
},
}
},
"triggers": [{"event": "app/foo"}],
}
],
"sdk": f"py:v{const.VERSION}",
"url": f"{host}/api/inngest",
"v": "0.1",
}

return base.Case(
name=_TEST_NAME,
run_test=run_test,
Expand Down
10 changes: 5 additions & 5 deletions tests/test_registration/cases/server_kind_mismatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ async def fn(
}
res = self.put(body={}, headers=headers)
assert res.status_code == 400
res_body = json.loads(res.body)
assert isinstance(res_body, dict)
assert (
res_body["code"] == server_lib.ErrorCode.SERVER_KIND_MISMATCH.value
)

assert json.loads(res.body.decode("utf-8")) == {
"code": "server_kind_mismatch",
"message": "Sync rejected since it's from a Dev Server but expected Cloud",
}

return base.Case(
name=_TEST_NAME,
Expand Down

0 comments on commit 1d1002c

Please sign in to comment.