docbrown/spec/requests/chat_channels_spec.rb
Sarthak Sharma 75608e3e55 Feature🚀: Ability to mention anyone in open chats and notify them (#5460) [deploy]
* Feature 🚀 : Ability to delete messages in chat channels

- Sending message ID to frontend
- Deleting Message
- Use pusher to delete message realtime

* Minor Bug 🐞: Show message action only for current user

- User can delete or edit their own messages

* Test cases added

* Bug 🐞: Update message id for receiver

Message id was not sent to receiver by pusher

* Refactoring🛠: Message controller refactoring

* Test Cases📝 : Specs for Delete message added

* Feature 🚀 : Ability to edit messages

* Test Cases📝 : Specs for Edit message added

* Feature 🚀 : Ability to mention member in Group [Frontend]

* Test Cases📝 : Test Cases added

* Feature 🚀 : Ability to mention member in Group [Backend]

* Bug Fix 🐞: Making messages realtime for open channels

* Feature 🚀 and Bug Fix 🐞: Ability to get notification if mentioned in open messages and increasing message number count

* Feature 🚀 : Ability to remove message count on channel open

* Bug Fix 🐞: Making Notification more reliable by checking if the new message is notified to user or not

* Feature 🚀 : Ability to metion @all and notify them

* Bug Fix 🐞: Minor fixes

* Fews tweaks in code

* Bug Fix 🐞: Feature was not present for Invite only Section

* Bug Fix 🐞: In open channels one can't mention all

* Bug Fix 🐞: Markdown conversion for all only in case of invite only

* Specs added

* Delete schema.rb

* Schema file updated

* Code refactoring

* Further Code refactoring backend

* Further Code refactoring backend

* Bug Fix 🐞: Mention List error problem in switching tabs

* Test Snapshots added

* Feature 🚀 : Ability to mention member in Group [Frontend]

* Test Cases📝 : Test Cases added

* Feature 🚀 : Ability to mention member in Group [Backend]

* Bug Fix 🐞: Making messages realtime for open channels

* Feature 🚀 and Bug Fix 🐞: Ability to get notification if mentioned in open messages and increasing message number count

* Feature 🚀 : Ability to remove message count on channel open

* Bug Fix 🐞: Making Notification more reliable by checking if the new message is notified to user or not

* Feature 🚀 : Ability to metion @all and notify them

* Bug Fix 🐞: Minor fixes

* Fews tweaks in code

* Bug Fix 🐞: Feature was not present for Invite only Section

* Bug Fix 🐞: In open channels one can't mention all

* Bug Fix 🐞: Markdown conversion for all only in case of invite only

* Specs added

* Code refactoring

* Further Code refactoring backend

* Further Code refactoring backend

* Bug Fix 🐞: Mention List error problem in switching tabs

* Test Snapshots added

* Feature 🚀: Ability to navigate through user list with keyboard only

* Bug Fix 🐞: Changing color of group avatars in dark themes

* Bug Fix 🐞: Setup pusher channel for open channels also
2020-01-23 18:06:35 -05:00

261 lines
8.9 KiB
Ruby

require "rails_helper"
RSpec.describe "ChatChannels", type: :request do
let(:user) { create(:user) }
let(:user_open_inbox) { create(:user, inbox_type: "open") }
let(:user_closed_inbox) { 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=unopened_ids" do
it "returns unopened chat channel ids" 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_ids"
expect(response.body).to include(direct_channel.id.to_s)
expect(response.body).to include("unopened_ids")
end
it "does not return chat channel ids if not signed in" do
direct_channel.add_users [user]
user.chat_channel_memberships.each { |m| m.update(has_unopened_messages: true) }
sign_out user
expect do
get "/chat_channels?state=unopened_ids"
end.to raise_error(Pundit::NotAuthorizedError)
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")
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")
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).to have_http_status(:ok)
end
it "returns the channel" do
result = { messages: [], chatChannelId: chat_channel.id }.to_json
expect(response.body).to eq(result)
end
end
context "when request is invalid" do
it "returns proper error message" do
expect { get "/chat_channels/1200" }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
describe "POST /chat_channels" do
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
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
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
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
allow(Pusher).to receive(:trigger).and_return(true)
post "/chat_channels/#{chat_channel.id}/open"
expect(response.body).to include("success")
end
it "marks chat_channel_membership as opened" do
allow(Pusher).to receive(:trigger).and_return(true)
post "/chat_channels/#{chat_channel.id}/open"
expect(user.chat_channel_memberships.last.has_unopened_messages).to eq(false)
end
end
describe "POST /chat_channels/create_chat" do
it "creates open chat with user who has open inbox" do
post "/chat_channels/create_chat",
params: { user_id: user_open_inbox.id }
expect(response.status).to eq(200)
end
it "does not create for non-open inbox user" do
post "/chat_channels/create_chat",
params: { user_id: user_closed_inbox.id }
expect(response.status).to eq(400)
end
it "creates ensures new chat channel is created for targeted user" do
post "/chat_channels/create_chat",
params: { user_id: user_open_inbox.id }
expect(user_open_inbox.chat_channel_memberships.size).to eq(1)
end
end
describe "POST /chat_channels/block_chat" do
it "blocks successfully when user has permissions" do
direct_channel.add_users [user]
post "/chat_channels/block_chat",
params: { chat_id: direct_channel.id }
expect(response.status).to eq(200)
end
it "makes chat channel have status of blocked" do
direct_channel.add_users [user]
post "/chat_channels/block_chat",
params: { chat_id: direct_channel.id }
expect(direct_channel.reload.status).to eq("blocked")
end
it "does not block when channel is open" do
expect { post "/chat_channels/block_chat", params: { chat_id: chat_channel.id } }.
to raise_error(Pundit::NotAuthorizedError)
end
it "does not block when user does not have permissions" do
expect { post "/chat_channels/block_chat", params: { chat_id: direct_channel.id } }.
to raise_error(Pundit::NotAuthorizedError)
end
end
end