Skip to content

Commit 191c281

Browse files
disable changelog display w/o perms
1 parent c0578ba commit 191c281

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

lib/console/graphql/schema.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule Console.GraphQl.Schema do
22
use Console.GraphQl.Schema.Base
33
alias Console.Schema
4+
alias Console.Middleware.{Rbac}
45
alias Console.GraphQl.Resolvers.{Build, User}
56

67
import_types Absinthe.Plug.Types
@@ -43,7 +44,10 @@ defmodule Console.GraphQl.Schema do
4344

4445
field :creator, :user, resolve: dataloader(User)
4546
field :approver, :user, resolve: dataloader(User)
46-
field :changelogs, list_of(:changelog), resolve: dataloader(Build)
47+
field :changelogs, list_of(:changelog) do
48+
middleware Rbac, perm: :configure, field: :repository
49+
resolve dataloader(Build)
50+
end
4751

4852
timestamps()
4953
end

test/console/graphql/queries/build_queries_test.exs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ defmodule Console.GraphQl.BuildQueriesTest do
2626
test "It can sideload commands for a build" do
2727
build = insert(:build)
2828
changelogs = insert_list(3, :changelog, build: build)
29+
user = insert(:user)
30+
setup_rbac(user, [build.repository], configure: true)
2931
commands = for i <- 1..3,
3032
do: insert(:command, build: build, inserted_at: Timex.now() |> Timex.shift(days: -i))
3133
expected = commands |> Enum.map(& &1.id) |> Enum.reverse()
@@ -49,12 +51,48 @@ defmodule Console.GraphQl.BuildQueriesTest do
4951
}
5052
}
5153
}
52-
""", %{"id" => build.id}, %{current_user: insert(:user)})
54+
""", %{"id" => build.id}, %{current_user: user})
5355

5456
assert found["id"] == build.id
5557
assert found["creator"]["id"] == build.creator_id
5658
assert ids_equal(found["changelogs"], changelogs)
5759
assert from_connection(found["commands"]) |> Enum.map(& &1["id"]) == expected
5860
end
61+
62+
test "users w/o perms cannot sideload changelogs" do
63+
user = insert(:user)
64+
build = insert(:build)
65+
setup_rbac(user, ["other"], configure: true)
66+
insert_list(3, :changelog, build: build)
67+
commands = for i <- 1..3,
68+
do: insert(:command, build: build, inserted_at: Timex.now() |> Timex.shift(days: -i))
69+
expected = commands |> Enum.map(& &1.id) |> Enum.reverse()
70+
71+
{:ok, %{data: %{"build" => found}, errors: [_ | _]}} = run_query("""
72+
query Build($id: ID!) {
73+
build(id: $id) {
74+
id
75+
creator {
76+
id
77+
}
78+
changelogs {
79+
id
80+
}
81+
commands(first: 10) {
82+
edges {
83+
node {
84+
id
85+
}
86+
}
87+
}
88+
}
89+
}
90+
""", %{"id" => build.id}, %{current_user: user})
91+
92+
assert found["id"] == build.id
93+
assert found["creator"]["id"] == build.creator_id
94+
refute found["changelogs"]
95+
assert from_connection(found["commands"]) |> Enum.map(& &1["id"]) == expected
96+
end
5997
end
6098
end

0 commit comments

Comments
 (0)