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
342
343
344
345
|
# frozen_string_literal: true
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
describe Profile, :type => :model do
describe 'validation' do
describe "of first_name" do
it "strips leading and trailing whitespace" do
profile = FactoryGirl.build(:profile, :first_name => " Shelly ")
expect(profile).to be_valid
expect(profile.first_name).to eq("Shelly")
end
it "can be 32 characters long" do
profile = FactoryGirl.build(:profile, :first_name => "Hexagoooooooooooooooooooooooooon")
expect(profile).to be_valid
end
it "cannot be 33 characters" do
profile = FactoryGirl.build(:profile, :first_name => "Hexagooooooooooooooooooooooooooon")
expect(profile).not_to be_valid
end
it 'cannot have ;' do
profile = FactoryGirl.build(:profile, :first_name => "Hex;agon")
expect(profile).not_to be_valid
end
end
describe 'from_omniauth_hash' do
before do
@from_omniauth = {'first_name' => 'bob', 'last_name' => 'jones', 'description' => 'this is my bio', 'location' => 'sf', 'image' => 'http://cats.com/gif.gif'}
end
it 'outputs a hash that can update a diaspora profile' do
profile = Profile.new
expect(profile.from_omniauth_hash(@from_omniauth)['bio']).to eq('this is my bio')
end
it 'does not overwrite any exsisting profile fields' do
profile = Profile.new(:first_name => 'maxwell')
expect(profile.from_omniauth_hash(@from_omniauth)['first_name']).to eq('maxwell')
end
it 'sets full name to first name' do
@from_omniauth = {'name' => 'bob jones', 'description' => 'this is my bio', 'location' => 'sf', 'image' => 'http://cats.com/gif.gif'}
profile = Profile.new
expect(profile.from_omniauth_hash(@from_omniauth)['first_name']).to eq('bob jones')
end
end
describe '#contruct_full_name' do
it 'generates a full name given only first name' do
profile = FactoryGirl.build(:person).profile
profile.first_name = "casimiro"
profile.last_name = nil
expect(profile.full_name).not_to eq("casimiro")
profile.save
expect(profile.full_name).to eq("casimiro")
end
it 'generates a full name given only last name' do
profile = FactoryGirl.build(:person).profile
profile.first_name = nil
profile.last_name = "grippi"
expect(profile.full_name).not_to eq("grippi")
profile.save
expect(profile.full_name).to eq("grippi")
end
it 'generates a full name given first and last names' do
profile = FactoryGirl.build(:person).profile
profile.first_name = "casimiro"
profile.last_name = "grippi"
expect(profile.full_name).not_to eq("casimiro grippi")
profile.save
expect(profile.full_name).to eq("casimiro grippi")
end
end
describe "of last_name" do
it "strips leading and trailing whitespace" do
profile = FactoryGirl.build(:profile, :last_name => " Ohba ")
expect(profile).to be_valid
expect(profile.last_name).to eq("Ohba")
end
it "can be 32 characters long" do
profile = FactoryGirl.build(:profile, :last_name => "Hexagoooooooooooooooooooooooooon")
expect(profile).to be_valid
end
it "cannot be 33 characters" do
profile = FactoryGirl.build(:profile, :last_name => "Hexagooooooooooooooooooooooooooon")
expect(profile).not_to be_valid
end
it 'cannot have ;' do
profile = FactoryGirl.build(:profile, :last_name => "Hex;agon")
expect(profile).not_to be_valid
end
it 'disallows ; with a newline in the string' do
profile = FactoryGirl.build(:profile, :last_name => "H\nex;agon")
expect(profile).not_to be_valid
end
end
end
describe "of location" do
it "can be 255 characters long" do
profile = FactoryGirl.build(:profile, :location => "a"*255)
expect(profile).to be_valid
end
it "cannot be 256 characters" do
profile = FactoryGirl.build(:profile, :location => "a"*256)
expect(profile).not_to be_valid
end
end
describe "of gender" do
it "can be 255 characters long" do
profile = FactoryGirl.build(:profile, gender: "a" * 255)
expect(profile).to be_valid
end
it "cannot be 256 characters" do
profile = FactoryGirl.build(:profile, gender: "a" * 256)
expect(profile).not_to be_valid
end
end
describe "image_url setters" do
%i(image_url image_url_small image_url_medium).each do |method|
describe "##{method}=" do
before do
@profile = FactoryGirl.build(:profile)
@profile.public_send("#{method}=", "http://tom.joindiaspora.com/images/user/tom.jpg")
@pod_url = AppConfig.pod_uri.to_s.chomp("/")
end
it "saves nil when setting nil" do
@profile.public_send("#{method}=", nil)
expect(@profile[method]).to be_nil
end
it "saves nil when setting an empty string" do
@profile.public_send("#{method}=", "")
expect(@profile[method]).to be_nil
end
it "makes relative urls absolute" do
@profile.public_send("#{method}=", "/relative/url")
expect(@profile.public_send(method)).to eq("#{@pod_url}/relative/url")
end
it "doesn't change absolute urls" do
@profile.public_send("#{method}=", "http://not/a/relative/url")
expect(@profile.public_send(method)).to eq("http://not/a/relative/url")
end
it "saves the default-url as nil" do
@profile.public_send("#{method}=", "/assets/user/default.png")
expect(@profile[method]).to be_nil
end
end
end
end
describe '#image_url' do
before do
@profile = FactoryGirl.build(:profile)
end
it 'returns a default rather than nil' do
@profile.image_url = nil
expect(@profile.image_url).not_to be_nil
end
it 'falls back to the large thumbnail if the small thumbnail is nil' do
#Backwards compatibility
@profile[:image_url] = 'large'
@profile[:image_url_small] = nil
@profile[:image_url_medium] = nil
expect(@profile.image_url(:thumb_small)).to eq('large')
expect(@profile.image_url(:thumb_medium)).to eq('large')
end
end
describe '#subscribers' do
it 'returns all non-pending contacts for a user' do
expect(bob.profile.subscribers.map(&:id)).to match_array([alice.person, eve.person].map(&:id))
end
end
describe "public?" do
it "is public if public_details is true" do
profile = FactoryGirl.build(:profile, public_details: true)
expect(profile.public?).to be_truthy
end
it "is not public if public_details is false" do
profile = FactoryGirl.build(:profile, public_details: false)
expect(profile.public?).to be_falsey
end
end
describe 'date=' do
let(:profile) { FactoryGirl.build(:profile) }
it 'accepts form data' do
profile.birthday = nil
profile.date = { 'year' => '2000', 'month' => '01', 'day' => '01' }
expect(profile.birthday.year).to eq(2000)
expect(profile.birthday.month).to eq(1)
expect(profile.birthday.day).to eq(1)
end
it 'unsets the birthday' do
profile.birthday = Date.new(2000, 1, 1)
profile.date = { 'year' => '', 'month' => '', 'day' => ''}
expect(profile.birthday).to eq(nil)
end
it 'does not change with blank month and day values' do
profile.birthday = Date.new(2000, 1, 1)
profile.date = { 'year' => '2001', 'month' => '', 'day' => ''}
expect(profile.birthday.year).to eq(2000)
expect(profile.birthday.month).to eq(1)
expect(profile.birthday.day).to eq(1)
end
it 'does not accept blank initial values' do
profile.birthday = nil
profile.date = { 'year' => '2001', 'month' => '', 'day' => ''}
expect(profile.birthday).to eq(nil)
end
it 'does not accept invalid dates' do
profile.birthday = nil
profile.date = { 'year' => '2001', 'month' => '02', 'day' => '31' }
expect(profile.birthday).to eq(nil)
end
it 'does not change with invalid dates' do
profile.birthday = Date.new(2000, 1, 1)
profile.date = { 'year' => '2001', 'month' => '02', 'day' => '31' }
expect(profile.birthday.year).to eq(2000)
expect(profile.birthday.month).to eq(1)
expect(profile.birthday.day).to eq(1)
end
end
describe "tags" do
let(:object) { FactoryGirl.build(:person).profile }
it "allows 5 tags" do
object.tag_string = "#one #two #three #four #five"
object.valid?
object.errors.full_messages
expect(object).to be_valid
end
it "strips more than 5 tags" do
object.tag_string = "#one #two #three #four #five #six"
object.save
expect(object.tags.count).to eq(5)
end
it "should require tag name not be more than 255 characters long" do
object.tag_string = "##{'a' * (255 + 1)}"
object.save
expect(object).not_to be_valid
end
it "keeps the order of the tag_string" do
ActsAsTaggableOn::Tag.create(name: "test2")
ActsAsTaggableOn::Tag.create(name: "test1")
string = "#test1 #test2"
object.tag_string = string
object.save
expect(Profile.find(object.id).tag_string).to eq(string)
end
it_should_behave_like "it is taggable"
end
describe "#tombstone!" do
before do
@profile = bob.person.profile
end
it "clears the profile fields" do
attributes = @profile.send(:clearable_fields)
@profile.tombstone!
@profile.reload
attributes.each{ |attr|
expect(@profile[attr.to_sym]).to be_blank
}
end
it 'removes all the tags from the profile' do
expect(@profile.taggings).to receive(:delete_all)
@profile.tombstone!
end
it "doesn't recreate taggings if tag string was requested" do
@profile.tag_string
@profile.tombstone!
expect(@profile.taggings).to be_empty
end
end
describe "#clearable_fields" do
it 'returns the current profile fields' do
profile = FactoryGirl.build :profile
expect(profile.send(:clearable_fields).sort).to eq(
["diaspora_handle",
"first_name",
"last_name",
"image_url",
"image_url_small",
"image_url_medium",
"birthday",
"gender",
"bio",
"searchable",
"nsfw",
"location",
"public_details",
"full_name"].sort
)
end
end
end
|