From f8ba88408bda1bb0fbe9bceab268b16ea8ab9e6e Mon Sep 17 00:00:00 2001 From: Frank Rousseau Date: Sun, 21 May 2017 00:34:40 +0200 Subject: [PATCH] =?UTF-8?q?Add=20messages=20API=C2=A0controller?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/v0/messages_controller.rb | 26 ++++++++ .../api/messages_controller_spec.rb | 65 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 app/controllers/api/v0/messages_controller.rb create mode 100644 spec/integration/api/messages_controller_spec.rb diff --git a/app/controllers/api/v0/messages_controller.rb b/app/controllers/api/v0/messages_controller.rb new file mode 100644 index 000000000..3d4df5b4d --- /dev/null +++ b/app/controllers/api/v0/messages_controller.rb @@ -0,0 +1,26 @@ +module Api + module V0 + class MessagesController < Api::V0::BaseController + before_action only: %i(create) 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 create + conversation = conversation_service.find!(params[:conversation_id]) + opts = params.require(:message).permit(:text) + message = current_user.build_message(conversation, opts) + message.save! + Diaspora::Federation::Dispatcher.defer_dispatch(current_user, message) + render json: message, status: 201 + end + + def conversation_service + ConversationService.new(current_user) + end + end + end +end diff --git a/spec/integration/api/messages_controller_spec.rb b/spec/integration/api/messages_controller_spec.rb new file mode 100644 index 000000000..2c3a605c8 --- /dev/null +++ b/spec/integration/api/messages_controller_spec.rb @@ -0,0 +1,65 @@ +require "spec_helper" + +describe Api::V0::MessagesController do + let(:auth) { FactoryGirl.create(:auth_with_read_and_write) } + let!(:access_token) { auth.create_access_token.to_s } + + before do + auth.user.seed_aspects + auth.user.share_with bob.person, auth.user.aspects[1] + auth.user.share_with alice.person, auth.user.aspects[1] + 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 + } + + @message = { + text: "reply to first message" + } + end + + describe "#create " do + before do + post api_v0_conversations_path, @conversation + @conversation_id = JSON.parse(response.body)["conversation"]["id"] + end + + context "with valid data" do + it "creates the message in the conversation scope" do + post( + api_v0_conversation_messages_path(@conversation_id), + message: @message, access_token: access_token + ) + expect(response.status).to eq 201 + expect(JSON.parse(response.body)["message"]["id"]).to_not be_nil + get( + api_v0_conversation_path(@conversation_id), + access_token: access_token + ) + response_body = JSON.parse(response.body)["conversation"] + expect(response_body["messages"].length).to eq 2 + text = response_body["messages"][1]["text"] + expect(text).to eq "reply to first message" + end + end + + context "without valid data" do + it "returns a wrong parameter error (400)" do + post( + api_v0_conversation_messages_path(@conversation_id), + access_token: access_token + ) + expect(response.status).to eq 400 + end + end + end +end