1 require File.dirname(__FILE__) + '/../test_helper'
3 class NoteControllerTest < ActionController::TestCase
4 fixtures :users, :notes, :note_comments
6 def test_note_create_success
7 assert_difference('Note.count') do
8 assert_difference('NoteComment.count') do
9 post :create, {:lat => -1.0, :lon => -1.0, :name => "new_tester", :text => "This is a comment"}
12 assert_response :success
13 id = @response.body.sub(/ok/,"").to_i
15 get :read, {:id => id, :format => 'json'}
16 assert_response :success
17 js = ActiveSupport::JSON.decode(@response.body)
19 assert_equal "Feature", js["type"]
20 assert_equal "Point", js["geometry"]["type"]
21 assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
22 assert_equal id, js["properties"]["id"]
23 assert_equal "open", js["properties"]["status"]
24 assert_equal 1, js["properties"]["comments"].count
25 assert_equal "opened", js["properties"]["comments"].last["action"]
26 assert_equal "This is a comment", js["properties"]["comments"].last["text"]
27 assert_equal "new_tester (a)", js["properties"]["comments"].last["user"]
30 def test_note_create_fail
31 assert_no_difference('Note.count') do
32 assert_no_difference('NoteComment.count') do
33 post :create, {:lon => -1.0, :name => "new_tester", :text => "This is a comment"}
36 assert_response :bad_request
38 assert_no_difference('Note.count') do
39 assert_no_difference('NoteComment.count') do
40 post :create, {:lat => -1.0, :name => "new_tester", :text => "This is a comment"}
43 assert_response :bad_request
45 assert_no_difference('Note.count') do
46 assert_no_difference('NoteComment.count') do
47 post :create, {:lat => -1.0, :lon => -1.0, :name => "new_tester"}
50 assert_response :bad_request
52 assert_no_difference('Note.count') do
53 assert_no_difference('NoteComment.count') do
54 post :create, {:lat => -100.0, :lon => -1.0, :name => "new_tester", :text => "This is a comment"}
57 assert_response :bad_request
59 assert_no_difference('Note.count') do
60 assert_no_difference('NoteComment.count') do
61 post :create, {:lat => -1.0, :lon => -200.0, :name => "new_tester", :text => "This is a comment"}
64 assert_response :bad_request
67 def test_note_comment_create_success
68 assert_difference('NoteComment.count') do
69 post :update, {:id => notes(:open_note_with_comment).id, :name => "new_tester2", :text => "This is an additional comment"}
71 assert_response :success
73 get :read, {:id => notes(:open_note_with_comment).id, :format => 'json'}
74 assert_response :success
75 js = ActiveSupport::JSON.decode(@response.body)
77 assert_equal "Feature", js["type"]
78 assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
79 assert_equal "open", js["properties"]["status"]
80 assert_equal 3, js["properties"]["comments"].count
81 assert_equal "commented", js["properties"]["comments"].last["action"]
82 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
83 assert_equal "new_tester2 (a)", js["properties"]["comments"].last["user"]
86 def test_note_comment_create_fail
87 assert_no_difference('NoteComment.count') do
88 post :update, {:name => "new_tester2", :text => "This is an additional comment"}
90 assert_response :bad_request
92 assert_no_difference('NoteComment.count') do
93 post :update, {:id => notes(:open_note_with_comment).id, :name => "new_tester2"}
95 assert_response :bad_request
97 assert_no_difference('NoteComment.count') do
98 post :update, {:id => 12345, :name => "new_tester2", :text => "This is an additional comment"}
100 assert_response :not_found
102 assert_no_difference('NoteComment.count') do
103 post :update, {:id => notes(:hidden_note_with_comment).id, :name => "new_tester2", :text => "This is an additional comment"}
105 assert_response :gone
108 def test_note_close_success
109 post :close, {:id => notes(:open_note_with_comment).id}
110 assert_response :success
112 get :read, {:id => notes(:open_note_with_comment).id, :format => 'json'}
113 assert_response :success
114 js = ActiveSupport::JSON.decode(@response.body)
116 assert_equal "Feature", js["type"]
117 assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
118 assert_equal "closed", js["properties"]["status"]
119 assert_equal 3, js["properties"]["comments"].count
120 assert_equal "closed", js["properties"]["comments"].last["action"]
121 assert_equal nil, js["properties"]["comments"].last["text"]
122 assert_equal "NoName (a)", js["properties"]["comments"].last["user"]
125 def test_note_close_fail
127 assert_response :bad_request
129 post :close, {:id => 12345}
130 assert_response :not_found
132 post :close, {:id => notes(:hidden_note_with_comment).id}
133 assert_response :gone
136 def test_note_read_success
137 # get :read, {:id => notes(:open_note).id}
138 # assert_response :success
139 # assert_equal "application/xml", @response.content_type
141 get :read, {:id => notes(:open_note).id, :format => "xml"}
142 assert_response :success
143 assert_equal "application/xml", @response.content_type
145 get :read, {:id => notes(:open_note).id, :format => "rss"}
146 assert_response :success
147 assert_equal "application/rss+xml", @response.content_type
149 get :read, {:id => notes(:open_note).id, :format => "json"}
150 assert_response :success
151 assert_equal "application/json", @response.content_type
153 get :read, {:id => notes(:open_note).id, :format => "gpx"}
154 assert_response :success
155 assert_equal "application/gpx+xml", @response.content_type
158 def test_note_read_hidden_comment
159 get :read, {:id => notes(:note_with_hidden_comment).id, :format => 'json'}
160 assert_response :success
161 js = ActiveSupport::JSON.decode(@response.body)
163 assert_equal notes(:note_with_hidden_comment).id, js["properties"]["id"]
164 assert_equal 2, js["properties"]["comments"].count
165 assert_equal "Valid comment for note 5", js["properties"]["comments"][0]["text"]
166 assert_equal "Another valid comment for note 5", js["properties"]["comments"][1]["text"]
169 def test_note_read_fail
171 assert_response :bad_request
173 get :read, {:id => 12345}
174 assert_response :not_found
176 get :read, {:id => notes(:hidden_note_with_comment).id}
177 assert_response :gone
180 def test_note_delete_success
181 delete :delete, {:id => notes(:open_note_with_comment).id}
182 assert_response :success
184 get :read, {:id => notes(:open_note_with_comment).id, :format => 'json'}
185 assert_response :gone
188 def test_note_delete_fail
190 assert_response :bad_request
192 delete :delete, {:id => 12345}
193 assert_response :not_found
195 delete :delete, {:id => notes(:hidden_note_with_comment).id}
196 assert_response :gone
199 def test_get_notes_success
200 # get :list, {:bbox => '1,1,1.2,1.2'}
201 # assert_response :success
202 # assert_equal "text/javascript", @response.content_type
204 get :list, {:bbox => '1,1,1.2,1.2', :format => 'rss'}
205 assert_response :success
206 assert_equal "application/rss+xml", @response.content_type
208 get :list, {:bbox => '1,1,1.2,1.2', :format => 'json'}
209 assert_response :success
210 assert_equal "application/json", @response.content_type
212 get :list, {:bbox => '1,1,1.2,1.2', :format => 'xml'}
213 assert_response :success
214 assert_equal "application/xml", @response.content_type
216 get :list, {:bbox => '1,1,1.2,1.2', :format => 'gpx'}
217 assert_response :success
218 assert_equal "application/gpx+xml", @response.content_type
221 def test_get_notes_large_area
222 # get :list, {:bbox => '-2.5,-2.5,2.5,2.5'}
223 # assert_response :success
225 # get :list, {:l => '-2.5', :b => '-2.5', :r => '2.5', :t => '2.5'}
226 # assert_response :success
228 get :list, {:bbox => '-10,-10,12,12'}
229 assert_response :bad_request
231 get :list, {:l => '-10', :b => '-10', :r => '12', :t => '12'}
232 assert_response :bad_request
235 def test_get_notes_closed
236 get :list, {:bbox => '1,1,1.7,1.7', :closed => '7', :format => 'json'}
237 assert_response :success
238 assert_equal "application/json", @response.content_type
239 js = ActiveSupport::JSON.decode(@response.body)
241 assert_equal "FeatureCollection", js["type"]
242 assert_equal 4, js["features"].count
244 get :list, {:bbox => '1,1,1.7,1.7', :closed => '0', :format => 'json'}
245 assert_response :success
246 assert_equal "application/json", @response.content_type
247 js = ActiveSupport::JSON.decode(@response.body)
249 assert_equal "FeatureCollection", js["type"]
250 assert_equal 4, js["features"].count
252 get :list, {:bbox => '1,1,1.7,1.7', :closed => '-1', :format => 'json'}
253 assert_response :success
254 assert_equal "application/json", @response.content_type
255 js = ActiveSupport::JSON.decode(@response.body)
257 assert_equal "FeatureCollection", js["type"]
258 assert_equal 6, js["features"].count
261 def test_get_notes_bad_params
262 get :list, {:bbox => '-2.5,-2.5,2.5'}
263 assert_response :bad_request
265 get :list, {:bbox => '-2.5,-2.5,2.5,2.5,2.5'}
266 assert_response :bad_request
268 get :list, {:b => '-2.5', :r => '2.5', :t => '2.5'}
269 assert_response :bad_request
271 get :list, {:l => '-2.5', :r => '2.5', :t => '2.5'}
272 assert_response :bad_request
274 get :list, {:l => '-2.5', :b => '-2.5', :t => '2.5'}
275 assert_response :bad_request
277 get :list, {:l => '-2.5', :b => '-2.5', :r => '2.5'}
278 assert_response :bad_request
281 def test_search_success
282 # get :search, {:q => 'note 1'}
283 # assert_response :success
284 # assert_equal "text/javascript", @response.content_type
286 get :search, {:q => 'note 1', :format => 'xml'}
287 assert_response :success
288 assert_equal "application/xml", @response.content_type
290 get :search, {:q => 'note 1', :format => 'json'}
291 assert_response :success
292 assert_equal "application/json", @response.content_type
294 get :search, {:q => 'note 1', :format => 'rss'}
295 assert_response :success
296 assert_equal "application/rss+xml", @response.content_type
298 get :search, {:q => 'note 1', :format => 'gpx'}
299 assert_response :success
300 assert_equal "application/gpx+xml", @response.content_type
303 def test_search_bad_params
305 assert_response :bad_request
310 # assert_response :success
311 # assert_equal "application/rss+xml", @response.content_type
313 # get :rss, {:bbox=>'1,1,1.2,1.2'}
314 # assert_response :success
315 # assert_equal "application/rss+xml", @response.content_type
319 get :rss, {:bbox=>'1,1,1.2'}
320 assert_response :bad_request
322 get :rss, {:bbox=>'1,1,1.2,1.2,1.2'}
323 assert_response :bad_request
326 def test_user_notes_success
327 get :mine, {:display_name=>'test'}
328 assert_response :success
330 get :mine, {:display_name=>'pulibc_test2'}
331 assert_response :success
333 get :mine, {:display_name=>'non-existent'}
334 assert_response :not_found