-
Notifications
You must be signed in to change notification settings - Fork 9
Description
Hi,
When a user signs in with his company's credentials and is redirected back to the provided endpoint, we get the user profile from
ElixirAuthMicrosoft.get_user_profile(token.access_token)
We store this in the session and redirect the user to the app:
conn = put_session(conn, :profile, profile)
conn
|> redirect(to: "/")
Fair enough, but how do I replicate this in a sign_in_user function in my Wallaby tests?
I found this post on ElixirForum. I've tried to massage it to fit with the auth logic provided by elixir-auth-mirosoft, but I don't understand everything and could need help to get the last pieces to fit together.
A simple test to see if a user has signed in
defmodule MyApp.FrontPageTest do
use ExUnit.Case, async: true
use Wallaby.Feature
import Wallaby.Query
import MyAppWeb.TestHelpers
setup context do
log_in(context)
end
feature "The frontpage as a logged in user", %{session: session} do
IO.inspect(session, label: "Session")
session
|> visit("/")
|> Wallaby.Browser.text() |> IO.inspect() # This is as expected for visitors, but not logged in users.
end
This is the session we inspect:
Session: %Wallaby.Session{
id: "222",
url: "http://localhost:51652/session/222",
session_url: "http://localhost:51652/session/22",
driver: Wallaby.Chrome,
capabilities: %{
version: "",
chromeOptions: %{
args: ["--no-sandbox", "window-size=1280,800", "--disable-gpu",
"--headless", "--fullscreen",
"--user-agent=Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36/BeamMetadata (2222==)"],
binary: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
},
cssSelectorsEnabled: true,
javascriptEnabled: false,
loadImages: false,
loggingPrefs: %{browser: "DEBUG"},
nativeEvents: false,
platform: "ANY",
rotatable: false,
takesScreenshot: true,
unhandledPromptBehavior: "accept"
},
server: Wallaby.Chrome.Chromedriver,
screenshots: []
TestHelpers
defmodule MyAppWeb.TestHelpers do
import Wallaby.Browser
use Wallaby.DSL
@user_remember_me "_my_app_web_user_remember_me"
def log_in(%{session: session} = context) do
# This is the profile data normally returned by ElixirAuthMicrosoft.get_user_profile(token.access_token)
# Implemented in the MicrosoftAuthController
profile = %{
id: "123",
mail: "[email protected]",
userPrincipalName: "[email protected]",
displayName: "John.Doe",
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
businessPhones: ["+47 955 22 44"],
givenName: "John",
surname: "Doe",
jobTitle: "Software Craftsman",
officeLocation: "Oslo"
}
conn = %Plug.Conn{secret_key_base: "secret_key_base"}
conn = Plug.Conn.put_session(conn, :profile, profile)
session
|> visit("/")
|> set_cookie(@user_remember_me, conn.resp_cookies[@user_remember_me][:value])
{:ok, %{session: session}}
end
end
What obvious steps am I missing?
How can I build a sign_in helper that mimics what we do in the MicrosoftAuthController callback?
BTW: I've been using ElixirAuthMicrosoft for 6 months now, and it works like a charm! I just need to get this final piece in place. 😊