forked from diaspora/diaspora
/
invitation_spec.rb
342 lines (297 loc) · 11.2 KB
/
invitation_spec.rb
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
335
336
337
338
339
340
341
# 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 Invitation do
let(:user) { alice }
let(:aspect) { user.aspects.first }
before do
user.invites = 20
user.save
@email = 'maggie@example.com'
Devise.mailer.deliveries = []
end
describe 'validations' do
before do
aspect
@invitation = Invitation.new(:sender => user, :recipient => eve, :aspect => aspect)
end
it 'is valid' do
@invitation.sender.should == user
@invitation.recipient.should == eve
@invitation.aspect.should == aspect
@invitation.should be_valid
end
it 'is from a user' do
@invitation.sender = nil
@invitation.should_not be_valid
end
it 'is to a user' do
@invitation.recipient = nil
@invitation.should_not be_valid
end
it 'is into an aspect' do
@invitation.aspect = nil
@invitation.should_not be_valid
end
end
it 'has a message' do
@invitation = Invitation.new(:sender => user, :recipient => eve, :aspect => aspect)
@invitation.message = "!"
@invitation.message.should == "!"
end
describe '.new_user_by_service_and_identifier' do
let(:inv) { Invitation.new_user_by_service_and_identifier(@type, @identifier) }
it 'returns User.new for a non-existent user for email' do
@type = "email"
@identifier = "maggie@example.org"
inv.invitation_identifier.should == @identifier
inv.invitation_service.should == 'email'
inv.should_not be_persisted
lambda {
inv.reload
}.should raise_error ActiveRecord::RecordNotFound
end
it 'returns User.new for a non-existent user' do
@type = "facebook"
@identifier = "1234892323"
inv.invitation_identifier.should == @identifier
inv.invitation_service.should == @type
inv.persisted?.should be_false
lambda {
inv.reload
}.should raise_error ActiveRecord::RecordNotFound
end
end
describe '.find_existing_user' do
let(:inv) { Invitation.find_existing_user(@type, @identifier) }
context 'send a request to an existing' do
context 'active user' do
it 'by email' do
@identifier = alice.email
@type = 'email'
inv.should == alice
end
it 'by service' do
uid = '123324234'
alice.services << Services::Facebook.new(:uid => uid)
alice.save
@type = 'facebook'
@identifier = uid
inv.should == alice
end
end
context 'invited user' do
it 'by email' do
@identifier = alice.email
@type = 'email'
alice.invitation_identifier = @identifier
alice.invitation_service = @type
alice.save
inv.should == alice
end
it 'by service' do
fb_id = 'abc123'
alice.invitation_service = 'facebook'
alice.invitation_identifier = fb_id
alice.save
@identifier = fb_id
@type = 'facebook'
inv.should == alice
end
end
end
end
describe '.invite' do
it 'creates an invitation' do
lambda {
Invitation.invite(:service => 'email', :identifier => @email, :from => user, :into => aspect)
}.should change(Invitation, :count).by(1)
end
it 'associates the invitation with the inviter' do
lambda {
Invitation.invite(:service => 'email', :identifier => @email, :from => user, :into => aspect)
}.should change { user.reload.invitations_from_me.count }.by(1)
end
it 'associates the invitation with the invitee' do
new_user = Invitation.invite(:service => 'email', :identifier => @email, :from => user, :into => aspect)
new_user.invitations_to_me.count.should == 1
end
it 'creates a user' do
lambda {
Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect)
}.should change(User, :count).by(1)
end
it 'returns the new user' do
new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect)
new_user.is_a?(User).should be_true
new_user.email.should == @email
end
it 'adds the inviter to the invited_user' do
new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect)
new_user.invitations_to_me.first.sender.should == user
end
it 'adds an optional message' do
message = "How've you been?"
new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect, :message => message)
new_user.invitations_to_me.first.message.should == message
end
it 'sends a contact request to a user with that email into the aspect' do
user.should_receive(:share_with).with(eve.person, aspect)
Invitation.invite(:from => user, :service => 'email', :identifier => eve.email, :into => aspect)
end
it 'decrements the invite count of the from user' do
message = "How've you been?"
lambda {
new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect, :message => message)
}.should change(user, :invites).by(-1)
end
it "doesn't decrement counter past zero" do
user.invites = 0
user.save!
message = "How've you been?"
lambda {
Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect, :message => message)
}.should_not change(user, :invites)
end
context 'invalid email' do
it 'return a user with errors' do
new_user = Invitation.invite(:service => 'email', :identifier => "fkjlsdf", :from => user, :into => aspect)
new_user.should have(1).errors_on(:email)
new_user.should_not be_persisted
end
end
end
describe '.create_invitee' do
context "when we're resending an invitation" do
before do
@valid_params = {:from => user,
:service => 'email',
:identifier => @email,
:into => aspect,
:message => @message}
@invitee = Invitation.create_invitee(:service => 'email', :identifier => @email)
@valid_params[:existing_user] = @invitee
end
it "does not create a user" do
expect { Invitation.create_invitee(@valid_params) }.should_not change(User, :count)
end
it "sends mail" do
expect {
Invitation.create_invitee(@valid_params)
}.should change { Devise.mailer.deliveries.size }.by(1)
end
it "does not set the key" do
expect {
Invitation.create_invitee(@valid_params)
}.should_not change { @invitee.reload.serialized_private_key }
end
it "does not change the invitation token" do
old_token = @invitee.invitation_token
Invitation.create_invitee(@valid_params)
@invitee.reload.invitation_token.should == old_token
end
end
context 'with an inviter' do
before do
@message = "whatever"
@valid_params = {:from => user, :service => 'email', :identifier => @email, :into => aspect, :message => @message}
end
it "sends mail" do
expect {
Invitation.create_invitee(@valid_params)
}.should change { Devise.mailer.deliveries.size }.by(1)
end
it "includes the message in the email" do
Invitation.create_invitee(@valid_params)
Devise.mailer.deliveries.last.to_s.should include(@message)
end
it "has no translation missing" do
Invitation.create_invitee(@valid_params)
Devise.mailer.deliveries.last.body.raw_source.should_not match(/(translation_missing.+)/)
end
it "doesn't create a user if the email is invalid" do
new_user = Invitation.create_invitee(@valid_params.merge(:identifier => 'fdfdfdfdf'))
new_user.should_not be_persisted
new_user.should have(1).error_on(:email)
end
it "does not save a user with an empty string email" do
@valid_params[:service] = 'facebook'
@valid_params[:identifier] = '3423423'
Invitation.create_invitee(@valid_params)
@valid_params[:identifier] = 'dfadsfdas'
expect { Invitation.create_invitee(@valid_params) }.should_not raise_error
end
end
context 'with no inviter' do
it 'sends an email that includes the right things' do
Invitation.create_invitee(:service => 'email', :identifier => @email)
Devise.mailer.deliveries.first.to_s.should include("Welcome #{@email}")
end
it 'creates a user' do
expect {
Invitation.create_invitee(:service => 'email', :identifier => @email)
}.should change(User, :count).by(1)
end
it 'sends email to the invited user' do
expect {
Invitation.create_invitee(:service => 'email', :identifier => @email)
}.should change { Devise.mailer.deliveries.size }.by(1)
end
it 'does not create an invitation' do
expect {
Invitation.create_invitee(:service => 'email', :identifier => @email)
}.should_not change(Invitation, :count)
end
end
end
describe '.resend' do
before do
aspect
user.invite_user(aspect.id, 'email', "a@a.com", "")
@invitation = user.reload.invitations_from_me.first
end
it 'sends another email' do
lambda { @invitation.resend }.should change(Devise.mailer.deliveries, :count).by(1)
end
end
describe '#share_with!' do
before do
@new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect)
acceptance_params = {:invitation_token => "abc",
:username => "user",
:email => @email,
:password => "secret",
:password_confirmation => "secret",
:person => {:profile => {:first_name => "Bob", :last_name => "Smith"}}}
@new_user.setup(acceptance_params)
@new_user.person.save
@new_user.save
@invitation = @new_user.invitations_to_me.first
end
it 'destroys the invitation' do
lambda {
@invitation.share_with!
}.should change(Invitation, :count).by(-1)
end
it 'creates a contact for the inviter and invitee' do
lambda {
@invitation.share_with!
}.should change(Contact, :count).by(2)
end
end
describe '#recipient_identifier' do
it 'calls email if the invitation_service is email' do
alice.invite_user(aspect.id, 'email', "a@a.com", "")
invitation = alice.reload.invitations_from_me.first
invitation.recipient_identifier.should == 'a@a.com'
end
it 'gets the name if the invitation_service is facebook' do
alice.services << Services::Facebook.new(:uid => "13234895")
alice.reload.services(true).first.service_users.create(:uid => "23526464", :photo_url => 'url', :name => "Remote User")
alice.invite_user(aspect.id, 'facebook', "23526464", '')
invitation = alice.reload.invitations_from_me.first
invitation.recipient_identifier.should == "Remote User"
end
end
end