Commit 00b944af authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch 'pl-json_response-ce' into 'master'

Backport "Cleanup usages of `JSON.parse` in specs"

See merge request gitlab-org/gitlab-ce!30712
parents e38a3bba 7b87ed14
......@@ -41,7 +41,7 @@ describe Admin::ApplicationSettingsController do
it 'returns JSON data' do
get :usage_data, format: :json
body = JSON.parse(response.body)
body = json_response
expect(body["version"]).to eq(Gitlab::VERSION)
expect(body).to include('counts')
expect(response.status).to eq(200)
......
......@@ -63,8 +63,6 @@ describe ApplicationController do
sign_in user
end
let(:json_response) { JSON.parse(response.body) }
controller(described_class) do
def index
render json: Gon.all_variables
......
......@@ -52,10 +52,8 @@ describe Boards::IssuesController do
list_issues user: user, board: board, list: list2
parsed_response = JSON.parse(response.body)
expect(response).to match_response_schema('entities/issue_boards')
expect(parsed_response['issues'].length).to eq 2
expect(json_response['issues'].length).to eq 2
expect(development.issues.map(&:relative_position)).not_to include(nil)
end
......@@ -123,10 +121,8 @@ describe Boards::IssuesController do
list_issues user: user, board: board
parsed_response = JSON.parse(response.body)
expect(response).to match_response_schema('entities/issue_boards')
expect(parsed_response['issues'].length).to eq 2
expect(json_response['issues'].length).to eq 2
end
end
......
......@@ -26,10 +26,8 @@ describe Boards::ListsController do
read_board_list user: user, board: board
parsed_response = JSON.parse(response.body)
expect(response).to match_response_schema('lists')
expect(parsed_response.length).to eq 3
expect(json_response.length).to eq 3
end
context 'with unauthorized user' do
......
......@@ -63,10 +63,8 @@ describe Groups::BoardsController do
list_boards format: :json
parsed_response = JSON.parse(response.body)
expect(response).to match_response_schema('boards')
expect(parsed_response.length).to eq 1
expect(json_response.length).to eq 1
end
context 'with unauthorized user' do
......
......@@ -73,7 +73,7 @@ describe Groups::MilestonesController do
it 'lists legacy group milestones and group milestones' do
get :index, params: { group_id: group.to_param }, format: :json
milestones = JSON.parse(response.body)
milestones = json_response
expect(milestones.count).to eq(2)
expect(milestones.first["title"]).to eq("group milestone")
......
......@@ -5,7 +5,6 @@ require 'spec_helper'
describe HealthCheckController do
include StubENV
let(:json_response) { JSON.parse(response.body) }
let(:xml_response) { Hash.from_xml(response.body)['hash'] }
let(:token) { Gitlab::CurrentSettings.health_check_access_token }
let(:whitelisted_ip) { '127.0.0.1' }
......
......@@ -5,7 +5,6 @@ require 'spec_helper'
describe HealthController do
include StubENV
let(:json_response) { JSON.parse(response.body) }
let(:token) { Gitlab::CurrentSettings.health_check_access_token }
let(:whitelisted_ip) { '127.0.0.1' }
let(:not_whitelisted_ip) { '127.0.0.2' }
......
......@@ -5,7 +5,6 @@ require 'spec_helper'
describe MetricsController do
include StubENV
let(:json_response) { JSON.parse(response.body) }
let(:metrics_multiproc_dir) { Dir.mktmpdir }
let(:whitelisted_ip) { '127.0.0.1' }
let(:whitelisted_ip_range) { '10.0.0.0/24' }
......
......@@ -160,7 +160,7 @@ describe Projects::BlobController do
it 'renders diff context lines Gitlab::Diff::Line array' do
do_get(since: 1, to: 2, offset: 0, from_merge_request: true)
lines = JSON.parse(response.body)
lines = json_response
expect(lines.size).to eq(diff_lines.size)
lines.each do |line|
......@@ -173,7 +173,7 @@ describe Projects::BlobController do
it 'handles full being true' do
do_get(full: true, from_merge_request: true)
lines = JSON.parse(response.body)
lines = json_response
expect(lines.size).to eq(diff_lines.size)
end
......
......@@ -69,10 +69,8 @@ describe Projects::BoardsController do
list_boards format: :json
parsed_response = JSON.parse(response.body)
expect(response).to match_response_schema('boards')
expect(parsed_response.length).to eq 2
expect(json_response.length).to eq 2
end
context 'with unauthorized user' do
......
......@@ -495,10 +495,8 @@ describe Projects::BranchesController do
search: 'master'
}
parsed_response = JSON.parse(response.body)
expect(parsed_response.length).to eq 1
expect(parsed_response.first).to eq 'master'
expect(json_response.length).to eq 1
expect(json_response.first).to eq 'master'
end
end
......@@ -591,8 +589,7 @@ describe Projects::BranchesController do
end
it 'returns the commit counts behind and ahead of default branch' do
parsed_response = JSON.parse(response.body)
expect(parsed_response).to eq(
expect(json_response).to eq(
"fix" => { "behind" => 29, "ahead" => 2 },
"branch-merged" => { "behind" => 1, "ahead" => 0 },
"add-pdf-file" => { "behind" => 0, "ahead" => 3 }
......
......@@ -378,8 +378,8 @@ describe Projects::CommitController do
get_pipelines(id: commit.id, format: :json)
expect(response).to be_ok
expect(JSON.parse(response.body)['pipelines']).not_to be_empty
expect(JSON.parse(response.body)['count']['all']).to eq 1
expect(json_response['pipelines']).not_to be_empty
expect(json_response['count']['all']).to eq 1
expect(response).to include_pagination_headers
end
end
......
......@@ -302,8 +302,7 @@ describe Projects::CompareController do
signatures_request
expect(response).to have_gitlab_http_status(200)
parsed_body = JSON.parse(response.body)
signatures = parsed_body['signatures']
signatures = json_response['signatures']
expect(signatures.size).to eq(1)
expect(signatures.first['commit_sha']).to eq(signature_commit.sha)
......@@ -332,8 +331,7 @@ describe Projects::CompareController do
signatures_request
expect(response).to have_gitlab_http_status(200)
parsed_body = JSON.parse(response.body)
expect(parsed_body['signatures']).to be_empty
expect(json_response['signatures']).to be_empty
end
end
......@@ -345,8 +343,7 @@ describe Projects::CompareController do
signatures_request
expect(response).to have_gitlab_http_status(200)
parsed_body = JSON.parse(response.body)
expect(parsed_body['signatures']).to be_empty
expect(json_response['signatures']).to be_empty
end
end
end
......
......@@ -52,12 +52,10 @@ describe Projects::DeployKeysController do
it 'returns json in a correct format' do
get :index, params: params.merge(format: :json)
json = JSON.parse(response.body)
expect(json.keys).to match_array(%w(enabled_keys available_project_keys public_keys))
expect(json['enabled_keys'].count).to eq(1)
expect(json['available_project_keys'].count).to eq(1)
expect(json['public_keys'].count).to eq(1)
expect(json_response.keys).to match_array(%w(enabled_keys available_project_keys public_keys))
expect(json_response['enabled_keys'].count).to eq(1)
expect(json_response['available_project_keys'].count).to eq(1)
expect(json_response['public_keys'].count).to eq(1)
end
end
end
......
......@@ -112,7 +112,7 @@ describe Projects::DiscussionsController do
it "returns the name of the resolving user" do
post :resolve, params: request_params
expect(JSON.parse(response.body)['resolved_by']['name']).to eq(user.name)
expect(json_response['resolved_by']['name']).to eq(user.name)
end
it "returns status 200" do
......@@ -135,7 +135,7 @@ describe Projects::DiscussionsController do
it "returns truncated diff lines" do
post :resolve, params: request_params
expect(JSON.parse(response.body)['truncated_diff_lines']).to be_present
expect(json_response['truncated_diff_lines']).to be_present
end
end
end
......
......@@ -53,10 +53,9 @@ describe Projects::FindFileController do
it 'returns an array of file path list' do
go
json = JSON.parse(response.body)
is_expected.to respond_with(:success)
expect(json).not_to eq(nil)
expect(json.length).to be >= 0
expect(json_response).not_to eq(nil)
expect(json_response.length).to be >= 0
end
end
......
......@@ -444,7 +444,7 @@ describe Projects::IssuesController do
it 'renders json with recaptcha_html' do
subject
expect(JSON.parse(response.body)).to have_key('recaptcha_html')
expect(json_response).to have_key('recaptcha_html')
end
end
end
......@@ -484,10 +484,8 @@ describe Projects::IssuesController do
it 'returns last edited time' do
go(id: issue.iid)
data = JSON.parse(response.body)
expect(data).to include('updated_at')
expect(data['updated_at']).to eq(issue.last_edited_at.to_time.iso8601)
expect(json_response).to include('updated_at')
expect(json_response['updated_at']).to eq(issue.last_edited_at.to_time.iso8601)
end
end
......@@ -520,10 +518,8 @@ describe Projects::IssuesController do
it 'returns the necessary data' do
go(id: issue.iid)
data = JSON.parse(response.body)
expect(data).to include('title_text', 'description', 'description_text')
expect(data).to include('task_status', 'lock_version')
expect(json_response).to include('title_text', 'description', 'description_text')
expect(json_response).to include('task_status', 'lock_version')
end
end
end
......@@ -692,9 +688,7 @@ describe Projects::IssuesController do
update_issue(issue_params: { assignee_ids: [assignee.id] })
body = JSON.parse(response.body)
expect(body['assignees'].first.keys)
expect(json_response['assignees'].first.keys)
.to match_array(%w(id name username avatar_url state web_url))
end
end
......@@ -1314,7 +1308,7 @@ describe Projects::IssuesController do
it 'filters notes that the user should not see' do
get :discussions, params: { namespace_id: project.namespace, project_id: project, id: issue.iid }
expect(JSON.parse(response.body).count).to eq(1)
expect(json_response.count).to eq(1)
end
it 'does not result in N+1 queries' do
......
......@@ -112,7 +112,7 @@ describe Projects::MergeRequests::DiffsController do
it 'only renders the diffs for the path given' do
diff_for_path(old_path: existing_path, new_path: existing_path)
paths = JSON.parse(response.body)["diff_files"].map { |file| file['new_path'] }
paths = json_response["diff_files"].map { |file| file['new_path'] }
expect(paths).to include(existing_path)
end
......
......@@ -242,9 +242,7 @@ describe Projects::MergeRequestsController do
update_merge_request({ assignee_ids: [assignee.id] }, format: :json)
body = JSON.parse(response.body)
expect(body['assignees']).to all(include(*%w(name username avatar_url id state web_url)))
expect(json_response['assignees']).to all(include(*%w(name username avatar_url id state web_url)))
end
end
......
......@@ -29,7 +29,7 @@ describe Projects::NotesController do
}
end
let(:parsed_response) { JSON.parse(response.body).with_indifferent_access }
let(:parsed_response) { json_response.with_indifferent_access }
let(:note_json) { parsed_response[:notes].first }
before do
......@@ -614,7 +614,7 @@ describe Projects::NotesController do
it "returns the name of the resolving user" do
post :resolve, params: request_params.merge(html: true)
expect(JSON.parse(response.body)["resolved_by"]).to eq(user.name)
expect(json_response["resolved_by"]).to eq(user.name)
end
it "returns status 200" do
......
......@@ -7,7 +7,6 @@ describe Projects::TemplatesController do
let(:user) { create(:user) }
let(:file_path_1) { '.gitlab/issue_templates/issue_template.md' }
let(:file_path_2) { '.gitlab/merge_request_templates/merge_request_template.md' }
let(:body) { JSON.parse(response.body) }
let!(:file_1) { project.repository.create_file(user, file_path_1, 'issue content', message: 'message', branch_name: 'master') }
let!(:file_2) { project.repository.create_file(user, file_path_2, 'merge request content', message: 'message', branch_name: 'master') }
......@@ -17,8 +16,8 @@ describe Projects::TemplatesController do
get(:show, params: { namespace_id: project.namespace, template_type: 'issue', key: 'issue_template', project_id: project }, format: :json)
expect(response.status).to eq(200)
expect(body['name']).to eq('issue_template')
expect(body['content']).to eq('issue content')
expect(json_response['name']).to eq('issue_template')
expect(json_response['content']).to eq('issue content')
end
end
......@@ -27,8 +26,8 @@ describe Projects::TemplatesController do
get(:show, params: { namespace_id: project.namespace, template_type: 'merge_request', key: 'merge_request_template', project_id: project }, format: :json)
expect(response.status).to eq(200)
expect(body['name']).to eq('merge_request_template')
expect(body['content']).to eq('merge request content')
expect(json_response['name']).to eq('merge_request_template')
expect(json_response['content']).to eq('merge request content')
end
end
......
......@@ -103,7 +103,7 @@ describe Projects::WikisController do
it 'renders json in a correct format' do
post :preview_markdown, params: { namespace_id: project.namespace, project_id: project, id: 'page/path', text: '*Markdown* text' }
expect(JSON.parse(response.body).keys).to match_array(%w(body references))
expect(json_response.keys).to match_array(%w(body references))
end
end
......
......@@ -740,20 +740,18 @@ describe ProjectsController do
it 'gets a list of branches and tags' do
get :refs, params: { namespace_id: project.namespace, id: project, sort: 'updated_desc' }
parsed_body = JSON.parse(response.body)
expect(parsed_body['Branches']).to include('master')
expect(parsed_body['Tags'].first).to eq('v1.1.0')
expect(parsed_body['Tags'].last).to eq('v1.0.0')
expect(parsed_body['Commits']).to be_nil
expect(json_response['Branches']).to include('master')
expect(json_response['Tags'].first).to eq('v1.1.0')
expect(json_response['Tags'].last).to eq('v1.0.0')
expect(json_response['Commits']).to be_nil
end
it "gets a list of branches, tags and commits" do
get :refs, params: { namespace_id: project.namespace, id: project, ref: "123456" }
parsed_body = JSON.parse(response.body)
expect(parsed_body["Branches"]).to include("master")
expect(parsed_body["Tags"]).to include("v1.0.0")
expect(parsed_body["Commits"]).to include("123456")
expect(json_response["Branches"]).to include("master")
expect(json_response["Tags"]).to include("v1.0.0")
expect(json_response["Commits"]).to include("123456")
end
context "when preferred language is Japanese" do
......@@ -765,10 +763,9 @@ describe ProjectsController do
it "gets a list of branches, tags and commits" do
get :refs, params: { namespace_id: project.namespace, id: project, ref: "123456" }
parsed_body = JSON.parse(response.body)
expect(parsed_body["Branches"]).to include("master")
expect(parsed_body["Tags"]).to include("v1.0.0")
expect(parsed_body["Commits"]).to include("123456")
expect(json_response["Branches"]).to include("master")
expect(json_response["Tags"]).to include("v1.0.0")
expect(json_response["Commits"]).to include("123456")
end
end
......@@ -797,7 +794,7 @@ describe ProjectsController do
it 'renders json in a correct format' do
post :preview_markdown, params: { namespace_id: public_project.namespace, id: public_project, text: '*Markdown* text' }
expect(JSON.parse(response.body).keys).to match_array(%w(body references))
expect(json_response.keys).to match_array(%w(body references))
end
context 'when not authorized' do
......@@ -821,8 +818,6 @@ describe ProjectsController do
text: issue.to_reference
}
json_response = JSON.parse(response.body)
expect(json_response['body']).to match(/\##{issue.iid} \(closed\)/)
end
......@@ -833,8 +828,6 @@ describe ProjectsController do
text: merge_request.to_reference
}
json_response = JSON.parse(response.body)
expect(json_response['body']).to match(/\!#{merge_request.iid} \(closed\)/)
end
end
......
......@@ -26,7 +26,7 @@ describe Snippets::NotesController do
end
it "returns not empty array of notes" do
expect(JSON.parse(response.body)["notes"].empty?).to be_falsey
expect(json_response["notes"].empty?).to be_falsey
end
end
......@@ -97,7 +97,7 @@ describe Snippets::NotesController do
it "returns 1 note" do
get :index, params: { snippet_id: private_snippet }
expect(JSON.parse(response.body)['notes'].count).to eq(1)
expect(json_response['notes'].count).to eq(1)
end
end
end
......@@ -114,7 +114,7 @@ describe Snippets::NotesController do
it "does not return any note" do
get :index, params: { snippet_id: public_snippet }
expect(JSON.parse(response.body)['notes'].count).to eq(0)
expect(json_response['notes'].count).to eq(0)
end
end
end
......
......@@ -622,7 +622,7 @@ describe SnippetsController do
post :preview_markdown, params: { id: snippet, text: '*Markdown* text' }
expect(JSON.parse(response.body).keys).to match_array(%w(body references))
expect(json_response.keys).to match_array(%w(body references))
end
end
end
......@@ -291,7 +291,7 @@ describe UsersController do
it 'response with snippets json data' do
get :snippets, params: { username: user.username }, format: :json
expect(response).to have_gitlab_http_status(200)
expect(JSON.parse(response.body)).to have_key('html')
expect(json_response).to have_key('html')
end
end
......
......@@ -89,7 +89,7 @@ describe API::Search do
it 'returns empty array' do
get api('/search', user), params: { scope: 'milestones', search: 'awesome' }
milestones = JSON.parse(response.body)
milestones = json_response
expect(milestones).to be_empty
end
......@@ -356,7 +356,7 @@ describe API::Search do
it 'returns empty array' do
get api("/projects/#{project.id}/search", user), params: { scope: 'milestones', search: 'awesome' }
milestones = JSON.parse(response.body)
milestones = json_response
expect(milestones).to be_empty
end
......
......@@ -1439,8 +1439,4 @@ describe 'Git LFS API and storage' do
post(url, params: params, headers: headers)
end
def json_response
@json_response ||= JSON.parse(response.body)
end
end
......@@ -163,8 +163,4 @@ describe 'Git LFS File Locking API' do
def do_get(url, params = nil, headers = nil)
get(url, params: (params || {}), headers: (headers || {}).merge('Content-Type' => LfsRequest::CONTENT_TYPE))
end
def json_response
@json_response ||= JSON.parse(response.body)
end
end
RSpec.configure do |config|
config.include_context 'JSON response'
config.include_context 'JSON response', type: :controller
config.include_context 'JSON response', type: :request
config.include_context 'JSON response', :api
end
......@@ -46,7 +46,7 @@ shared_examples 'issuable notes filter' do
user.set_notes_filter(UserPreference::NOTES_FILTERS[:only_comments], issuable)
get :discussions, params: params
discussions = JSON.parse(response.body)
discussions = json_response
expect(discussions.count).to eq(1)
expect(discussions.first["notes"].first["system"]).to be(false)
......@@ -56,7 +56,7 @@ shared_examples 'issuable notes filter' do
user.set_notes_filter(UserPreference::NOTES_FILTERS[:only_activity], issuable)
get :discussions, params: params
discussions = JSON.parse(response.body)
discussions = json_response
expect(discussions.count).to eq(1)
expect(discussions.first["notes"].first["system"]).to be(true)
......
......@@ -38,7 +38,7 @@ shared_examples 'update invalid issuable' do |klass|
put :update, params: params
expect(response.status).to eq(409)
expect(JSON.parse(response.body)).to have_key('errors')
expect(json_response).to have_key('errors')
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment