Use ApiController for ChatChannels and return not_found error if channel cant be accessed by user (#5194) [deploy]

This commit is contained in:
Molly Struve 2019-12-24 12:12:29 -06:00 committed by Ben Halpern
parent 2d6df70dea
commit 448adfdd23
2 changed files with 29 additions and 2 deletions

View file

@ -1,9 +1,9 @@
module Api
module V0
class ChatChannelsController < ApplicationController
class ChatChannelsController < ApiController
def show
@chat_channel = ChatChannel.find(params[:id])
raise unless @chat_channel.has_member?(current_user)
error_not_found unless @chat_channel.has_member?(current_user)
end
end
end

View file

@ -0,0 +1,27 @@
require "rails_helper"
RSpec.describe "Api::V0::ChatChannels", type: :request do
let(:user) { create(:user) }
let(:chat_channel) { create(:chat_channel) }
let(:invite_channel) { create(:chat_channel, channel_type: "invite_only") }
before { sign_in user }
describe "GET /api/chat_channels/:id" do
it "returns 200 if user is a member of the channel" do
chat_channel.add_users([user])
get "/api/chat_channels/#{chat_channel.id}"
expect(response).to have_http_status(:ok)
end
it "returns a 404 if user is not a memeber of the channel" do
get "/api/chat_channels/#{invite_channel.id}"
expect(response.status).to eq(404)
end
it "returns a 404 if channel is not found" do
get "/api/chat_channels/#{invite_channel.id + 100}"
expect(response.status).to eq(404)
end
end
end