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