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
|
# 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, 'lib/diaspora/importer')
describe Diaspora::Importer do
let!(:user1) { Factory(:user) }
let!(:user2) { Factory(:user) }
let!(:user3) { Factory(:user) }
let(:aspect1) { user1.aspect(:name => "Work") }
let(:aspect2) { user2.aspect(:name => "Family") }
let(:aspect3) { user3.aspect(:name => "Pivots") }
let!(:status_message1) { user1.post(:status_message, :message => "One", :public => true, :to => aspect1.id) }
let!(:status_message2) { user1.post(:status_message, :message => "Two", :public => true, :to => aspect1.id) }
let!(:status_message3) { user2.post(:status_message, :message => "Three", :public => false, :to => aspect2.id) }
let(:importer) { Diaspora::Importer.new(Diaspora::Parsers::XML) }
context 'serialized user' do
describe '#verify_user' do
it 'should return true for a new valid user' do
new_user = Factory(:user)
new_user.delete
importer.verify_user(new_user).should be true
end
it 'should return false if vaild user already exists' do
u = User.first
lambda{ importer.verify_user(user1) }.should raise_error
end
end
describe '#verify_person_for_user' do
it 'should pass if keys match' do
importer.verify_person_for_user(user1, user1.person).should be true
end
it 'should fail if private and public keys do not match' do
person = Factory(:person)
lambda{ importer.verify_person_for_user(user1, person) }.should raise_error
end
it 'should pass if the person does not exist' do
user = Factory.build(:user)
importer.verify_person_for_user(user, user.person)
end
end
describe 'verify contacts' do
let(:contact1) {Contact.new(:user => user1, :person => user2.person, :aspects => [aspect1])}
let(:contact2) {Contact.new(:user => user1, :person => user3.person, :aspects => [aspect2])}
let(:contact3) {Contact.new(:user => user1, :person => user3.person, :aspects => [aspect3])}
let(:less_contacts) {[contact1]}
let(:same_contacts) {[contact1, contact2]}
let(:more_contacts) {[contact1, contact2, contact3]}
let(:person_ids) {[user2.person.id, user3.person.id]}
it 'should be false if the number of the number of contacts is not equal to the number of imported people' do
importer.verify_contacts(less_contacts, person_ids).should be false
importer.verify_contacts(same_contacts, person_ids).should be true
importer.verify_contacts(more_contacts, person_ids).should be false
end
end
describe '#filter_posts' do
it 'should make sure all found posts are owned by the user' do
posts = [status_message1, status_message2]
whitelist = importer.filter_posts(posts, user1.person)[:whitelist]
whitelist.should have(2).posts
whitelist.should include status_message1.id.to_s
whitelist.should include status_message2.id.to_s
end
it 'should remove posts not owned by the user' do
posts = [status_message1, status_message2, status_message3]
whitelist = importer.filter_posts(posts, user1.person)[:whitelist]
whitelist.should have(2).posts
whitelist.should_not include status_message3.id
end
it 'should return a list of unknown posts' do
posts = [status_message1, status_message2, Factory.build(:status_message)]
unknown = importer.filter_posts(posts, user1.person)[:unknown]
unknown.should have(1).post
end
it 'should generate a whitelist, unknown posts inclusive' do
posts = [status_message1, status_message2, Factory.build(:status_message)]
filters = importer.filter_posts(posts, user1.person)
filters[:whitelist].should include filters[:unknown].keys.first
end
end
describe '#clean_aspects' do
it 'should purge posts not in whitelist that are present in aspects' do
whitelist = {status_message1.id.to_s => true, status_message2.id.to_s => true}
aspect1.reload
aspect1.post_ids << status_message3.id.to_s
proc{ importer.clean_aspects([aspect1], whitelist) }.should change(aspect1.post_ids, :count).by(-1)
aspect1.post_ids.should_not include status_message3.id
end
end
describe '#filter_people' do
it 'should filter people who already exist in the database' do
new_peep = Factory.build(:person)
people = [user1.person, user2.person, new_peep]
importer.filter_people(people).keys.should == [new_peep.id.to_s]
end
end
end
end
|