communities_test.rb
5.31 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
require File.dirname(__FILE__) + '/test_helper'
class CommunitiesTest < ActiveSupport::TestCase
def setup
Community.delete_all
login_api
end
should 'list only communities' do
community = fast_create(Community)
enterprise = fast_create(Enterprise) # should not list this enterprise
get "/api/v1/communities?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_not_includes json['communities'].map {|c| c['id']}, enterprise.id
assert_includes json['communities'].map {|c| c['id']}, community.id
end
should 'list all communities' do
community1 = fast_create(Community, :public_profile => true)
community2 = fast_create(Community)
get "/api/v1/communities?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent [community1.id, community2.id], json['communities'].map {|c| c['id']}
end
should 'not list invisible communities' do
community1 = fast_create(Community)
fast_create(Community, :visible => false)
get "/api/v1/communities?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal [community1.id], json['communities'].map {|c| c['id']}
end
should 'not list private communities without permission' do
community1 = fast_create(Community)
fast_create(Community, :public_profile => false)
get "/api/v1/communities?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal [community1.id], json['communities'].map {|c| c['id']}
end
should 'list private community for members' do
c1 = fast_create(Community)
c2 = fast_create(Community, :public_profile => false)
c2.add_member(person)
get "/api/v1/communities?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent [c1.id, c2.id], json['communities'].map {|c| c['id']}
end
should 'create a community' do
params[:community] = {:name => 'some'}
post "/api/v1/communities?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal 'some', json['community']['name']
end
should 'return 400 status for invalid community creation' do
post "/api/v1/communities?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal 400, last_response.status
end
should 'get community' do
community = fast_create(Community)
get "/api/v1/communities/#{community.id}?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal community.id, json['community']['id']
end
should 'not get invisible community' do
community = fast_create(Community, :visible => false)
get "/api/v1/communities/#{community.id}?#{params.to_query}"
json = JSON.parse(last_response.body)
assert json['community'].blank?
end
should 'not get private communities without permission' do
community = fast_create(Community)
fast_create(Community, :public_profile => false)
get "/api/v1/communities/#{community.id}?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal community.id, json['community']['id']
end
should 'get private community for members' do
community = fast_create(Community, :public_profile => false)
community.add_member(person)
get "/api/v1/communities/#{community.id}?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal community.id, json['community']['id']
end
should 'list person communities' do
community = fast_create(Community)
fast_create(Community)
community.add_member(person)
get "/api/v1/people/#{person.id}/communities?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent [community.id], json['communities'].map {|c| c['id']}
end
should 'not list person communities invisible' do
c1 = fast_create(Community)
c2 = fast_create(Community, :visible => false)
c1.add_member(person)
c2.add_member(person)
get "/api/v1/people/#{person.id}/communities?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent [c1.id], json['communities'].map {|c| c['id']}
end
should 'list communities with pagination' do
community1 = fast_create(Community, :public_profile => true)
community2 = fast_create(Community)
params[:page] = 2
params[:per_page] = 1
get "/api/v1/communities?#{params.to_query}"
json_page_two = JSON.parse(last_response.body)
params[:page] = 1
params[:per_page] = 1
get "/api/v1/communities?#{params.to_query}"
json_page_one = JSON.parse(last_response.body)
assert_includes json_page_one["communities"].map { |a| a["id"] }, community1.id
assert_not_includes json_page_one["communities"].map { |a| a["id"] }, community2.id
assert_includes json_page_two["communities"].map { |a| a["id"] }, community2.id
assert_not_includes json_page_two["communities"].map { |a| a["id"] }, community1.id
end
should 'list communities with timestamp' do
community1 = fast_create(Community, :public_profile => true)
community2 = fast_create(Community)
community1.updated_at = Time.now + 3.hours
community1.save!
params[:timestamp] = Time.now + 1.hours
get "/api/v1/communities/?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_includes json["communities"].map { |a| a["id"] }, community1.id
assert_not_includes json["communities"].map { |a| a["id"] }, community2.id
end
end