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 get user_notes_path(first_user)
44 assert_response :success
45 assert_select ".content-heading a[href='#{user_path first_user}']", :text => first_user.display_name
46 assert_select "table.note_list tbody tr", :count => 1
48 get user_notes_path(second_user)
49 assert_response :success
50 assert_select ".content-heading a[href='#{user_path second_user}']", :text => second_user.display_name
51 assert_select "table.note_list tbody tr", :count => 1
53 get user_notes_path("non-existent")
54 assert_response :not_found
56 session_for(moderator_user)
58 get user_notes_path(first_user)
59 assert_response :success
60 assert_select "table.note_list tbody tr", :count => 1
62 get user_notes_path(second_user)
63 assert_response :success
64 assert_select "table.note_list tbody tr", :count => 2
66 get user_notes_path("non-existent")
67 assert_response :not_found
73 create_list(:note, 50) do |note|
74 create(:note_comment, :note => note, :author => user)
77 get user_notes_path(user)
78 assert_response :success
79 assert_select "table.note_list tbody tr", :count => 10
81 get user_notes_path(user, :page => 2)
82 assert_response :success
83 assert_select "table.note_list tbody tr", :count => 10
86 def test_index_invalid_paged
89 %w[-1 0 fred].each do |page|
90 get user_notes_path(user, :page => page)
91 assert_redirected_to :controller => :errors, :action => :bad_request
97 get user_notes_path(user)
98 assert_response :success
99 assert_select "h4", :html => "No notes"
103 open_note = create(:note_with_comments)
105 sidebar_browse_check :note_path, open_note.id, "notes/show"
108 def test_read_hidden_note
109 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
111 get note_path(hidden_note_with_comment)
112 assert_response :not_found
113 assert_template "browse/not_found"
114 assert_template :layout => "map"
116 get note_path(hidden_note_with_comment), :xhr => true
117 assert_response :not_found
118 assert_template "browse/not_found"
119 assert_template :layout => "xhr"
121 session_for(create(:moderator_user))
123 sidebar_browse_check :note_path, hidden_note_with_comment.id, "notes/show"
126 def test_read_note_hidden_comments
127 note_with_hidden_comment = create(:note_with_comments, :comments_count => 2) do |note|
128 create(:note_comment, :note => note, :visible => false)
131 sidebar_browse_check :note_path, note_with_hidden_comment.id, "notes/show"
132 assert_select "div.note-comments ul li", :count => 1
134 session_for(create(:moderator_user))
136 sidebar_browse_check :note_path, note_with_hidden_comment.id, "notes/show"
137 assert_select "div.note-comments ul li", :count => 2
140 def test_read_note_hidden_user_comment
141 hidden_user = create(:user, :deleted)
142 note_with_hidden_user_comment = create(:note_with_comments, :comments_count => 2) do |note|
143 create(:note_comment, :note => note, :author => hidden_user)
146 sidebar_browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
147 assert_select "div.note-comments ul li", :count => 1
149 session_for(create(:moderator_user))
151 sidebar_browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
152 assert_select "div.note-comments ul li", :count => 1
155 def test_read_note_hidden_opener
156 hidden_user = create(:user, :deleted)
157 note_with_hidden_opener = create(:note)
158 create(:note_comment, :author => hidden_user, :note => note_with_hidden_opener)
160 sidebar_browse_check :note_path, note_with_hidden_opener.id, "notes/show"
161 assert_select "div.note-comments ul li", :count => 0
164 def test_read_note_suspended_opener_and_comment
166 create(:note_comment, :note => note, :author => create(:user, :suspended))
167 create(:note_comment, :note => note)
169 sidebar_browse_check :note_path, note.id, "notes/show"
170 assert_select "div.note-comments ul li", :count => 1
173 def test_read_closed_note
175 closed_note = create(:note_with_comments, :closed, :closed_by => user, :comments_count => 2)
177 sidebar_browse_check :note_path, closed_note.id, "notes/show"
178 assert_select "div.note-comments ul li", :count => 2
179 assert_select "div.details", /Resolved by #{user.display_name}/
185 sidebar_browse_check :note_path, closed_note.id, "notes/show"
186 assert_select "div.note-comments ul li", :count => 1
187 assert_select "div.details", /Resolved by deleted/
190 def test_new_note_anonymous
192 assert_response :success
193 assert_template "notes/new"
194 assert_select "#sidebar_content a[href='#{login_path(:referer => new_note_path)}']", :count => 1
198 session_for(create(:user))
201 assert_response :success
202 assert_template "notes/new"
203 assert_select "#sidebar_content a[href='#{login_path(:referer => new_note_path)}']", :count => 0
206 def test_index_filter_by_status
208 other_user = create(:user)
210 open_note = create(:note, :status => "open")
211 create(:note_comment, :note => open_note, :author => user)
213 closed_note = create(:note, :status => "closed")
214 create(:note_comment, :note => closed_note, :author => user)
216 hidden_note = create(:note, :status => "hidden")
217 create(:note_comment, :note => hidden_note, :author => user)
219 commented_note = create(:note, :status => "open")
220 create(:note_comment, :note => commented_note, :author => other_user)
221 create(:note_comment, :note => commented_note, :author => user)
223 get user_notes_path(user, :status => "all")
224 assert_response :success
225 assert_select "table.note_list tbody tr", :count => 3
227 get user_notes_path(user, :status => "open")
228 assert_response :success
229 assert_select "table.note_list tbody tr", :count => 2
231 get user_notes_path(user, :status => "closed")
232 assert_response :success
233 assert_select "table.note_list tbody tr", :count => 1
235 get user_notes_path(user)
236 assert_response :success
237 assert_select "table.note_list tbody tr", :count => 3