Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/statifier_examples_web/journey_host.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ defmodule StatifierExamplesWeb.JourneyHost do
and the open scope every visitor holds for the signup Path.
`start/2` refuses a scope of another kind.

`start/2` does not ask whether its caller holds the scope, and need
not: `c:Riddler.Journey.Host.start/2` is handed a Path and a scope and
no actor, so this module cannot read who is asking, and the behaviour
puts that question on the caller, which asks
`Riddler.Journey.Access.check/4` with `:present` and the scope before
it calls `start/2` ("What a renderer asks, and when"). Both of this
app's starts do: the Journey page and the settings page take the scope
from `StatifierExamplesWeb.JourneyAccess.start_scope/2` for the
session's actor and ask the access module with it first. A caller that
skips that check starts a Journey under a scope it names, of the right
kind; the check of the kind here is what this module can answer for
itself.

`present/1` answers the scope the presented execution records, so the
component checks it with `StatifierExamplesWeb.JourneyAccess`, the
access module for all of them. A setup the branch onboarding runs is a
Expand Down Expand Up @@ -215,6 +228,8 @@ defmodule StatifierExamplesWeb.JourneyHost do
Starts a Journey of the Path named `path` - a page key, or
`"signup_path"` for the signup Path - under `scope`, which has to be of
the kind `scope_kind/1` names for it. The execution records `scope`.
Whether the caller may start a Journey under `scope` is the caller's
check, asked before this (the moduledoc's "The scope").
"""
@impl Riddler.Journey.Host
@spec start(String.t(), Scope.t()) ::
Expand Down
53 changes: 51 additions & 2 deletions lib/statifier_examples_web/live/editor_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ defmodule StatifierExamplesWeb.EditorLive do
* the drawer height, per ADR-0005 2A: the package hands each new height
out through `on_drawer_resize` and reads back whatever the host stored.

## Whose execution the page opens

The execution id in the URL resumes any stored execution, and some of
them are Journeys: a Journey a page started records the scope it runs
under, and a setup the branch onboarding runs records the onboarding's.
This page is a second door onto those by id, so it asks
`StatifierExamplesWeb.JourneyAccess` before it resumes one, with
`:present` and the actor the session names
(`StatifierExamplesWeb.JourneyAccess.actor/1`), as the Journey page
does. An actor outside the scope is answered what an id nobody stored
is answered, so the page does not say that the Journey exists. A member
goes on to the resume, which refuses the library Paths' Journeys on its
own, since their charts are not ones this page resumes; the signup Path
records the open scope every visitor holds, and resumes. An execution
that records no scope, which is every one this page's Run starts,
resumes for anyone who has its id.

## The themes are CSS, not the `theme` assign

`docs/theming.md` in the package offers two seams and they differ in reach
Expand All @@ -53,6 +70,8 @@ defmodule StatifierExamplesWeb.EditorLive do

use StatifierExamplesWeb, :live_view

alias Riddler.Journey.Access
alias Riddler.Journey.Scope
alias StatifierBlocks.Compiler
alias StatifierBlocks.Document
alias StatifierBlocks.Editor
Expand All @@ -64,16 +83,18 @@ defmodule StatifierExamplesWeb.EditorLive do
alias StatifierExamples.Charts.Replay
alias StatifierExamples.Documents
alias StatifierExamplesWeb.Icons
alias StatifierExamplesWeb.JourneyAccess

@default_theme :light

@impl Phoenix.LiveView
def mount(_params, _session, socket) do
def mount(_params, session, socket) do
parent = self()

{:ok,
assign(socket,
page_title: "Editor",
actor: JourneyAccess.actor(session),
palette: Charts.palette(),
field_candidates: Charts.field_candidates(),
fixtures: Charts.fixtures(),
Expand Down Expand Up @@ -475,12 +496,40 @@ defmodule StatifierExamplesWeb.EditorLive do
socket

true ->
adopt(socket, resumed(execution_id))
adopt(socket, resumed_in_reach(execution_id, socket.assigns.actor))
end
end

defp restore_execution(socket, _absent), do: socket

# The access check on a resume by id (the moduledoc's "Whose execution
# the page opens"). The scope is read from the execution's own record,
# where a durable child keeps the copy it inherited, so a setup is
# checked against its onboarding's branch. A scope the access module
# refuses, or one the record holds in a shape `Scope.from_data/1` does
# not read, answers `:execution_not_found`, the refusal an unknown id
# meets; a read storage refuses answers its own reason, which for an
# unknown id is that same one.
@spec resumed_in_reach(String.t(), JourneyAccess.actor()) ::
{:ok, Durable.driven()} | {:error, term()}
defp resumed_in_reach(execution_id, actor) do
case Durable.scope(execution_id) do
{:ok, nil} ->
resumed(execution_id)

{:ok, data} ->
with {:ok, scope} <- Scope.from_data(data),
:ok <- Access.check(JourneyAccess, actor, scope, :present) do
resumed(execution_id)
else
_refused -> {:error, :execution_not_found}
end

{:error, _reason} = error ->
error
end
end

@spec resumed(String.t()) :: {:ok, Durable.driven()} | {:error, term()}
defp resumed(execution_id) do
case Durable.resume(execution_id) do
Expand Down
7 changes: 6 additions & 1 deletion lib/statifier_examples_web/visitor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ defmodule StatifierExamplesWeb.Visitor do
`"riverside"`, so the dev server's settings page and branch
onboarding work for whoever opens them. The test configuration names
none, and a test that needs a librarian puts the key in its own
session.
session. The prod configuration names none either, so a prod build
makes no session a librarian: every visitor holds its own patron
scope and the open scope and nothing else, so a start of the branch
onboarding or of a setup draws the one refusal for everyone. A
deployment that wants librarians signs them in and puts the key in
the session before this plug runs; this app has no sign-in to do it.

Neither key is written over once it is there.
"""
Expand Down
48 changes: 48 additions & 0 deletions test/statifier_examples_web/live/editor_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule StatifierExamplesWeb.EditorLiveTest do
import Phoenix.LiveViewTest

alias Ecto.Adapters.SQL
alias Riddler.Journey.Scope
alias StatifierBlocks.Compiler
alias StatifierBlocks.Document
alias StatifierBlocks.Editor
Expand All @@ -19,6 +20,7 @@ defmodule StatifierExamplesWeb.EditorLiveTest do
alias StatifierExamples.Documents
alias StatifierExamples.Repo
alias StatifierExamples.TwoStageDocument
alias StatifierExamplesWeb.JourneyHost

@themes ["light", "dark", "brand"]

Expand Down Expand Up @@ -1315,6 +1317,52 @@ defmodule StatifierExamplesWeb.EditorLiveTest do
assert html =~ "execution_not_found"
end

# A Journey's execution records the scope it runs under, and the editor
# is a second door onto it by id. An actor outside the scope is answered
# what an id nobody stored is answered. A member goes on to the resume:
# a patron registration's chart is not one the editor resumes, so the
# member meets that refusal instead, which is the resume's own.
#
# Sabotage: made the `{:ok, data}` branch of `resumed_in_reach/2` resume
# without asking the access module; the other visitor was answered
# `:chart_unknown` and this went red, then reverted.
test "a Journey's execution is refused to an actor outside its scope" do
{:ok, scope} = Scope.new("patron", "P-1001")
{:ok, journey, _answer} = JourneyHost.start("patron_registration_steps", scope)
{:ok, _path, execution_id} = JourneyHost.parse(journey)
url = ~p"/editor?#{[doc: "signup_wizard", execution: execution_id]}"

for outsider <- [init_test_session(build_conn(), visitor: "P-2002"), build_conn()] do
{:ok, _view, html} = live(outsider, url)

assert html =~ "execution refused: :execution_not_found"
end

{:ok, _view, html} = live(init_test_session(build_conn(), visitor: "P-1001"), url)

assert html =~ "execution refused: :chart_unknown"
end

# The signup Path records the open scope every visitor holds, so the
# check lets any visitor's page resume it.
#
# Sabotage: made `resumed_in_reach/2` refuse every execution that records
# a scope; the signup Journey was refused and this went red, then
# reverted.
test "a Journey under the open scope resumes for any visitor" do
{:ok, journey, _answer} = JourneyHost.start("signup_path", JourneyHost.scope())
{:ok, nil, execution_id} = JourneyHost.parse(journey)

{:ok, view, html} =
live(
init_test_session(build_conn(), visitor: "P-2002"),
~p"/editor?#{[doc: "signup_wizard", execution: execution_id]}"
)

refute html =~ "execution refused"
assert has_element?(view, "[data-run-status]")
end

# Stop is the host's own terminal transition, and the page stops naming
# an execution it has abandoned.
#
Expand Down
Loading