Skip to content

Commit

Permalink
Add tests to test websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin5605 committed Jul 26, 2023
1 parent c421e07 commit 94022c0
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/test_scans.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import random
import string

from fastapi.testclient import TestClient

from mainframe.constants import mainframe_settings


def _gen_random_string() -> str:
"""Generate a random string"""

return "".join(random.choices(string.printable, k=20))


def test_scans_websocket(client: TestClient):
res = client.post("/jobs")
res.raise_for_status()
job = res.json()
if len(job) == 0:
return

job = job[0]

payload = dict(
name=job["name"],
version=job["version"],
commit=_gen_random_string(),
score=random.randint(mainframe_settings.score_threshold, 25),
inspector_url=_gen_random_string(),
rules_matched=[_gen_random_string() for _ in range(random.randint(0, 10))],
)

with client.websocket_connect("/scans") as ws:
# Send the package scan results
res = client.put("/package", json=payload)
res.raise_for_status()

res = ws.receive_json(mode="text")
assert res["name"] == payload["name"]
assert res["version"] == payload["version"]
assert res["inspector_url"] == payload["inspector_url"]
assert res["rules"] == payload["rules_matched"]

0 comments on commit 94022c0

Please sign in to comment.