4 class UsersControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/user/1", :method => :get },
10 { :controller => "api/users", :action => "show", :id => "1" }
13 { :path => "/api/0.6/user/1.json", :method => :get },
14 { :controller => "api/users", :action => "show", :id => "1", :format => "json" }
17 { :path => "/api/0.6/user/details", :method => :get },
18 { :controller => "api/users", :action => "details" }
21 { :path => "/api/0.6/user/details.json", :method => :get },
22 { :controller => "api/users", :action => "details", :format => "json" }
25 { :path => "/api/0.6/user/gpx_files", :method => :get },
26 { :controller => "api/users", :action => "gpx_files" }
29 { :path => "/api/0.6/users", :method => :get },
30 { :controller => "api/users", :action => "index" }
33 { :path => "/api/0.6/users.json", :method => :get },
34 { :controller => "api/users", :action => "index", :format => "json" }
39 user = create(:user, :description => "test", :terms_agreed => Date.yesterday)
40 # check that a visible user is returned properly
41 get api_user_path(:id => user.id)
42 assert_response :success
43 assert_equal "application/xml", response.media_type
45 # check the data that is returned
46 assert_select "description", :count => 1, :text => "test"
47 assert_select "contributor-terms", :count => 1 do
48 assert_select "[agreed='true']"
50 assert_select "img", :count => 0
51 assert_select "roles", :count => 1 do
52 assert_select "role", :count => 0
54 assert_select "changesets", :count => 1 do
55 assert_select "[count='0']"
57 assert_select "traces", :count => 1 do
58 assert_select "[count='0']"
60 assert_select "blocks", :count => 1 do
61 assert_select "received", :count => 1 do
62 assert_select "[count='0'][active='0']"
64 assert_select "issued", :count => 0
67 # check that we aren't revealing private information
68 assert_select "contributor-terms[pd]", false
69 assert_select "home", false
70 assert_select "languages", false
71 assert_select "messages", false
73 # check that a suspended user is not returned
74 get api_user_path(:id => create(:user, :suspended).id)
77 # check that a deleted user is not returned
78 get api_user_path(:id => create(:user, :deleted).id)
81 # check that a non-existent user is not returned
82 get api_user_path(:id => 0)
83 assert_response :not_found
87 user = create(:user, :description => "test", :terms_agreed => Date.yesterday, :home_lat => 12.1, :home_lon => 12.1, :languages => ["en"])
88 create(:message, :read, :recipient => user)
89 create(:message, :sender => user)
91 # check that nothing is returned when not logged in
93 assert_response :unauthorized
95 # check that we get a response when logged in
96 auth_header = basic_authorization_header user.email, "test"
97 get user_details_path, :headers => auth_header
98 assert_response :success
99 assert_equal "application/xml", response.media_type
101 # check the data that is returned
102 assert_select "description", :count => 1, :text => "test"
103 assert_select "contributor-terms", :count => 1 do
104 assert_select "[agreed='true'][pd='false']"
106 assert_select "img", :count => 0
107 assert_select "roles", :count => 1 do
108 assert_select "role", :count => 0
110 assert_select "changesets", :count => 1 do
111 assert_select "[count='0']", :count => 1
113 assert_select "traces", :count => 1 do
114 assert_select "[count='0']", :count => 1
116 assert_select "blocks", :count => 1 do
117 assert_select "received", :count => 1 do
118 assert_select "[count='0'][active='0']"
120 assert_select "issued", :count => 0
122 assert_select "home", :count => 1 do
123 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
125 assert_select "languages", :count => 1 do
126 assert_select "lang", :count => 1, :text => "en"
128 assert_select "messages", :count => 1 do
129 assert_select "received", :count => 1 do
130 assert_select "[count='1'][unread='0']"
132 assert_select "sent", :count => 1 do
133 assert_select "[count='1']"
139 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
140 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
141 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
143 get api_users_path(:users => user1.id)
144 assert_response :success
145 assert_equal "application/xml", response.media_type
146 assert_select "user", :count => 1 do
147 assert_select "user[id='#{user1.id}']", :count => 1
148 assert_select "user[id='#{user2.id}']", :count => 0
149 assert_select "user[id='#{user3.id}']", :count => 0
153 get api_users_path(:users => user1.id, :format => "json")
154 assert_response :success
155 assert_equal "application/json", response.media_type
157 js = ActiveSupport::JSON.decode(@response.body)
159 assert_equal 1, js["users"].count
161 get api_users_path(:users => user2.id)
162 assert_response :success
163 assert_equal "application/xml", response.media_type
164 assert_select "user", :count => 1 do
165 assert_select "user[id='#{user1.id}']", :count => 0
166 assert_select "user[id='#{user2.id}']", :count => 1
167 assert_select "user[id='#{user3.id}']", :count => 0
170 get api_users_path(:users => "#{user1.id},#{user3.id}")
171 assert_response :success
172 assert_equal "application/xml", response.media_type
173 assert_select "user", :count => 2 do
174 assert_select "user[id='#{user1.id}']", :count => 1
175 assert_select "user[id='#{user2.id}']", :count => 0
176 assert_select "user[id='#{user3.id}']", :count => 1
179 get api_users_path(:users => create(:user, :suspended).id)
180 assert_response :not_found
182 get api_users_path(:users => create(:user, :deleted).id)
183 assert_response :not_found
185 get api_users_path(:users => 0)
186 assert_response :not_found
191 trace1 = create(:trace, :user => user) do |trace|
192 create(:tracetag, :trace => trace, :tag => "London")
194 trace2 = create(:trace, :user => user) do |trace|
195 create(:tracetag, :trace => trace, :tag => "Birmingham")
197 # check that nothing is returned when not logged in
198 get user_gpx_files_path
199 assert_response :unauthorized
201 # check that we get a response when logged in
202 auth_header = basic_authorization_header user.email, "test"
203 get user_gpx_files_path, :headers => auth_header
204 assert_response :success
205 assert_equal "application/xml", response.media_type
207 # check the data that is returned
208 assert_select "gpx_file[id='#{trace1.id}']", 1 do
209 assert_select "tag", "London"
211 assert_select "gpx_file[id='#{trace2.id}']", 1 do
212 assert_select "tag", "Birmingham"