Add conversation visibilities API controller

This commit is contained in:
Frank Rousseau 2017-05-21 00:35:24 +02:00
parent 43a8cbff5d
commit 84f972b368
2 changed files with 115 additions and 0 deletions

View file

@ -0,0 +1,32 @@
module Api
module V0
class ConversationVisibilitiesController < Api::V0::BaseController
before_action only: %i(destroy) do
require_access_token %w(read write)
end
rescue_from ActiveRecord::RecordNotFound do
render json: {error: I18n.t("conversations.not_found")}, status: 404
end
def destroy
vis = conversation_service.get_visibility(params[:conversation_id])
participants = vis.conversation.participants.count
vis.destroy!
if participants == 1
render json: {
message: I18n.t("conversations.destroy.delete_success")
}
else
render json: {
message: I18n.t("conversations.destroy.hide_success")
}
end
end
def conversation_service
ConversationService.new(current_user)
end
end
end
end

View file

@ -0,0 +1,83 @@
require "spec_helper"
describe Api::V0::ConversationVisibilitiesController do
let(:auth) { FactoryGirl.create(:auth_with_read_and_write) }
let(:auth_participant) { FactoryGirl.create(:auth_with_read_and_write) }
let!(:access_token) { auth.create_access_token.to_s }
let!(:access_token_participant) { auth_participant.create_access_token.to_s }
before do
auth.user.seed_aspects
auth.user.share_with auth_participant.user.person, auth.user.aspects[1]
auth_participant.user.share_with(
auth.user.person, auth_participant.user.aspects[0]
)
@conversation = {
author_id: auth.user.id,
subject: "new conversation",
text: "first message",
messages: [{
author: auth.user,
text: "first message"
}],
recipients: [auth_participant.user.person.id],
access_token: access_token
}
end
describe "#destroy " do
before do
post api_v0_conversations_path, @conversation
@conversation_id = JSON.parse(response.body)["conversation"]["id"]
end
context "destroy" do
it "destroys the first participant visibility" do
delete(
api_v0_conversation_visibility_path(@conversation_id),
access_token: access_token
)
expect(response.status).to eq 200
expect(JSON.parse(response.body)["message"]).to_not be_nil
get api_v0_conversation_path(
@conversation_id,
access_token: access_token
)
expect(response.status).to eq 404
get api_v0_conversation_path(
@conversation_id,
access_token: access_token_participant
)
expect(response.status).to eq 200
end
end
context "destroy again" do
it "destroys the second participant visibilty and the conversation" do
delete(
api_v0_conversation_visibility_path(@conversation_id),
access_token: access_token
)
delete(
api_v0_conversation_visibility_path(@conversation_id),
access_token: access_token_participant
)
expect(response.status).to eq 200
expect(JSON.parse(response.body)["message"]).to_not be_nil
get api_v0_conversation_path(
@conversation_id,
access_token: access_token_participant
)
expect(response.status).to eq 404
expect {
Conversation.find(@conversation_id)
}.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
end