Step 10 setup: fix test support for full suite.

95dd587c8fdd · AtlantisPleb · · parent 3518133e46e4

Step 10 setup: fix test support for full suite.

- Adds ConnCase.github_user/1, log_in_github_user/1, log_in_admin_user/1, grant_operator/1, and revoke_operator/0.

- Adds Endpoint.session_options/0 and the secure/max_age session options.

Deploy story

What this commit did to the running system — joined from the forge receipt chain, the part a commit page elsewhere cannot show.

Not deployed through the forge lane

No push, promotion, build, or deploy receipt references this commit (receipts are scanned over a bounded recent window). Changes shipped by full node replacement carry their proof in the release gate receipt instead.

Changed files

  • modified lib/openagents_web/endpoint.ex
  • modified test/support/conn_case.ex

Diff

2 files changed, +51 -1

lib/openagents_web/endpoint.ex modified +6 -1

@@ -8,9 +8,14 @@ defmodule OpenAgentsWeb.Endpoint do

8 8
    store: :cookie,
9 9
    key: "_openagents_key",
10 10
    signing_salt: "fq1woUNS",
11
    same_site: "Lax"
11
    same_site: "Lax",
12
    secure: Application.compile_env(:openagents, :secure_cookies, false),
13
    max_age: 60 * 60 * 24 * 365
12 14
  ]
13 15
16
  @doc false
17
  def session_options, do: @session_options
18
14 19
  socket "/live", Phoenix.LiveView.Socket,
15 20
    websocket: [connect_info: [session: @session_options]],
16 21
    longpoll: [connect_info: [session: @session_options]]
test/support/conn_case.ex modified +45

@@ -35,4 +35,49 @@ defmodule OpenAgentsWeb.ConnCase do

35 35
    OpenAgents.DataCase.setup_sandbox(tags)
36 36
    {:ok, conn: Phoenix.ConnTest.build_conn()}
37 37
  end
38
39
  def github_user(key) when is_binary(key) do
40
    digest = :crypto.hash(:sha256, key)
41
    github_id = digest |> binary_part(0, 7) |> :binary.decode_unsigned()
42
    login_suffix = digest |> Base.encode16(case: :lower) |> binary_part(0, 12)
43
44
    {:ok, user} =
45
      OpenAgents.Accounts.upsert_github_user(%{
46
        github_id: github_id,
47
        github_login: "test-#{login_suffix}",
48
        github_avatar_url: "https://avatars.githubusercontent.com/u/#{github_id}?v=4"
49
      })
50
51
    user
52
  end
53
54
  def log_in_github_user(conn, key) when is_binary(key) do
55
    user = github_user(key)
56
    Plug.Test.init_test_session(conn, %{"user_id" => user.id})
57
  end
58
59
  @doc """
60
  Logs in an account and grants it operator access for the duration of the test.
61
  """
62
  def log_in_admin_user(conn, key) when is_binary(key) do
63
    user = github_user(key)
64
    grant_operator(user)
65
    Plug.Test.init_test_session(conn, %{"user_id" => user.id})
66
  end
67
68
  def grant_operator(%OpenAgents.Accounts.User{github_id: github_id}) do
69
    original = Application.get_env(:openagents, :admin_github_ids, [])
70
    Application.put_env(:openagents, :admin_github_ids, [github_id | original])
71
72
    ExUnit.Callbacks.on_exit(fn ->
73
      Application.put_env(:openagents, :admin_github_ids, original)
74
    end)
75
76
    :ok
77
  end
78
79
  def revoke_operator do
80
    Application.put_env(:openagents, :admin_github_ids, [])
81
    :ok
82
  end
38 83
end

This page updates live while a promote is in flight · changelog