Merge pull request #9 from discourse/fix_rails_5_tests

Fix tests to work with Rails 5.1.
This commit is contained in:
Guo Xiang Tan 2017-09-25 11:06:42 +08:00 committed by GitHub
commit 9fd366a9fd
1 changed files with 60 additions and 50 deletions

View File

@ -28,27 +28,28 @@ describe DataExplorer::QueryController 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; expect(response.body).to be_empty # check_xhr fail get :index
expect(response.body).to be_empty
xhr :get, :index get :index, format: :json
expect(response.status).to eq(404) expect(response.status).to eq(404)
xhr :get, :schema get :schema, format: :json
expect(response.status).to eq(404) expect(response.status).to eq(404)
xhr :get, :show, id: 3 get :show, params: { id: 3 }, format: :json
expect(response.status).to eq(404) expect(response.status).to eq(404)
xhr :post, :create, id: 3 post :create, params: { id: 3 }, format: :json
expect(response.status).to eq(404) expect(response.status).to eq(404)
xhr :post, :run, id: 3 post :run, params: { id: 3 }, format: :json
expect(response.status).to eq(404) expect(response.status).to eq(404)
xhr :put, :update, id: 3 put :update, params: { id: 3 }, format: :json
expect(response.status).to eq(404) expect(response.status).to eq(404)
xhr :delete, :destroy, id: 3 delete :destroy, params: { id: 3 }, format: :json
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
end end
@ -56,7 +57,7 @@ describe DataExplorer::QueryController do
describe "#index" do describe "#index" do
it "behaves nicely with no queries" do it "behaves nicely with no queries" do
DataExplorer::Query.destroy_all DataExplorer::Query.destroy_all
xhr :get, :index get :index, format: :json
expect(response).to be_success expect(response).to be_success
expect(response_json['queries']).to eq([]) expect(response_json['queries']).to eq([])
end end
@ -65,7 +66,7 @@ describe DataExplorer::QueryController do
DataExplorer::Query.destroy_all DataExplorer::Query.destroy_all
q1 = make_query q1 = make_query
q2 = make_query q2 = make_query
xhr :get, :index get :index, format: :json
expect(response).to be_success expect(response).to be_success
expect(response_json['queries'].length).to eq(2) expect(response_json['queries'].length).to eq(2)
expect(response_json['queries'][0]['name']).to eq(q1.name) expect(response_json['queries'][0]['name']).to eq(q1.name)
@ -78,7 +79,7 @@ describe DataExplorer::QueryController do
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] }]
xhr :post, :run, id: id, _params: MultiJson.dump(params) post :run, params: { id: id, _params: MultiJson.dump(params) }, format: :json
end end
it "can run queries" do it "can run queries" do
q = make_query('SELECT 23 as my_value') q = make_query('SELECT 23 as my_value')
@ -87,28 +88,29 @@ describe DataExplorer::QueryController do
expect(response_json['success']).to eq(true) expect(response_json['success']).to eq(true)
expect(response_json['errors']).to eq([]) expect(response_json['errors']).to eq([])
expect(response_json['columns']).to eq(['my_value']) expect(response_json['columns']).to eq(['my_value'])
expect(response_json['rows']).to eq([['23']]) expect(response_json['rows']).to eq([[23]])
end end
it "can process parameters" do it "can process parameters" do
q = make_query <<SQL q = make_query <<~SQL
-- [params] -- [params]
-- int :foo = 34 -- int :foo = 34
SELECT :foo as my_value SELECT :foo as my_value
SQL SQL
run_query q.id, foo: 23 run_query q.id, foo: 23
expect(response).to be_success expect(response).to be_success
expect(response_json['errors']).to eq([]) expect(response_json['errors']).to eq([])
expect(response_json['success']).to eq(true) expect(response_json['success']).to eq(true)
expect(response_json['columns']).to eq(['my_value']) expect(response_json['columns']).to eq(['my_value'])
expect(response_json['rows']).to eq([['23']]) expect(response_json['rows']).to eq([[23]])
run_query q.id run_query q.id
expect(response).to be_success expect(response).to be_success
expect(response_json['errors']).to eq([]) expect(response_json['errors']).to eq([])
expect(response_json['success']).to eq(true) expect(response_json['success']).to eq(true)
expect(response_json['columns']).to eq(['my_value']) expect(response_json['columns']).to eq(['my_value'])
expect(response_json['rows']).to eq([['34']]) expect(response_json['rows']).to eq([[34]])
# 2.3 is not an integer # 2.3 is not an integer
run_query q.id, foo: '2.3' run_query q.id, foo: '2.3'
@ -120,10 +122,12 @@ SQL
it "doesn't allow you to modify the database #1" do it "doesn't allow you to modify the database #1" do
p = create_post p = create_post
q = make_query <<-SQL
UPDATE posts SET cooked = '<p>you may already be a winner!</p>' WHERE id = #{p.id} q = make_query <<~SQL
RETURNING id UPDATE posts SET cooked = '<p>you may already be a winner!</p>' WHERE id = #{p.id}
SQL RETURNING id
SQL
run_query q.id run_query q.id
p.reload p.reload
@ -140,18 +144,20 @@ SQL
it "doesn't allow you to modify the database #2" do it "doesn't allow you to modify the database #2" do
p = create_post p = create_post
q = make_query <<-SQL
SELECT 1 q = make_query <<~SQL
) SELECT 1
SELECT * FROM query; )
RELEASE SAVEPOINT active_record_1; SELECT * FROM query;
SET TRANSACTION READ WRITE; RELEASE SAVEPOINT active_record_1;
UPDATE posts SET cooked = '<p>you may already be a winner!</p>' WHERE id = #{p.id}; SET TRANSACTION READ WRITE;
SAVEPOINT active_record_1; UPDATE posts SET cooked = '<p>you may already be a winner!</p>' WHERE id = #{p.id};
SET TRANSACTION READ ONLY; SAVEPOINT active_record_1;
WITH query AS ( SET TRANSACTION READ ONLY;
SELECT 1 WITH query AS (
SQL SELECT 1
SQL
run_query q.id run_query q.id
p.reload p.reload
@ -174,9 +180,9 @@ SQL
end end
it "doesn't allow you to lock rows" do it "doesn't allow you to lock rows" do
q = make_query <<SQL q = make_query <<~SQL
SELECT id FROM posts FOR UPDATE SELECT id FROM posts FOR UPDATE
SQL SQL
run_query q.id run_query q.id
expect(response).to_not be_success expect(response).to_not be_success
@ -186,9 +192,10 @@ SQL
end end
it "doesn't allow you to create a table" do it "doesn't allow you to create a table" do
q = make_query <<SQL q = make_query <<~SQL
CREATE TABLE mytable (id serial) CREATE TABLE mytable (id serial)
SQL SQL
run_query q.id run_query q.id
expect(response).to_not be_success expect(response).to_not be_success
expect(response_json['errors']).to_not eq([]) expect(response_json['errors']).to_not eq([])
@ -197,27 +204,30 @@ SQL
end end
it "doesn't allow you to break the transaction" do it "doesn't allow you to break the transaction" do
q = make_query <<SQL q = make_query <<~SQL
COMMIT COMMIT
SQL SQL
run_query q.id run_query q.id
expect(response).to_not be_success expect(response).to_not be_success
expect(response_json['errors']).to_not eq([]) expect(response_json['errors']).to_not eq([])
expect(response_json['success']).to eq(false) expect(response_json['success']).to eq(false)
expect(response_json['errors'].first).to match(/syntax error/) expect(response_json['errors'].first).to match(/syntax error/)
q.sql = <<SQL q.sql = <<~SQL
) )
SQL SQL
run_query q.id run_query q.id
expect(response).to_not be_success expect(response).to_not be_success
expect(response_json['errors']).to_not eq([]) expect(response_json['errors']).to_not eq([])
expect(response_json['success']).to eq(false) expect(response_json['success']).to eq(false)
expect(response_json['errors'].first).to match(/syntax error/) expect(response_json['errors'].first).to match(/syntax error/)
q.sql = <<SQL q.sql = <<~SQL
RELEASE SAVEPOINT active_record_1 RELEASE SAVEPOINT active_record_1
SQL SQL
run_query q.id run_query q.id
expect(response).to_not be_success expect(response).to_not be_success
expect(response_json['errors']).to_not eq([]) expect(response_json['errors']).to_not eq([])