raise AR not found error if Userblock is not found (#7388)

This commit is contained in:
Molly Struve 2020-04-20 09:56:21 -05:00 committed by GitHub
parent e97d2ccf63
commit 2f6952ed38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 15 deletions

View file

@ -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

View file

@ -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