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

aspects_controller_spec.rb « controllers « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8e5b0f10fe7ae0fee2be6056124b3f6a4a71f73f (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#   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'
require File.join(Rails.root, "spec", "shared_behaviors", "log_override")

describe AspectsController do
  render_views

  before do
    @bob   = bob
    @alice = alice
    @alice.getting_started = false
    @alice.save
    sign_in :user, @alice
    @alices_aspect_1  = @alice.aspects.first
    @alices_aspect_2  = @alice.aspects.create(:name => "another aspect")

    @controller.stub(:current_user).and_return(@alice)
    request.env["HTTP_REFERER"] = 'http://' + request.host
  end

  describe "custom logging on success" do
    before do
      @action = :index
    end
    it_should_behave_like "it overrides the logs on success"
  end

  describe "custom logging on error" do
    class FakeError < RuntimeError; attr_accessor :original_exception; end
    before do
      @action = :index
      @desired_error_message = "I love errors"
      @error = FakeError.new(@desired_error_message)
      @orig_error_message = "I loooooove nested errors!"
      @error.original_exception = NoMethodError.new(@orig_error_message)
      @controller.stub(:index).and_raise(@error)
    end
    it_should_behave_like "it overrides the logs on error"
  end

  describe "custom logging on redirect" do
    before do
      @action = :show
      @action_params = {'id' => @alices_aspect_1.id.to_s}
    end
    it_should_behave_like "it overrides the logs on redirect"
  end

  describe "#index" do
    it "generates a jasmine fixture" do
      get :index
      save_fixture(html_for("body"), "aspects_index")
    end

    it "generates a jasmine fixture with a prefill" do
      get :index, :prefill => "reshare things"
      save_fixture(html_for("body"), "aspects_index_prefill")
    end

    it 'generates a jasmine fixture with services' do
      @alice.services << Services::Facebook.create(:user_id => @alice.id)
      @alice.services << Services::Twitter.create(:user_id => @alice.id)
      get :index, :prefill => "reshare things"
      save_fixture(html_for("body"), "aspects_index_services")
    end

    it 'generates a jasmine fixture with posts' do
      @alice.post(:status_message, :text => "hello", :to => @alices_aspect_2.id)
      get :index
      save_fixture(html_for("body"), "aspects_index_with_posts")
    end

    context 'with getting_started = true' do
      before do
        @alice.getting_started = true
        @alice.save
      end
      it 'redirects to getting_started' do
        get :index
        response.should redirect_to getting_started_path
      end
      it 'does not redirect mobile users to getting_started' do
        get :index, :format => :mobile
        response.should_not be_redirect
      end
      it 'does not redirect ajax to getting_started' do
        get :index, :format => :js
        response.should_not be_redirect
      end
    end

    context 'with posts in multiple aspects' do
      before do
        @posts = []
        2.times do |n|
          user = Factory(:user)
          aspect = user.aspects.create(:name => 'people')
          connect_users(@alice, @alices_aspect_1, user, aspect)
          target_aspect = n.even? ? @alices_aspect_1 : @alices_aspect_2
          post = @alice.post(:status_message, :text=> "hello#{n}", :to => target_aspect)
          post.created_at = Time.now - (2 - n).seconds
          post.save!
          @posts << post
        end
        @alice.build_comment('lalala', :on => @posts.first ).save
      end

      describe "post visibilities" do
        before do
          @status = @bob.post(:status_message, :text=> "hello", :to => @bob.aspects.first)
          @vis = @status.post_visibilities.first
        end

        it "pulls back non hidden posts" do
          get :index
          assigns[:posts].include?(@status).should be_true
        end
        it "does not pull back hidden posts" do
          @vis.update_attributes( :hidden => true )
          get :index
          assigns[:posts].include?(@status).should be_false
        end
      end

      describe "ordering" do
        it "orders posts by updated_at by default" do
          get :index
          assigns(:posts).should == @posts
        end

        it "orders posts by created_at on request" do
          get :index, :sort_order => 'created_at'
          assigns(:posts).should == @posts.reverse
        end
        
        it "remembers your sort order and lets you override the memory" do
          get :index, :sort_order => "created_at"
          assigns(:posts).should == @posts.reverse
          get :index
          assigns(:posts).should == @posts.reverse
          get :index, :sort_order => "updated_at"
          assigns(:posts).should == @posts
        end

        it "doesn't allow SQL injection" do
          get :index, :sort_order => "\"; DROP TABLE users;"
          assigns(:posts).should == @posts
          get :index, :sort_order => "created_at"
          assigns(:posts).should == @posts.reverse
        end
      end

      it "returns all posts by default" do
        @alice.aspects.reload
        get :index
        assigns(:posts).length.should == 2
      end

      it "can filter to a single aspect" do
        get :index, :a_ids => [@alices_aspect_2.id.to_s]
        assigns(:posts).length.should == 1
      end

      it "can filter to multiple aspects" do
        get :index, :a_ids => [@alices_aspect_1.id.to_s, @alices_aspect_2.id.to_s]
        assigns(:posts).length.should == 2
      end
    end

    describe 'performance', :performance => true do
      before do
        require 'benchmark'
        8.times do |n|
          user = Factory.create(:user)
          aspect = user.aspects.create(:name => 'people')
          connect_users(@alice, @alices_aspect_1, user, aspect)
          post =  @alice.post(:status_message, :text => "hello#{n}", :to => @alices_aspect_2.id)
          8.times do |n|
            user.comment "yo#{post.text}", :on => post
          end
        end
      end
      it 'takes time' do
        Benchmark.realtime{
          get :index
        }.should < 1.5
      end
    end
  end

  describe "#show" do
    it "succeeds" do
      get :show, 'id' => @alices_aspect_1.id.to_s
      response.should be_redirect
    end
    it 'redirects on an invalid id' do
      get :show, 'id' => 4341029835
      response.should be_redirect
    end
  end

  describe "#create" do
    context "with valid params" do
      it "creates an aspect" do
        @alice.aspects.count.should == 2
        post :create, "aspect" => {"name" => "new aspect"}
        @alice.reload.aspects.count.should == 3
      end
      it "redirects to the aspect page" do
        post :create, "aspect" => {"name" => "new aspect"}
        response.should redirect_to(aspect_path(Aspect.find_by_name("new aspect")))
      end
    end
    context "with invalid params" do
      it "does not create an aspect" do
        @alice.aspects.count.should == 2
        post :create, "aspect" => {"name" => ""}
        @alice.reload.aspects.count.should == 2
      end
      it "goes back to the page you came from" do
        post :create, "aspect" => {"name" => ""}
        response.should redirect_to(:back)
      end
    end
  end

  describe "#manage" do
    it "succeeds" do
      get :manage
      response.should be_success
    end
    it "performs reasonably", :performance => true do
        require 'benchmark'
        8.times do |n|
          aspect = @alice.aspects.create(:name => "aspect#{n}")
          8.times do |o|
            person = Factory(:person)
            @alice.contacts.create(:person => person, :aspects => [aspect])
          end
        end
        Benchmark.realtime{
          get :manage
        }.should < 4.5
    end
    it "assigns aspect to manage" do
      get :manage
      assigns(:aspect).should == :manage
    end
    it "assigns contacts to only non-pending" do
      contact = @alice.contact_for(bob.person)
      Contact.unscoped.where(:user_id => @alice.id).count.should == 1
      @alice.send_contact_request_to(Factory(:user).person, @alices_aspect_1)
      Contact.unscoped.where(:user_id => @alice.id).count.should == 2

      get :manage
      contacts = assigns(:contacts)
      contacts.count.should == 1
      contacts.first.should == contact
    end
  end

  describe "#update" do
    before do
      @alices_aspect_1 = @alice.aspects.create(:name => "Bruisers")
    end
    it "doesn't overwrite random attributes" do
      new_user         = Factory.create :user
      params           = {"name" => "Bruisers"}
      params[:user_id] = new_user.id
      put('update', :id => @alices_aspect_1.id, "aspect" => params)
      Aspect.find(@alices_aspect_1.id).user_id.should == @alice.id
    end
  end

  describe '#edit' do
    before do
      @bob = bob
      @eve = eve
      @eve.profile.first_name = nil
      @eve.profile.save
      @eve.save

      @zed   = Factory(:user_with_aspect, :username => "zed")
      @zed.profile.first_name = "zed"
      @zed.profile.save
      @zed.save
      @katz   = Factory(:user_with_aspect, :username => "katz")
      @katz.profile.first_name = "katz"
      @katz.profile.save
      @katz.save

      connect_users(@alice, @alices_aspect_2, @eve, @eve.aspects.first)
      connect_users(@alice, @alices_aspect_2, @zed, @zed.aspects.first)
      connect_users(@alice, @alices_aspect_1, @katz, @katz.aspects.first)
    end
    it 'renders' do
      get :edit, :id => @alices_aspect_1.id
      response.should be_success
    end

    it 'assigns the contacts in alphabetical order with people in aspects first' do
      get :edit, :id => @alices_aspect_2.id
      assigns[:contacts].map(&:id).should == [@alice.contact_for(@eve.person), @alice.contact_for(@zed.person), @alice.contact_for(@bob.person), @alice.contact_for(@katz.person)].map(&:id)
    end

    it 'assigns all the contacts if noone is there' do
      alices_aspect_3  = @alice.aspects.create(:name => "aspect 3")

      get :edit, :id => alices_aspect_3.id
      assigns[:contacts].map(&:id).should == [@alice.contact_for(@bob.person), @alice.contact_for(@eve.person), @alice.contact_for(@katz.person), @alice.contact_for(@zed.person)].map(&:id)
    end
  end

  describe "#toggle_contact_visibility" do
    it 'sets contacts visible' do
      @alices_aspect_1.contacts_visible = false
      @alices_aspect_1.save

      get :toggle_contact_visibility, :format => 'js', :aspect_id => @alices_aspect_1.id
      @alices_aspect_1.reload.contacts_visible.should be_true
    end

    it 'sets contacts hidden' do
      @alices_aspect_1.contacts_visible = true
      @alices_aspect_1.save

      get :toggle_contact_visibility, :format => 'js', :aspect_id => @alices_aspect_1.id
      @alices_aspect_1.reload.contacts_visible.should be_false
    end
  end
end