diff --git a/app/controllers/api/v1/streams_controller.rb b/app/controllers/api/v1/streams_controller.rb index 190a0567f..2f46af2b5 100644 --- a/app/controllers/api/v1/streams_controller.rb +++ b/app/controllers/api/v1/streams_controller.rb @@ -8,7 +8,7 @@ module Api end def aspects - aspect_ids = (params[:a_ids] || []) + aspect_ids = params.has_key?(:aspect_ids) ? JSON.parse(params[:aspect_ids]) : [] @stream = Stream::Aspect.new(current_user, aspect_ids, max_time: max_time) stream_responder end @@ -29,7 +29,7 @@ module Api stream_responder(Stream::Likes) end - def mentioned + def mentions stream_responder(Stream::Mention) end @@ -42,7 +42,7 @@ module Api def stream_responder(stream_klass=nil) @stream = stream_klass.present? ? stream_klass.new(current_user, max_time: max_time) : @stream - render json: @stream.stream_posts.map {|p| LastThreeCommentsDecorator.new(PostPresenter.new(p, current_user)) } + render json: @stream.stream_posts.map {|p| PostPresenter.new(p, current_user).as_api_response } end end end diff --git a/config/routes.rb b/config/routes.rb index 34fcc239a..38cf01e2d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -234,7 +234,7 @@ Rails.application.routes.draw do get "streams/activity" => "streams#activity", :as => "activity_stream" get "streams/main" => "streams#multi", :as => "stream" get "streams/tags" => "streams#followed_tags", :as => "followed_tags_stream" - get "streams/mentions" => "streams#mentioned", :as => "mentioned_stream" + get "streams/mentions" => "streams#mentions", :as => "mentions_stream" get "streams/liked" => "streams#liked", :as => "liked_stream" get "streams/commented" => "streams#commented", :as => "commented_stream" get "streams/aspects" => "streams#aspects", :as => "aspects_stream" diff --git a/spec/integration/api/streams_controller_spec.rb b/spec/integration/api/streams_controller_spec.rb index baf70a303..82c3b227c 100644 --- a/spec/integration/api/streams_controller_spec.rb +++ b/spec/integration/api/streams_controller_spec.rb @@ -2,22 +2,38 @@ require "spec_helper" -describe Api::V1::PostsController do +describe Api::V1::StreamsController do let(:auth) { FactoryGirl.create(:auth_with_read_and_write) } let!(:access_token) { auth.create_access_token.to_s } before do @aspect = auth.user.aspects.first - @status = auth.user.post(:status_message, text: "This is a status message", public: true, to: "all") + @created_status = auth.user.post(:status_message, text: "This is a status message #test", public: true, to: "all") + auth.user.like!(@created_status) + @status = PostService.new(auth.user).find(@created_status.id) end describe "#aspect" do it "contains expected aspect message" do get( - api_v1_aspects_stream_path(a_ids: [@aspect.id]), + api_v1_aspects_stream_path(aspect_ids: JSON.generate([@aspect.id])), params: {access_token: access_token} ) - expect(response.body).to include("This is a status message") + expect(response.status).to eq 200 + post = JSON.parse(response.body) + expect(post.length).to eq 1 + confirm_post_format(post[0], auth.user, @status) + end + + it "all aspects expected aspect message" do + get( + api_v1_aspects_stream_path, + params: {access_token: access_token} + ) + expect(response.status).to eq 200 + post = JSON.parse(response.body) + expect(post.length).to eq 1 + confirm_post_format(post[0], auth.user, @status) end it "does not save to requested aspects to session" do @@ -28,4 +44,165 @@ describe Api::V1::PostsController do expect(session[:a_ids]).to be_nil end end + + describe "#tags" do + it "all tags expected aspect message" do + get( + api_v1_followed_tags_stream_path, + params: {access_token: access_token} + ) + expect(response.status).to eq 200 + post = JSON.parse(response.body) + expect(post.length).to eq 0 + end + end + + describe "#activity" do + it "contains activity message" do + get( + api_v1_activity_stream_path, + params: {access_token: access_token} + ) + expect(response.status).to eq 200 + post = JSON.parse(response.body) + expect(post.length).to eq 1 + confirm_post_format(post[0], auth.user, @status) + end + end + + describe "#main" do + it "contains main message" do + get( + api_v1_stream_path, + params: {access_token: access_token} + ) + expect(response.status).to eq 200 + post = JSON.parse(response.body) + expect(post.length).to eq 1 + confirm_post_format(post[0], auth.user, @status) + end + end + + describe "#commented" do + it "contains commented message" do + get( + api_v1_commented_stream_path, + params: {access_token: access_token} + ) + expect(response.status).to eq 200 + post = JSON.parse(response.body) + expect(post.length).to eq 0 + end + end + + describe "#mentions" do + it "contains mentions message" do + get( + api_v1_mentions_stream_path, + params: {access_token: access_token} + ) + expect(response.status).to eq 200 + post = JSON.parse(response.body) + expect(post.length).to eq 0 + end + end + + describe "#liked" do + it "contains liked message" do + get( + api_v1_liked_stream_path, + params: {access_token: access_token} + ) + expect(response.status).to eq 200 + post = JSON.parse(response.body) + expect(post.length).to eq 1 + confirm_post_format(post[0], auth.user, @status) + end + end + + private + + # rubocop:disable Metrics/AbcSize + def confirm_post_format(post, user, reference_post, mentions=[]) + confirm_post_top_level(post, reference_post) + confirm_person_format(post["author"], user) + confirm_interactions(post["interaction_counters"], reference_post) + + mentions.each do |mention| + post_mentions = post["mentioned_people"] + post_mention = post_mentions.find {|m| m["guid"] == mention.guid } + confirm_person_format(post_mention, mention) + end + + confirm_poll(post["poll"], reference_post.poll, false) if reference_post.poll + confirm_location(post["location"], reference_post.location) if reference_post.location + confirm_photos(post["photos"], reference_post.photos) if reference_post.photos + end + + def confirm_post_top_level(post, reference_post) + expect(post.has_key?("guid")).to be_truthy + expect(post.has_key?("created_at")).to be_truthy + expect(post["created_at"]).not_to be_nil + expect(post["title"]).to eq(reference_post.message.title) + expect(post["body"]).to eq(reference_post.message.plain_text_for_json) + expect(post["post_type"]).to eq(reference_post.post_type) + expect(post["provider_display_name"]).to eq(reference_post.provider_display_name) + expect(post["public"]).to eq(reference_post.public) + expect(post["nsfw"]).to eq(reference_post.nsfw) + end + + def confirm_interactions(interactions, reference_post) + expect(interactions["comments"]).to eq(reference_post.comments_count) + expect(interactions["likes"]).to eq(reference_post.likes_count) + expect(interactions["reshares"]).to eq(reference_post.reshares_count) + end + + def confirm_person_format(post_person, user) + expect(post_person["guid"]).to eq(user.guid) + expect(post_person["diaspora_id"]).to eq(user.diaspora_handle) + expect(post_person["name"]).to eq(user.name) + expect(post_person["avatar"]).to eq(user.profile.image_url) + end + + def confirm_poll(post_poll, ref_poll, expected_participation) + return unless ref_poll + + expect(post_poll.has_key?("guid")).to be_truthy + expect(post_poll["participation_count"]).to eq(ref_poll.participation_count) + expect(post_poll["already_participated"]).to eq(expected_participation) + expect(post_poll["question"]).to eq(ref_poll.question) + + answers = post_poll["poll_answers"] + answers.each do |answer| + actual_answer = ref_poll.poll_answers.find {|a| a[:answer] == answer["answer"] } + expect(answer["answer"]).to eq(actual_answer[:answer]) + expect(answer["vote_count"]).to eq(actual_answer[:vote_count]) + end + end + + def confirm_location(location, ref_location) + expect(location["address"]).to eq(ref_location[:address]) + expect(location["lat"]).to eq(ref_location[:lat]) + expect(location["lng"]).to eq(ref_location[:lng]) + end + + def confirm_photos(photos, ref_photos) + expect(photos.size).to eq(ref_photos.size) + photos.each do |photo| + expect(photo["dimensions"].has_key?("height")).to be_truthy + expect(photo["dimensions"].has_key?("height")).to be_truthy + expect(photo["sizes"]["small"]).to be_truthy + expect(photo["sizes"]["medium"]).to be_truthy + expect(photo["sizes"]["large"]).to be_truthy + end + end + + def confirm_reshare_format(post, root_post, root_poster) + root = post["root"] + expect(root.has_key?("guid")).to be_truthy + expect(root["guid"]).to eq(root_post[:guid]) + expect(root.has_key?("created_at")).to be_truthy + confirm_person_format(root["author"], root_poster) + end + # rubocop:enable Metrics/AbcSize end