]> git.openstreetmap.org Git - rails.git/blob - test/controllers/notes_controller_test.rb
Merge remote-tracking branch 'upstream/pull/7459'
[rails.git] / test / controllers / notes_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class NotesControllerTest < ActionDispatch::IntegrationTest
6   def setup
7     super
8     # Stub nominatim response for note locations
9     stub_request(:get, %r{^https://nominatim\.openstreetmap\.org/reverse\?})
10       .to_return(:status => 404)
11   end
12
13   ##
14   # test all routes which lead to this controller
15   def test_routes
16     assert_routing(
17       { :path => "/user/username/notes", :method => :get },
18       { :controller => "notes", :action => "index", :display_name => "username" }
19     )
20     assert_routing(
21       { :path => "/note/1", :method => :get },
22       { :controller => "notes", :action => "show", :id => "1" }
23     )
24     assert_routing(
25       { :path => "/note/new", :method => :get },
26       { :controller => "notes", :action => "new" }
27     )
28   end
29
30   def test_index_success
31     first_user = create(:user)
32     second_user = create(:user)
33     moderator_user = create(:moderator_user)
34
35     create(:note) do |note|
36       create(:note_comment, :note => note, :author => first_user)
37     end
38     create(:note) do |note|
39       create(:note_comment, :note => note, :author => second_user)
40     end
41     create(:note, :status => "hidden") do |note|
42       create(:note_comment, :note => note, :author => second_user)
43     end
44
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
49
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
54
55     get user_notes_path("non-existent")
56     assert_response :not_found
57
58     session_for(moderator_user)
59
60     get user_notes_path(first_user)
61     assert_response :success
62     assert_select "table.note_list tbody tr", :count => 1
63
64     get user_notes_path(second_user)
65     assert_response :success
66     assert_select "table.note_list tbody tr", :count => 2
67
68     get user_notes_path("non-existent")
69     assert_response :not_found
70   end
71
72   def test_index_paged
73     user = create(:user)
74
75     create_list(:note, 50) do |note|
76       create(:note_comment, :note => note, :author => user)
77     end
78
79     get user_notes_path(user)
80     assert_response :success
81     assert_select "table.note_list tbody tr", :count => 10
82
83     get user_notes_path(user, :page => 2)
84     assert_response :success
85     assert_select "table.note_list tbody tr", :count => 10
86   end
87
88   def test_index_invalid_paged
89     user = create(:user)
90
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
94     end
95   end
96
97   def test_index_invalid_status
98     user = create(:user)
99
100     get user_notes_path(user, :status => "invalid")
101     assert_redirected_to :controller => :errors, :action => :bad_request
102   end
103
104   def test_index_user_not_found_language
105     I18n.with_locale "en" do
106       get user_notes_path("no_such_user"), :headers => { "Accept-Language" => "fr" }
107
108       assert_response :not_found
109       assert_dom "html" do
110         assert_dom "> @lang", "fr"
111       end
112     end
113   end
114
115   def test_empty_page
116     user = create(:user)
117     get user_notes_path(user)
118     assert_response :success
119     assert_select "h4", :html => "No notes"
120   end
121
122   def test_read_note
123     open_note = create(:note_with_comments)
124
125     sidebar_browse_check :note_path, open_note.id, "notes/show"
126   end
127
128   def test_read_hidden_note
129     hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
130
131     get note_path(hidden_note_with_comment)
132     assert_response :not_found
133     assert_template "browse/not_found"
134     assert_template :layout => "map"
135
136     get note_path(hidden_note_with_comment), :headers => { "Turbo-Frame" => "sidebar_content_frame" }
137     assert_response :not_found
138     assert_template "browse/not_found"
139     assert_template :layout => "xhr"
140
141     session_for(create(:moderator_user))
142
143     sidebar_browse_check :note_path, hidden_note_with_comment.id, "notes/show"
144   end
145
146   def test_read_note_hidden_comments
147     note_with_hidden_comment = create(:note_with_comments, :comments_count => 2) do |note|
148       create(:note_comment, :note => note, :visible => false)
149     end
150
151     sidebar_browse_check :note_path, note_with_hidden_comment.id, "notes/show"
152     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 1
153
154     session_for(create(:moderator_user))
155
156     sidebar_browse_check :note_path, note_with_hidden_comment.id, "notes/show"
157     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 2
158   end
159
160   def test_read_note_hidden_user_comment
161     hidden_user = create(:user, :deleted)
162     note_with_hidden_user_comment = create(:note_with_comments, :comments_count => 2) do |note|
163       create(:note_comment, :note => note, :author => hidden_user)
164     end
165
166     sidebar_browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
167     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 1
168
169     session_for(create(:moderator_user))
170
171     sidebar_browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
172     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 1
173   end
174
175   def test_read_note_hidden_opener
176     hidden_user = create(:user, :deleted)
177     note_with_hidden_opener = create(:note)
178     create(:note_comment, :author => hidden_user, :note => note_with_hidden_opener)
179
180     sidebar_browse_check :note_path, note_with_hidden_opener.id, "notes/show"
181     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 0
182   end
183
184   def test_read_note_suspended_opener_and_comment
185     note = create(:note)
186     create(:note_comment, :note => note, :author => create(:user, :suspended))
187     create(:note_comment, :note => note, :event => "commented")
188
189     sidebar_browse_check :note_path, note.id, "notes/show"
190     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 1
191   end
192
193   def test_read_closed_note
194     user = create(:user)
195     closed_note = create(:note_with_comments, :closed, :closed_by => user, :comments_count => 2)
196
197     sidebar_browse_check :note_path, closed_note.id, "notes/show"
198     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 2
199     assert_dom "div.details", /Resolved by #{user.display_name}/
200
201     user.soft_destroy!
202
203     reset!
204
205     sidebar_browse_check :note_path, closed_note.id, "notes/show"
206     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 1
207     assert_dom "div.details", /Resolved by deleted/
208   end
209
210   def test_new_note_anonymous
211     get new_note_path
212     assert_response :success
213     assert_template "notes/new"
214     assert_select "#sidebar_content a[href='#{login_path(:referer => new_note_path)}']", :count => 1
215   end
216
217   def test_new_note
218     session_for(create(:user))
219
220     get new_note_path
221     assert_response :success
222     assert_template "notes/new"
223     assert_select "#sidebar_content a[href='#{login_path(:referer => new_note_path)}']", :count => 0
224   end
225
226   def test_index_filter_by_status
227     user = create(:user)
228     other_user = create(:user)
229
230     open_note = create(:note, :status => "open")
231     create(:note_comment, :note => open_note, :author => user)
232
233     closed_note = create(:note, :status => "closed")
234     create(:note_comment, :note => closed_note, :author => user)
235
236     hidden_note = create(:note, :status => "hidden")
237     create(:note_comment, :note => hidden_note, :author => user)
238
239     commented_note = create(:note, :status => "open")
240     create(:note_comment, :note => commented_note, :author => other_user)
241     create(:note_comment, :note => commented_note, :author => user)
242
243     get user_notes_path(user, :status => "all")
244     assert_response :success
245     assert_select "table.note_list tbody tr", :count => 3
246
247     get user_notes_path(user, :status => "open")
248     assert_response :success
249     assert_select "table.note_list tbody tr", :count => 2
250
251     get user_notes_path(user, :status => "closed")
252     assert_response :success
253     assert_select "table.note_list tbody tr", :count => 1
254
255     get user_notes_path(user)
256     assert_response :success
257     assert_select "table.note_list tbody tr", :count => 3
258   end
259 end