1 # frozen_string_literal: true
5 class NotesControllerTest < ActionDispatch::IntegrationTest
8 # Stub nominatim response for note locations
9 stub_request(:get, %r{^https://nominatim\.openstreetmap\.org/reverse\?})
10 .to_return(:status => 404)
14 # test all routes which lead to this controller
17 { :path => "/user/username/notes", :method => :get },
18 { :controller => "notes", :action => "index", :display_name => "username" }
21 { :path => "/note/1", :method => :get },
22 { :controller => "notes", :action => "show", :id => "1" }
25 { :path => "/note/new", :method => :get },
26 { :controller => "notes", :action => "new" }
30 def test_index_success
31 first_user = create(:user)
32 second_user = create(:user)
33 moderator_user = create(:moderator_user)
35 create(:note) do |note|
36 create(:note_comment, :note => note, :author => first_user)
38 create(:note) do |note|
39 create(:note_comment, :note => note, :author => second_user)
41 create(:note, :status => "hidden") do |note|
42 create(:note_comment, :note => note, :author => second_user)
45 get user_notes_path(first_user)
46 assert_response :success
47 assert_select ".content-heading a[href='#{user_path first_user}']", :text => first_user.display_name
48 assert_select "table.note_list tbody tr", :count => 1
50 get user_notes_path(second_user)
51 assert_response :success
52 assert_select ".content-heading a[href='#{user_path second_user}']", :text => second_user.display_name
53 assert_select "table.note_list tbody tr", :count => 1
55 get user_notes_path("non-existent")
56 assert_response :not_found
58 session_for(moderator_user)
60 get user_notes_path(first_user)
61 assert_response :success
62 assert_select "table.note_list tbody tr", :count => 1
64 get user_notes_path(second_user)
65 assert_response :success
66 assert_select "table.note_list tbody tr", :count => 2
68 get user_notes_path("non-existent")
69 assert_response :not_found
72 def test_index_unsupported_format
75 get user_notes_path(user, :format => "xml")
77 assert_response :not_acceptable
83 create_list(:note, 50) do |note|
84 create(:note_comment, :note => note, :author => user)
87 get user_notes_path(user)
88 assert_response :success
89 assert_select "table.note_list tbody tr", :count => 10
91 get user_notes_path(user, :page => 2)
92 assert_response :success
93 assert_select "table.note_list tbody tr", :count => 10
96 def test_index_invalid_paged
99 %w[-1 0 fred].each do |page|
100 get user_notes_path(user, :page => page)
101 assert_response :bad_request
105 def test_index_invalid_status
108 get user_notes_path(user, :status => "invalid")
109 assert_response :bad_request
112 def test_index_user_not_found_language
113 I18n.with_locale "en" do
114 get user_notes_path("no_such_user"), :headers => { "Accept-Language" => "fr" }
116 assert_response :not_found
118 assert_dom "> @lang", "fr"
125 get user_notes_path(user)
126 assert_response :success
127 assert_select "h4", :html => "No notes"
131 open_note = create(:note_with_comments)
133 sidebar_browse_check :note_path, open_note.id, "notes/show"
136 def test_read_hidden_note
137 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
139 get note_path(hidden_note_with_comment)
140 assert_response :not_found
141 assert_template "browse/not_found"
142 assert_template :layout => "map"
144 get note_path(hidden_note_with_comment), :headers => { "Turbo-Frame" => "sidebar_content_frame" }
145 assert_response :not_found
146 assert_template "browse/not_found"
147 assert_template :layout => "xhr"
149 session_for(create(:moderator_user))
151 sidebar_browse_check :note_path, hidden_note_with_comment.id, "notes/show"
154 def test_read_note_hidden_comments
155 note_with_hidden_comment = create(:note_with_comments, :comments_count => 2) do |note|
156 create(:note_comment, :note => note, :visible => false)
159 sidebar_browse_check :note_path, note_with_hidden_comment.id, "notes/show"
160 assert_dom "article > @id", :text => /^c\d+$/, :count => 1
162 session_for(create(:moderator_user))
164 sidebar_browse_check :note_path, note_with_hidden_comment.id, "notes/show"
165 assert_dom "article > @id", :text => /^c\d+$/, :count => 2
168 def test_read_note_hidden_user_comment
169 hidden_user = create(:user, :deleted)
170 note_with_hidden_user_comment = create(:note_with_comments, :comments_count => 2) do |note|
171 create(:note_comment, :note => note, :author => hidden_user)
174 sidebar_browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
175 assert_dom "article > @id", :text => /^c\d+$/, :count => 1
177 session_for(create(:moderator_user))
179 sidebar_browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
180 assert_dom "article > @id", :text => /^c\d+$/, :count => 1
183 def test_read_note_hidden_opener
184 hidden_user = create(:user, :deleted)
185 note_with_hidden_opener = create(:note)
186 create(:note_comment, :author => hidden_user, :note => note_with_hidden_opener)
188 sidebar_browse_check :note_path, note_with_hidden_opener.id, "notes/show"
189 assert_dom "article > @id", :text => /^c\d+$/, :count => 0
192 def test_read_note_suspended_opener_and_comment
194 create(:note_comment, :note => note, :author => create(:user, :suspended))
195 create(:note_comment, :note => note, :event => "commented")
197 sidebar_browse_check :note_path, note.id, "notes/show"
198 assert_dom "article > @id", :text => /^c\d+$/, :count => 1
201 def test_read_closed_note
203 closed_note = create(:note_with_comments, :closed, :closed_by => user, :comments_count => 2)
205 sidebar_browse_check :note_path, closed_note.id, "notes/show"
206 assert_dom "article > @id", :text => /^c\d+$/, :count => 2
207 assert_dom "div.details", /Resolved by #{user.display_name}/
213 sidebar_browse_check :note_path, closed_note.id, "notes/show"
214 assert_dom "article > @id", :text => /^c\d+$/, :count => 1
215 assert_dom "div.details", /Resolved by deleted/
218 def test_new_note_anonymous
220 assert_response :success
221 assert_template "notes/new"
222 assert_select "#sidebar_content a[href='#{login_path(:referer => new_note_path)}']", :count => 1
226 session_for(create(:user))
229 assert_response :success
230 assert_template "notes/new"
231 assert_select "#sidebar_content a[href='#{login_path(:referer => new_note_path)}']", :count => 0
234 def test_index_filter_by_status
236 other_user = create(:user)
238 open_note = create(:note, :status => "open")
239 create(:note_comment, :note => open_note, :author => user)
241 closed_note = create(:note, :status => "closed")
242 create(:note_comment, :note => closed_note, :author => user)
244 hidden_note = create(:note, :status => "hidden")
245 create(:note_comment, :note => hidden_note, :author => user)
247 commented_note = create(:note, :status => "open")
248 create(:note_comment, :note => commented_note, :author => other_user)
249 create(:note_comment, :note => commented_note, :author => user)
251 get user_notes_path(user, :status => "all")
252 assert_response :success
253 assert_select "table.note_list tbody tr", :count => 3
255 get user_notes_path(user, :status => "open")
256 assert_response :success
257 assert_select "table.note_list tbody tr", :count => 2
259 get user_notes_path(user, :status => "closed")
260 assert_response :success
261 assert_select "table.note_list tbody tr", :count => 1
263 get user_notes_path(user)
264 assert_response :success
265 assert_select "table.note_list tbody tr", :count => 3