4 class UsersControllerTest < ActionController::TestCase
10 # test all routes which lead to this controller
13 { :path => "/api/0.6/user/1", :method => :get },
14 { :controller => "api/users", :action => "show", :id => "1" }
17 { :path => "/api/0.6/user/details", :method => :get },
18 { :controller => "api/users", :action => "details" }
21 { :path => "/api/0.6/user/gpx_files", :method => :get },
22 { :controller => "api/users", :action => "gpx_files" }
25 { :path => "/api/0.6/users", :method => :get },
26 { :controller => "api/users", :action => "index" }
31 user = create(:user, :description => "test", :terms_agreed => Date.yesterday)
32 # check that a visible user is returned properly
33 get :show, :params => { :id => user.id }
34 assert_response :success
35 assert_equal "text/xml", response.content_type
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']"
42 assert_select "img", :count => 0
43 assert_select "roles", :count => 1 do
44 assert_select "role", :count => 0
46 assert_select "changesets", :count => 1 do
47 assert_select "[count='0']"
49 assert_select "traces", :count => 1 do
50 assert_select "[count='0']"
52 assert_select "blocks", :count => 1 do
53 assert_select "received", :count => 1 do
54 assert_select "[count='0'][active='0']"
56 assert_select "issued", :count => 0
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
65 # check that a suspended user is not returned
66 get :show, :params => { :id => create(:user, :suspended).id }
69 # check that a deleted user is not returned
70 get :show, :params => { :id => create(:user, :deleted).id }
73 # check that a non-existent user is not returned
74 get :show, :params => { :id => 0 }
75 assert_response :not_found
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)
83 # check that nothing is returned when not logged in
85 assert_response :unauthorized
87 # check that we get a response when logged in
88 basic_authorization user.email, "test"
90 assert_response :success
91 assert_equal "text/xml", response.content_type
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']"
98 assert_select "img", :count => 0
99 assert_select "roles", :count => 1 do
100 assert_select "role", :count => 0
102 assert_select "changesets", :count => 1 do
103 assert_select "[count='0']", :count => 1
105 assert_select "traces", :count => 1 do
106 assert_select "[count='0']", :count => 1
108 assert_select "blocks", :count => 1 do
109 assert_select "received", :count => 1 do
110 assert_select "[count='0'][active='0']"
112 assert_select "issued", :count => 0
114 assert_select "home", :count => 1 do
115 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
117 assert_select "languages", :count => 1 do
118 assert_select "lang", :count => 1, :text => "en"
120 assert_select "messages", :count => 1 do
121 assert_select "received", :count => 1 do
122 assert_select "[count='1'][unread='0']"
124 assert_select "sent", :count => 1 do
125 assert_select "[count='1']"
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)
135 get :index, :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
144 get :index, :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
153 get :index, :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
162 get :index, :params => { :users => create(:user, :suspended).id }
163 assert_response :not_found
165 get :index, :params => { :users => create(:user, :deleted).id }
166 assert_response :not_found
168 get :index, :params => { :users => 0 }
169 assert_response :not_found
174 trace1 = create(:trace, :user => user) do |trace|
175 create(:tracetag, :trace => trace, :tag => "London")
177 trace2 = create(:trace, :user => user) do |trace|
178 create(:tracetag, :trace => trace, :tag => "Birmingham")
180 # check that nothing is returned when not logged in
182 assert_response :unauthorized
184 # check that we get a response when logged in
185 basic_authorization user.email, "test"
187 assert_response :success
188 assert_equal "application/xml", response.content_type
190 # check the data that is returned
191 assert_select "gpx_file[id='#{trace1.id}']", 1 do
192 assert_select "tag", "London"
194 assert_select "gpx_file[id='#{trace2.id}']", 1 do
195 assert_select "tag", "Birmingham"