Skip to content

Commit

Permalink
Fix sso login (#1361)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino authored Sep 18, 2024
1 parent 7c7985c commit 4566e48
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
6 changes: 3 additions & 3 deletions apps/core/lib/core/services/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,13 @@ defmodule Core.Services.Users do
"""
@spec bootstrap_user(Core.OAuth.method, map) :: user_resp
def bootstrap_user(service, %{email: email} = attrs) do
case get_user_by_email(email) do
nil ->
case {service, get_user_by_email(email)} do
{_, nil} ->
attrs
|> Map.merge(login_args(service))
|> Map.put(:password, Ecto.UUID.generate())
|> create_user()
%User{login_method: ^service} = user ->
{service, %User{login_method: svc} = user} when service == :sso or service == svc ->
update_user(login_args(service), user)
_ -> {:error, "you don't have login with #{service} enabled"}
end
Expand Down
12 changes: 10 additions & 2 deletions apps/core/test/services/users_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ defmodule Core.Services.UsersTest do
end
end

describe "#bootstrap_users/2" do
describe "#bootstrap_user/2" do
test "it will create new users and set login method" do
{:ok, user} = Users.bootstrap_user(:google, %{email: "[email protected]", name: "New User"})

Expand All @@ -532,11 +532,19 @@ defmodule Core.Services.UsersTest do
assert upd.login_method == :google
end

test "it will not allow logins w/o login method set" do
test "it will not allow logins w/o correct login method set" do
user = insert(:user)

{:error, _} = Users.bootstrap_user(:google, %{email: user.email})
end

test "it will allow sso logins w/ whatever login method set" do
user = insert(:user)

{:ok, upd} = Users.bootstrap_user(:sso, %{email: user.email})

assert upd.id == user.id
end
end

describe "#create_trust_relationship" do
Expand Down
2 changes: 1 addition & 1 deletion apps/graphql/test/mutations/user_mutation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule GraphQl.UserMutationTest do
end

test "it will fail on invalid captchas" do
{:ok, user} = Users.create_user(%{
{:ok, _} = Users.create_user(%{
name: "Michael Guarino",
email: "[email protected]",
password: "super strong password"
Expand Down

0 comments on commit 4566e48

Please sign in to comment.