Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MER-2920] Extend live session plugs #4562

Merged
merged 4 commits into from
Jan 18, 2024
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
8 changes: 7 additions & 1 deletion lib/oli_web/live_session_plugs/set_ctx.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
defmodule OliWeb.LiveSessionPlugs.SetCtx do
@moduledoc """
This "live" plug is responsible for setting the session context in the socket assigns.
It is generally used after OliWeb.LiveSessionPlugs.SetCurrentUser to guarantee there is already
a current_user assigned to the socket.
"""

import Phoenix.Component, only: [assign: 2]
alias OliWeb.Common.SessionContext

Expand All @@ -7,7 +13,7 @@ defmodule OliWeb.LiveSessionPlugs.SetCtx do
assign(socket,
ctx:
SessionContext.init(socket, session,
user: socket.assigns.current_user,
user: socket.assigns[:current_user],
is_liveview: true
)
)
Expand Down
13 changes: 13 additions & 0 deletions lib/oli_web/live_session_plugs/set_current_author.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
defmodule OliWeb.LiveSessionPlugs.SetCurrentAuthor do
@moduledoc """
This plug is responsible for setting the current author in the socket assigns, so it expects
to have a current_author_id in the session.
If there is no current_author_id we redirect the author to the login.
"""
use OliWeb, :verified_routes

import Phoenix.Component, only: [assign: 2]
import Phoenix.LiveView, only: [redirect: 2]

def on_mount(:default, _params, %{"current_author_id" => current_author_id}, socket) do
socket = assign(socket, current_author: Oli.Accounts.get_author(current_author_id))

{:cont, socket}
end

def on_mount(_, _params, _session, socket) do
# when there is no current_author_id in the session we redirect the user to the login
{:halt, redirect(socket, to: ~p"/authoring/session/new")}
end
end
12 changes: 11 additions & 1 deletion lib/oli_web/live_session_plugs/set_current_user.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
defmodule OliWeb.LiveSessionPlugs.SetCurrentUser do
@moduledoc """
This plug is responsible for setting the current user in the socket assigns, so it expects
to have a current_user_id in the session.
If there is no current_user_id we redirect the user to the login.
"""

use OliWeb, :verified_routes

import Phoenix.Component, only: [assign: 2]
import Phoenix.LiveView, only: [redirect: 2]

alias Oli.Accounts
alias Oli.Accounts.User
Expand All @@ -16,7 +25,8 @@ defmodule OliWeb.LiveSessionPlugs.SetCurrentUser do
end

def on_mount(_, _params, _session, socket) do
{:cont, socket}
# when there is no current_user_id in the session we redirect the user to the login
{:halt, redirect(socket, to: ~p"/session/new")}
end

defp get_user(user_id) do
Expand Down
12 changes: 12 additions & 0 deletions test/oli_web/live/new_course/course_details_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ defmodule OliWeb.NewCourse.CourseDetailsTest do
"current_step" => 3
})

wait_for_completion()

flash =
assert_redirect(
view,
Expand Down Expand Up @@ -135,6 +137,8 @@ defmodule OliWeb.NewCourse.CourseDetailsTest do
"current_step" => 3
})

wait_for_completion()

flash =
assert_redirect(
view,
Expand Down Expand Up @@ -201,6 +205,8 @@ defmodule OliWeb.NewCourse.CourseDetailsTest do
"current_step" => 3
})

wait_for_completion()

flash =
assert_redirect(
view,
Expand Down Expand Up @@ -229,6 +235,8 @@ defmodule OliWeb.NewCourse.CourseDetailsTest do
"current_step" => 3
})

wait_for_completion()

flash =
assert_redirect(
view,
Expand Down Expand Up @@ -294,6 +302,8 @@ defmodule OliWeb.NewCourse.CourseDetailsTest do
"current_step" => 3
})

wait_for_completion()

flash = assert_redirect(view, Routes.delivery_path(OliWeb.Endpoint, :index))
assert flash["info"] == "Section successfully created."
end
Expand Down Expand Up @@ -326,6 +336,8 @@ defmodule OliWeb.NewCourse.CourseDetailsTest do

assert blueprint_section.contains_explorations == true

wait_for_completion()

flash = assert_redirect(view, Routes.delivery_path(OliWeb.Endpoint, :index))

assert flash["info"] == "Section successfully created."
Expand Down
18 changes: 18 additions & 0 deletions test/support/test_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3377,4 +3377,22 @@ defmodule Oli.TestHelpers do
defp slug_for(resource_id), do: "slug_for_resource_id_#{resource_id}"

### Ends helpers to create resources ###

### Begins helper that waits for Tasks to complete ###

def wait_for_completion() do
pids = Task.Supervisor.children(Oli.TaskSupervisor)
Enum.each(pids, &Process.monitor/1)
wait_for_pids(pids)
end

defp wait_for_pids([]), do: nil

defp wait_for_pids(pids) do
receive do
{:DOWN, _ref, :process, pid, _reason} -> wait_for_pids(List.delete(pids, pid))
end
end

### Ends helper that waits for Tasks to complete ###
end
Loading