Welcome to mirror list, hosted at ThFree Co, Russian Federation.

status_messages_controller_spec.rb « controllers « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ce6fa6485b99bd4db87748c9cf450b1fde64eb35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#   Copyright (c) 2010, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

require 'spec_helper'

describe StatusMessagesController do
  before do
    @aspect1 = alice.aspects.first
    @aspect2 = bob.aspects.first

    request.env["HTTP_REFERER"] = ""
    sign_in :user, alice
    @controller.stub!(:current_user).and_return(alice)
    alice.reload
  end

  describe '#bookmarklet' do
    it 'succeeds' do
      get :bookmarklet
      response.should be_success
    end
  end

  describe '#new' do
    it 'succeeds' do
      get :new,
        :person_id => bob.person.id
      response.should be_success
    end

    it 'generates a jasmine fixture', :fixture => true do
      contact = alice.contact_for(bob.person)
      aspect = alice.aspects.create(:name => 'people')
      contact.aspects << aspect
      contact.save
      get :new, :person_id => bob.person.id, :layout => true
      save_fixture(html_for("body"), "status_message_new")
    end
  end

  describe '#create' do
    let(:status_message_hash) {
      { :status_message => {
        :public  => "true",
        :text => "facebook, is that you?",
        },
      :aspect_ids => [@aspect1.id.to_s] }
    }

    context 'js requests' do
      it 'responds' do
        post :create, status_message_hash.merge(:format => 'js')
        response.status.should == 201
      end

      it 'responds with json' do
        post :create, status_message_hash.merge(:format => 'js')
        json = JSON.parse(response.body)
        json['post_id'].should_not be_nil
        json['html'].should_not be_nil
      end

      it 'escapes XSS' do
        xss = "<script> alert('hi browser') </script>"
        post :create, status_message_hash.merge(:format => 'js', :text => xss)
        json = JSON.parse(response.body)
        json['html'].should_not =~ /<script>/
      end
    end

    it "dispatches the post to the specified services" do
      s1 = Services::Facebook.new
      alice.services << s1
      alice.services << Services::Twitter.new
      status_message_hash[:services] = ['facebook']
      alice.should_receive(:dispatch_post).with(anything(), hash_including(:services => [s1]))
      post :create, status_message_hash
    end

    it "doesn't overwrite author_id" do
      status_message_hash[:status_message][:author_id] = bob.person.id
      post :create, status_message_hash
      new_message = StatusMessage.find_by_text(status_message_hash[:status_message][:text])
      new_message.author_id.should == alice.person.id
    end

    it "doesn't overwrite id" do
      old_status_message = alice.post(:status_message, :text => "hello", :to => @aspect1.id)
      status_message_hash[:status_message][:id] = old_status_message.id
      post :create, status_message_hash
      old_status_message.reload.text.should == 'hello'
    end

    it 'calls dispatch post once subscribers is set' do
      alice.should_receive(:dispatch_post){|post, opts|
        post.subscribers(alice).should == [bob.person]
      }
      post :create, status_message_hash
    end

    it 'sends the errors in the body on js' do
      post :create, status_message_hash.merge!(:format => 'js', :status_message => {:text => ''})
      response.body.should include('Status message requires a message or at least one photo')
    end

    context 'with photos' do
      before do
        fixture_filename  = 'button.png'
        fixture_name      = File.join(File.dirname(__FILE__), '..', 'fixtures', fixture_filename)

        @photo1 = alice.build_post(:photo, :pending => true, :user_file=> File.open(fixture_name), :to => @aspect1.id)
        @photo2 = alice.build_post(:photo, :pending => true, :user_file=> File.open(fixture_name), :to => @aspect1.id)

        @photo1.save!
        @photo2.save!

        @hash = status_message_hash
        @hash[:photos] = [@photo1.id.to_s, @photo2.id.to_s]
      end
      it "will post a photo without text" do
        @hash.delete :text
        post :create, @hash
        response.should be_redirect
      end
      it "dispatches all referenced photos" do
        alice.should_receive(:dispatch_post).exactly(3).times
        post :create, @hash
      end
      it "sets the pending bit of referenced photos" do
        post :create, @hash
        @photo1.reload.pending.should be_false
        @photo2.reload.pending.should be_false
      end
    end
  end
end