]> git.openstreetmap.org Git - rails.git/blob - test/controllers/notes_controller_test.rb
Add validation for before/after parameters to pagination concern
[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_empty_page
96     user = create(:user)
97     get user_notes_path(user)
98     assert_response :success
99     assert_select "h4", :html => "No notes"
100   end
101
102   def test_read_note
103     open_note = create(:note_with_comments)
104
105     browse_check :note_path, open_note.id, "notes/show"
106   end
107
108   def test_read_hidden_note
109     hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
110
111     get note_path(hidden_note_with_comment)
112     assert_response :not_found
113     assert_template "browse/not_found"
114     assert_template :layout => "map"
115
116     get note_path(hidden_note_with_comment), :xhr => true
117     assert_response :not_found
118     assert_template "browse/not_found"
119     assert_template :layout => "xhr"
120
121     session_for(create(:moderator_user))
122
123     browse_check :note_path, hidden_note_with_comment.id, "notes/show"
124   end
125
126   def test_read_note_hidden_comments
127     note_with_hidden_comment = create(:note_with_comments, :comments_count => 2) do |note|
128       create(:note_comment, :note => note, :visible => false)
129     end
130
131     browse_check :note_path, note_with_hidden_comment.id, "notes/show"
132     assert_select "div.note-comments ul li", :count => 1
133
134     session_for(create(:moderator_user))
135
136     browse_check :note_path, note_with_hidden_comment.id, "notes/show"
137     assert_select "div.note-comments ul li", :count => 2
138   end
139
140   def test_read_note_hidden_user_comment
141     hidden_user = create(:user, :deleted)
142     note_with_hidden_user_comment = create(:note_with_comments, :comments_count => 2) do |note|
143       create(:note_comment, :note => note, :author => hidden_user)
144     end
145
146     browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
147     assert_select "div.note-comments ul li", :count => 1
148
149     session_for(create(:moderator_user))
150
151     browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
152     assert_select "div.note-comments ul li", :count => 1
153   end
154
155   def test_read_closed_note
156     user = create(:user)
157     closed_note = create(:note_with_comments, :closed, :closed_by => user, :comments_count => 2)
158
159     browse_check :note_path, closed_note.id, "notes/show"
160     assert_select "div.note-comments ul li", :count => 2
161     assert_select "div.details", /Resolved by #{user.display_name}/
162
163     user.soft_destroy!
164
165     reset!
166
167     browse_check :note_path, closed_note.id, "notes/show"
168     assert_select "div.note-comments ul li", :count => 1
169     assert_select "div.details", /Resolved by deleted/
170   end
171
172   def test_new_note
173     get new_note_path
174     assert_response :success
175     assert_template "notes/new"
176   end
177
178   private
179
180   # This is a convenience method for most of the above checks
181   # First we check that when we don't have an id, it will correctly return a 404
182   # then we check that we get the correct 404 when a non-existant id is passed
183   # then we check that it will get a successful response, when we do pass an id
184   def browse_check(path, id, template)
185     path_method = method(path)
186
187     assert_raise ActionController::UrlGenerationError do
188       get path_method.call
189     end
190
191     # assert_raise ActionController::UrlGenerationError do
192     #   get path_method.call(:id => -10) # we won't have an id that's negative
193     # end
194
195     get path_method.call(:id => 0)
196     assert_response :not_found
197     assert_template "browse/not_found"
198     assert_template :layout => "map"
199
200     get path_method.call(:id => 0), :xhr => true
201     assert_response :not_found
202     assert_template "browse/not_found"
203     assert_template :layout => "xhr"
204
205     get path_method.call(:id => id)
206     assert_response :success
207     assert_template template
208     assert_template :layout => "map"
209
210     get path_method.call(:id => id), :xhr => true
211     assert_response :success
212     assert_template template
213     assert_template :layout => "xhr"
214   end
215 end