4 class UsersControllerTest < ActionController::TestCase
12 # test all routes which lead to this controller
15 { :path => "/api/0.6/user/1", :method => :get },
16 { :controller => "api/users", :action => "show", :id => "1" }
19 { :path => "/api/0.6/user/details", :method => :get },
20 { :controller => "api/users", :action => "details" }
23 { :path => "/api/0.6/user/gpx_files", :method => :get },
24 { :controller => "api/users", :action => "gpx_files" }
27 { :path => "/api/0.6/users", :method => :get },
28 { :controller => "api/users", :action => "index" }
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
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']"
44 assert_select "img", :count => 0
45 assert_select "roles", :count => 1 do
46 assert_select "role", :count => 0
48 assert_select "changesets", :count => 1 do
49 assert_select "[count='0']"
51 assert_select "traces", :count => 1 do
52 assert_select "[count='0']"
54 assert_select "blocks", :count => 1 do
55 assert_select "received", :count => 1 do
56 assert_select "[count='0'][active='0']"
58 assert_select "issued", :count => 0
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
67 # check that a suspended user is not returned
68 get :show, :params => { :id => create(:user, :suspended).id }
71 # check that a deleted user is not returned
72 get :show, :params => { :id => create(:user, :deleted).id }
75 # check that a non-existent user is not returned
76 get :show, :params => { :id => 0 }
77 assert_response :not_found
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)
85 # check that nothing is returned when not logged in
87 assert_response :unauthorized
89 # check that we get a response when logged in
90 basic_authorization user.email, "test"
92 assert_response :success
93 assert_equal "text/xml", response.media_type
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']"
100 assert_select "img", :count => 0
101 assert_select "roles", :count => 1 do
102 assert_select "role", :count => 0
104 assert_select "changesets", :count => 1 do
105 assert_select "[count='0']", :count => 1
107 assert_select "traces", :count => 1 do
108 assert_select "[count='0']", :count => 1
110 assert_select "blocks", :count => 1 do
111 assert_select "received", :count => 1 do
112 assert_select "[count='0'][active='0']"
114 assert_select "issued", :count => 0
116 assert_select "home", :count => 1 do
117 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
119 assert_select "languages", :count => 1 do
120 assert_select "lang", :count => 1, :text => "en"
122 assert_select "messages", :count => 1 do
123 assert_select "received", :count => 1 do
124 assert_select "[count='1'][unread='0']"
126 assert_select "sent", :count => 1 do
127 assert_select "[count='1']"
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)
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
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
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
164 get :index, :params => { :users => create(:user, :suspended).id }
165 assert_response :not_found
167 get :index, :params => { :users => create(:user, :deleted).id }
168 assert_response :not_found
170 get :index, :params => { :users => 0 }
171 assert_response :not_found
176 trace1 = create(:trace, :user => user) do |trace|
177 create(:tracetag, :trace => trace, :tag => "London")
179 trace2 = create(:trace, :user => user) do |trace|
180 create(:tracetag, :trace => trace, :tag => "Birmingham")
182 # check that nothing is returned when not logged in
184 assert_response :unauthorized
186 # check that we get a response when logged in
187 basic_authorization user.email, "test"
189 assert_response :success
190 assert_equal "application/xml", response.media_type
192 # check the data that is returned
193 assert_select "gpx_file[id='#{trace1.id}']", 1 do
194 assert_select "tag", "London"
196 assert_select "gpx_file[id='#{trace2.id}']", 1 do
197 assert_select "tag", "Birmingham"