]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/users_controller_test.rb
Merge pull request #2427 from mmd-osm/patch/remove_expand_bbox
[rails.git] / test / controllers / api / users_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class UsersControllerTest < ActionController::TestCase
5     def setup
6       super
7
8       stub_hostip_requests
9     end
10
11     ##
12     # test all routes which lead to this controller
13     def test_routes
14       assert_routing(
15         { :path => "/api/0.6/user/1", :method => :get },
16         { :controller => "api/users", :action => "show", :id => "1" }
17       )
18       assert_routing(
19         { :path => "/api/0.6/user/details", :method => :get },
20         { :controller => "api/users", :action => "details" }
21       )
22       assert_routing(
23         { :path => "/api/0.6/user/gpx_files", :method => :get },
24         { :controller => "api/users", :action => "gpx_files" }
25       )
26       assert_routing(
27         { :path => "/api/0.6/users", :method => :get },
28         { :controller => "api/users", :action => "index" }
29       )
30     end
31
32     def test_show
33       user = create(:user, :description => "test", :terms_agreed => Date.yesterday)
34       # check that a visible user is returned properly
35       get :show, :params => { :id => user.id }
36       assert_response :success
37       assert_equal "text/xml", response.media_type
38
39       # check the data that is returned
40       assert_select "description", :count => 1, :text => "test"
41       assert_select "contributor-terms", :count => 1 do
42         assert_select "[agreed='true']"
43       end
44       assert_select "img", :count => 0
45       assert_select "roles", :count => 1 do
46         assert_select "role", :count => 0
47       end
48       assert_select "changesets", :count => 1 do
49         assert_select "[count='0']"
50       end
51       assert_select "traces", :count => 1 do
52         assert_select "[count='0']"
53       end
54       assert_select "blocks", :count => 1 do
55         assert_select "received", :count => 1 do
56           assert_select "[count='0'][active='0']"
57         end
58         assert_select "issued", :count => 0
59       end
60
61       # check that we aren't revealing private information
62       assert_select "contributor-terms[pd]", false
63       assert_select "home", false
64       assert_select "languages", false
65       assert_select "messages", false
66
67       # check that a suspended user is not returned
68       get :show, :params => { :id => create(:user, :suspended).id }
69       assert_response :gone
70
71       # check that a deleted user is not returned
72       get :show, :params => { :id => create(:user, :deleted).id }
73       assert_response :gone
74
75       # check that a non-existent user is not returned
76       get :show, :params => { :id => 0 }
77       assert_response :not_found
78     end
79
80     def test_details
81       user = create(:user, :description => "test", :terms_agreed => Date.yesterday, :home_lat => 12.1, :home_lon => 12.1, :languages => ["en"])
82       create(:message, :read, :recipient => user)
83       create(:message, :sender => user)
84
85       # check that nothing is returned when not logged in
86       get :details
87       assert_response :unauthorized
88
89       # check that we get a response when logged in
90       basic_authorization user.email, "test"
91       get :details
92       assert_response :success
93       assert_equal "text/xml", response.media_type
94
95       # check the data that is returned
96       assert_select "description", :count => 1, :text => "test"
97       assert_select "contributor-terms", :count => 1 do
98         assert_select "[agreed='true'][pd='false']"
99       end
100       assert_select "img", :count => 0
101       assert_select "roles", :count => 1 do
102         assert_select "role", :count => 0
103       end
104       assert_select "changesets", :count => 1 do
105         assert_select "[count='0']", :count => 1
106       end
107       assert_select "traces", :count => 1 do
108         assert_select "[count='0']", :count => 1
109       end
110       assert_select "blocks", :count => 1 do
111         assert_select "received", :count => 1 do
112           assert_select "[count='0'][active='0']"
113         end
114         assert_select "issued", :count => 0
115       end
116       assert_select "home", :count => 1 do
117         assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
118       end
119       assert_select "languages", :count => 1 do
120         assert_select "lang", :count => 1, :text => "en"
121       end
122       assert_select "messages", :count => 1 do
123         assert_select "received", :count => 1 do
124           assert_select "[count='1'][unread='0']"
125         end
126         assert_select "sent", :count => 1 do
127           assert_select "[count='1']"
128         end
129       end
130     end
131
132     def test_index
133       user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
134       user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
135       user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
136
137       get :index, :params => { :users => user1.id }
138       assert_response :success
139       assert_equal "text/xml", response.media_type
140       assert_select "user", :count => 1 do
141         assert_select "user[id='#{user1.id}']", :count => 1
142         assert_select "user[id='#{user2.id}']", :count => 0
143         assert_select "user[id='#{user3.id}']", :count => 0
144       end
145
146       get :index, :params => { :users => user2.id }
147       assert_response :success
148       assert_equal "text/xml", response.media_type
149       assert_select "user", :count => 1 do
150         assert_select "user[id='#{user1.id}']", :count => 0
151         assert_select "user[id='#{user2.id}']", :count => 1
152         assert_select "user[id='#{user3.id}']", :count => 0
153       end
154
155       get :index, :params => { :users => "#{user1.id},#{user3.id}" }
156       assert_response :success
157       assert_equal "text/xml", response.media_type
158       assert_select "user", :count => 2 do
159         assert_select "user[id='#{user1.id}']", :count => 1
160         assert_select "user[id='#{user2.id}']", :count => 0
161         assert_select "user[id='#{user3.id}']", :count => 1
162       end
163
164       get :index, :params => { :users => create(:user, :suspended).id }
165       assert_response :not_found
166
167       get :index, :params => { :users => create(:user, :deleted).id }
168       assert_response :not_found
169
170       get :index, :params => { :users => 0 }
171       assert_response :not_found
172     end
173
174     def test_gpx_files
175       user = create(:user)
176       trace1 = create(:trace, :user => user) do |trace|
177         create(:tracetag, :trace => trace, :tag => "London")
178       end
179       trace2 = create(:trace, :user => user) do |trace|
180         create(:tracetag, :trace => trace, :tag => "Birmingham")
181       end
182       # check that nothing is returned when not logged in
183       get :gpx_files
184       assert_response :unauthorized
185
186       # check that we get a response when logged in
187       basic_authorization user.email, "test"
188       get :gpx_files
189       assert_response :success
190       assert_equal "application/xml", response.media_type
191
192       # check the data that is returned
193       assert_select "gpx_file[id='#{trace1.id}']", 1 do
194         assert_select "tag", "London"
195       end
196       assert_select "gpx_file[id='#{trace2.id}']", 1 do
197         assert_select "tag", "Birmingham"
198       end
199     end
200   end
201 end