]> git.openstreetmap.org Git - rails.git/blob - test/controllers/notes_controller_test.rb
Add test for set_locale fix in notes controller
[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     get user_notes_path("no_such_user"), :headers => { "Accept-Language" => "fr" }
97
98     assert_response :not_found
99     assert_dom "html" do
100       assert_dom "> @lang", "fr"
101     end
102   end
103
104   def test_empty_page
105     user = create(:user)
106     get user_notes_path(user)
107     assert_response :success
108     assert_select "h4", :html => "No notes"
109   end
110
111   def test_read_note
112     open_note = create(:note_with_comments)
113
114     sidebar_browse_check :note_path, open_note.id, "notes/show"
115   end
116
117   def test_read_hidden_note
118     hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
119
120     get note_path(hidden_note_with_comment)
121     assert_response :not_found
122     assert_template "browse/not_found"
123     assert_template :layout => "map"
124
125     get note_path(hidden_note_with_comment), :xhr => true
126     assert_response :not_found
127     assert_template "browse/not_found"
128     assert_template :layout => "xhr"
129
130     session_for(create(:moderator_user))
131
132     sidebar_browse_check :note_path, hidden_note_with_comment.id, "notes/show"
133   end
134
135   def test_read_note_hidden_comments
136     note_with_hidden_comment = create(:note_with_comments, :comments_count => 2) do |note|
137       create(:note_comment, :note => note, :visible => false)
138     end
139
140     sidebar_browse_check :note_path, note_with_hidden_comment.id, "notes/show"
141     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 1
142
143     session_for(create(:moderator_user))
144
145     sidebar_browse_check :note_path, note_with_hidden_comment.id, "notes/show"
146     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 2
147   end
148
149   def test_read_note_hidden_user_comment
150     hidden_user = create(:user, :deleted)
151     note_with_hidden_user_comment = create(:note_with_comments, :comments_count => 2) do |note|
152       create(:note_comment, :note => note, :author => hidden_user)
153     end
154
155     sidebar_browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
156     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 1
157
158     session_for(create(:moderator_user))
159
160     sidebar_browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
161     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 1
162   end
163
164   def test_read_note_hidden_opener
165     hidden_user = create(:user, :deleted)
166     note_with_hidden_opener = create(:note)
167     create(:note_comment, :author => hidden_user, :note => note_with_hidden_opener)
168
169     sidebar_browse_check :note_path, note_with_hidden_opener.id, "notes/show"
170     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 0
171   end
172
173   def test_read_note_suspended_opener_and_comment
174     note = create(:note)
175     create(:note_comment, :note => note, :author => create(:user, :suspended))
176     create(:note_comment, :note => note, :event => "commented")
177
178     sidebar_browse_check :note_path, note.id, "notes/show"
179     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 1
180   end
181
182   def test_read_closed_note
183     user = create(:user)
184     closed_note = create(:note_with_comments, :closed, :closed_by => user, :comments_count => 2)
185
186     sidebar_browse_check :note_path, closed_note.id, "notes/show"
187     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 2
188     assert_dom "div.details", /Resolved by #{user.display_name}/
189
190     user.soft_destroy!
191
192     reset!
193
194     sidebar_browse_check :note_path, closed_note.id, "notes/show"
195     assert_dom "article:match('id', ?)", /^c\d+$/, :count => 1
196     assert_dom "div.details", /Resolved by deleted/
197   end
198
199   def test_new_note_anonymous
200     get new_note_path
201     assert_response :success
202     assert_template "notes/new"
203     assert_select "#sidebar_content a[href='#{login_path(:referer => new_note_path)}']", :count => 1
204   end
205
206   def test_new_note
207     session_for(create(:user))
208
209     get new_note_path
210     assert_response :success
211     assert_template "notes/new"
212     assert_select "#sidebar_content a[href='#{login_path(:referer => new_note_path)}']", :count => 0
213   end
214
215   def test_index_filter_by_status
216     user = create(:user)
217     other_user = create(:user)
218
219     open_note = create(:note, :status => "open")
220     create(:note_comment, :note => open_note, :author => user)
221
222     closed_note = create(:note, :status => "closed")
223     create(:note_comment, :note => closed_note, :author => user)
224
225     hidden_note = create(:note, :status => "hidden")
226     create(:note_comment, :note => hidden_note, :author => user)
227
228     commented_note = create(:note, :status => "open")
229     create(:note_comment, :note => commented_note, :author => other_user)
230     create(:note_comment, :note => commented_note, :author => user)
231
232     get user_notes_path(user, :status => "all")
233     assert_response :success
234     assert_select "table.note_list tbody tr", :count => 3
235
236     get user_notes_path(user, :status => "open")
237     assert_response :success
238     assert_select "table.note_list tbody tr", :count => 2
239
240     get user_notes_path(user, :status => "closed")
241     assert_response :success
242     assert_select "table.note_list tbody tr", :count => 1
243
244     get user_notes_path(user)
245     assert_response :success
246     assert_select "table.note_list tbody tr", :count => 3
247   end
248 end