|
| 1 | +defmodule Orcasite.Radio.SeedTest do |
| 2 | + @moduledoc """ |
| 3 | + Tests for https://github.com/orcasound/orcasite/issues/1020. |
| 4 | +
|
| 5 | + The seed mutations are exposed in the GraphQL schema unconditionally. A |
| 6 | + validation in `Orcasite.Radio.Seed` already rejects them when |
| 7 | + `ENABLE_SEED_FROM_PROD` is off, but resource validations only run for |
| 8 | + create/update/destroy actions -- not for generic actions. `seed_all` maps to |
| 9 | + the generic `:time_range` action, so it reached its `run` block and did work |
| 10 | + before failing partway through on a nested create. |
| 11 | +
|
| 12 | + A policy now covers every action type, so seeding is refused up front. |
| 13 | + """ |
| 14 | + |
| 15 | + use Orcasite.DataCase, async: false |
| 16 | + |
| 17 | + alias Orcasite.Radio.Seed |
| 18 | + |
| 19 | + setup do |
| 20 | + original = Application.get_env(:orcasite, :enable_seed_from_prod, false) |
| 21 | + on_exit(fn -> Application.put_env(:orcasite, :enable_seed_from_prod, original) end) |
| 22 | + :ok |
| 23 | + end |
| 24 | + |
| 25 | + defp set_seeding(enabled?), do: Application.put_env(:orcasite, :enable_seed_from_prod, enabled?) |
| 26 | + |
| 27 | + describe "with seeding disabled" do |
| 28 | + setup do |
| 29 | + set_seeding(false) |
| 30 | + :ok |
| 31 | + end |
| 32 | + |
| 33 | + test "the generic time_range action is forbidden outright" do |
| 34 | + assert {:error, %Ash.Error.Forbidden{}} = run_time_range() |
| 35 | + end |
| 36 | + |
| 37 | + test "creating feeds is rejected" do |
| 38 | + assert {:error, error} = Ash.create(Seed, %{}, action: :feeds) |
| 39 | + assert error_message(error) =~ "Seeding is disabled" |
| 40 | + end |
| 41 | + |
| 42 | + test "seeding a resource is rejected" do |
| 43 | + assert {:error, error} = |
| 44 | + Ash.create( |
| 45 | + Seed, |
| 46 | + %{ |
| 47 | + resource: :detection, |
| 48 | + feed_id: "whatever", |
| 49 | + start_time: DateTime.utc_now(), |
| 50 | + end_time: DateTime.utc_now() |
| 51 | + }, |
| 52 | + action: :resource |
| 53 | + ) |
| 54 | + |
| 55 | + assert error_message(error) =~ "Seeding is disabled" |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + describe "with seeding enabled" do |
| 60 | + setup do |
| 61 | + set_seeding(true) |
| 62 | + :ok |
| 63 | + end |
| 64 | + |
| 65 | + test "authorization no longer refuses the generic time_range action" do |
| 66 | + # Not asserting success -- that would reach the prod GraphQL API. Only |
| 67 | + # that the policy is no longer what stops it. |
| 68 | + refute match?({:error, %Ash.Error.Forbidden{}}, run_time_range()) |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + defp run_time_range do |
| 73 | + Seed |
| 74 | + |> Ash.ActionInput.for_action(:time_range, %{}) |
| 75 | + |> Ash.run_action() |
| 76 | + end |
| 77 | + |
| 78 | + defp error_message(error), do: Exception.message(error) |
| 79 | +end |
0 commit comments