posts_controller_spec.rb 4.27 KB
Newer Older
danielgrippi's avatar
danielgrippi committed
1
#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
maxwell's avatar
maxwell committed
2 3 4
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

5 6 7 8
require "spec_helper"

describe PostsController, type: :controller do
  let!(:post_service_double) { double("post_service") }
maxwell's avatar
maxwell committed
9 10

  before do
11
    aspect = alice.aspects.first
12
    @message = alice.build_post :status_message, text: "ohai", to: aspect.id
13
    @message.save!
14

15
    alice.add_to_streams(@message, [aspect])
16 17 18
    alice.dispatch_post @message, to: aspect.id

    allow(PostService).to receive(:new).and_return(post_service_double)
19 20
  end

21 22 23 24 25 26 27
  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
28 29 30 31 32
      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
33

34 35 36 37
        it "succeeds" do
          get :show, id: @message.id
          expect(response).to be_success
        end
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        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
55

56 57 58 59
        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
60 61
      end

62 63 64 65 66 67 68 69 70 71
      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
72
      end
73 74
    end

75 76
    context "user not signed in" do
      context "given a public post" do
77
        before :each do
78 79
          @status = alice.post(:status_message, text: "hello", public: true, to: "all")
          expect(post_service_double).to receive(:post).and_return(@status)
80
        end
81

82 83 84
        it "shows a public post" do
          get :show, id: @status.id
          expect(response.body).to match "hello"
85 86
        end

87
        it "succeeds for statusnet" do
88
          @request.env["HTTP_ACCEPT"] = "application/html+xml,text/html"
89 90
          get :show, id: @status.id
          expect(response.body).to match "hello"
91 92
        end

93 94
        it "responds with diaspora xml if format is xml" do
          get :show, id: @status.guid, format: :xml
95
          expect(response.body).to eq(@status.to_diaspora_xml)
96
        end
97
      end
98 99 100 101 102 103 104 105 106 107 108 109

      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
110
    end
111
  end
112

113 114 115
  describe "iframe" do
    it "contains an iframe" do
      get :iframe, id: @message.id
116
      expect(response.body).to match /iframe/
117 118
    end
  end
119

120 121 122 123
  describe "oembed" do
    it "receives a present oembed" do
      expect(post_service_double).to receive(:present_oembed)
      get :oembed, url: "/posts/#{@message.id}"
124 125 126
    end
  end

127
  describe "#destroy" do
128 129 130
    before do
      sign_in alice
    end
131

132 133 134 135 136
    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
137
    end
maxwell's avatar
maxwell committed
138 139
  end
end