diff --git a/app/controllers/api/v0/conversations_controller.rb b/app/controllers/api/v0/conversations_controller.rb new file mode 100644 index 000000000..638b1a129 --- /dev/null +++ b/app/controllers/api/v0/conversations_controller.rb @@ -0,0 +1,61 @@ +module Api + module V0 + class ConversationsController < Api::V0::BaseController + include ConversationsHelper + + before_action only: %i(create index show) do + require_access_token %w(read) + end + + before_action only: %i(create 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 index + conversations = conversation_service.all_for_user + render json: conversations.map {|x| conversation_as_json(x) } + end + + def show + conversation = conversation_service.find!(params[:id]) + render json: { + conversation: conversation_as_json(conversation) + } + end + + def create + conversation = conversation_service.build( + params[:subject], + params[:text], + params[:recipients] + ) + conversation.save! + Diaspora::Federation::Dispatcher.defer_dispatch( + current_user, + conversation + ) + + render json: { + conversation: conversation_as_json(conversation) + }, status: 201 + end + + def destroy + conversation_service.destroy!(params[:id]) + head :no_content + end + + def conversation_service + ConversationService.new(current_user) + end + + def conversation_as_json(conversation) + ConversationPresenter.new(conversation).as_json + end + end + end +end diff --git a/spec/integration/api/conversations_controller_spec.rb b/spec/integration/api/conversations_controller_spec.rb new file mode 100644 index 000000000..ac84f7ced --- /dev/null +++ b/spec/integration/api/conversations_controller_spec.rb @@ -0,0 +1,119 @@ +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 } + + before do + auth.user.share_with bob.person, auth.user.aspects[0] + auth.user.share_with alice.person, auth.user.aspects[0] + alice.share_with auth.user.person, alice.aspects[0] + + @conversation = { + author_id: auth.user.id, + subject: "new conversation", + text: "first message", + messages: [{ + author: auth.user, + text: "first message" + }], + recipients: [alice.person.id], + access_token: access_token + } + end + + describe "#create" do + context "with valid data" do + it "creates the conversation" do + post api_v0_conversations_path, @conversation + response_body = JSON.parse(response.body)["conversation"] + + expect(response.status).to eq 201 + expect(response_body["messages"][0]["id"]).to_not be_nil + expect(response_body["id"]).to_not be_nil + expect(response_body["participants"].length).to eq 2 + end + end + + context "without valid data" do + it "fails at creating the conversation" do + post api_v0_conversations_path, access_token: access_token + expect(response.status).to eq 400 + end + end + end + + describe "#index" do + before do + post api_v0_conversations_path, @conversation + post api_v0_conversations_path, @conversation + end + + it "returns all the user conversations" do + get api_v0_conversations_path, access_token: access_token + expect(response.status).to eq 200 + expect(JSON.parse(response.body).length).to eq 2 + end + end + + describe "#show" do + context "valid conversation ID" do + before do + post api_v0_conversations_path, @conversation + end + + it "returns the corresponding conversation" do + conversation_id = JSON.parse(response.body)["conversation"]["id"] + get( + api_v0_conversation_path(conversation_id), + access_token: access_token + ) + expect(response.status).to eq 200 + result_id = JSON.parse(response.body)["conversation"]["id"] + expect(result_id).to eq conversation_id + end + end + + context "non existing conversation ID" do + it "returns a not found error (404)" do + get( + api_v0_conversation_path(42), + access_token: access_token + ) + expect(response.status).to eq 404 + end + end + end + + describe "#delete" do + context "valid conversation ID" do + before do + post api_v0_conversations_path, @conversation + end + + it "deletes the conversation" do + conversation_id = JSON.parse(response.body)["conversation"]["id"] + delete( + api_v0_conversation_path(conversation_id), + access_token: access_token + ) + expect(response.status).to eq 204 + get( + api_v0_conversation_path(conversation_id), + access_token: access_token + ) + expect(response.status).to eq 404 + end + end + + context "non existing conversation ID" do + it "returns a not found error (404)" do + delete( + api_v0_conversation_path(42), + access_token: access_token + ) + expect(response.status).to eq 404 + end + end + end +end