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

aspect_spec.rb « models « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4c7eb2a877c30b3320ff882b6155f6d342d22b35 (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
#   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 Aspect do
  let(:user ) { make_user }
  let(:friend) { Factory.create(:person) }
  let(:user2) { make_user }
  let(:friend_2) { Factory.create(:person) }

  let(:aspect) {user.aspect(:name => 'losers')}
  let(:aspect2) {user2.aspect(:name => 'failures')}
  let(:aspect1) {user.aspect(:name => 'cats')}
  let(:not_friend) { Factory(:person, :diaspora_handle => "not@person.com")}
  let(:user3) {make_user}
  let(:aspect3) {user3.aspect(:name => "lala")}

  describe 'creation' do
    let!(:aspect){user.aspect(:name => 'losers')}
    it 'should have a name' do
      aspect.name.should == "losers"
    end

    it 'should not allow duplicate names' do
      lambda {
        invalid_aspect = user.aspect(:name => "losers ")
      }.should_not change(Aspect, :count)
    end

    it 'should not be creatable with people' do
      aspect = user.aspect(:name => 'losers', :people => [friend, friend_2])
      aspect.people.size.should == 0
    end

    it 'should be able to have other users' do
      Contact.create(:user => user, :person => user2.person, :aspects => [aspect])
      aspect.people.first(:person_id => user.person.id).should be_nil
      aspect.people.first(:person_id => user2.person.id).should_not be_nil
      aspect.people.size.should == 1
    end

    it 'should be able to have users and people' do
      contact1 = Contact.create(:user => user, :person => user2.person, :aspects => [aspect])
      contact2 = Contact.create(:user => user, :person => friend_2, :aspects => [aspect])
      aspect.people.include?(contact1).should be_true
      aspect.people.include?(contact2).should be_true
      aspect.save.should be_true
    end
  end

  describe 'validation' do
    before do
      aspect
    end
    it 'has a unique name for one user' do
      aspect2 = user.aspect(:name => aspect.name)
      aspect2.valid?.should be_false
    end

    it 'has no uniqueness between users' do
      aspect2 = user2.aspect(:name => aspect.name)
      aspect2.valid?.should be_true
    end
  end

  describe 'querying' do
    before do
      aspect
      user.activate_friend(friend, aspect)
    end

    it 'belong to a user' do
      aspect.user.id.should == user.id
      user.aspects.should == [aspect]
    end

    it 'should have people' do
      aspect.people.first(:person_id => friend.id).should be_true
      aspect.people.size.should == 1
    end

    describe '#aspects_with_person' do
      let!(:aspect_without_friend) {user.aspect(:name => "Another aspect")}
      it 'should return the aspects with given friend' do
        user.reload
        aspects = user.aspects_with_person(friend)
        aspects.size.should == 1
        aspects.first.should == aspect
      end

      it 'returns multiple aspects if the person is there' do
        user.reload
        user.add_person_to_aspect(friend.id, aspect1.id)
        aspects = user.aspects_with_person(friend)
        aspects.count.should == 2
        contact = user.contact_for(friend)
        aspects.each{ |asp| asp.people.include?(contact).should be_true }
        aspects.include?(aspect_without_friend).should be_false
      end
    end
  end

  describe 'posting' do

    it 'should add post to aspect via post method' do
      aspect = user.aspect(:name => 'losers', :people => [friend])

      status_message = user.post( :status_message, :message => "hey", :to => aspect.id )

      aspect.reload
      aspect.posts.include?(status_message).should be true
    end

    it 'should add post to aspect via receive method' do
      aspect  = user.aspect(:name => 'losers')
      aspect2 = user2.aspect(:name => 'winners')
      friend_users(user, aspect, user2, aspect2)

      message = user2.post(:status_message, :message => "Hey Dude", :to => aspect2.id)

      user.receive message.to_diaspora_xml, user2.person

      aspect.reload
      aspect.posts.include?(message).should be true
      user.visible_posts(:by_members_of => aspect).include?(message).should be true
    end

    it 'should retract the post from the aspects as well' do
      aspect  = user.aspect(:name => 'losers')
      aspect2 = user2.aspect(:name => 'winners')
      friend_users(user, aspect, user2, aspect2)

      message = user2.post(:status_message, :message => "Hey Dude", :to => aspect2.id)

      user.receive message.to_diaspora_xml, user2.person
      aspect.reload

      aspect.post_ids.include?(message.id).should be true

      retraction = user2.retract(message)
      user.receive retraction.to_diaspora_xml, user2.person


      aspect.reload
      aspect.post_ids.include?(message.id).should be false
    end
  end

  context "aspect management" do
    let(:contact){user.contact_for(user2.person)}
    before do
      friend_users(user, aspect, user2, aspect2)
      aspect.reload
      user.reload
    end
    

    describe "#add_person_to_aspect" do
      it 'adds the user to the aspect' do
        aspect1.people.include?(contact).should be_false 
        user.add_person_to_aspect(user2.person.id, aspect1.id)
        aspect1.reload
        aspect1.people.include?(contact).should be_true
      end

      it 'raises if its an aspect that the user does not own'do
        proc{user.add_person_to_aspect(user2.person.id, aspect2.id) }.should raise_error /Can not add person to an aspect you do not own/
      end

      it 'does not allow to have duplicate people in an aspect' do
        proc{user.add_person_to_aspect(not_friend.id, aspect1.id) }.should raise_error /Can not add person you are not friends with/
      end

      it 'does not allow you to add a person if they are already in the aspect' do
        proc{user.add_person_to_aspect(user2.person.id, aspect.id) }.should raise_error /Can not add person who is already in the aspect/
      end
    end

    describe '#delete_person_from_aspect' do
      it 'deletes a user from the aspect' do
         user.add_person_to_aspect(user2.person.id, aspect1.id)
         user.reload
         aspect1.reload.people.include?(contact).should be true
         user.delete_person_from_aspect(user2.person.id, aspect1.id)
         user.reload
         aspect1.reload.people.include?(contact).should be false
      end

      it 'should check to make sure you have the aspect ' do
        proc{user.delete_person_from_aspect(user2.person.id, aspect2.id) }.should raise_error /Can not delete a person from an aspect you do not own/
      end
    end

    context 'moving and removing posts' do

      let(:message) { user2.post(:status_message, :message => "Hey Dude", :to => aspect2.id)}
      let(:message2){user3.post(:status_message, :message => "other post", :to => aspect3.id)}

      before do
        user.receive message.to_diaspora_xml, user2.person
        aspect.reload
        @post_count  = aspect.posts.count
        @post_count1 = aspect1.posts.count

        user.reload
      end
      
      it 'moves the persons posts into the new aspect' do
        user.add_person_to_aspect(user2.person.id, aspect1.id, :posts => [message] )
        aspect1.reload
        aspect1.posts.should == [message]
      end

      
      it 'should remove the users posts from that aspect' do
        user.delete_person_from_aspect(user2.person.id, aspect.id)
        aspect.reload
        aspect.posts.count.should == @post_count - 1
      end

      it 'should not delete other peoples posts' do
        friend_users(user, aspect, user3, aspect3)
        user.receive message2.to_diaspora_xml, user3.person
        user.delete_person_from_aspect(user2.person.id, aspect.id)
        aspect.reload
        aspect.posts.should == [message2]
      end

      describe '#move_friend' do
        it 'should be able to move a friend from one of users existing aspects to another' do
          user.move_friend(:friend_id => user2.person.id, :from => aspect.id, :to => aspect1.id)
          aspect.reload
          aspect1.reload

          aspect.people.include?(contact).should be_false
          aspect1.people.include?(contact).should be_true
        end

        it "should not move a person who is not a friend" do
          proc{ user.move_friend(:friend_id => friend.id, :from => aspect.id, :to => aspect1.id) }.should raise_error /Can not add person you are not friends with/
          aspect.reload
          aspect1.reload
          aspect.people.first(:person_id => friend.id).should be_nil
          aspect1.people.first(:person_id => friend.id).should be_nil
        end

        it "should not move a person to a aspect that's not his" do
          proc {user.move_friend(:friend_id => user2.person.id, :from => aspect.id, :to => aspect2.id )}.should raise_error /Can not add person to an aspect you do not own/
          aspect.reload
          aspect2.reload
          aspect.people.include?(contact).should be true
          aspect2.people.include?(contact).should be false
        end

        it 'should move all posts by that user to the new aspect' do
          user.move_friend(:friend_id => user2.person.id, :from => aspect.id, :to => aspect1.id)
          aspect.reload
          aspect1.reload

          aspect1.posts.count.should == @post_count1 + 1
          aspect.posts.count.should == @post_count - 1
        end

        it 'does not try to delete if add person did not go through' do
          user.should_receive(:add_person_to_aspect).and_return(false)
          user.should_not_receive(:delete_person_from_aspect)
          user.move_friend(:friend_id => user2.person.id, :from => aspect.id, :to => aspect1.id)
        end
      end
    end
  end
end