docbrown/spec/requests/user_blocks_spec.rb
Andy Zhao 73caa9a864 New Feature: Block Users (#4411)
* Prevent need for eager loading

* Add initial implementation of user block

* Remove debugger oops

* Add index and foreign keys for user_block table

* Use private method instead of exists

* Move channel handling logic to service object

* Update styling a bit

* Use profileDropdown file for future proofing

* Remove commented out code

* Render json: { result } for all endpoints

* Add statuses for sad paths

* Add better sad path handling in the JS

* Remove accidentally committed spec file

* Don't wait for DOM to load block button

* Use equality for accuracy in slug query

* Add status code for unauthorized requests

* Add missing comma sigh
2019-10-23 17:14:28 -04:00

105 lines
4.4 KiB
Ruby

require "rails_helper"
RSpec.describe "UserBlock", type: :request do
let(:blocker) { create(:user) }
let(:blocked) { create(:user) }
before { sign_in blocker }
describe "GET /user_blocks/:blocked_id or #show" 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"
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"
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"
end
end
describe "POST /user_blocks/:blocked_id or #create" do
it "renders 'not-logged-in' when not logged in" do
sign_out blocker
post "/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"
end
it "creates the correct user_block" do
post "/user_blocks/#{blocked.id}", params: { user_block: { blocked_id: blocked.id } }
expect(UserBlock.count).to eq 1
expect(UserBlock.first.blocker_id).to eq blocker.id
expect(UserBlock.first.blocked_id).to eq blocked.id
end
it "returns a JSON response with blocked" do
post "/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 "blocked"
end
it "blocks the potential chat channel" do
chat_channel = create(:chat_channel, channel_type: "direct", slug: "#{blocker.username}/#{blocked.username}", status: "active")
create(:chat_channel_membership, chat_channel_id: chat_channel.id, user_id: blocker.id)
create(:chat_channel_membership, chat_channel_id: chat_channel.id, user_id: blocked.id)
post "/user_blocks/#{blocked.id}", params: { user_block: { blocked_id: blocked.id } }
expect(chat_channel.reload.status).to eq "blocked"
end
end
describe "DELETE /user_blocks/:blocked_id or #delete" do
before do
create(:user_block, blocker: blocker, blocked: blocked)
blocker.update(blocking_others_count: 1)
end
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"
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"
end
it "removes the correct user_block" do
delete "/user_blocks/#{blocked.id}", params: { user_block: { blocked_id: blocked.id } }
expect(blocker.blocking?(blocked)).to eq false
end
it "unblocks the direct chat channel" do
chat_channel = create(:chat_channel, channel_type: "direct", slug: "#{blocker.username}/#{blocked.username}", status: "blocked")
create(:chat_channel_membership, chat_channel_id: chat_channel.id, user_id: blocker.id)
create(:chat_channel_membership, chat_channel_id: chat_channel.id, user_id: blocked.id)
delete "/user_blocks/#{blocked.id}", params: { user_block: { blocked_id: blocked.id } }
expect(chat_channel.reload.status).to eq "active"
end
end
end