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