3 class NotesControllerTest < ActionDispatch::IntegrationTest
6 # Stub nominatim response for note locations
7 stub_request(:get, %r{^https://nominatim\.openstreetmap\.org/reverse\?})
8 .to_return(:status => 404)
12 # test all routes which lead to this controller
15 { :path => "/user/username/notes", :method => :get },
16 { :controller => "notes", :action => "index", :display_name => "username" }
19 { :path => "/note/1", :method => :get },
20 { :controller => "notes", :action => "show", :id => "1" }
23 { :path => "/note/new", :method => :get },
24 { :controller => "notes", :action => "new" }
28 def test_index_success
29 first_user = create(:user)
30 second_user = create(:user)
31 moderator_user = create(:moderator_user)
33 create(:note) do |note|
34 create(:note_comment, :note => note, :author => first_user)
36 create(:note) do |note|
37 create(:note_comment, :note => note, :author => second_user)
39 create(:note, :status => "hidden") do |note|
40 create(:note_comment, :note => note, :author => second_user)
43 # Note that the table rows include a header row
44 get user_notes_path(:display_name => first_user.display_name)
45 assert_response :success
46 assert_select "table.note_list tr", :count => 2
48 get user_notes_path(:display_name => second_user.display_name)
49 assert_response :success
50 assert_select "table.note_list tr", :count => 2
52 get user_notes_path(:display_name => "non-existent")
53 assert_response :not_found
55 session_for(moderator_user)
57 get user_notes_path(:display_name => first_user.display_name)
58 assert_response :success
59 assert_select "table.note_list tr", :count => 2
61 get user_notes_path(:display_name => second_user.display_name)
62 assert_response :success
63 assert_select "table.note_list tr", :count => 3
65 get user_notes_path(:display_name => "non-existent")
66 assert_response :not_found
72 create_list(:note, 50) do |note|
73 create(:note_comment, :note => note, :author => user)
76 get user_notes_path(:display_name => user.display_name)
77 assert_response :success
78 assert_select "table.note_list tr", :count => 11
80 get user_notes_path(:display_name => user.display_name, :page => 2)
81 assert_response :success
82 assert_select "table.note_list tr", :count => 11
87 get user_notes_path(:display_name => user.display_name)
88 assert_response :success
89 assert_select "h4", :html => "No notes"
93 open_note = create(:note_with_comments)
95 browse_check :note_path, open_note.id, "notes/show"
98 def test_read_hidden_note
99 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
101 get note_path(:id => hidden_note_with_comment)
102 assert_response :not_found
103 assert_template "browse/not_found"
104 assert_template :layout => "map"
106 get note_path(:id => hidden_note_with_comment), :xhr => true
107 assert_response :not_found
108 assert_template "browse/not_found"
109 assert_template :layout => "xhr"
111 session_for(create(:moderator_user))
113 browse_check :note_path, hidden_note_with_comment.id, "notes/show"
116 def test_read_note_hidden_comments
117 note_with_hidden_comment = create(:note_with_comments, :comments_count => 2) do |note|
118 create(:note_comment, :note => note, :visible => false)
121 browse_check :note_path, note_with_hidden_comment.id, "notes/show"
122 assert_select "div.note-comments ul li", :count => 1
124 session_for(create(:moderator_user))
126 browse_check :note_path, note_with_hidden_comment.id, "notes/show"
127 assert_select "div.note-comments ul li", :count => 2
130 def test_read_note_hidden_user_comment
131 hidden_user = create(:user, :deleted)
132 note_with_hidden_user_comment = create(:note_with_comments, :comments_count => 2) do |note|
133 create(:note_comment, :note => note, :author => hidden_user)
136 browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
137 assert_select "div.note-comments ul li", :count => 1
139 session_for(create(:moderator_user))
141 browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
142 assert_select "div.note-comments ul li", :count => 1
145 def test_read_closed_note
147 closed_note = create(:note_with_comments, :status => "closed", :closed_at => Time.now.utc, :comments_count => 2) do |note|
148 create(:note_comment, :event => "closed", :note => note, :author => user)
151 browse_check :note_path, closed_note.id, "notes/show"
152 assert_select "div.note-comments ul li", :count => 2
153 assert_select "div.details", /Resolved by #{user.display_name}/
159 browse_check :note_path, closed_note.id, "notes/show"
160 assert_select "div.note-comments ul li", :count => 1
161 assert_select "div.details", /Resolved by deleted/
166 assert_response :success
167 assert_template "notes/new"
172 # This is a convenience method for most of the above checks
173 # First we check that when we don't have an id, it will correctly return a 404
174 # then we check that we get the correct 404 when a non-existant id is passed
175 # then we check that it will get a successful response, when we do pass an id
176 def browse_check(path, id, template)
177 path_method = method(path)
179 assert_raise ActionController::UrlGenerationError do
183 # assert_raise ActionController::UrlGenerationError do
184 # get path_method.call(:id => -10) # we won't have an id that's negative
187 get path_method.call(:id => 0)
188 assert_response :not_found
189 assert_template "browse/not_found"
190 assert_template :layout => "map"
192 get path_method.call(:id => 0), :xhr => true
193 assert_response :not_found
194 assert_template "browse/not_found"
195 assert_template :layout => "xhr"
197 get path_method.call(:id => id)
198 assert_response :success
199 assert_template template
200 assert_template :layout => "map"
202 get path_method.call(:id => id), :xhr => true
203 assert_response :success
204 assert_template template
205 assert_template :layout => "xhr"