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
75 create_list(:note, 50) do |note|
76 create(:note_comment, :note => note, :author => user)
79 get user_notes_path(user)
80 assert_response :success
81 assert_select "table.note_list tbody tr", :count => 10
83 get user_notes_path(user, :page => 2)
84 assert_response :success
85 assert_select "table.note_list tbody tr", :count => 10
88 def test_index_invalid_paged
91 %w[-1 0 fred].each do |page|
92 get user_notes_path(user, :page => page)
93 assert_redirected_to :controller => :errors, :action => :bad_request
97 def test_index_user_not_found_language
98 I18n.with_locale "en" do
99 get user_notes_path("no_such_user"), :headers => { "Accept-Language" => "fr" }
101 assert_response :not_found
103 assert_dom "> @lang", "fr"
110 get user_notes_path(user)
111 assert_response :success
112 assert_select "h4", :html => "No notes"
116 open_note = create(:note_with_comments)
118 sidebar_browse_check :note_path, open_note.id, "notes/show"
121 def test_read_hidden_note
122 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
124 get note_path(hidden_note_with_comment)
125 assert_response :not_found
126 assert_template "browse/not_found"
127 assert_template :layout => "map"
129 get note_path(hidden_note_with_comment), :xhr => true
130 assert_response :not_found
131 assert_template "browse/not_found"
132 assert_template :layout => "xhr"
134 session_for(create(:moderator_user))
136 sidebar_browse_check :note_path, hidden_note_with_comment.id, "notes/show"
139 def test_read_note_hidden_comments
140 note_with_hidden_comment = create(:note_with_comments, :comments_count => 2) do |note|
141 create(:note_comment, :note => note, :visible => false)
144 sidebar_browse_check :note_path, note_with_hidden_comment.id, "notes/show"
145 assert_dom "article:match('id', ?)", /^c\d+$/, :count => 1
147 session_for(create(:moderator_user))
149 sidebar_browse_check :note_path, note_with_hidden_comment.id, "notes/show"
150 assert_dom "article:match('id', ?)", /^c\d+$/, :count => 2
153 def test_read_note_hidden_user_comment
154 hidden_user = create(:user, :deleted)
155 note_with_hidden_user_comment = create(:note_with_comments, :comments_count => 2) do |note|
156 create(:note_comment, :note => note, :author => hidden_user)
159 sidebar_browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
160 assert_dom "article:match('id', ?)", /^c\d+$/, :count => 1
162 session_for(create(:moderator_user))
164 sidebar_browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
165 assert_dom "article:match('id', ?)", /^c\d+$/, :count => 1
168 def test_read_note_hidden_opener
169 hidden_user = create(:user, :deleted)
170 note_with_hidden_opener = create(:note)
171 create(:note_comment, :author => hidden_user, :note => note_with_hidden_opener)
173 sidebar_browse_check :note_path, note_with_hidden_opener.id, "notes/show"
174 assert_dom "article:match('id', ?)", /^c\d+$/, :count => 0
177 def test_read_note_suspended_opener_and_comment
179 create(:note_comment, :note => note, :author => create(:user, :suspended))
180 create(:note_comment, :note => note, :event => "commented")
182 sidebar_browse_check :note_path, note.id, "notes/show"
183 assert_dom "article:match('id', ?)", /^c\d+$/, :count => 1
186 def test_read_closed_note
188 closed_note = create(:note_with_comments, :closed, :closed_by => user, :comments_count => 2)
190 sidebar_browse_check :note_path, closed_note.id, "notes/show"
191 assert_dom "article:match('id', ?)", /^c\d+$/, :count => 2
192 assert_dom "div.details", /Resolved by #{user.display_name}/
198 sidebar_browse_check :note_path, closed_note.id, "notes/show"
199 assert_dom "article:match('id', ?)", /^c\d+$/, :count => 1
200 assert_dom "div.details", /Resolved by deleted/
203 def test_new_note_anonymous
205 assert_response :success
206 assert_template "notes/new"
207 assert_select "#sidebar_content a[href='#{login_path(:referer => new_note_path)}']", :count => 1
211 session_for(create(:user))
214 assert_response :success
215 assert_template "notes/new"
216 assert_select "#sidebar_content a[href='#{login_path(:referer => new_note_path)}']", :count => 0
219 def test_index_filter_by_status
221 other_user = create(:user)
223 open_note = create(:note, :status => "open")
224 create(:note_comment, :note => open_note, :author => user)
226 closed_note = create(:note, :status => "closed")
227 create(:note_comment, :note => closed_note, :author => user)
229 hidden_note = create(:note, :status => "hidden")
230 create(:note_comment, :note => hidden_note, :author => user)
232 commented_note = create(:note, :status => "open")
233 create(:note_comment, :note => commented_note, :author => other_user)
234 create(:note_comment, :note => commented_note, :author => user)
236 get user_notes_path(user, :status => "all")
237 assert_response :success
238 assert_select "table.note_list tbody tr", :count => 3
240 get user_notes_path(user, :status => "open")
241 assert_response :success
242 assert_select "table.note_list tbody tr", :count => 2
244 get user_notes_path(user, :status => "closed")
245 assert_response :success
246 assert_select "table.note_list tbody tr", :count => 1
248 get user_notes_path(user)
249 assert_response :success
250 assert_select "table.note_list tbody tr", :count => 3