From 3f00195eedffeb08753b2c954823fbd249353942 Mon Sep 17 00:00:00 2001 From: Frank Rousseau Date: Sat, 3 Jun 2017 12:32:03 +0200 Subject: [PATCH] Merge conv deletion and visibility deletion --- .../conversation_visibilities_controller.rb | 32 ------- .../api/v0/conversations_controller.rb | 3 +- .../api/conversation_visibilities_spec.rb | 83 ------------------- .../api/conversations_controller_spec.rb | 64 +++++++++++--- 4 files changed, 56 insertions(+), 126 deletions(-) delete mode 100644 app/controllers/api/v0/conversation_visibilities_controller.rb delete mode 100644 spec/integration/api/conversation_visibilities_spec.rb diff --git a/app/controllers/api/v0/conversation_visibilities_controller.rb b/app/controllers/api/v0/conversation_visibilities_controller.rb deleted file mode 100644 index b70abe077..000000000 --- a/app/controllers/api/v0/conversation_visibilities_controller.rb +++ /dev/null @@ -1,32 +0,0 @@ -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 diff --git a/app/controllers/api/v0/conversations_controller.rb b/app/controllers/api/v0/conversations_controller.rb index e977297b5..8e394396e 100644 --- a/app/controllers/api/v0/conversations_controller.rb +++ b/app/controllers/api/v0/conversations_controller.rb @@ -45,7 +45,8 @@ module Api end def destroy - conversation_service.destroy!(params[:id]) + vis = conversation_service.get_visibility(params[:id]) + vis.destroy! head :no_content end diff --git a/spec/integration/api/conversation_visibilities_spec.rb b/spec/integration/api/conversation_visibilities_spec.rb deleted file mode 100644 index 437f9fc11..000000000 --- a/spec/integration/api/conversation_visibilities_spec.rb +++ /dev/null @@ -1,83 +0,0 @@ -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 diff --git a/spec/integration/api/conversations_controller_spec.rb b/spec/integration/api/conversations_controller_spec.rb index e6c45adec..5024111c5 100644 --- a/spec/integration/api/conversations_controller_spec.rb +++ b/spec/integration/api/conversations_controller_spec.rb @@ -3,6 +3,8 @@ require "spec_helper" describe Api::V0::ConversationsController do let(:auth) { FactoryGirl.create(:auth_with_read_and_write) } let!(:access_token) { auth.create_access_token.to_s } + let(:auth_participant) { FactoryGirl.create(:auth_with_read_and_write) } + let!(:access_token_participant) { auth_participant.create_access_token.to_s } before do auth.user.share_with bob.person, auth.user.aspects[0] @@ -86,24 +88,66 @@ describe Api::V0::ConversationsController do end end - describe "#delete" do - context "valid conversation GUID" do - before do - post api_v0_conversations_path, @conversation - end + describe "#destroy " do + 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] + ) - it "deletes the conversation" do - conversation_guid = JSON.parse(response.body)["conversation"]["guid"] + @conversation = { + author_id: auth.user.id, + subject: "new conversation", + body: "first message", + recipients: [auth_participant.user.person.id], + access_token: access_token + } + post api_v0_conversations_path, @conversation + @conversation_guid = JSON.parse(response.body)["conversation"]["guid"] + end + + context "destroy" do + it "destroys the first participant visibility" do delete( - api_v0_conversation_path(conversation_guid), + api_v0_conversation_path(@conversation_guid), access_token: access_token ) expect(response.status).to eq 204 - get( - api_v0_conversation_path(conversation_guid), + get api_v0_conversation_path( + @conversation_guid, access_token: access_token ) expect(response.status).to eq 404 + get api_v0_conversation_path( + @conversation_guid, + access_token: access_token_participant + ) + expect(response.status).to eq 200 + end + end + + context "destroy all visibilities" do + it "destroys the second participant visibilty and the conversation" do + delete( + api_v0_conversation_path(@conversation_guid), + access_token: access_token + ) + delete( + api_v0_conversation_path(@conversation_guid), + access_token: access_token_participant + ) + expect(response.status).to eq 204 + + get api_v0_conversation_path( + @conversation_guid, + access_token: access_token_participant + ) + expect(response.status).to eq 404 + + expect { + Conversation.find({guid: @conversation_guid}) + }.to raise_error(ActiveRecord::RecordNotFound) end end