people_test.rb
9.08 KB
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
require_relative 'test_helper'
class PeopleTest < ActiveSupport::TestCase
def setup
Person.delete_all
login_api
end
should 'list all people' do
person1 = fast_create(Person, :public_profile => true)
person2 = fast_create(Person)
get "/api/v1/people?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent [person1.id, person2.id, person.id], json['people'].map {|c| c['id']}
end
should 'list all members of a community' do
person1 = fast_create(Person)
person2 = fast_create(Person)
community = fast_create(Community)
community.add_member(person1)
community.add_member(person2)
get "/api/v1/profiles/#{community.id}/members?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal 2, json["people"].count
assert_equivalent [person1.id,person2.id], json["people"].map{|p| p["id"]}
end
should 'not list invisible people' do
invisible_person = fast_create(Person, :visible => false)
get "/api/v1/people?#{params.to_query}"
assert_not_includes json_response_ids(:people), invisible_person.id
end
should 'not list private people without permission' do
private_person = fast_create(Person, :public_profile => false)
get "/api/v1/people?#{params.to_query}"
assert_not_includes json_response_ids(:people), private_person.id
end
should 'list private person for friends' do
p1 = fast_create(Person)
p2 = fast_create(Person, :public_profile => false)
person.add_friend(p2)
p2.add_friend(person)
get "/api/v1/people?#{params.to_query}"
assert_includes json_response_ids(:people), p2.id
end
should 'get person' do
some_person = fast_create(Person)
get "/api/v1/people/#{some_person.id}?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal some_person.id, json['person']['id']
end
should 'people endpoint filter by fields parameter' do
get "/api/v1/people?#{params.to_query}&fields=name"
json = JSON.parse(last_response.body)
expected = {'people' => [{'name' => person.name}]}
assert_equal expected, json
end
should 'people endpoint filter by fields parameter with hierarchy' do
fields = URI.encode({only: [:name, {user: [:login]}]}.to_json)
get "/api/v1/people?#{params.to_query}&fields=#{fields}"
json = JSON.parse(last_response.body)
expected = {'people' => [{'name' => person.name, 'user' => {'login' => 'testapi'}}]}
assert_equal expected, json
end
should 'get logged person' do
get "/api/v1/people/me?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal person.id, json['person']['id']
end
should 'me endpoint filter by fields parameter' do
get "/api/v1/people/me?#{params.to_query}&fields=name"
json = JSON.parse(last_response.body)
expected = {'person' => {'name' => person.name}}
assert_equal expected, json
end
should 'not get invisible person' do
person = fast_create(Person, :visible => false)
get "/api/v1/people/#{person.id}?#{params.to_query}"
json = JSON.parse(last_response.body)
assert json['person'].blank?
end
should 'not get private people without permission' do
private_person = fast_create(Person, :public_profile => false)
get "/api/v1/people/#{private_person.id}?#{params.to_query}"
json = JSON.parse(last_response.body)
assert json['person'].blank?
end
should 'get private person for friends' do
private_person = fast_create(Person, :public_profile => false)
person.add_friend(private_person)
private_person.add_friend(person)
get "/api/v1/people/#{private_person.id}?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal private_person.id, json['person']['id']
end
should 'list person friends' do
friend = fast_create(Person)
person.add_friend(friend)
friend.add_friend(person)
get "/api/v1/people/#{friend.id}/friends?#{params.to_query}"
assert_includes json_response_ids(:people), person.id
end
should 'not list person invisible friends' do
friend = fast_create(Person)
invisible_friend = fast_create(Person, :visible => false)
person.add_friend(friend)
person.add_friend(invisible_friend)
friend.add_friend(person)
invisible_friend.add_friend(person)
get "/api/v1/people/#{person.id}/friends?#{params.to_query}"
friends = json_response_ids(:people)
assert_includes friends, friend.id
assert_not_includes friends, invisible_friend.id
end
should 'create a person' do
login = 'some'
params[:person] = {:login => login, :password => '123456', :password_confirmation => '123456', :email => 'some@some.com'}
post "/api/v1/people?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal login, json['person']['identifier']
end
should 'return 400 status for invalid person creation' do
params[:person] = {:login => 'some'}
post "/api/v1/people?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal 400, last_response.status
end
should 'display permissions' do
community = fast_create(Community)
community.add_member(fast_create(Person))
community.add_member(person)
permissions = Profile::Roles.member(person.environment.id).permissions
get "/api/v1/people/#{person.id}/permissions?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal json[community.identifier], permissions
end
should 'display permissions if self' do
get "/api/v1/people/#{person.id}/permissions?#{params.to_query}"
assert_equal 200, last_response.status
end
should 'display permissions if admin' do
environment = person.environment
environment.add_admin(person)
some_person = fast_create(Person)
get "/api/v1/people/#{some_person.id}/permissions?#{params.to_query}"
assert_equal 200, last_response.status
end
should 'not display permissions if not admin or self' do
some_person = create_user('some-person').person
get "/api/v1/people/#{some_person.id}/permissions?#{params.to_query}"
assert_equal 403, last_response.status
end
should 'not update another person' do
person = fast_create(Person, :environment_id => environment.id)
post "/api/v1/people/#{person.id}?#{params.to_query}"
assert_equal 403, last_response.status
end
should 'update yourself' do
another_name = 'Another Name'
params[:person] = {}
params[:person][:name] = another_name
assert_not_equal another_name, person.name
post "/api/v1/people/#{person.id}?#{params.to_query}"
person.reload
assert_equal another_name, person.name
end
should 'display public custom fields' do
CustomField.create!(:name => "Custom Blog", :format => "string", :customized_type => "Person", :active => true, :environment => Environment.default)
some_person = create_user('some-person').person
some_person.custom_values = { "Custom Blog" => { "value" => "www.blog.org", "public" => "true"} }
some_person.save!
get "/api/v1/people/#{some_person.id}?#{params.to_query}"
json = JSON.parse(last_response.body)
assert json['person']['additional_data'].has_key?('Custom Blog')
assert_equal "www.blog.org", json['person']['additional_data']['Custom Blog']
end
should 'not display non-public custom fields' do
CustomField.create!(:name => "Custom Blog", :format => "string", :customized_type => "Person", :active => true, :environment => Environment.default)
some_person = create_user('some-person').person
some_person.custom_values = { "Custom Blog" => { "value" => "www.blog.org", "public" => "0"} }
some_person.save!
get "/api/v1/people/#{some_person.id}?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal json['person']['additional_data'], {}
end
should 'display non-public custom fields to friend' do
CustomField.create!(:name => "Custom Blog", :format => "string", :customized_type => "Person", :active => true, :environment => Environment.default)
some_person = create_user('some-person').person
some_person.custom_values = { "Custom Blog" => { "value" => "www.blog.org", "public" => "0"} }
some_person.save!
f = Friendship.new
f.friend = some_person
f.person = person
f.save!
get "/api/v1/people/#{some_person.id}?#{params.to_query}"
json = JSON.parse(last_response.body)
assert json['person']['additional_data'].has_key?("Custom Blog")
assert_equal "www.blog.org", json['person']['additional_data']['Custom Blog']
end
PERSON_ATTRIBUTES = %w(vote_count comments_count articles_count)
PERSON_ATTRIBUTES.map do |attribute|
define_method "test_should_not_expose_#{attribute}_attribute_in_person_enpoint_if_field_parameter_does_not_contain_the_attribute" do
get "/api/v1/people/me?#{params.to_query}&fields=name"
json = JSON.parse(last_response.body)
assert_nil json['person'][attribute]
end
define_method "test_should_expose_#{attribute}_attribute_in_person_enpoints_if_field_parameter_is_passed" do
get "/api/v1/people/me?#{params.to_query}&fields=#{attribute}"
json = JSON.parse(last_response.body)
assert_not_nil json['person'][attribute]
end
end
end