Created
May 5, 2016 17:04
-
-
Save Fadhil/7bcb716b46007545a50c09a23f22e46a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Plug.Anonymous do | |
import Plug.Conn | |
require IEx | |
def init(options), do: options | |
def call(conn, _opts) do | |
anonymous_id = get_session(conn, "anonymous_id") | |
case anonymous_id do | |
nil -> put_session(conn, "anonymous_id", "Testing") | |
_ -> put_session(conn, "anonymous_id", "Lovely") | |
end | |
# case conn.cookies do | |
# %{anonymous_id: _id} -> conn | |
# _ -> conn |> put_resp_cookie("anonymous_id", "testing") | |
# end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Plug.AnonymousTest do | |
use ExUnit.Case, async: false | |
use Plug.Test | |
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 | |
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 | |
new_conn = conn(:get, "/") | |
new_conn = put_in(new_conn.secret_key_base, String.duplicate("h", 64)) | |
new_conn = put_in(new_conn.private, %{plug_session: %{"anonymous_id" => "some_id"}}) | |
new_conn = new_conn |> SomeApp.SomeController.call([]) | |
assert get_session(new_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