2017-07-16 96 views
0

從這個Simple authentication tutorial鳳凰簡單的身份驗證測試登錄的用戶

我期待測試:login_required管道內的應用途徑(這只是檢查,如果客戶端已調用Guardian.Plug.sign_in(conn, user)

由於user_pathshow行動需要通過:login_required管道進行管道輸送,我本以爲要測試這條路線,我只需要編寫以下內容:

Auth.login_by_email_and_pass(conn, user.email, @password)

然後通過管道散發出來的是成conn

get conn, user_path(conn, :show, user.id)

和檢查,我得到一個200狀態代碼。

但我不能讓過去的Auth.login_by_email_and_pass(conn, email, password)線並且得到錯誤:

session not fetched, call fetch_session/2

我應該在哪裏獲取會話?

我已經試過bypass_through(conn, [:browser, :with_session])

我會認爲:browser管道調用fetch_session這將解決這個問題。

我也嘗試作爲之前和之後調用fetch_session,但仍然得到同樣的錯誤

+1

嘗試調用'init_test_session/2',像這樣:'康恩=康恩|> init_test_session(%{})|> Auth.login_by_email_and_pass(user.email,@password)'。 – Dogbert

+0

@Dogbert謝謝你,現在,如果你發佈一個答案,我會接受它 –

回答

1

您可以使用Plug.Test.init_test_session/2初始化會話。由於您不需要輸入任何數據,因此您可以傳遞空白地圖作爲第二個參數。

conn = conn 
    |> init_test_session(%{}) 
    |> Auth.login_by_email_and_pass(user.email, @password)