Skip to content

Commit

Permalink
Only add namespace the first time a key is created
Browse files Browse the repository at this point in the history
Otherwise the namespace is appended each time the session is updated
  • Loading branch information
aellispierce committed Feb 3, 2017
1 parent 3df4268 commit 2ca092c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
9 changes: 4 additions & 5 deletions lib/redbird/plug/session/redis.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ defmodule Plug.Session.REDIS do
end

def put(conn, nil, data, init_options) do
put(conn, generate_random_key(), data, init_options)
put(conn, add_namespace(generate_random_key()), data, init_options)
end
def put(_conn, redis_key, data, init_options) do
key = add_namespace(redis_key)
def put(_conn, namespaced_key, data, init_options) do
setex(%{
key: key,
key: namespaced_key,
value: data,
seconds: session_expiration(init_options)
})
key
namespaced_key
end

def delete(_conn, redis_key, _kinit_options) do
Expand Down
11 changes: 6 additions & 5 deletions test/mix/tasks/redbird/delete_all_sessions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ defmodule Mix.Tasks.Redbird.DeleteAllSessionsTest do
end

test "deletes all redbird session keys" do
key = "redis_session"
conn = %{}
options = []
REDIS.put(conn, key, %{foo: :bar}, options)
key = REDIS.put(conn, nil, %{foo: :bar}, options)

Mix.Tasks.Redbird.DeleteAllSessions.run([])

assert {nil, %{}} = REDIS.get(conn, key, options)
end

test "deletes user defined namespaced session keys" do
Application.put_env(:redbird, :key_namespace, "test_")
conn = %{}
key = "redis_session"
options = []
REDIS.put(conn, key, %{foo: :bar}, options)
key = REDIS.put(conn, nil, %{foo: :bar}, options)

Mix.Tasks.Redbird.DeleteAllSessions.run([])

assert {nil, %{}} = REDIS.get(conn, "test_" <> key, options)
assert {nil, %{}} = REDIS.get(conn, key, options)
Application.delete_env(:redbird, :key_namespace)
end
end
11 changes: 5 additions & 6 deletions test/redbird_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,21 @@ defmodule RedbirdTest do

test "redbird_session is appended to key names by default" do
conn = %{}
key = "redis_session"
options = []
REDIS.put(conn, key, %{foo: :bar}, options)
key = REDIS.put(conn, nil, %{foo: :bar}, options)

assert {"redbird_session_redis_session", %{foo: :bar}} = REDIS.get(conn, "redbird_session_" <> key, options)
assert key =~ "redbird_session_"
end

test "user can set their own key namespace" do
Application.put_env(:redbird, :key_namespace, "test_")
Redbird.Redis.keys("test_*")
|> Redbird.Redis.del
conn = %{}
key = "redis_session"
options = []
REDIS.put(conn, key, %{foo: :bar}, options)
key = REDIS.put(conn, nil, %{foo: :bar}, options)

assert {"test_redis_session", %{foo: :bar}} = REDIS.get(conn, "test_" <> key, options)
assert key =~ "test_"
Application.delete_env(:redbird, :key_namespace)
end
end

0 comments on commit 2ca092c

Please sign in to comment.