Skip to content

Instantly share code, notes, and snippets.

@Fadhil
Created May 5, 2016 14:40
Show Gist options
  • Save Fadhil/82306b6fd5616adf6483823878c46a56 to your computer and use it in GitHub Desktop.
Save Fadhil/82306b6fd5616adf6483823878c46a56 to your computer and use it in GitHub Desktop.
defmodule Plug.AnonymousTest do
use ExUnit.Case, async: true
use Plug.Test
alias Plug.Session.COOKIE, as: CookieStore
require IEx
alias Plug.Anonymous
defmodule SomeApp do
defmodule SomeController do
use Plug.Builder
plug Plug.Session,
store: :cookie,
key: "_wankrank_key",
signing_salt: "cmwAUEpB",
max_age: 86400
plug :fetch_session
plug Anonymous
end
end
@default_opts [
store: :cookie,
key: "_wankrank_key",
signing_salt: "cmwAUEpB",
max_age: 86400
]
@custom_serializer_opts Plug.Session.init(@default_opts)
test "assigns user_id session key" do
conn = conn(:get, "/") |> SomeApp.SomeController.call([])
refute get_session(conn, :anonymous_id) == nil
end
test "returns existing user session key" do
conn = conn(:get, "/") |> Plug.Session.call(@custom_serializer_opts) |> fetch_session
conn = put_session(conn, :anonymous_id, "some_id")
conn = conn |> SomeApp.SomeController.call([])
# conn = put_session(conn, :anonymous_id, "some_id")
# conn = conn |> SomeApp.SomeController.call([])
assert get_session(conn, :anonymous_id) == "some_id"
end
test "raises Error if User is not provided" do
conn = conn(:get, "/") |> SomeApp.SomeController.call([])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment