diff --git a/guard/lib/guard/api/project.ex b/guard/lib/guard/api/project.ex index 5fe969c1b..7a0653387 100644 --- a/guard/lib/guard/api/project.ex +++ b/guard/lib/guard/api/project.ex @@ -49,11 +49,8 @@ defmodule Guard.Api.Project do req = build_list_by_owner_id_request(user_id, 1) case InternalApi.Projecthub.ProjectService.Stub.list(channel, req, timeout: 30_000) do - {:ok, response} when response.metadata.status.code == 0 -> - response.projects |> length > 0 - - _ -> - false + {:ok, response} when response.metadata.status.code == 0 -> length(response.projects) > 0 + _ -> true end end diff --git a/guard/test/guard/api/project_test.exs b/guard/test/guard/api/project_test.exs index 15f1988d8..0b218c8d9 100644 --- a/guard/test/guard/api/project_test.exs +++ b/guard/test/guard/api/project_test.exs @@ -43,4 +43,49 @@ defmodule Guard.Api.ProjectTest do end end end + + describe "user_has_any_project?/1" do + test "returns true when user has at least one project" do + user_id = "user_with_projects" + + with_mock InternalApi.Projecthub.ProjectService.Stub, + list: fn _channel, _req, _opts -> + {:ok, + %{ + metadata: %{status: %{code: 0}}, + projects: [ + %{metadata: %{id: 1, org_id: "org_id", owner_id: user_id}} + ] + }} + end do + assert Project.user_has_any_project?(user_id) == true + end + end + + test "returns false when user has no projects" do + user_id = "user_without_projects" + + with_mock InternalApi.Projecthub.ProjectService.Stub, + list: fn _channel, _req, _opts -> + {:ok, + %{ + metadata: %{status: %{code: 0}}, + projects: [] + }} + end do + assert Project.user_has_any_project?(user_id) == false + end + end + + test "If communication with projecthub fails, we consider that user does own projects" do + user_id = "user_with_error" + + with_mock InternalApi.Projecthub.ProjectService.Stub, + list: fn _channel, _req, _opts -> + {:error, "Failed to retrieve projects"} + end do + assert Project.user_has_any_project?(user_id) + end + end + end end