diff --git a/app/controllers/user_blocks_controller.rb b/app/controllers/user_blocks_controller.rb index b35ac808e..47b203321 100644 --- a/app/controllers/user_blocks_controller.rb +++ b/app/controllers/user_blocks_controller.rb @@ -35,7 +35,7 @@ class UserBlocksController < ApplicationController return end - @user_block = UserBlock.find_by(blocked_id: permitted_attributes(UserBlock)[:blocked_id], blocker: current_user) + @user_block = UserBlock.find_by!(blocked_id: permitted_attributes(UserBlock)[:blocked_id], blocker: current_user) authorize @user_block if @user_block.destroy diff --git a/spec/requests/user_blocks_spec.rb b/spec/requests/user_blocks_spec.rb index 5a2d1ef54..07445cc02 100644 --- a/spec/requests/user_blocks_spec.rb +++ b/spec/requests/user_blocks_spec.rb @@ -10,25 +10,22 @@ RSpec.describe "UserBlock", type: :request do it "rejects when not-logged-in" do sign_out(blocker) get "/user_blocks/#{blocked.id}" - json_response = JSON.parse(response.body) expect(response.content_type).to eq "application/json" expect(response.status).to eq 401 - expect(json_response["result"]).to eq "not-logged-in" + expect(response.parsed_body["result"]).to eq "not-logged-in" end it "returns 'not-blocking' when the user is not blocked" do get "/user_blocks/#{blocked.id}" - json_response = JSON.parse(response.body) expect(response.content_type).to eq "application/json" - expect(json_response["result"]).to eq "not-blocking" + expect(response.parsed_body["result"]).to eq "not-blocking" end it "returns 'blocking' when blocking" do create(:user_block, blocker: blocker, blocked: blocked) get "/user_blocks/#{blocked.id}" - json_response = JSON.parse(response.body) expect(response.content_type).to eq "application/json" - expect(json_response["result"]).to eq "blocking" + expect(response.parsed_body["result"]).to eq "blocking" end end @@ -36,10 +33,9 @@ RSpec.describe "UserBlock", type: :request do it "renders 'not-logged-in' when not logged in" do sign_out blocker post "/user_blocks", params: { user_block: { blocked_id: blocked.id } } - json_response = JSON.parse(response.body) expect(response.content_type).to eq "application/json" expect(response.status).to eq 401 - expect(json_response["result"]).to eq "not-logged-in" + expect(response.parsed_body["result"]).to eq "not-logged-in" end it "creates the correct user_block" do @@ -51,9 +47,8 @@ RSpec.describe "UserBlock", type: :request do it "returns a JSON response with blocked" do post "/user_blocks", params: { user_block: { blocked_id: blocked.id } } - json_response = JSON.parse(response.body) expect(response.content_type).to eq "application/json" - expect(json_response["result"]).to eq "blocked" + expect(response.parsed_body["result"]).to eq "blocked" end it "blocks the potential chat channel" do @@ -74,19 +69,23 @@ RSpec.describe "UserBlock", type: :request do it "renders 'not-logged-in' when not logged in" do sign_out blocker delete "/user_blocks/#{blocked.id}", params: { user_block: { blocked_id: blocked.id } } - json_response = JSON.parse(response.body) expect(response.content_type).to eq "application/json" expect(response.status).to eq 401 - expect(json_response["result"]).to eq "not-logged-in" + expect(response.parsed_body["result"]).to eq "not-logged-in" end it "renders 'not-blocking-anyone' if there is no one to unblock" do UserBlock.delete_all blocker.update(blocking_others_count: 0) delete "/user_blocks/#{blocked.id}", params: { user_block: { blocked_id: blocked.id } } - json_response = JSON.parse(response.body) expect(response.content_type).to eq "application/json" - expect(json_response["result"]).to eq "not-blocking-anyone" + expect(response.parsed_body["result"]).to eq "not-blocking-anyone" + end + + it "raises ActiveRecord::RecordNotFound error if UserBlock not found" do + expect do + delete "/user_blocks/#{blocked.id}", params: { user_block: { blocked_id: blocked.id + 10 } } + end.to raise_error(ActiveRecord::RecordNotFound) end it "removes the correct user_block" do