From eed5281094b7b202e3f0f2f824600032a81c144a Mon Sep 17 00:00:00 2001 From: nelsonic Date: Sun, 7 Nov 2021 00:47:46 +0000 Subject: [PATCH] create Auth.Session.end_session/1 function for #30 --- lib/auth/session.ex | 10 +++++----- .../20211106234911_add_app_id_to_sessions.exs | 2 +- test/auth/session_test.exs | 14 ++++++++++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/auth/session.ex b/lib/auth/session.ex index 7bd8c273..fa76b90a 100644 --- a/lib/auth/session.ex +++ b/lib/auth/session.ex @@ -9,7 +9,7 @@ defmodule Auth.Session do schema "sessions" do field :app_id, :id field :auth_provider, :string - field :end_at, :utc_datetime + field :end, :naive_datetime # field :key_id, :integer field :person_id, :id field :user_agent_id, :id @@ -18,7 +18,7 @@ defmodule Auth.Session do end def changeset(session, attrs) do - cast(session, attrs, [:app_id, :person_id, :auth_provider, :user_agent_id, :end_at]) + cast(session, attrs, [:app_id, :person_id, :auth_provider, :user_agent_id, :end]) |> validate_required([:app_id, :person_id]) end @@ -43,7 +43,7 @@ defmodule Auth.Session do and # match on UA in case person has multiple devices/sessions s.user_agent_id == ^Auth.UserAgent.get_user_agent_id(conn) and # only the sessions that haven't been "ended" - is_nil(s.end_at), + is_nil(s.end), # sort by most recent in case there are older un-ended sessions: order_by: [desc: :inserted_at] ) @@ -52,7 +52,7 @@ defmodule Auth.Session do # update session to end it def end_session(conn) do get(conn) - |> changeset(%{end_at: DateTime.utc_now()}) - |> Repo.update() + |> changeset(%{end: DateTime.utc_now()}) + |> Repo.update!() end end diff --git a/priv/repo/migrations/20211106234911_add_app_id_to_sessions.exs b/priv/repo/migrations/20211106234911_add_app_id_to_sessions.exs index 6e376b5c..7a59d587 100644 --- a/priv/repo/migrations/20211106234911_add_app_id_to_sessions.exs +++ b/priv/repo/migrations/20211106234911_add_app_id_to_sessions.exs @@ -5,7 +5,7 @@ defmodule Auth.Repo.Migrations.AddAppIdToSessions do alter table(:sessions) do add :app_id, references(:apps, on_delete: :nothing) add :auth_provider, :string - add :end_at, :utc_datetime + add :end, :naive_datetime remove :person_id # avoid tight coupling add :person_id, references(:people, on_delete: :nothing) add :user_agent_id, :integer diff --git a/test/auth/session_test.exs b/test/auth/session_test.exs index 1e585184..f6d0ae04 100644 --- a/test/auth/session_test.exs +++ b/test/auth/session_test.exs @@ -8,7 +8,7 @@ defmodule Auth.SessionTest do assert session.app_id == conn.assigns.person.app_id assert session.person_id == conn.assigns.person.id assert session.auth_provider == conn.assigns.person.auth_provider - assert session.end_at == nil + assert session.end == nil end test "Auth.Session.get/1 retrieves a session record", %{conn: conn} do @@ -20,6 +20,16 @@ defmodule Auth.SessionTest do assert session.app_id == ses.app_id assert session.person_id == ses.person_id assert session.auth_provider == ses.auth_provider - assert ses.end_at == nil + assert ses.end == nil + end + + test "Auth.Session.end_session/1 updates the session record", %{conn: conn} do + conn = non_admin_login(conn) + session = Auth.Session.insert(conn) + assert session.end == nil + + # End the Session: + updated = Auth.Session.end_session(conn) + assert updated.end == updated.updated_at end end