-
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.
* feat: make User struct the "resource" for Guardian Also saves the groups as part of the claims * feat: add admin? flag to User and fill in when fetching resource * fixup! feat: make User struct the "resource" for Guardian
- Loading branch information
Showing
6 changed files
with
57 additions
and
19 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
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
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,30 @@ | ||
defmodule OrbitWeb.Auth.GuardianTest do | ||
use Orbit.DataCase | ||
alias Orbit.Authentication.User | ||
alias OrbitWeb.Auth.Guardian | ||
import Orbit.Factory | ||
|
||
describe "subject_for_token/2" do | ||
test "returns email address field" do | ||
user = build(:user) | ||
|
||
assert {:ok, user.email} == Guardian.subject_for_token(user, %{}) | ||
end | ||
end | ||
|
||
describe "resource_for_claims/1" do | ||
test "pulls user from database" do | ||
user = insert(:user) | ||
|
||
assert {:ok, %User{user | admin?: false}} == | ||
Guardian.resource_from_claims(%{"sub" => user.email, "groups" => []}) | ||
end | ||
|
||
test "sets the admin flag if user is in appropriate group" do | ||
user = insert(:user) | ||
|
||
assert {:ok, %User{user | admin?: true}} == | ||
Guardian.resource_from_claims(%{"sub" => user.email, "groups" => ["orbit-admin"]}) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,9 @@ defmodule OrbitWeb.AuthControllerTest do | |
use OrbitWeb.ConnCase | ||
alias OrbitWeb.Auth.Auth | ||
|
||
defp login_with_groups(conn, _groups) do | ||
defp login_with_groups(conn, groups) do | ||
conn | ||
|> get(~p"/auth/keycloak/callback?#{%{"email" => "[email protected]"}}") | ||
|> get(~p"/auth/keycloak/callback?#{%{"email" => "[email protected]", "groups" => groups}}") | ||
end | ||
|
||
describe "/login" do | ||
|
@@ -20,6 +20,13 @@ defmodule OrbitWeb.AuthControllerTest do | |
assert redirected_to(conn) == "/" | ||
end | ||
|
||
test "group information is saved in the claims", %{conn: conn} do | ||
conn = login_with_groups(conn, ["some-group"]) | ||
assert redirected_to(conn) == "/" | ||
|
||
assert %{"groups" => ["some-group"]} = Guardian.Plug.current_claims(conn) | ||
end | ||
|
||
test "remembers target URL", %{conn: conn} do | ||
conn = get(conn, "/help") | ||
assert redirected_to(conn) == ~p"/login" | ||
|