From b00df8c2e78ad34f83a7e2ca8e20528a33ed80c1 Mon Sep 17 00:00:00 2001 From: Frank Rousseau Date: Sun, 5 Nov 2017 22:30:56 +0100 Subject: [PATCH] Adapt API tests to recent changes --- .../api/comments_controller_spec.rb | 41 +++++++++++--- .../api/conversations_controller_spec.rb | 41 +++++++------- spec/integration/api/likes_controller_spec.rb | 22 ++++++-- .../api/messages_controller_spec.rb | 14 ++--- spec/integration/api/posts_controller_spec.rb | 55 +++++++++++++++---- .../api/streams_controller_spec.rb | 10 +++- spec/services/conversation_service.rb | 42 +++++++------- 7 files changed, 153 insertions(+), 72 deletions(-) diff --git a/spec/integration/api/comments_controller_spec.rb b/spec/integration/api/comments_controller_spec.rb index fb506b4d1..d4cfdf446 100644 --- a/spec/integration/api/comments_controller_spec.rb +++ b/spec/integration/api/comments_controller_spec.rb @@ -2,18 +2,26 @@ require "spec_helper" -describe Api::V0::PostsController do +describe Api::V0::CommentsController do let(:auth) { FactoryGirl.create(:auth_with_read_and_write) } let!(:access_token) { auth.create_access_token.to_s } before do - @status = auth.user.post(:status_message, text: "This is a status message", public: true, to: "all") + @status = auth.user.post("Post", + status_message: {text: "This is a status message"}, + public: true, + to: "all", + type: "Post" + ) end describe "#create" do context "valid post ID" do it "succeeds" do - post api_v0_post_comments_path(post_id: @status.id), text: "This is a comment", access_token: access_token + post( + api_v0_post_comments_path(post_id: @status.id), + params: {text: "This is a comment", access_token: access_token} + ) expect(JSON.parse(response.body)["text"]).to eq("This is a comment") end end @@ -22,7 +30,10 @@ describe Api::V0::PostsController do before do post( api_v0_post_comments_path(post_id: @status.id), - text: "This is a long comment" * 99_999, access_token: access_token + params: { + text: "This is a long comment" * 99_999, + access_token: access_token + } ) end @@ -35,23 +46,35 @@ describe Api::V0::PostsController do describe "#delete" do context "valid comment ID" do before do - post api_v0_post_comments_path(post_id: @status.id), text: "This is a comment", access_token: access_token + post( + api_v0_post_comments_path(post_id: @status.id), + params: {text: "This is a comment", access_token: access_token} + ) end it "succeeds" do first_comment_id = JSON.parse(response.body)["id"] - delete api_v0_post_comment_path(id: first_comment_id), access_token: access_token - expect(response.body).to eq("Comment " + first_comment_id.to_s + " has been successfully deleted") + delete( + api_v0_post_comment_path(id: first_comment_id), + params: {access_token: access_token} + ) + expect(response).to be_success end end context "invalid comment ID" do before do - post api_v0_post_comments_path(post_id: @status.id), text: "This is a comment", access_token: access_token + post( + api_v0_post_comments_path(post_id: @status.id), + params: {text: "This is a comment", access_token: access_token} + ) end it "fails to delete" do - delete api_v0_post_comment_path(id: 1_234_567), access_token: access_token + delete( + api_v0_post_comment_path(id: 1_234_567), + params: {access_token: access_token} + ) expect(response.body).to eq("Post or comment not found") end end diff --git a/spec/integration/api/conversations_controller_spec.rb b/spec/integration/api/conversations_controller_spec.rb index 07abd992e..a7e3f7729 100644 --- a/spec/integration/api/conversations_controller_spec.rb +++ b/spec/integration/api/conversations_controller_spec.rb @@ -25,7 +25,7 @@ describe Api::V0::ConversationsController do describe "#create" do context "with valid data" do it "creates the conversation" do - post api_v0_conversations_path, @conversation + post api_v0_conversations_path, params: @conversation @conversation_guid = JSON.parse(response.body)["conversation"]["guid"] conversation = JSON.parse(response.body)["conversation"] @@ -40,7 +40,7 @@ describe Api::V0::ConversationsController do context "without valid data" do it "fails at creating the conversation" do - post api_v0_conversations_path, access_token: access_token + post api_v0_conversations_path, params: { access_token: access_token } expect(response.status).to eq 400 end end @@ -48,10 +48,10 @@ describe Api::V0::ConversationsController do describe "#index" do before do - post api_v0_conversations_path, @conversation - post api_v0_conversations_path, @conversation + post api_v0_conversations_path, params: @conversation + post api_v0_conversations_path, params: @conversation sleep(1) - post api_v0_conversations_path, @conversation + post api_v0_conversations_path, params: @conversation conversation_guid = JSON.parse(response.body)["conversation"]["guid"] conversation = conversation_service.find!(conversation_guid) conversation.conversation_visibilities[0].unread = 1 @@ -62,13 +62,16 @@ describe Api::V0::ConversationsController do end it "returns all the user conversations" do - get api_v0_conversations_path, access_token: access_token + get api_v0_conversations_path, params: { access_token: access_token } expect(response.status).to eq 200 expect(JSON.parse(response.body).length).to eq 3 end it "returns all the user unread conversations" do - get api_v0_conversations_path, unread: true, access_token: access_token + get( + api_v0_conversations_path, + params: { unread: true, access_token: access_token } + ) expect(response.status).to eq 200 expect(JSON.parse(response.body).length).to eq 2 end @@ -76,7 +79,7 @@ describe Api::V0::ConversationsController do it "returns all the user conversations after a given date" do get( api_v0_conversations_path, - only_after: @date, access_token: access_token + params: { only_after: @date, access_token: access_token } ) expect(response.status).to eq 200 expect(JSON.parse(response.body).length).to eq 1 @@ -86,14 +89,14 @@ describe Api::V0::ConversationsController do describe "#show" do context "valid conversation ID" do before do - post api_v0_conversations_path, @conversation + post api_v0_conversations_path, params: @conversation end it "returns the corresponding conversation" do conversation_guid = JSON.parse(response.body)["conversation"]["guid"] get( api_v0_conversation_path(conversation_guid), - access_token: access_token + params: { access_token: access_token } ) expect(response.status).to eq 200 conversation = JSON.parse(response.body)["conversation"] @@ -108,7 +111,7 @@ describe Api::V0::ConversationsController do it "returns a not found error (404)" do get( api_v0_conversation_path(42), - access_token: access_token + params: { access_token: access_token } ) expect(response.status).to eq 404 end @@ -130,7 +133,7 @@ describe Api::V0::ConversationsController do recipients: [auth_participant.user.person.id], access_token: access_token } - post api_v0_conversations_path, @conversation + post api_v0_conversations_path, params: @conversation @conversation_guid = JSON.parse(response.body)["conversation"]["guid"] end @@ -138,17 +141,17 @@ describe Api::V0::ConversationsController do it "destroys the first participant visibility" do delete( api_v0_conversation_path(@conversation_guid), - access_token: access_token + params: { access_token: access_token } ) expect(response.status).to eq 204 get api_v0_conversation_path( @conversation_guid, - access_token: access_token + params: { access_token: access_token } ) expect(response.status).to eq 404 get api_v0_conversation_path( @conversation_guid, - access_token: access_token_participant + params: { access_token: access_token_participant } ) expect(response.status).to eq 200 end @@ -158,17 +161,17 @@ describe Api::V0::ConversationsController do it "destroys the second participant visibilty and the conversation" do delete( api_v0_conversation_path(@conversation_guid), - access_token: access_token + params: { access_token: access_token } ) delete( api_v0_conversation_path(@conversation_guid), - access_token: access_token_participant + params: { access_token: access_token_participant } ) expect(response.status).to eq 204 get api_v0_conversation_path( @conversation_guid, - access_token: access_token_participant + params: { access_token: access_token_participant } ) expect(response.status).to eq 404 @@ -182,7 +185,7 @@ describe Api::V0::ConversationsController do it "returns a not found error (404)" do delete( api_v0_conversation_path(42), - access_token: access_token + params: { access_token: access_token } ) expect(response.status).to eq 404 end diff --git a/spec/integration/api/likes_controller_spec.rb b/spec/integration/api/likes_controller_spec.rb index a306220d8..73a0b3320 100644 --- a/spec/integration/api/likes_controller_spec.rb +++ b/spec/integration/api/likes_controller_spec.rb @@ -12,30 +12,42 @@ describe Api::V0::LikesController do describe "#create" do it "returns the expected author" do - post api_v0_post_likes_path(post_id: @status.id), access_token: access_token + post( + api_v0_post_likes_path(post_id: @status.id), + params: { access_token: access_token } + ) json = JSON.parse(response.body) expect(json["author"]["id"]).to eq(auth.user.person.id) end it "fails on random post id" do - post api_v0_post_likes_path(post_id: 99_999_999), access_token: access_token + post api_v0_post_likes_path(post_id: 99_999_999), params: {access_token: access_token} expect(response.body).to eq("Post or like not found") end end describe "#delete" do before do - post api_v0_post_likes_path(post_id: @status.id), access_token: access_token + post( + api_v0_post_likes_path(post_id: @status.id), + params: { access_token: access_token } + ) @like_id = JSON.parse(response.body)["id"] end it "succeeds" do - delete api_v0_post_like_path(post_id: @status.id, id: @like_id), access_token: access_token + delete( + api_v0_post_like_path(post_id: @status.id, id: @like_id), + params: { access_token: access_token } + ) expect(response).to be_success end it "fails on random like id" do - delete api_v0_post_like_path(post_id: @status.id, id: 99_999_999), access_token: access_token + delete( + api_v0_post_like_path(post_id: @status.id, id: 99_999_999), + params: { access_token: access_token } + ) expect(response.body).to eq("Post or like not found") end end diff --git a/spec/integration/api/messages_controller_spec.rb b/spec/integration/api/messages_controller_spec.rb index 4a5a89bb7..cb5aa6ece 100644 --- a/spec/integration/api/messages_controller_spec.rb +++ b/spec/integration/api/messages_controller_spec.rb @@ -27,7 +27,7 @@ describe Api::V0::MessagesController do describe "#create " do before do - post api_v0_conversations_path, @conversation + post api_v0_conversations_path, params: @conversation @conversation_guid = JSON.parse(response.body)["conversation"]["guid"] end @@ -35,7 +35,7 @@ describe Api::V0::MessagesController do it "creates the message in the conversation scope" do post( api_v0_conversation_messages_path(@conversation_guid), - body: @message, access_token: access_token + params: {body: @message, access_token: access_token} ) expect(response.status).to eq 201 @@ -47,7 +47,7 @@ describe Api::V0::MessagesController do get( api_v0_conversation_messages_path(@conversation_guid), - access_token: access_token + params: {access_token: access_token} ) messages = JSON.parse(response.body) expect(messages.length).to eq 2 @@ -60,7 +60,7 @@ describe Api::V0::MessagesController do it "returns a wrong parameter error (400)" do post( api_v0_conversation_messages_path(@conversation_guid), - access_token: access_token + params: {access_token: access_token} ) expect(response.status).to eq 400 end @@ -70,7 +70,7 @@ describe Api::V0::MessagesController do it "returns a a not found error (404)" do post( api_v0_conversation_messages_path(42), - access_token: access_token + params: {access_token: access_token} ) expect(response.status).to eq 404 end @@ -79,7 +79,7 @@ describe Api::V0::MessagesController do describe "#index " do before do - post api_v0_conversations_path, @conversation + post api_v0_conversations_path, params: @conversation @conversation_guid = JSON.parse(response.body)["conversation"]["guid"] end @@ -87,7 +87,7 @@ describe Api::V0::MessagesController do it "returns all messages related to conversation" do get( api_v0_conversation_messages_path(@conversation_guid), - access_token: access_token + params: {access_token: access_token} ) messages = JSON.parse(response.body) expect(messages.length).to eq 1 diff --git a/spec/integration/api/posts_controller_spec.rb b/spec/integration/api/posts_controller_spec.rb index af56363fe..038668025 100644 --- a/spec/integration/api/posts_controller_spec.rb +++ b/spec/integration/api/posts_controller_spec.rb @@ -22,14 +22,23 @@ describe Api::V0::PostsController do it "shows attempts to show the info and mark the user notifications" do expect(post_service_double).to receive(:mark_user_notifications) @status = auth_with_read.user.post(:status_message, text: "hello", public: true, to: "all") - get api_v0_post_path(@status.id), access_token: access_token_with_read + get( + api_v0_post_path(@status.id), + params: {access_token: access_token_with_read} + ) end end context "when mark notifications is false" do it "shows attempts to show the info" do @status = auth_with_read.user.post(:status_message, text: "hello", public: true, to: "all") - get api_v0_post_path(@status.id), access_token: access_token_with_read, mark_notifications: "false" + get( + api_v0_post_path(@status.id), + params: { + access_token: access_token_with_read, + mark_notifications: "false" + } + ) end end end @@ -37,22 +46,40 @@ describe Api::V0::PostsController do describe "#create" do context "when given read-write access token" do it "creates a public post" do - post api_v0_posts_path, access_token: access_token_with_read_and_write, - status_message: {text: "Hello this is a public post!"}, aspect_ids: "public" - expect(Post.find_by(text: "Hello this is a public post!").public).to eq(true) + post( + api_v0_posts_path, + params: { + access_token: access_token_with_read_and_write, + status_message: {text: "Hello this is a public post!"}, + aspect_ids: "public" + } + ) + expect(Post.find_by_text("Hello this is a public post!").public).to eq(true) end it "creates a private post" do - post api_v0_posts_path, access_token: access_token_with_read_and_write, - status_message: {text: "Hello this is a post!"}, aspect_ids: "1" + response = post( + api_v0_posts_path, + params: { + access_token: access_token_with_read_and_write, + status_message: {text: "Hello this is a post!"}, + aspect_ids: "1" + } + ) expect(Post.find_by(text: "Hello this is a post!").public).to eq(false) end end context "when given read only access token" do before do - post api_v0_posts_path, access_token: access_token_with_read, - status_message: {text: "Hello this is a post!"}, aspect_ids: "public" + post( + api_v0_posts_path, + params: { + access_token: access_token_with_read, + status_message: {text: "Hello this is a post!"}, + aspect_ids: "public" + } + ) end it "doesn't create the post" do @@ -67,14 +94,20 @@ describe Api::V0::PostsController do it "attempts to destroy the post" do expect(post_service_double).to receive(:retract_post) @status = auth_with_read_and_write.user.post(:status_message, text: "hello", public: true, to: "all") - delete api_v0_post_path(@status.id), access_token: access_token_with_read_and_write + delete( + api_v0_post_path(@status.id), + params: {access_token: access_token_with_read_and_write} + ) end end context "when given read only access token" do before do @status = auth_with_read.user.post(:status_message, text: "hello", public: true, to: "all") - delete api_v0_post_path(@status.id), access_token: access_token_with_read + delete( + api_v0_post_path(@status.id), + params: {access_token: access_token_with_read} + ) end it "doesn't delete the post" do diff --git a/spec/integration/api/streams_controller_spec.rb b/spec/integration/api/streams_controller_spec.rb index 8c951b3fc..1177c6571 100644 --- a/spec/integration/api/streams_controller_spec.rb +++ b/spec/integration/api/streams_controller_spec.rb @@ -13,12 +13,18 @@ describe Api::V0::PostsController do describe "#aspect" do it "contains expected aspect message" do - get api_v0_aspects_stream_path(a_ids: [@aspect.id]), access_token: access_token + get( + api_v0_aspects_stream_path(a_ids: [@aspect.id]), + params: {access_token: access_token} + ) expect(response.body).to include("This is a status message") end it "does not save to requested aspects to session" do - get api_v0_aspects_stream_path(a_ids: [@aspect.id]), access_token: access_token + get( + api_v0_aspects_stream_path(a_ids: [@aspect.id]), + params: {access_token: access_token} + ) expect(session[:a_ids]).to be_nil end end diff --git a/spec/services/conversation_service.rb b/spec/services/conversation_service.rb index 37d26b8ed..7a04b3a78 100644 --- a/spec/services/conversation_service.rb +++ b/spec/services/conversation_service.rb @@ -1,13 +1,15 @@ # frozen_string_literal: true describe ConversationService do - opts = { - subject: "conversation subject", - message: {text: "conversation text"}, - participant_ids: [bob.person.id] - } - conversation = alice.build_conversation(opts) - conversation.save + before do + opts = { + subject: "conversation subject", + message: {text: "conversation text"}, + participant_ids: [bob.person.id] + } + @conversation = alice.build_conversation(opts) + @conversation.save + end describe "#all_for_user" do before do @@ -23,9 +25,9 @@ describe ConversationService do opts = { subject: "conversation subject 3", message: {text: "conversation text 3"}, - participant_ids: [bob.person.id] + participant_ids: [] } - @conversation = alice.build_conversation(opts) + @conversation = bob.build_conversation(opts) @conversation.save! end @@ -49,14 +51,14 @@ describe ConversationService do describe "#find!" do it "returns the conversation, if it is the user's conversation" do - expect(bob_conversation_service.find!(conversation.guid)).to eq( - conversation + expect(bob_conversation_service.find!(@conversation.guid)).to eq( + @conversation ) end it "returns the conversation, if the user is recipient" do - expect(bob_conversation_service.find!(conversation.guid)).to eq( - conversation + expect(bob_conversation_service.find!(@conversation.guid)).to eq( + @conversation ) end @@ -68,7 +70,7 @@ describe ConversationService do it "raises RecordNotFound if the user is not recipient" do expect { - eve_conversation_service.find!(conversation.guid) + eve_conversation_service.find!(@conversation.guid) }.to raise_error ActiveRecord::RecordNotFound end end @@ -101,22 +103,24 @@ describe ConversationService do describe "#get_visibility" do it "returns visibility for current user" do - visibility = alice_conversation_service.get_visibility(conversation.guid) + visibility = alice_conversation_service.get_visibility( + @conversation.guid + ) expect(visibility).to_not be_nil end it "raises RecordNotFound if the user has no visibility" do expect { - eve_conversation_service.get_visibility(conversation.id) + eve_conversation_service.get_visibility(@conversation.id) }.to raise_error ActiveRecord::RecordNotFound end end describe "#destroy!" do it "deletes the conversation, when it is the user conversation" do - alice_conversation_service.destroy!(conversation.guid) + alice_conversation_service.destroy!(@conversation.guid) expect { - alice_conversation_service.find!(conversation.guid) + alice_conversation_service.find!(@conversation.guid) }.to raise_error ActiveRecord::RecordNotFound end @@ -128,7 +132,7 @@ describe ConversationService do it "raises RecordNotFound if the user is not part of the conversation" do expect { - eve_conversation_service.destroy!(conversation.guid) + eve_conversation_service.destroy!(@conversation.guid) }.to raise_error ActiveRecord::RecordNotFound end end