forked from oestrich/grapevine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API to receive a session token for mobile clients
- Loading branch information
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule Web.SessionTokenController do | ||
use Web, :controller | ||
|
||
def create(conn = %{assigns: %{session_token: token}}, _params) do | ||
conn | ||
|> put_status(201) | ||
|> json(%{session_token: token}) | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule Web.SessionTokenControllerTest do | ||
use Web.ConnCase | ||
|
||
describe "POST /session_tokens" do | ||
test "a session token is returned", %{conn: conn} do | ||
conn = post(conn, Routes.session_token_path(Web.Endpoint, :create)) | ||
assert %{"session_token" => token} = json_response(conn, 201) | ||
|
||
assert {:ok, uuid} = | ||
Phoenix.Token.verify(Web.Endpoint, "session token", token, max_age: 86_400) | ||
|
||
assert UUID.info!(uuid) | ||
end | ||
|
||
test "an existing session token can be returned", %{conn: conn} do | ||
conn = | ||
conn | ||
|> Plug.Test.init_test_session(session_token: "steve") | ||
|> post(Routes.session_token_path(conn, :create)) | ||
|
||
assert %{"session_token" => token} = json_response(conn, 201) | ||
|
||
assert Phoenix.Token.verify(Web.Endpoint, "session token", token, max_age: 86_400) == | ||
{:ok, "steve"} | ||
end | ||
end | ||
end |