DEV: Improve tests (#155)

This commit is contained in:
Osama Sayegh 2022-01-21 07:15:23 +03:00 committed by GitHub
parent ea66bcdc75
commit 8464d517b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 61 deletions

View File

@ -133,7 +133,6 @@ class DataExplorer::QueryController < ::ApplicationController
response.sending_file = true response.sending_file = true
end end
params[:params] = params[:_params] if params[:_params] # testing workaround
query_params = {} query_params = {}
query_params = MultiJson.load(params[:params]) if params[:params] query_params = MultiJson.load(params[:params]) if params[:params]

View File

@ -20,37 +20,39 @@ describe DataExplorer::QueryController do
end end
describe "Admin" do describe "Admin" do
routes { ::DataExplorer::Engine.routes } fab!(:admin) { Fabricate(:admin) }
let!(:admin) { log_in_user(Fabricate(:admin)) } before do
sign_in(admin)
end
describe "when disabled" do describe "when disabled" do
before do before do
SiteSetting.data_explorer_enabled = false SiteSetting.data_explorer_enabled = false
end end
it 'denies every request' do it 'denies every request' do
get :index get "/admin/plugins/explorer/queries.json"
expect(response.body).to be_empty
get :index, format: :json
expect(response.status).to eq(404) expect(response.status).to eq(404)
get :schema, format: :json get "/admin/plugins/explorer/schema.json"
expect(response.status).to eq(404) expect(response.status).to eq(404)
get :show, params: { id: 3 }, format: :json get "/admin/plugins/explorer/queries/3.json"
expect(response.status).to eq(404) expect(response.status).to eq(404)
post :create, params: { id: 3 }, format: :json post "/admin/plugins/explorer/queries.json", params: {
id: 3
}
expect(response.status).to eq(404) expect(response.status).to eq(404)
post :run, params: { id: 3 }, format: :json post "/admin/plugins/explorer/queries/3/run.json"
expect(response.status).to eq(404) expect(response.status).to eq(404)
put :update, params: { id: 3 }, format: :json put "/admin/plugins/explorer/queries/3.json"
expect(response.status).to eq(404) expect(response.status).to eq(404)
delete :destroy, params: { id: 3 }, format: :json delete "/admin/plugins/explorer/queries/3.json"
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
end end
@ -62,7 +64,7 @@ describe DataExplorer::QueryController do
it "behaves nicely with no user created queries" do it "behaves nicely with no user created queries" do
DataExplorer::Query.destroy_all DataExplorer::Query.destroy_all
get :index, format: :json get "/admin/plugins/explorer/queries.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(response_json['queries'].count).to eq(Queries.default.count) expect(response_json['queries'].count).to eq(Queries.default.count)
end end
@ -71,7 +73,7 @@ describe DataExplorer::QueryController do
DataExplorer::Query.destroy_all DataExplorer::Query.destroy_all
make_query('SELECT 1 as value', name: 'B') make_query('SELECT 1 as value', name: 'B')
make_query('SELECT 1 as value', name: 'A') make_query('SELECT 1 as value', name: 'A')
get :index, format: :json get "/admin/plugins/explorer/queries.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(response_json['queries'].length).to eq(Queries.default.count + 2) expect(response_json['queries'].length).to eq(Queries.default.count + 2)
expect(response_json['queries'][0]['name']).to eq('A') expect(response_json['queries'][0]['name']).to eq('A')
@ -83,19 +85,18 @@ describe DataExplorer::QueryController do
make_query('SELECT 1 as value', name: 'A', hidden: false) make_query('SELECT 1 as value', name: 'A', hidden: false)
make_query('SELECT 1 as value', name: 'B', hidden: true) make_query('SELECT 1 as value', name: 'B', hidden: true)
make_query('SELECT 1 as value', name: 'C', hidden: true) make_query('SELECT 1 as value', name: 'C', hidden: true)
get :index, format: :json get "/admin/plugins/explorer/queries.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(response_json['queries'].length).to eq(Queries.default.count + 1) expect(response_json['queries'].length).to eq(Queries.default.count + 1)
end end
end end
describe "#run" do describe "#run" do
let!(:admin) { log_in(:admin) }
def run_query(id, params = {}) def run_query(id, params = {})
params = Hash[params.map { |a| [a[0], a[1].to_s] }] params = Hash[params.map { |a| [a[0], a[1].to_s] }]
post :run, params: { id: id, _params: MultiJson.dump(params) }, format: :json post "/admin/plugins/explorer/queries/#{id}/run.json", params: { params: params.to_json }
end end
it "can run queries" do it "can run queries" do
query = make_query('SELECT 23 as my_value') query = make_query('SELECT 23 as my_value')
run_query query.id run_query query.id
@ -252,7 +253,7 @@ describe DataExplorer::QueryController do
it "can export data in CSV format" do it "can export data in CSV format" do
query = make_query('SELECT 23 as my_value') query = make_query('SELECT 23 as my_value')
post :run, params: { id: query.id, download: 1 }, format: :csv post "/admin/plugins/explorer/queries/#{query.id}/run.json", params: { download: 1 }
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
@ -272,10 +273,10 @@ describe DataExplorer::QueryController do
run_query query.id run_query query.id
expect(response_json['rows'].count).to eq(2) expect(response_json['rows'].count).to eq(2)
post :run, params: { id: query.id, limit: 1 }, format: :json post "/admin/plugins/explorer/queries/#{query.id}/run.json", params: { limit: 1 }
expect(response_json['rows'].count).to eq(1) expect(response_json['rows'].count).to eq(1)
post :run, params: { id: query.id, limit: "ALL" }, format: :json post "/admin/plugins/explorer/queries/#{query.id}/run.json", params: { limit: "ALL" }
expect(response_json['rows'].count).to eq(3) expect(response_json['rows'].count).to eq(3)
end end
@ -291,14 +292,14 @@ describe DataExplorer::QueryController do
SELECT id FROM posts SELECT id FROM posts
SQL SQL
post :run, params: { id: query.id, download: 1 }, format: :csv post "/admin/plugins/explorer/queries/#{query.id}/run.csv", params: { download: 1 }
expect(response.body.split("\n").count).to eq(3) expect(response.body.split("\n").count).to eq(3)
post :run, params: { id: query.id, download: 1, limit: 1 }, format: :csv post "/admin/plugins/explorer/queries/#{query.id}/run.csv", params: { download: 1, limit: 1 }
expect(response.body.split("\n").count).to eq(2) expect(response.body.split("\n").count).to eq(2)
# The value `ALL` is not supported in csv exports. # The value `ALL` is not supported in csv exports.
post :run, params: { id: query.id, download: 1, limit: "ALL" }, format: :csv post "/admin/plugins/explorer/queries/#{query.id}/run.csv", params: { download: 1, limit: "ALL" }
expect(response.body.split("\n").count).to eq(1) expect(response.body.split("\n").count).to eq(1)
ensure ensure
DataExplorer.send(:remove_const, "QUERY_RESULT_MAX_LIMIT") DataExplorer.send(:remove_const, "QUERY_RESULT_MAX_LIMIT")
@ -310,13 +311,11 @@ describe DataExplorer::QueryController do
end end
describe "Non-Admin" do describe "Non-Admin" do
routes { Discourse::Application.routes } fab!(:user) { Fabricate(:user) }
fab!(:group) { Fabricate(:group, users: [user]) }
let(:user) { Fabricate(:user) }
let(:group) { Fabricate(:group, users: [user]) }
before do before do
log_in_user(user) sign_in(user)
end end
describe "when disabled" do describe "when disabled" do
@ -325,24 +324,29 @@ describe DataExplorer::QueryController do
end end
it 'denies every request' do it 'denies every request' do
get :group_reports_index, params: { group_name: 1 }, format: :json get "/g/1/reports.json"
expect(response.status).to eq(404) expect(response.status).to eq(404)
get :group_reports_show, params: { group_name: 1, id: 1 }, format: :json get "/g/1/reports/1.json"
expect(response.status).to eq(404) expect(response.status).to eq(404)
post :group_reports_run, params: { group_name: 1, id: 1 }, format: :json post "/g/1/reports/1/run.json"
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
end end
it "cannot access admin endpoints" do
query = make_query('SELECT 1 as value')
post "/admin/plugins/explorer/queries/#{query.id}/run.json"
expect(response.status).to eq(403)
end
describe "#group_reports_index" do describe "#group_reports_index" do
it "only returns queries that the group has access to" do it "only returns queries that the group has access to" do
group.add(user) group.add(user)
make_query('SELECT 1 as value', { name: 'A' }, ["#{group.id}"]) make_query('SELECT 1 as value', { name: 'A' }, ["#{group.id}"])
get :group_reports_index, params: { group_name: group.name }, format: :json get "/g/#{group.name}/reports.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(response_json['queries'].length).to eq(1) expect(response_json['queries'].length).to eq(1)
expect(response_json['queries'][0]['name']).to eq('A') expect(response_json['queries'][0]['name']).to eq('A')
@ -350,26 +354,25 @@ describe DataExplorer::QueryController do
it "returns a 404 when the user should not have access to the query " do it "returns a 404 when the user should not have access to the query " do
other_user = Fabricate(:user) other_user = Fabricate(:user)
log_in_user(other_user) sign_in(other_user)
get :group_reports_index, params: { group_name: group.name }, format: :json get "/g/#{group.name}/reports.json"
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
it "return a 200 when the user has access the the query" do it "return a 200 when the user has access the the query" do
group.add(user) group.add(user)
get :group_reports_index, params: { group_name: group.name }, format: :json get "/g/#{group.name}/reports.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
it "does not return hidden queries" do it "does not return hidden queries" do
group.add(user) group.add(user)
make_query('SELECT 1 as value', { name: 'A', hidden: true }, ["#{group.id}"]) make_query('SELECT 1 as value', { name: 'A', hidden: true }, ["#{group.id}"])
make_query('SELECT 1 as value', { name: 'B' }, ["#{group.id}"]) make_query('SELECT 1 as value', { name: 'B' }, ["#{group.id}"])
get :group_reports_index, params: { group_name: group.name }, format: :json get "/g/#{group.name}/reports.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(response_json['queries'].length).to eq(1) expect(response_json['queries'].length).to eq(1)
expect(response_json['queries'][0]['name']).to eq('B') expect(response_json['queries'][0]['name']).to eq('B')
@ -377,18 +380,21 @@ describe DataExplorer::QueryController do
end end
describe "#group_reports_run" do describe "#group_reports_run" do
it "calls run on QueryController" do it "runs the query" do
query = make_query('SELECT 1 as value', { name: 'B' }, ["#{group.id}"]) query = make_query('SELECT 1828 as value', { name: 'B' }, ["#{group.id}"])
controller.expects(:run).at_least_once
get :group_reports_run, params: { group_name: group.name, id: query.id }, format: :json post "/g/#{group.name}/reports/#{query.id}/run.json"
expect(response.status).to eq(200)
expect(response.parsed_body["success"]).to eq(true)
expect(response.parsed_body["columns"]).to eq(["value"])
expect(response.parsed_body["rows"]).to eq([[1828]])
end end
it "returns a 404 when the user should not have access to the query " do it "returns a 404 when the user should not have access to the query " do
group.add(user) group.add(user)
query = make_query('SELECT 1 as value', {}, []) query = make_query('SELECT 1 as value', {}, [])
get :group_reports_run, params: { group_name: group.name, id: query.id }, format: :json post "/g/#{group.name}/reports/#{query.id}/run.json"
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
@ -396,7 +402,7 @@ describe DataExplorer::QueryController do
group.add(user) group.add(user)
query = make_query('SELECT 1 as value', {}, [group.id.to_s]) query = make_query('SELECT 1 as value', {}, [group.id.to_s])
get :group_reports_run, params: { group_name: group.name, id: query.id }, format: :json post "/g/#{group.name}/reports/#{query.id}/run.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
@ -404,41 +410,30 @@ describe DataExplorer::QueryController do
group.add(user) group.add(user)
query = make_query('SELECT 1 as value', { hidden: true }, [group.id.to_s]) query = make_query('SELECT 1 as value', { hidden: true }, [group.id.to_s])
get :group_reports_run, params: { group_name: group.name, id: query.id }, format: :json post "/g/#{group.name}/reports/#{query.id}/run.json"
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
end end
describe "#group_reports_show" do describe "#group_reports_show" do
let(:group) { Fabricate(:group) }
it "returns a 404 when the user should not have access to the query " do it "returns a 404 when the user should not have access to the query " do
user = Fabricate(:user)
log_in_user(user)
group.add(user)
query = make_query('SELECT 1 as value', {}, []) query = make_query('SELECT 1 as value', {}, [])
get :group_reports_show, params: { group_name: group.name, id: query.id }, format: :json get "/g/#{group.name}/reports/#{query.id}.json"
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
it "return a 200 when the user has access the the query" do it "return a 200 when the user has access the the query" do
user = Fabricate(:user)
log_in_user(user)
group.add(user)
query = make_query('SELECT 1 as value', {}, [group.id.to_s]) query = make_query('SELECT 1 as value', {}, [group.id.to_s])
get :group_reports_show, params: { group_name: group.name, id: query.id }, format: :json get "/g/#{group.name}/reports/#{query.id}.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
it "return a 404 when the query is hidden" do it "return a 404 when the query is hidden" do
user = Fabricate(:user)
log_in_user(user)
group.add(user)
query = make_query('SELECT 1 as value', { hidden: true }, [group.id.to_s]) query = make_query('SELECT 1 as value', { hidden: true }, [group.id.to_s])
get :group_reports_show, params: { group_name: group.name, id: query.id }, format: :json get "/g/#{group.name}/reports/#{query.id}.json"
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
end end