Alternera.Plugin.Test (alternera_plugin_sdk v0.9.0)

Copy Markdown View Source

What a plugin's own tests build on, with no proxy around: a well-formed context, a way to run a step's call/3 in a phase the way the runner does (through Plugin.CallAdapter), and the checks of the contract as plain predicates, so they work under any test framework. Alternera.Plugin.Test.Contract turns the predicates into ExUnit tests.

step = Plugin.Test.step!(MyPlugin, %{"direction" => "response"})
{:ok, body, summary} = Plugin.Test.run_step(step, :response_body, "{}")

What a plugin asks of the proxy (Alternera.Plugin.Host) is answered by Alternera.Plugin.Test.Host, with no setup.

Summary

Functions

Whether result is one the adapter may hand the runner for a step run in a body phase (run/5): {:ok, body, summary[, extras]}, a skip, or a stop (stop?/1).

apply/3 on the plugin: a test that names the module directly would be told by the type checker that a check always or never holds for that one plugin, which is the point of a contract test.

Whether settings hold every required field a Configuration step may fill: the runner runs no step that lacks one (not_configured).

A context like the one the runner hands a step (Alternera.Plugin.context/0): nil tenant and pipeline ids, a step path, the phase :request, an empty view under :view; overrides are merged on top.

Whether result is one the adapter may hand the runner for a step run in a head phase (run/5): {:cont, message[, extras]}, a skip, a hold, a refusal that skips to After, or a stop (stop?/1).

Checks settings as the host does, the adapter's own keys included (Plugin.CallAdapter.init/2).

Problems with a plugin's head or body skip values (skips_head, skips_body): only a plugin declared checks_head: true lists them.

The phases a step may run in, in the order they run.

Whether term is plain data the compiler can embed as a literal (Provisional.Source.plain_data?/1).

The prepared settings, as the runner loads them (load/1, lenient), or raises with the plugin's messages.

What preparing a step decides from its settings and opts, which a Configuration step's fill must leave as it is: the signature's direction, reads, writes and reads_any, the pipelines calls/1 names and the errors raises/1 lists.

Gives back what a step held, as the host does at its Close (Plugin.CallAdapter.release/4).

Gives back what a prepared step (step!/2) held at its Close, with view the exchange as it stands there (the response head under :response), as the host does: leave/3 reads it through context.inputs, over the step's signature.

Runs the plugin's call/3 in phase on message (a head map or a body binary) with the prepared settings and a context (context/1 with phase set, unless one is given), the way the runner does, through Plugin.CallAdapter: an Alternera.Plugin.UnresolvedVariableError the plugin raises is the step's unresolved_variable failure, not a crash. Returns the adapter's result, the runner's vocabulary (head_result?/1, body_result?/1).

Runs a prepared step (step!/2) in one phase, as run/5 does, with the step's own signature: what a test of a plugin's call/3 normally uses.

Values a Configuration step might fill field with, for the contract's check that a fill changes nothing prepared: each value of an :enum, both booleans, the ends of an :integer's range, a plain text. A list or a map gets none: it is replaced whole, and its items say nothing alone.

What is wrong with a settings_schema/0 field (Alternera.Plugin.schema_field/0), as a list of messages; [] for a well-formed one.

Marks store name of tenant secret in the calling process, for Plugin.Provisional.Store.secret?/2.

Signs subject in as the exchange's caller in the calling process, for Plugin.Provisional.Identity; nil signs it out (anonymous, the default).

Whether a streaming step that skips passes its input on unchanged (a Plugin.BodyFilter rule): when streaming body skips (stream/5), every byte it sent is body's. True when it does not skip.

A context like the one the proxy opens a message source with (Alternera.Plugin.Provisional.Source.context/0): a nil tenant, a proxy user's id, the reveal: true keys of source (a source module, or nil for none) and 10 000 ms; overrides are merged on top. Put the secrets a source reveals under :test_secrets, as for a step.

A step as the host prepares one: the plugin, its opts from load/1, and the model's data beside them (Plugin.CallAdapter.extras/2: its signature), for run_step/4, release/4 and stream/5. Raises when the plugin refuses the settings.

Whether result ends the walk the way the contract allows: an answer, a return, or a failure with the keys the contract names.

Streams body through a streaming step (a streams: true plugin's call/3, then its filter's chunk/2 on pieces of at most piece_bytes, then its finish/1, through Plugin.CallAdapter), as the proxy does when the body streams. step is a prepared step (step!/2), whose signature the start reads through, or a plugin, run with its opts and an empty signature. Returns {:ok, out} with every byte the step sent, {:skip, out, reason} when it skipped at its start or its finish, or the failure.

Sets what the test host answers for credentials (Plugin.Provisional.Credentials.authorization/3) in the calling process: stub gets the context, the target URL and the request URL and answers as the host would. Without one no credentials apply.

Answers the outbound calls (Plugin.Provisional.Outbound.call/3) the calling process makes from here on with stub, a function of the request, the context and the options (:upstream carries the plugin's id) that returns what call/3 would: {:ok, %{status:, headers:, body:, exchange_id:}} or {:error, type, message}. Without one a call fails unavailable.

Sets the resource actions the calling process runs through Alternera.Plugin.Host.resource_run/5: a map of resource name to a map of action name to a function of the input that answers what the host would, e.g. %{"finance.exchange_rate" => %{"convert" => fn _ -> {:ok, %{"result" => "8.75"}} end}}.

Sets the resource records the calling process reads through Alternera.Plugin.Host.resource_get/3: a map of resource name ("<package>.<resource>") to a map of lookup key to record, e.g. %{"finance.vat_code" => %{"SE25" => %{"code" => "SE25", "rate" => "25"}}}. A key not in a resource's map is :not_found, a name not in the map an unknown resource.

Sets the tenant variables Plugin.Secret.values/4 reads in the calling process from here on: NAME to a plain variable's value, secret.NAME to a secret's, for every tenant.

Functions

body_result?(other)

@spec body_result?(term()) :: boolean()

Whether result is one the adapter may hand the runner for a step run in a body phase (run/5): {:ok, body, summary[, extras]}, a skip, or a stop (stop?/1).

call(plugin, function, args)

@spec call(module(), atom(), list()) :: term()

apply/3 on the plugin: a test that names the module directly would be told by the type checker that a check always or never holds for that one plugin, which is the point of a contract test.

configured?(plugin, settings)

@spec configured?(module(), map()) :: boolean()

Whether settings hold every required field a Configuration step may fill: the runner runs no step that lacks one (not_configured).

context(overrides \\ %{})

@spec context(map() | keyword()) :: Alternera.Plugin.context()

A context like the one the runner hands a step (Alternera.Plugin.context/0): nil tenant and pipeline ids, a step path, the phase :request, an empty view under :view; overrides are merged on top.

head_result?(other)

@spec head_result?(term()) :: boolean()

Whether result is one the adapter may hand the runner for a step run in a head phase (run/5): {:cont, message[, extras]}, a skip, a hold, a refusal that skips to After, or a stop (stop?/1).

init(plugin, settings)

@spec init(module(), term()) :: {:ok, term()} | {:error, [String.t()]}

Checks settings as the host does, the adapter's own keys included (Plugin.CallAdapter.init/2).

part_skip_problems(module)

@spec part_skip_problems(module()) :: [String.t()]

Problems with a plugin's head or body skip values (skips_head, skips_body): only a plugin declared checks_head: true lists them.

phases()

@spec phases() :: [Alternera.Plugin.phase()]

The phases a step may run in, in the order they run.

plain_data?(term)

@spec plain_data?(term()) :: boolean()

Whether term is plain data the compiler can embed as a literal (Provisional.Source.plain_data?/1).

prepare!(plugin, settings)

The prepared settings, as the runner loads them (load/1, lenient), or raises with the plugin's messages.

prepared_facts(plugin, settings, opts)

@spec prepared_facts(module(), map(), term()) :: map()

What preparing a step decides from its settings and opts, which a Configuration step's fill must leave as it is: the signature's direction, reads, writes and reads_any, the pipelines calls/1 names and the errors raises/1 lists.

release(plugin, held, outcome)

@spec release(module() | map(), term(), term()) :: term()

Gives back what a step held, as the host does at its Close (Plugin.CallAdapter.release/4).

release(map, held, outcome, view)

@spec release(map(), term(), term(), map()) :: term()

Gives back what a prepared step (step!/2) held at its Close, with view the exchange as it stands there (the response head under :response), as the host does: leave/3 reads it through context.inputs, over the step's signature.

run(plugin, phase, message, opts, context \\ nil)

@spec run(
  module(),
  Alternera.Plugin.phase(),
  map() | binary(),
  Alternera.Plugin.opts(),
  map() | nil
) ::
  term()

Runs the plugin's call/3 in phase on message (a head map or a body binary) with the prepared settings and a context (context/1 with phase set, unless one is given), the way the runner does, through Plugin.CallAdapter: an Alternera.Plugin.UnresolvedVariableError the plugin raises is the step's unresolved_variable failure, not a crash. Returns the adapter's result, the runner's vocabulary (head_result?/1, body_result?/1).

The step's signature comes from context.adapter (Plugin.CallAdapter.extras/2 of the step's settings, as run_step/4 puts it there), or that of empty settings when the context has none. The step reads the exchange from context.view, as the runner hands it one: message goes into the view's slot for the phase (request, response, request_body or response_body) when the slot is empty. As the runner does, the view keeps only the bodies the signature reads or writes, besides the phase's own (Location.only_bodies/2): a body the step does not name is nil.

run_step(map, phase, message, context \\ nil)

@spec run_step(map(), Alternera.Plugin.phase(), map() | binary(), map() | nil) ::
  term()

Runs a prepared step (step!/2) in one phase, as run/5 does, with the step's own signature: what a test of a plugin's call/3 normally uses.

sample_values(arg1)

@spec sample_values(map()) :: [term()]

Values a Configuration step might fill field with, for the contract's check that a fill changes nothing prepared: each value of an :enum, both booleans, the ends of an :integer's range, a plain text. A list or a map gets none: it is replaced whole, and its items say nothing alone.

schema_field_problems(field)

@spec schema_field_problems(term()) :: [String.t()]

What is wrong with a settings_schema/0 field (Alternera.Plugin.schema_field/0), as a list of messages; [] for a well-formed one.

secret_store(tenant, name)

@spec secret_store(String.t(), String.t()) :: :ok

Marks store name of tenant secret in the calling process, for Plugin.Provisional.Store.secret?/2.

sign_in(subject)

@spec sign_in(String.t() | nil) :: :ok

Signs subject in as the exchange's caller in the calling process, for Plugin.Provisional.Identity; nil signs it out (anonymous, the default).

skip_passes_through?(plugin, opts, body, phase \\ :response_body)

@spec skip_passes_through?(
  module() | map(),
  Alternera.Plugin.opts(),
  binary(),
  Alternera.Plugin.phase()
) :: boolean()

Whether a streaming step that skips passes its input on unchanged (a Plugin.BodyFilter rule): when streaming body skips (stream/5), every byte it sent is body's. True when it does not skip.

source_context(source \\ nil, overrides \\ %{})

@spec source_context(module() | nil, map() | keyword()) :: map()

A context like the one the proxy opens a message source with (Alternera.Plugin.Provisional.Source.context/0): a nil tenant, a proxy user's id, the reveal: true keys of source (a source module, or nil for none) and 10 000 ms; overrides are merged on top. Put the secrets a source reveals under :test_secrets, as for a step.

step!(plugin, settings)

@spec step!(module(), map()) :: %{module: module(), opts: term(), adapter: term()}

A step as the host prepares one: the plugin, its opts from load/1, and the model's data beside them (Plugin.CallAdapter.extras/2: its signature), for run_step/4, release/4 and stream/5. Raises when the plugin refuses the settings.

stop?(arg1)

@spec stop?(term()) :: boolean()

Whether result ends the walk the way the contract allows: an answer, a return, or a failure with the keys the contract names.

stream(step, opts, body, phase \\ :response_body, piece_bytes \\ 7)

@spec stream(
  module() | map(),
  Alternera.Plugin.opts(),
  binary(),
  Alternera.Plugin.phase(),
  pos_integer()
) :: {:ok, binary()} | {:skip, binary(), term()} | term()

Streams body through a streaming step (a streams: true plugin's call/3, then its filter's chunk/2 on pieces of at most piece_bytes, then its finish/1, through Plugin.CallAdapter), as the proxy does when the body streams. step is a prepared step (step!/2), whose signature the start reads through, or a plugin, run with its opts and an empty signature. Returns {:ok, out} with every byte the step sent, {:skip, out, reason} when it skipped at its start or its finish, or the failure.

stub_authorization(stub)

@spec stub_authorization((map(), String.t() | nil, String.t() | nil -> term())) :: :ok

Sets what the test host answers for credentials (Plugin.Provisional.Credentials.authorization/3) in the calling process: stub gets the context, the target URL and the request URL and answers as the host would. Without one no credentials apply.

stub_outbound(stub)

@spec stub_outbound((map(), map(), keyword() -> term())) :: :ok

Answers the outbound calls (Plugin.Provisional.Outbound.call/3) the calling process makes from here on with stub, a function of the request, the context and the options (:upstream carries the plugin's id) that returns what call/3 would: {:ok, %{status:, headers:, body:, exchange_id:}} or {:error, type, message}. Without one a call fails unavailable.

stub_resource_actions(actions)

@spec stub_resource_actions(%{
  required(String.t()) => %{required(String.t()) => (map() -> term())}
}) ::
  :ok

Sets the resource actions the calling process runs through Alternera.Plugin.Host.resource_run/5: a map of resource name to a map of action name to a function of the input that answers what the host would, e.g. %{"finance.exchange_rate" => %{"convert" => fn _ -> {:ok, %{"result" => "8.75"}} end}}.

stub_resources(resources)

@spec stub_resources(%{required(String.t()) => %{required(term()) => map()}}) :: :ok

Sets the resource records the calling process reads through Alternera.Plugin.Host.resource_get/3: a map of resource name ("<package>.<resource>") to a map of lookup key to record, e.g. %{"finance.vat_code" => %{"SE25" => %{"code" => "SE25", "rate" => "25"}}}. A key not in a resource's map is :not_found, a name not in the map an unknown resource.

stub_tenant_vars(vars)

@spec stub_tenant_vars(%{required(String.t()) => String.t()}) :: :ok

Sets the tenant variables Plugin.Secret.values/4 reads in the calling process from here on: NAME to a plain variable's value, secret.NAME to a secret's, for every tenant.