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