Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit a907bc8

Browse files
authored
FIX: improve admin api for artifact key values (#1425)
Previously we had a logic error and were showing admins keys that are not theirs when querying for all keys This makes the API cleaner, to get all results you need to be explicit always
1 parent d97307e commit a907bc8

File tree

2 files changed

+28
-31
lines changed

2 files changed

+28
-31
lines changed

app/controllers/discourse_ai/ai_bot/artifact_key_values_controller.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,12 @@ def build_index_query
102102

103103
query = query.where("key = ?", index_params[:key]) if index_params[:key].present?
104104

105-
if !index_params[:all_users].to_s == "true" && current_user
106-
query = query.where(user_id: current_user.id)
105+
if index_params[:all_users].to_s != "true"
106+
if current_user
107+
query = query.where(user_id: current_user.id)
108+
else
109+
query = query.where("1 = 0")
110+
end
107111
end
108112

109113
query

spec/requests/ai_bot/artifact_key_values_controller_spec.rb

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,23 @@
5050
)
5151
end
5252

53+
fab!(:admin_key_value) do
54+
Fabricate(
55+
:ai_artifact_key_value,
56+
ai_artifact: artifact,
57+
user: admin,
58+
key: "admin_key",
59+
value: "admin_value",
60+
public: false,
61+
)
62+
end
63+
5364
context "when not logged in" do
5465
it "returns only public key values" do
55-
get "/discourse-ai/ai-bot/artifact-key-values/#{artifact.id}.json"
66+
get "/discourse-ai/ai-bot/artifact-key-values/#{artifact.id}.json",
67+
params: {
68+
all_users: true,
69+
}
5670

5771
expect(response.status).to eq(200)
5872
json = response.parsed_body
@@ -86,7 +100,10 @@
86100
before { sign_in(user) }
87101

88102
it "returns public key values and own private key values" do
89-
get "/discourse-ai/ai-bot/artifact-key-values/#{artifact.id}.json"
103+
get "/discourse-ai/ai-bot/artifact-key-values/#{artifact.id}.json",
104+
params: {
105+
all_users: true,
106+
}
90107

91108
expect(response.status).to eq(200)
92109
json = response.parsed_body
@@ -138,17 +155,13 @@
138155
context "when logged in as admin" do
139156
before { sign_in(admin) }
140157

141-
it "returns all key values including private ones from other users" do
158+
it "returns only my own keys by default" do
142159
get "/discourse-ai/ai-bot/artifact-key-values/#{artifact.id}.json"
143160

144161
expect(response.status).to eq(200)
145162
json = response.parsed_body
146-
expect(json["key_values"].length).to eq(3)
147-
expect(json["key_values"].map { |kv| kv["key"] }).to contain_exactly(
148-
"test_key",
149-
"private_key",
150-
"other_key",
151-
)
163+
expect(json["key_values"].length).to eq(1)
164+
expect(json["key_values"].map { |kv| kv["key"] }).to contain_exactly("admin_key")
152165
end
153166

154167
it "can access private artifacts" do
@@ -420,24 +433,4 @@
420433
end
421434
end
422435
end
423-
424-
describe "private methods" do
425-
let(:controller) { described_class.new }
426-
427-
before do
428-
controller.instance_variable_set(:@artifact, artifact)
429-
allow(controller).to receive(:params).and_return(
430-
ActionController::Parameters.new(test_params),
431-
)
432-
end
433-
434-
describe "#key_value_params" do
435-
let(:test_params) { { key: "test", value: "value", public: true, extra: "ignored" } }
436-
437-
it "permits only allowed parameters" do
438-
# This would need to be tested by calling the actual method or through integration tests
439-
# since private methods are typically tested through their public interfaces
440-
end
441-
end
442-
end
443436
end

0 commit comments

Comments
 (0)