This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
"""Shared loomio test helpers.
|
||||
|
||||
Loomio is a Rails app. There is no unauthenticated write API (account creation is email/login-token
|
||||
based and the REST API is session-authenticated), so state is created through the app's own console
|
||||
(`bundle exec rails runner`) using the same models/services the API controllers use — STYLE §1 option
|
||||
2. Each test then reads its object back through the PUBLIC HTTP API, a different path, so a broken
|
||||
DB/serializer/routing layer is named rather than hidden.
|
||||
|
||||
Every helper is idempotent: it looks the user/group/discussion up by a run-scoped identifier first.
|
||||
Identifiers carry the `ccci-` prefix and use `.example.com` (§8) so state left behind on a warm
|
||||
canonical is attributable.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "..", "runner"))
|
||||
from harness import lifecycle # noqa: E402
|
||||
|
||||
RUN_ID = uuid.uuid4().hex[:10]
|
||||
|
||||
ADMIN_EMAIL = f"ccci-loomio-{RUN_ID}@ccci.example.com"
|
||||
ADMIN_PW = "Ccci-Loomio-Test-2026!"
|
||||
GROUP_NAME = f"ccci loomio {RUN_ID}"
|
||||
GROUP_HANDLE = f"ccci-loomio-{RUN_ID}"
|
||||
DISCUSSION_TITLE = f"ccci discussion {RUN_ID}"
|
||||
DISCUSSION_MARKER = f"ccci-marker-{RUN_ID}-roundtrip"
|
||||
|
||||
_EMAIL = json.dumps(ADMIN_EMAIL) # JSON string literals are valid Ruby double-quoted strings
|
||||
_PW = json.dumps(ADMIN_PW)
|
||||
_GROUP_NAME = json.dumps(GROUP_NAME)
|
||||
_GROUP_HANDLE = json.dumps(GROUP_HANDLE)
|
||||
_DISCUSSION_TITLE = json.dumps(DISCUSSION_TITLE)
|
||||
_DISCUSSION_MARKER = json.dumps(DISCUSSION_MARKER)
|
||||
|
||||
# Creates a verified user + an OPEN, public, explore-listed group owned by that user. Prints the
|
||||
# group key on its own line so the caller can read it back through the public API.
|
||||
_GROUP_RUNNER = f"""
|
||||
email = {_EMAIL}
|
||||
user = User.find_by(email: email)
|
||||
unless user
|
||||
user = User.new(name: "ccci loomio", email: email, email_verified: true,
|
||||
password: {_PW}, password_confirmation: {_PW})
|
||||
user.save!
|
||||
end
|
||||
user.update!(email_verified: true) unless user.email_verified?
|
||||
|
||||
group = Group.find_by(handle: {_GROUP_HANDLE})
|
||||
unless group
|
||||
group = Group.new(name: {_GROUP_NAME}, handle: {_GROUP_HANDLE},
|
||||
group_privacy: "open", listed_in_explore: true)
|
||||
group.save!
|
||||
group.add_admin!(user)
|
||||
end
|
||||
puts "GROUP_KEY=" + group.reload.key.to_s
|
||||
"""
|
||||
|
||||
# Same, plus a public discussion in that group. Prints both keys.
|
||||
_DISCUSSION_RUNNER = (
|
||||
_GROUP_RUNNER
|
||||
+ f"""
|
||||
discussion = Discussion.find_by(group_id: group.id, title: {_DISCUSSION_TITLE})
|
||||
unless discussion
|
||||
discussion = Discussion.new(title: {_DISCUSSION_TITLE}, description: {_DISCUSSION_MARKER},
|
||||
group: group, private: false)
|
||||
DiscussionService.create(discussion: discussion, actor: user)
|
||||
end
|
||||
puts "DISCUSSION_KEY=" + discussion.reload.key.to_s
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def _rails(domain: str, code: str) -> str:
|
||||
"""Run Ruby through the app's Rails console (the same process the API controllers run in)."""
|
||||
return lifecycle.exec_in_app(
|
||||
domain, ["bundle", "exec", "rails", "runner", code], service="app", timeout=300
|
||||
)
|
||||
|
||||
|
||||
def _key(out: str, prefix: str) -> str:
|
||||
for line in out.splitlines():
|
||||
if line.startswith(prefix):
|
||||
return line.split("=", 1)[1].strip()
|
||||
raise AssertionError(
|
||||
f"rails runner did not report {prefix} (last 800 chars of output: {out[-800:]!r})"
|
||||
)
|
||||
|
||||
|
||||
def ensure_group(domain: str) -> str:
|
||||
"""Create (or reuse) the run's user + public group; return the group key."""
|
||||
return _key(_rails(domain, _GROUP_RUNNER), "GROUP_KEY=")
|
||||
|
||||
|
||||
def ensure_discussion(domain: str) -> tuple[str, str]:
|
||||
"""Create (or reuse) the run's user + public group + public discussion; return (group, discussion) keys."""
|
||||
out = _rails(domain, _DISCUSSION_RUNNER)
|
||||
return _key(out, "GROUP_KEY="), _key(out, "DISCUSSION_KEY=")
|
||||
|
||||
|
||||
def unwrap(body: object, root: str) -> dict:
|
||||
"""AMS wraps responses with a root key (`{"groups": {...}}`); accept a bare object too."""
|
||||
if isinstance(body, dict) and isinstance(body.get(root), dict):
|
||||
return body[root]
|
||||
assert isinstance(body, dict), f"expected a JSON object, got {type(body).__name__}: {body!r}"
|
||||
return body
|
||||
@@ -0,0 +1,25 @@
|
||||
"""loomio — health/liveness of the Rails app through its own JSON API.
|
||||
|
||||
`GET /api/v1/boot/version` is served by Api::V1::BootController#version (anonymous) and returns the
|
||||
running release. It proves the Rails stack booted past DB migrations and is answering — not just that
|
||||
Traefik served a static page. The generic install tier already asserts real HTTPS serving.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "..", "runner"))
|
||||
from harness import http as harness_http # noqa: E402
|
||||
|
||||
|
||||
def test_api_boot_version_serves(live_app):
|
||||
url = f"https://{live_app}/api/v1/boot/version"
|
||||
status, body = harness_http.retry_http_get(url, expect_status=200, max_wait=120, interval=5)
|
||||
assert status == 200, f"GET {url} HTTP {status}"
|
||||
assert isinstance(body, dict), f"GET {url} did not return a JSON object: {body!r}"
|
||||
version = body.get("version")
|
||||
assert isinstance(version, str) and version.strip(), (
|
||||
f"GET {url} returned no version string: {body!r}"
|
||||
)
|
||||
@@ -0,0 +1,38 @@
|
||||
"""loomio — create a discussion and read its body back through the public API.
|
||||
|
||||
Loomio's core object is a discussion. This creates a user + open group + public discussion through the
|
||||
app's Rails console (`DiscussionService.create`, the same service the API controller calls), then reads
|
||||
the discussion back ANONYMOUSLY at `GET /api/v1/discussions/<key>`, asserting the title and the unique
|
||||
body marker survive.
|
||||
|
||||
Non-vacuous: it exercises the discussion write path (privacy validation against the group, author
|
||||
assignment, event publication) and the anonymous visibility rule (`DiscussionQuery.visible_to` includes
|
||||
public discussions). A read-back of a different path means a stale/echoed setup cannot pass.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "..", "runner"))
|
||||
import _loomio # noqa: E402
|
||||
from harness import http as harness_http # noqa: E402
|
||||
|
||||
|
||||
def test_public_discussion_roundtrip(live_app):
|
||||
_group_key, discussion_key = _loomio.ensure_discussion(live_app)
|
||||
|
||||
url = f"https://{live_app}/api/v1/discussions/{discussion_key}"
|
||||
status, body = harness_http.retry_http_get(url, expect_status=200, max_wait=60, interval=3)
|
||||
assert status == 200, f"anonymous GET {url} HTTP {status} (public discussion not readable)"
|
||||
discussion = _loomio.unwrap(body, "discussions")
|
||||
assert discussion.get("title") == _loomio.DISCUSSION_TITLE, (
|
||||
f"discussion title did not round-trip: sent {_loomio.DISCUSSION_TITLE!r}, "
|
||||
f"got {discussion.get('title')!r}"
|
||||
)
|
||||
assert _loomio.DISCUSSION_MARKER in str(discussion.get("description")), (
|
||||
f"discussion body marker missing: sent {_loomio.DISCUSSION_MARKER!r}, "
|
||||
f"got {discussion.get('description')!r}"
|
||||
)
|
||||
@@ -0,0 +1,35 @@
|
||||
"""loomio — §4.3 create-an-object + read-it-back for a group.
|
||||
|
||||
Creates a verified user and a public/open group through the app's own Rails console (the same
|
||||
`Group`/`Membership` path the API controller uses — STYLE §1 option 2), then reads the group back
|
||||
ANONYMOUSLY through the public HTTP API (`GET /api/v1/groups/<key>`), a different path.
|
||||
|
||||
Non-vacuous: if DB persistence, the public-group ability rule (`Ability::Group#show` requires
|
||||
`is_visible_to_public`), FriendlyId key lookup, or group serialization is broken, this fails even
|
||||
though the landing page and health endpoint still answer. The group name/handle are run-scoped.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "..", "runner"))
|
||||
import _loomio # noqa: E402
|
||||
from harness import http as harness_http # noqa: E402
|
||||
|
||||
|
||||
def test_public_group_roundtrip(live_app):
|
||||
group_key = _loomio.ensure_group(live_app)
|
||||
|
||||
url = f"https://{live_app}/api/v1/groups/{group_key}"
|
||||
status, body = harness_http.retry_http_get(url, expect_status=200, max_wait=60, interval=3)
|
||||
assert status == 200, f"anonymous GET {url} HTTP {status} (public group not readable)"
|
||||
group = _loomio.unwrap(body, "groups")
|
||||
assert group.get("handle") == _loomio.GROUP_HANDLE, (
|
||||
f"group handle did not round-trip: sent {_loomio.GROUP_HANDLE!r}, got {group.get('handle')!r}"
|
||||
)
|
||||
assert group.get("name") == _loomio.GROUP_NAME, (
|
||||
f"group name did not round-trip: sent {_loomio.GROUP_NAME!r}, got {group.get('name')!r}"
|
||||
)
|
||||
Reference in New Issue
Block a user