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
85 # check that a visible user is returned properly in json
86 get api_user_path(:id => user.id, :format => "json")
87 assert_response :success
88 assert_equal "application/json", response.media_type
90 js = ActiveSupport::JSON.decode(@response.body)
92 assert_equal user.id, js["user"]["id"]
96 user = create(:user, :description => "test", :terms_agreed => Date.yesterday, :home_lat => 12.1, :home_lon => 12.1, :languages => ["en"])
97 create(:message, :read, :recipient => user)
98 create(:message, :sender => user)
100 # check that nothing is returned when not logged in
101 get user_details_path
102 assert_response :unauthorized
104 # check that we get a response when logged in
105 auth_header = basic_authorization_header user.email, "test"
106 get user_details_path, :headers => auth_header
107 assert_response :success
108 assert_equal "application/xml", response.media_type
110 # check the data that is returned
111 assert_select "description", :count => 1, :text => "test"
112 assert_select "contributor-terms", :count => 1 do
113 assert_select "[agreed='true'][pd='false']"
115 assert_select "img", :count => 0
116 assert_select "roles", :count => 1 do
117 assert_select "role", :count => 0
119 assert_select "changesets", :count => 1 do
120 assert_select "[count='0']", :count => 1
122 assert_select "traces", :count => 1 do
123 assert_select "[count='0']", :count => 1
125 assert_select "blocks", :count => 1 do
126 assert_select "received", :count => 1 do
127 assert_select "[count='0'][active='0']"
129 assert_select "issued", :count => 0
131 assert_select "home", :count => 1 do
132 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
134 assert_select "languages", :count => 1 do
135 assert_select "lang", :count => 1, :text => "en"
137 assert_select "messages", :count => 1 do
138 assert_select "received", :count => 1 do
139 assert_select "[count='1'][unread='0']"
141 assert_select "sent", :count => 1 do
142 assert_select "[count='1']"
148 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
149 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
150 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
152 get api_users_path(:users => user1.id)
153 assert_response :success
154 assert_equal "application/xml", response.media_type
155 assert_select "user", :count => 1 do
156 assert_select "user[id='#{user1.id}']", :count => 1
157 assert_select "user[id='#{user2.id}']", :count => 0
158 assert_select "user[id='#{user3.id}']", :count => 0
162 get api_users_path(:users => user1.id, :format => "json")
163 assert_response :success
164 assert_equal "application/json", response.media_type
166 js = ActiveSupport::JSON.decode(@response.body)
168 assert_equal 1, js["users"].count
170 get api_users_path(:users => user2.id)
171 assert_response :success
172 assert_equal "application/xml", response.media_type
173 assert_select "user", :count => 1 do
174 assert_select "user[id='#{user1.id}']", :count => 0
175 assert_select "user[id='#{user2.id}']", :count => 1
176 assert_select "user[id='#{user3.id}']", :count => 0
179 get api_users_path(:users => "#{user1.id},#{user3.id}")
180 assert_response :success
181 assert_equal "application/xml", response.media_type
182 assert_select "user", :count => 2 do
183 assert_select "user[id='#{user1.id}']", :count => 1
184 assert_select "user[id='#{user2.id}']", :count => 0
185 assert_select "user[id='#{user3.id}']", :count => 1
188 get api_users_path(:users => create(:user, :suspended).id)
189 assert_response :not_found
191 get api_users_path(:users => create(:user, :deleted).id)
192 assert_response :not_found
194 get api_users_path(:users => 0)
195 assert_response :not_found
200 trace1 = create(:trace, :user => user) do |trace|
201 create(:tracetag, :trace => trace, :tag => "London")
203 trace2 = create(:trace, :user => user) do |trace|
204 create(:tracetag, :trace => trace, :tag => "Birmingham")
206 # check that nothing is returned when not logged in
207 get user_gpx_files_path
208 assert_response :unauthorized
210 # check that we get a response when logged in
211 auth_header = basic_authorization_header user.email, "test"
212 get user_gpx_files_path, :headers => auth_header
213 assert_response :success
214 assert_equal "application/xml", response.media_type
216 # check the data that is returned
217 assert_select "gpx_file[id='#{trace1.id}']", 1 do
218 assert_select "tag", "London"
220 assert_select "gpx_file[id='#{trace2.id}']", 1 do
221 assert_select "tag", "Birmingham"