Skip to content

Guard/fix list project by owner #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions guard/lib/guard/api/project.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
45 changes: 45 additions & 0 deletions guard/test/guard/api/project_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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