2022-01-20 23:15:04 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
require "rails_helper"
|
2022-01-20 23:15:04 -05:00
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
describe "API keys scoped to query#run" do
|
|
|
|
before { SiteSetting.data_explorer_enabled = true }
|
2022-01-20 23:15:04 -05:00
|
|
|
|
2023-03-22 17:29:08 -04:00
|
|
|
fab!(:query1) do
|
|
|
|
DiscourseDataExplorer::Query.create!(name: "Query 1", sql: "SELECT 1 AS query1_res")
|
|
|
|
end
|
|
|
|
fab!(:query2) do
|
|
|
|
DiscourseDataExplorer::Query.create!(name: "Query 2", sql: "SELECT 1 AS query2_res")
|
|
|
|
end
|
2024-02-29 12:15:57 -05:00
|
|
|
fab!(:admin)
|
2022-01-20 23:15:04 -05:00
|
|
|
|
|
|
|
let(:all_queries_api_key) do
|
|
|
|
key = ApiKey.create!
|
2023-03-22 17:29:08 -04:00
|
|
|
ApiKeyScope.create!(
|
|
|
|
resource: "discourse_data_explorer",
|
|
|
|
action: "run_queries",
|
|
|
|
api_key_id: key.id,
|
|
|
|
)
|
2022-01-20 23:15:04 -05:00
|
|
|
key
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:single_query_api_key) do
|
|
|
|
key = ApiKey.create!
|
|
|
|
ApiKeyScope.create!(
|
2023-03-22 17:29:08 -04:00
|
|
|
resource: "discourse_data_explorer",
|
2022-01-20 23:15:04 -05:00
|
|
|
action: "run_queries",
|
|
|
|
api_key_id: key.id,
|
2022-12-29 07:31:29 -05:00
|
|
|
allowed_parameters: {
|
|
|
|
"id" => [query1.id.to_s],
|
|
|
|
},
|
2022-01-20 23:15:04 -05:00
|
|
|
)
|
|
|
|
key
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
it "cannot hit any other endpoints" do
|
|
|
|
get "/latest.json",
|
|
|
|
headers: {
|
|
|
|
"Api-Key" => all_queries_api_key.key,
|
|
|
|
"Api-Username" => admin.username,
|
|
|
|
}
|
2022-01-20 23:15:04 -05:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
get "/latest.json",
|
|
|
|
headers: {
|
|
|
|
"Api-Key" => single_query_api_key.key,
|
|
|
|
"Api-Username" => admin.username,
|
|
|
|
}
|
2022-01-20 23:15:04 -05:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
get "/u/#{admin.username}.json",
|
|
|
|
headers: {
|
|
|
|
"Api-Key" => all_queries_api_key.key,
|
|
|
|
"Api-Username" => admin.username,
|
|
|
|
}
|
2022-01-20 23:15:04 -05:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
get "/u/#{admin.username}.json",
|
|
|
|
headers: {
|
|
|
|
"Api-Key" => single_query_api_key.key,
|
|
|
|
"Api-Username" => admin.username,
|
|
|
|
}
|
2022-01-20 23:15:04 -05:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can only run the queries they're allowed to run" do
|
|
|
|
expect {
|
2022-12-29 07:31:29 -05:00
|
|
|
post "/admin/plugins/explorer/queries/#{query1.id}/run.json",
|
|
|
|
headers: {
|
|
|
|
"Api-Key" => single_query_api_key.key,
|
|
|
|
"Api-Username" => admin.username,
|
|
|
|
}
|
2022-01-20 23:15:04 -05:00
|
|
|
}.to change { query1.reload.last_run_at }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["success"]).to eq(true)
|
|
|
|
expect(response.parsed_body["columns"]).to eq(["query1_res"])
|
|
|
|
|
|
|
|
expect {
|
2022-12-29 07:31:29 -05:00
|
|
|
post "/admin/plugins/explorer/queries/#{query2.id}/run.json",
|
|
|
|
headers: {
|
|
|
|
"Api-Key" => single_query_api_key.key,
|
|
|
|
"Api-Username" => admin.username,
|
|
|
|
}
|
2022-01-20 23:15:04 -05:00
|
|
|
}.not_to change { query2.reload.last_run_at }
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can run all queries if they're not restricted to any queries" do
|
|
|
|
expect {
|
2022-12-29 07:31:29 -05:00
|
|
|
post "/admin/plugins/explorer/queries/#{query1.id}/run.json",
|
|
|
|
headers: {
|
|
|
|
"Api-Key" => all_queries_api_key.key,
|
|
|
|
"Api-Username" => admin.username,
|
|
|
|
}
|
2022-01-20 23:15:04 -05:00
|
|
|
}.to change { query1.reload.last_run_at }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["success"]).to eq(true)
|
|
|
|
expect(response.parsed_body["columns"]).to eq(["query1_res"])
|
|
|
|
|
|
|
|
expect {
|
2022-12-29 07:31:29 -05:00
|
|
|
post "/admin/plugins/explorer/queries/#{query2.id}/run.json",
|
|
|
|
headers: {
|
|
|
|
"Api-Key" => all_queries_api_key.key,
|
|
|
|
"Api-Username" => admin.username,
|
|
|
|
}
|
2022-01-20 23:15:04 -05:00
|
|
|
}.to change { query2.reload.last_run_at }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["success"]).to eq(true)
|
|
|
|
expect(response.parsed_body["columns"]).to eq(["query2_res"])
|
|
|
|
end
|
|
|
|
end
|