|
1 | | -"""Branches of the mutate endpoint the wire tier cannot reach: raw bodies upstream would read off a Request.""" |
| 1 | +"""Branches of the mutate endpoint the wire tier cannot reach: raw bodies upstream would read off a Request, |
| 2 | +and bodies the caller has already parsed.""" |
2 | 3 |
|
| 4 | +import json |
3 | 5 | from typing import Any |
4 | 6 |
|
5 | 7 | import pytest |
6 | 8 |
|
7 | 9 | from zero_server.protocol.mutation import CustomMutation, MutationResponse |
| 10 | +from zero_server.protocol.push import PushBody |
8 | 11 | from zero_server.server.process_mutations import TransactFn, handle_mutate_request |
9 | 12 |
|
10 | 13 | QUERY = {"schema": "zero_0", "appID": "zero"} |
@@ -41,3 +44,58 @@ async def test_raw_body_is_parsed() -> None: |
41 | 44 | db_provider=NoDatabase(), handler=handler, query=QUERY, body=body, user_id="u" |
42 | 45 | ) |
43 | 46 | assert response.to_json() == {"kind": "MutateResponse", "userID": "u", "mutations": []} |
| 47 | + |
| 48 | + |
| 49 | +PUSH = {"clientGroupID": "g", "mutations": [], "pushVersion": 1, "timestamp": 1, "requestID": "r"} |
| 50 | +MUTATION = {"type": "custom", "id": 1, "clientID": "c", "name": "issue.create", "args": [], "timestamp": 1} |
| 51 | + |
| 52 | + |
| 53 | +class AcceptingDatabase: |
| 54 | + """Accepts each mutation's ID as the next one, and records nothing.""" |
| 55 | + |
| 56 | + async def transaction(self, callback: Any, transaction_input: Any = None) -> Any: |
| 57 | + class Hooks: |
| 58 | + async def update_client_mutation_id(self) -> int: |
| 59 | + return transaction_input.mutation_id |
| 60 | + |
| 61 | + async def write_mutation_result(self, result: MutationResponse) -> None: |
| 62 | + pass |
| 63 | + |
| 64 | + return await callback(None, Hooks()) |
| 65 | + |
| 66 | + |
| 67 | +async def failing_handler(transact: TransactFn[Any], mutation: CustomMutation) -> MutationResponse: |
| 68 | + raise ValueError("no such issue") |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize( |
| 72 | + ("push", "query"), |
| 73 | + [ |
| 74 | + (PUSH, QUERY), |
| 75 | + ({**PUSH, "mutations": [MUTATION]}, QUERY), |
| 76 | + ({**PUSH, "mutations": [MUTATION], "pushVersion": 2}, QUERY), |
| 77 | + ({**PUSH, "mutations": [MUTATION]}, {"appID": "zero"}), |
| 78 | + ], |
| 79 | + ids=["success", "mutation-error", "unsupported-push-version", "bad-query"], |
| 80 | +) |
| 81 | +async def test_parsed_body_matches_raw(push: dict[str, Any], query: dict[str, str]) -> None: |
| 82 | + async def respond(body: object) -> object: |
| 83 | + response = await handle_mutate_request( |
| 84 | + db_provider=AcceptingDatabase(), handler=failing_handler, query=query, body=body, user_id="u" |
| 85 | + ) |
| 86 | + return response.to_json() |
| 87 | + |
| 88 | + assert await respond(PushBody.from_json(push)) == await respond(json.dumps(push).encode()) |
| 89 | + |
| 90 | + |
| 91 | +async def test_parsed_body_is_not_validated_again(monkeypatch: pytest.MonkeyPatch) -> None: |
| 92 | + push_body = PushBody.from_json(PUSH) |
| 93 | + |
| 94 | + def refuse(value: object) -> PushBody: |
| 95 | + raise AssertionError("parsed body validated again") |
| 96 | + |
| 97 | + monkeypatch.setattr(PushBody, "from_json", refuse) |
| 98 | + response = await handle_mutate_request( |
| 99 | + db_provider=NoDatabase(), handler=handler, query=QUERY, body=push_body, user_id="u" |
| 100 | + ) |
| 101 | + assert response.to_json() == {"kind": "MutateResponse", "userID": "u", "mutations": []} |
0 commit comments