4 class UsersControllerTest < ActionController::TestCase
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/details", :method => :get },
14 { :controller => "api/users", :action => "details" }
17 { :path => "/api/0.6/user/gpx_files", :method => :get },
18 { :controller => "api/users", :action => "gpx_files" }
21 { :path => "/api/0.6/users", :method => :get },
22 { :controller => "api/users", :action => "index" }
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
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']"
38 assert_select "img", :count => 0
39 assert_select "roles", :count => 1 do
40 assert_select "role", :count => 0
42 assert_select "changesets", :count => 1 do
43 assert_select "[count='0']"
45 assert_select "traces", :count => 1 do
46 assert_select "[count='0']"
48 assert_select "blocks", :count => 1 do
49 assert_select "received", :count => 1 do
50 assert_select "[count='0'][active='0']"
52 assert_select "issued", :count => 0
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
61 # check that a suspended user is not returned
62 get :show, :params => { :id => create(:user, :suspended).id }
65 # check that a deleted user is not returned
66 get :show, :params => { :id => create(:user, :deleted).id }
69 # check that a non-existent user is not returned
70 get :show, :params => { :id => 0 }
71 assert_response :not_found
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)
79 # check that nothing is returned when not logged in
81 assert_response :unauthorized
83 # check that we get a response when logged in
84 basic_authorization user.email, "test"
86 assert_response :success
87 assert_equal "text/xml", response.media_type
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']"
94 assert_select "img", :count => 0
95 assert_select "roles", :count => 1 do
96 assert_select "role", :count => 0
98 assert_select "changesets", :count => 1 do
99 assert_select "[count='0']", :count => 1
101 assert_select "traces", :count => 1 do
102 assert_select "[count='0']", :count => 1
104 assert_select "blocks", :count => 1 do
105 assert_select "received", :count => 1 do
106 assert_select "[count='0'][active='0']"
108 assert_select "issued", :count => 0
110 assert_select "home", :count => 1 do
111 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
113 assert_select "languages", :count => 1 do
114 assert_select "lang", :count => 1, :text => "en"
116 assert_select "messages", :count => 1 do
117 assert_select "received", :count => 1 do
118 assert_select "[count='1'][unread='0']"
120 assert_select "sent", :count => 1 do
121 assert_select "[count='1']"
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)
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
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
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
158 get :index, :params => { :users => create(:user, :suspended).id }
159 assert_response :not_found
161 get :index, :params => { :users => create(:user, :deleted).id }
162 assert_response :not_found
164 get :index, :params => { :users => 0 }
165 assert_response :not_found
170 trace1 = create(:trace, :user => user) do |trace|
171 create(:tracetag, :trace => trace, :tag => "London")
173 trace2 = create(:trace, :user => user) do |trace|
174 create(:tracetag, :trace => trace, :tag => "Birmingham")
176 # check that nothing is returned when not logged in
178 assert_response :unauthorized
180 # check that we get a response when logged in
181 basic_authorization user.email, "test"
183 assert_response :success
184 assert_equal "application/xml", response.media_type
186 # check the data that is returned
187 assert_select "gpx_file[id='#{trace1.id}']", 1 do
188 assert_select "tag", "London"
190 assert_select "gpx_file[id='#{trace2.id}']", 1 do
191 assert_select "tag", "Birmingham"