Skip to content

Commit

Permalink
Add tests and implementation of end_session/1
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Nov 22, 2021
1 parent 227b9f0 commit 7499915
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
13 changes: 11 additions & 2 deletions lib/auth/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,20 @@ defmodule Auth.Session do
end

@doc """
`end_session/1` update session to end it.
`update_session_end/1` update session to end it.
"""
def end_session(conn) do
def update_session_end(conn) do
get(conn)
|> changeset(%{end: DateTime.utc_now()})
|> Repo.update!()
end


@doc """
`end_session/1` update session to end it.
"""
def end_session(conn) do
update_session_end(conn)
update_in(conn.assigns, &Map.drop(&1, [:sid]))
end
end
21 changes: 18 additions & 3 deletions test/auth/session_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Auth.SessionTest do
use AuthWeb.ConnCase
use AuthWeb.ConnCase, async: true

test "Auth.Session.insert/1 inserts a session record", %{conn: conn} do
conn = non_admin_login(conn)
Expand Down Expand Up @@ -34,17 +34,32 @@ defmodule Auth.SessionTest do
assert ses.end == nil
end

test "Auth.Session.end_session/1 updates the session record", %{conn: conn} do
test "Auth.Session.update_session_end/1 updates the session record", %{conn: conn} do
conn = non_admin_login(conn)
session = Auth.Session.insert(conn)

# Initially the session.end is nil (i.e. not yet ended)
assert session.end == nil

# End the Session:
updated = Auth.Session.end_session(conn)
updated = Auth.Session.update_session_end(conn)

# Confirm the end value has been set.
assert updated.end == updated.updated_at
end

test "Auth.Session.end_session/1 terminates the session", %{conn: conn} do
conn = conn |> non_admin_login() |> Auth.Session.start_session()

session = Auth.Session.get(conn)
# The Session is set on conn.assigns
assert session.id == conn.assigns.sid

# Initially the session.end is nil (i.e. not yet ended)
assert session.end == nil

# End the Session:
conn = Auth.Session.end_session(conn)
assert Map.get(conn.assigns, :sid) == nil
end
end

0 comments on commit 7499915

Please sign in to comment.