Skip to content
Extraits de code Groupes Projets
posts_controller_spec.rb 4,73 ko
Newer Older
  • Learn to ignore specific revisions
  • danielgrippi's avatar
    danielgrippi a validé
    #   Copyright (c) 2010-2011, Diaspora Inc.  This file is
    
    maxwell's avatar
    maxwell a validé
    #   licensed under the Affero General Public License version 3 or later.  See
    #   the COPYRIGHT file.
    
    
    require "spec_helper"
    
    describe PostsController, type: :controller do
      let!(:post_service_double) { double("post_service") }
    
    maxwell's avatar
    maxwell a validé
    
      before do
    
        @message = alice.build_post :status_message, text: "ohai", to: aspect.id
    
        alice.add_to_streams(@message, [aspect])
    
        alice.dispatch_post @message, to: aspect.id
    
        allow(PostService).to receive(:new).and_return(post_service_double)
    
      describe "#show" do
        before do
          expect(post_service_double).to receive(:mark_user_notifications)
          allow(post_service_double).to receive(:present_json)
        end
    
        context "user signed in" do
    
          context "given a post that the user is allowed to see" do
            before do
              sign_in :user, alice
              expect(post_service_double).to receive(:post).and_return(@message)
            end
    
            it "succeeds" do
              get :show, id: @message.id
              expect(response).to be_success
            end
    
            it 'succeeds after removing a mention when closing the mentioned user\'s account' do
              user = FactoryGirl.create(:user, username: "user")
              alice.share_with(user.person, alice.aspects.first)
              msg = alice.build_post :status_message,
                                     text: "Mention @{User ; #{user.diaspora_handle}}", public: true, to: "all"
              msg.save!
              expect(msg.mentioned_people.count).to eq(1)
              user.destroy
              get :show, id: msg.id
              expect(response).to be_success
            end
    
            it "renders the application layout on mobile" do
              get :show, id: @message.id, format: :mobile
              expect(response).to render_template("layouts/application")
            end
    
            it "succeeds on mobile with a reshare" do
              get :show, id: FactoryGirl.create(:reshare, author: alice.person).id, format: :mobile
              expect(response).to be_success
            end
    
          context "given a post that the user is not allowed to see" do
            before do
              sign_in :user, alice
              expect(post_service_double).to receive(:post).and_raise(Diaspora::NonPublic)
            end
    
            it "returns a 404" do
              get :show, id: @message.id
              expect(response.code).to eq("404")
            end
    
        context "user not signed in" do
          context "given a public post" do
    
              @status = alice.post(:status_message, text: "hello", public: true, to: "all")
              expect(post_service_double).to receive(:post).and_return(@status)
    
            it "shows a public post" do
              get :show, id: @status.id
              expect(response.body).to match "hello"
    
            it "succeeds for statusnet" do
    
              @request.env["HTTP_ACCEPT"] = "application/html+xml,text/html"
    
              get :show, id: @status.id
              expect(response.body).to match "hello"
    
            it "responds with diaspora xml if format is xml" do
              get :show, id: @status.guid, format: :xml
    
              expect(response.body).to eq(@status.to_diaspora_xml)
    
    
          context "given a limited post" do
            before do
              expect(post_service_double).to receive(:post).and_raise(Diaspora::NonPublic)
            end
    
            it "forces the user to sign" do
              get :show, id: @message.id
              expect(response).to be_redirect
              expect(response).to redirect_to new_user_session_path
            end
          end
    
      describe "iframe" do
        it "contains an iframe" do
          get :iframe, id: @message.id
    
          expect(response.body).to match /iframe/
    
      describe "oembed" do
        it "receives a present oembed" do
          expect(post_service_double).to receive(:present_oembed)
          get :oembed, url: "/posts/#{@message.id}"
    
      describe "#destroy" do
    
        before do
          sign_in alice
        end
    
        it "will receive a retract post" do
          expect(post_service_double).to receive(:retract_post)
          expect(post_service_double).to receive(:post).and_return(@message)
          message = alice.post(:status_message, text: "hey", to: alice.aspects.first.id)
          delete :destroy, format: :js, id: message.id
    
    
        context "when Diaspora::NotMine is raised by retract post" do
          it "will respond with a 403" do
            expect(post_service_double).to receive(:retract_post).and_raise(Diaspora::NotMine)
            message = alice.post(:status_message, text: "hey", to: alice.aspects.first.id)
            delete :destroy, format: :js, id: message.id
            expect(response.body).to eq("You are not allowed to do that")
            expect(response.status).to eq(403)
          end
        end