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
|
# 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 User do
let(:inviter) {Factory.create :user}
let(:aspect) {inviter.aspect(:name => "awesome")}
let(:another_user) {Factory.create :user}
let(:wrong_aspect) {another_user.aspect(:name => "super")}
let(:inviter_with_3_invites) {Factory.create :user, :invites => 3}
let(:aspect2) {inviter_with_3_invites.aspect(:name => "Jersey Girls")}
before do
deliverable = Object.new
deliverable.stub!(:deliver)
::Devise.mailer.stub!(:invitation).and_return(deliverable)
end
context "creating invites" do
it 'requires an apect' do
proc{inviter.invite_user(:email => "maggie@example.com")}.should raise_error /Must invite into aspect/
end
it 'requires your aspect' do
proc{inviter.invite_user(:email => "maggie@example.com", :aspect_id => wrong_aspect.id)}.should raise_error /Must invite to your aspect/
end
it 'creates a user' do
inviter
lambda {
inviter.invite_user(:email => "joe@example.com", :aspect_id => aspect.id )
}.should change(User, :count).by(1)
end
it 'sends email to the invited user' do
::Devise.mailer.should_receive(:invitation).once
inviter.invite_user(:email => "ian@example.com", :aspect_id => aspect.id)
end
it 'adds the inviter to the invited_user' do
invited_user = inviter.invite_user(:email => "marcy@example.com", :aspect_id => aspect.id)
invited_user.reload
invited_user.inviters.include?(inviter).should be_true
end
it 'adds an optional message' do
invited_user = inviter.invite_user(:email => "marcy@example.com", :invite_message => "How've you been?",:aspect_id => aspect.id)
invited_user.reload
invited_user.invite_messages[inviter.id.to_s].should == "How've you been?"
end
it 'adds a pending request to the invited user' do
invited_user = inviter.invite_user(:email => "marcy@example.com", :aspect_id => aspect.id)
invited_user.reload
invited_user.pending_requests.find_by_callback_url(inviter.receive_url).nil?.should == false
end
it 'adds a pending request to the inviter' do
inviter.invite_user(:email => "marcy@example.com", :aspect_id => aspect.id)
inviter.reload
inviter.pending_requests.find_by_callback_url(inviter.receive_url).nil?.should == false
end
it 'throws if you try to add someone you"re friends with' do
friend_users(inviter, aspect, another_user, wrong_aspect)
inviter.reload
proc{inviter.invite_user(:email => another_user.email, :aspect_id => aspect.id)}.should raise_error /You are already friends with that person/
end
it 'sends a friend request to a user with that email into the aspect' do
inviter.should_receive(:send_friend_request_to){ |a, b|
a.should == another_user.person
b.should == aspect
}
inviter.invite_user(:email => another_user.email, :aspect_id => aspect.id)
end
end
context "limit on invites" do
it 'does not invite users after 3 invites' do
inviter_with_3_invites.invite_user(:email => "email1@example.com", :aspect_id => aspect2.id)
inviter_with_3_invites.invite_user(:email => "email2@example.com", :aspect_id => aspect2.id)
inviter_with_3_invites.invite_user(:email => "email3@example.com", :aspect_id => aspect2.id)
proc{inviter_with_3_invites.invite_user(:email => "email4@example.com", :aspect_id => aspect2.id)}.should raise_error /You have no invites/
end
it 'does not invite people I already invited' do
inviter_with_3_invites.invite_user(:email => "email1@example.com", :aspect_id => aspect2.id)
proc{inviter_with_3_invites.invite_user(:email => "email1@example.com", :aspect_id => aspect2.id)}.should raise_error /You already invited this person/
end
end
context "the acceptance of an invitation" do
let!(:invited_user1) { create_user_with_invitation("abc", :email => "email@example.com", :inviter => inviter)}
let!(:invited_user2) { inviter.invite_user(:email => "jane@example.com", :aspect_id => aspect.id) }
it "should create the person with the passed in params" do
person_count = Person.count
u = invited_user1.accept_invitation!(:invitation_token => "abc",
:username => "user",
:password => "secret",
:password_confirmation => "secret",
:person => {:profile => {:first_name => "Bob",
:last_name => "Smith"}} )
Person.count.should be person_count + 1
u.person.profile.first_name.should == "Bob"
end
it 'should auto accept the request for the sender into the right aspect' do
u = invited_user2.accept_invitation!(:invitation_token => invited_user2.invitation_token,
:username => "user",
:password => "secret",
:password_confirmation => "secret",
:person => {:profile => {:first_name => "Bob",
:last_name => "Smith"}} )
u.pending_requests
u.pending_requests.count.should == 1
request = u.pending_requests.first
aspect2 = u.aspect(:name => "dudes")
u.reload
inviter
inviter.receive_salmon(u.salmon(u.accept_friend_request(request.id, aspect2.id)).xml_for(inviter.person))
inviter.contact_for(u.person).should_not be_nil
end
end
end
def create_user_with_invitation(invitation_token, attributes={})
inviter = attributes.delete(:inviter)
user = User.new({:password => nil, :password_confirmation => nil}.update(attributes))
#user.skip_confirmation!
user.invitation_token = invitation_token
user.invitation_sent_at = Time.now.utc
user.inviters << inviter
user.save(:validate => false)
user
end
|