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 id, js["note"]["id"]
20 assert_equal "open", js["note"]["status"]
21 assert_equal "opened", js["note"]["comments"].last["event"]
22 assert_equal "This is a comment", js["note"]["comments"].last["body"]
23 assert_equal "new_tester (a)", js["note"]["comments"].last["author_name"]
26 def test_note_create_fail
27 assert_no_difference('Note.count') do
28 assert_no_difference('NoteComment.count') do
29 post :create, {:lon => -1.0, :name => "new_tester", :text => "This is a comment"}
32 assert_response :bad_request
34 assert_no_difference('Note.count') do
35 assert_no_difference('NoteComment.count') do
36 post :create, {:lat => -1.0, :name => "new_tester", :text => "This is a comment"}
39 assert_response :bad_request
41 assert_no_difference('Note.count') do
42 assert_no_difference('NoteComment.count') do
43 post :create, {:lat => -1.0, :lon => -1.0, :name => "new_tester"}
46 assert_response :bad_request
48 assert_no_difference('Note.count') do
49 assert_no_difference('NoteComment.count') do
50 post :create, {:lat => -100.0, :lon => -1.0, :name => "new_tester", :text => "This is a comment"}
53 assert_response :bad_request
55 assert_no_difference('Note.count') do
56 assert_no_difference('NoteComment.count') do
57 post :create, {:lat => -1.0, :lon => -200.0, :name => "new_tester", :text => "This is a comment"}
60 assert_response :bad_request
63 def test_note_comment_create_success
64 assert_difference('NoteComment.count') do
65 post :update, {:id => notes(:open_note_with_comment).id, :name => "new_tester2", :text => "This is an additional comment"}
67 assert_response :success
69 get :read, {:id => notes(:open_note_with_comment).id, :format => 'json'}
70 assert_response :success
71 js = ActiveSupport::JSON.decode(@response.body)
73 assert_equal notes(:open_note_with_comment).id, js["note"]["id"]
74 assert_equal "open", js["note"]["status"]
75 assert_equal "commented", js["note"]["comments"].last["event"]
76 assert_equal "This is an additional comment", js["note"]["comments"].last["body"]
77 assert_equal "new_tester2 (a)", js["note"]["comments"].last["author_name"]
80 def test_note_comment_create_fail
81 assert_no_difference('NoteComment.count') do
82 post :update, {:name => "new_tester2", :text => "This is an additional comment"}
84 assert_response :bad_request
86 assert_no_difference('NoteComment.count') do
87 post :update, {:id => notes(:open_note_with_comment).id, :name => "new_tester2"}
89 assert_response :bad_request
91 assert_no_difference('NoteComment.count') do
92 post :update, {:id => 12345, :name => "new_tester2", :text => "This is an additional comment"}
94 assert_response :not_found
96 assert_no_difference('NoteComment.count') do
97 post :update, {:id => notes(:hidden_note_with_comment).id, :name => "new_tester2", :text => "This is an additional comment"}
102 def test_note_close_success
103 post :close, {:id => notes(:open_note_with_comment).id}
104 assert_response :success
106 get :read, {:id => notes(:open_note_with_comment).id, :format => 'json'}
107 assert_response :success
108 js = ActiveSupport::JSON.decode(@response.body)
110 assert_equal notes(:open_note_with_comment).id, js["note"]["id"]
111 assert_equal "closed", js["note"]["status"]
112 assert_equal "closed", js["note"]["comments"].last["event"]
113 assert_equal "NoName (a)", js["note"]["comments"].last["author_name"]
116 def test_note_close_fail
118 assert_response :bad_request
120 post :close, {:id => 12345}
121 assert_response :not_found
123 post :close, {:id => notes(:hidden_note_with_comment).id}
124 assert_response :gone
127 def test_note_read_success
128 get :read, {:id => notes(:open_note).id}
129 assert_response :success
130 assert_equal "application/xml", @response.content_type
132 get :read, {:id => notes(:open_note).id, :format => "xml"}
133 assert_response :success
134 assert_equal "application/xml", @response.content_type
136 get :read, {:id => notes(:open_note).id, :format => "rss"}
137 assert_response :success
138 assert_equal "application/rss+xml", @response.content_type
140 get :read, {:id => notes(:open_note).id, :format => "json"}
141 assert_response :success
142 assert_equal "application/json", @response.content_type
144 get :read, {:id => notes(:open_note).id, :format => "gpx"}
145 assert_response :success
146 assert_equal "application/gpx+xml", @response.content_type
149 def test_note_read_hidden_comment
150 get :read, {:id => notes(:note_with_hidden_comment).id, :format => 'json'}
151 assert_response :success
152 js = ActiveSupport::JSON.decode(@response.body)
154 assert_equal notes(:note_with_hidden_comment).id, js["note"]["id"]
155 assert_equal 2, js["note"]["comments"].count
156 assert_equal "Valid comment for note 5", js["note"]["comments"][0]["body"]
157 assert_equal "Another valid comment for note 5", js["note"]["comments"][1]["body"]
160 def test_note_read_fail
162 assert_response :bad_request
164 get :read, {:id => 12345}
165 assert_response :not_found
167 get :read, {:id => notes(:hidden_note_with_comment).id}
168 assert_response :gone
171 def test_note_delete_success
172 delete :delete, {:id => notes(:open_note_with_comment).id}
173 assert_response :success
175 get :read, {:id => notes(:open_note_with_comment).id, :format => 'json'}
176 assert_response :gone
179 def test_note_delete_fail
181 assert_response :bad_request
183 delete :delete, {:id => 12345}
184 assert_response :not_found
186 delete :delete, {:id => notes(:hidden_note_with_comment).id}
187 assert_response :gone
190 def test_get_notes_success
191 get :list, {:bbox => '1,1,1.2,1.2'}
192 assert_response :success
193 assert_equal "text/javascript", @response.content_type
195 get :list, {:bbox => '1,1,1.2,1.2', :format => 'rss'}
196 assert_response :success
197 assert_equal "application/rss+xml", @response.content_type
199 get :list, {:bbox => '1,1,1.2,1.2', :format => 'json'}
200 assert_response :success
201 assert_equal "application/json", @response.content_type
203 get :list, {:bbox => '1,1,1.2,1.2', :format => 'xml'}
204 assert_response :success
205 assert_equal "application/xml", @response.content_type
207 get :list, {:bbox => '1,1,1.2,1.2', :format => 'gpx'}
208 assert_response :success
209 assert_equal "application/gpx+xml", @response.content_type
212 def test_get_notes_large_area
213 get :list, {:bbox => '-2.5,-2.5,2.5,2.5'}
214 assert_response :success
216 get :list, {:l => '-2.5', :b => '-2.5', :r => '2.5', :t => '2.5'}
217 assert_response :success
219 get :list, {:bbox => '-10,-10,12,12'}
220 assert_response :bad_request
222 get :list, {:l => '-10', :b => '-10', :r => '12', :t => '12'}
223 assert_response :bad_request
226 def test_get_notes_closed
227 get :list, {:bbox=>'1,1,1.7,1.7', :closed => '7', :format => 'json'}
228 assert_response :success
229 assert_equal "application/json", @response.content_type
230 js = ActiveSupport::JSON.decode(@response.body)
232 assert_equal 4, js.count
234 get :list, {:bbox=>'1,1,1.7,1.7', :closed => '0', :format => 'json'}
235 assert_response :success
236 assert_equal "application/json", @response.content_type
237 js = ActiveSupport::JSON.decode(@response.body)
239 assert_equal 4, js.count
241 get :list, {:bbox=>'1,1,1.7,1.7', :closed => '-1', :format => 'json'}
242 assert_response :success
243 assert_equal "application/json", @response.content_type
244 js = ActiveSupport::JSON.decode(@response.body)
246 assert_equal 6, js.count
249 def test_get_notes_bad_params
250 get :list, {:bbox => '-2.5,-2.5,2.5'}
251 assert_response :bad_request
253 get :list, {:bbox => '-2.5,-2.5,2.5,2.5,2.5'}
254 assert_response :bad_request
256 get :list, {:b => '-2.5', :r => '2.5', :t => '2.5'}
257 assert_response :bad_request
259 get :list, {:l => '-2.5', :r => '2.5', :t => '2.5'}
260 assert_response :bad_request
262 get :list, {:l => '-2.5', :b => '-2.5', :t => '2.5'}
263 assert_response :bad_request
265 get :list, {:l => '-2.5', :b => '-2.5', :r => '2.5'}
266 assert_response :bad_request
269 def test_search_success
270 get :search, {:q => 'note 1'}
271 assert_response :success
272 assert_equal "text/javascript", @response.content_type
274 get :search, {:q => 'note 1', :format => 'xml'}
275 assert_response :success
276 assert_equal "application/xml", @response.content_type
278 get :search, {:q => 'note 1', :format => 'json'}
279 assert_response :success
280 assert_equal "application/json", @response.content_type
282 get :search, {:q => 'note 1', :format => 'rss'}
283 assert_response :success
284 assert_equal "application/rss+xml", @response.content_type
286 get :search, {:q => 'note 1', :format => 'gpx'}
287 assert_response :success
288 assert_equal "application/gpx+xml", @response.content_type
291 def test_search_bad_params
293 assert_response :bad_request
298 assert_response :success
299 assert_equal "application/rss+xml", @response.content_type
301 get :rss, {:bbox=>'1,1,1.2,1.2'}
302 assert_response :success
303 assert_equal "application/rss+xml", @response.content_type
307 get :rss, {:bbox=>'1,1,1.2'}
308 assert_response :bad_request
310 get :rss, {:bbox=>'1,1,1.2,1.2,1.2'}
311 assert_response :bad_request
314 def test_user_notes_success
315 get :mine, {:display_name=>'test'}
316 assert_response :success
318 get :mine, {:display_name=>'pulibc_test2'}
319 assert_response :success
321 get :mine, {:display_name=>'non-existent'}
322 assert_response :not_found