* Initial automatic cleanup with rubocop * Fix syntax error introduced by rubocop * Cleanup seeds file * Cleanup lib folder * Exclude bin folder because it contains auto generated files * Make Rubocop a little bit more chatty * Block length should not include comments in the count * Cleanup config folder * Cleanup specs * Updated Rubocop version and generated a todo file * Fix broken ArticlesApi spec * Fix tests * Restored rubocop pre-commit hook
192 lines
6.5 KiB
Ruby
192 lines
6.5 KiB
Ruby
require "rails_helper"
|
|
|
|
RSpec.describe "ChatChannels", type: :request do
|
|
let(:user) { create(:user) }
|
|
let(:test_subject) { create(:user) }
|
|
let(:chat_channel) { create(:chat_channel) }
|
|
let(:invite_channel) { create(:chat_channel, channel_type: "invite_only") }
|
|
let(:direct_channel) do
|
|
create(:chat_channel, channel_type: "direct", slug: "hello/#{user.username}")
|
|
end
|
|
|
|
before do
|
|
sign_in user
|
|
chat_channel.add_users([user])
|
|
end
|
|
|
|
describe "GET /connect" do
|
|
context "when logged in" do
|
|
before do
|
|
get "/connect"
|
|
end
|
|
|
|
it "has proper content" do
|
|
expect(response.body).to include("chat-page-wrapper")
|
|
end
|
|
end
|
|
|
|
context "when logged in and visiting existing channel" do
|
|
before do
|
|
invite_channel.add_users [user]
|
|
sign_in user
|
|
get "/connect/#{invite_channel.slug}"
|
|
end
|
|
|
|
it "has proper content" do
|
|
expect(response.body).to include("chat-page-wrapper")
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "get /chat_channels?state=unopened" do
|
|
it "returns unopened channels" do
|
|
direct_channel.add_users [user]
|
|
user.chat_channel_memberships.each { |m| m.update(has_unopened_messages: true) }
|
|
sign_in user
|
|
get "/chat_channels?state=unopened"
|
|
expect(response.body).to include(direct_channel.slug)
|
|
end
|
|
end
|
|
|
|
describe "get /chat_channels?state=pending" do
|
|
it "returns pending channels" do
|
|
ChatChannelMembership.create(chat_channel_id: invite_channel.id, user_id: user.id, status: "pending") # rubocop:disable Metrics/LineLength
|
|
sign_in user
|
|
get "/chat_channels?state=pending"
|
|
expect(response.body).to include(invite_channel.slug)
|
|
end
|
|
|
|
it "returns no pending channels if no invites" do
|
|
sign_in user
|
|
get "/chat_channels?state=pending"
|
|
expect(response.body).not_to include(invite_channel.slug)
|
|
end
|
|
|
|
it "returns no pending channels if not pending" do
|
|
ChatChannelMembership.create(chat_channel_id: invite_channel.id, user_id: user.id, status: "rejected") # rubocop:disable Metrics/LineLength
|
|
sign_in user
|
|
get "/chat_channels?state=pending"
|
|
expect(response.body).not_to include(invite_channel.slug)
|
|
end
|
|
end
|
|
|
|
describe "GET /chat_channels/:id" do
|
|
context "when request is valid" do
|
|
before do
|
|
get "/chat_channels/#{chat_channel.id}", headers: { HTTP_ACCEPT: "application/json" }
|
|
end
|
|
|
|
it "returns 200" do
|
|
expect(response.status).to eq(200)
|
|
end
|
|
|
|
it "returns the channel" do
|
|
expect(response).to render_template(:show)
|
|
end
|
|
end
|
|
|
|
context "when request is invalid" do
|
|
it "returns proper error message" do
|
|
expect { get "/chat_channels/1200" }.to raise_error(ActionController::RoutingError)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "POST /chat_channels" do
|
|
# rubocop:disable RSpec/MultipleExpectations
|
|
it "creates chat_channel for current user" do
|
|
post "/chat_channels",
|
|
params: { chat_channel: { channel_name: "Hello Channel", slug: "hello-channel" } },
|
|
headers: { HTTP_ACCEPT: "application/json" }
|
|
expect(ChatChannel.last.slug).to eq("hello-channel")
|
|
expect(ChatChannel.last.active_users).to include(user)
|
|
end
|
|
# rubocop:enable RSpec/MultipleExpectations
|
|
|
|
it "returns errors if channel is invalid" do
|
|
# slug should be taken
|
|
post "/chat_channels",
|
|
params: { chat_channel: { channel_name: "HEy hey hoho", slug: chat_channel.slug } },
|
|
headers: { HTTP_ACCEPT: "application/json" }
|
|
expect(response.body).to include("Slug has already been taken")
|
|
end
|
|
end
|
|
|
|
describe "PUT /chat_channels/:id" do
|
|
it "updates channel for valid user" do
|
|
user.add_role(:super_admin)
|
|
put "/chat_channels/#{chat_channel.id}",
|
|
params: { chat_channel: { channel_name: "Hello Channel", slug: "hello-channelly" } },
|
|
headers: { HTTP_ACCEPT: "application/json" }
|
|
expect(ChatChannel.last.slug).to eq("hello-channelly")
|
|
end
|
|
it "dissallows invalid users" do
|
|
expect do
|
|
put "/chat_channels/#{chat_channel.id}",
|
|
params: { chat_channel: { channel_name: "Hello Channel", slug: "hello-channelly" } },
|
|
headers: { HTTP_ACCEPT: "application/json" }
|
|
end.to raise_error(Pundit::NotAuthorizedError)
|
|
end
|
|
it "returns errors if channel is invalid" do
|
|
# slug should be taken
|
|
user.add_role(:super_admin)
|
|
put "/chat_channels/#{chat_channel.id}",
|
|
params: { chat_channel: { channel_name: "HEy hey hoho", slug: invite_channel.slug } },
|
|
headers: { HTTP_ACCEPT: "application/json" }
|
|
expect(response.body).to include("Slug has already been taken")
|
|
end
|
|
end
|
|
|
|
describe "POST /chat_channels/:id/moderate" do
|
|
it "raises NotAuthorizedError if user is not logged in" do
|
|
expect do
|
|
post "/chat_channels/#{chat_channel.id}/moderate",
|
|
params: { chat_channel: { command: "/ban huh" } },
|
|
headers: { HTTP_ACCEPT: "application/json" }
|
|
end.to raise_error(Pundit::NotAuthorizedError)
|
|
end
|
|
|
|
# rubocop:disable RSpec/ExampleLength
|
|
it "raises NotAuthorizedError if user is logged in but not authorized" do
|
|
sign_in user
|
|
expect do
|
|
post "/chat_channels/#{chat_channel.id}/moderate",
|
|
params: { chat_channel: { command: "/ban huh" } },
|
|
headers: { HTTP_ACCEPT: "application/json" }
|
|
end.to raise_error(Pundit::NotAuthorizedError)
|
|
end
|
|
# rubocop:enable RSpec/ExampleLength
|
|
|
|
context "when user is logged-in and authorized" do
|
|
before do
|
|
user.add_role :super_admin
|
|
sign_in user
|
|
allow(Pusher).to receive(:trigger).and_return(true)
|
|
end
|
|
|
|
it "enforces chat_channel_params on ban" do
|
|
post "/chat_channels/#{chat_channel.id}/moderate",
|
|
params: { chat_channel: { command: "/ban #{test_subject.username}" } }
|
|
expect(response.status).to eq(200)
|
|
end
|
|
|
|
it "enforces chat_channel_params on unban" do
|
|
post "/chat_channels/#{chat_channel.id}/moderate",
|
|
params: { chat_channel: { command: "/unban #{test_subject.username}" } }
|
|
expect(response.status).to eq(200)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "POST /chat_channels/:id/open" do
|
|
it "returns success" do
|
|
post "/chat_channels/#{chat_channel.id}/open"
|
|
expect(response.body).to include("success")
|
|
end
|
|
|
|
it "marks chat_channel_membership as opened" do
|
|
post "/chat_channels/#{chat_channel.id}/open"
|
|
expect(user.chat_channel_memberships.last.has_unopened_messages).to eq(false)
|
|
end
|
|
end
|
|
end
|