]> git.openstreetmap.org Git - rails.git/blob - test/controllers/notes_controller_test.rb
Move browse#note to notes#show
[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     # Note that the table rows include a header row
44     get user_notes_path(:display_name => first_user.display_name)
45     assert_response :success
46     assert_select "table.note_list tr", :count => 2
47
48     get user_notes_path(:display_name => second_user.display_name)
49     assert_response :success
50     assert_select "table.note_list tr", :count => 2
51
52     get user_notes_path(:display_name => "non-existent")
53     assert_response :not_found
54
55     session_for(moderator_user)
56
57     get user_notes_path(:display_name => first_user.display_name)
58     assert_response :success
59     assert_select "table.note_list tr", :count => 2
60
61     get user_notes_path(:display_name => second_user.display_name)
62     assert_response :success
63     assert_select "table.note_list tr", :count => 3
64
65     get user_notes_path(:display_name => "non-existent")
66     assert_response :not_found
67   end
68
69   def test_index_paged
70     user = create(:user)
71
72     create_list(:note, 50) do |note|
73       create(:note_comment, :note => note, :author => user)
74     end
75
76     get user_notes_path(:display_name => user.display_name)
77     assert_response :success
78     assert_select "table.note_list tr", :count => 11
79
80     get user_notes_path(:display_name => user.display_name, :page => 2)
81     assert_response :success
82     assert_select "table.note_list tr", :count => 11
83   end
84
85   def test_empty_page
86     user = create(:user)
87     get user_notes_path(:display_name => user.display_name)
88     assert_response :success
89     assert_select "h4", :html => "No notes"
90   end
91
92   def test_read_note
93     open_note = create(:note_with_comments)
94
95     browse_check :note_path, open_note.id, "notes/show"
96   end
97
98   def test_read_hidden_note
99     hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
100
101     get note_path(:id => hidden_note_with_comment)
102     assert_response :not_found
103     assert_template "browse/not_found"
104     assert_template :layout => "map"
105
106     get note_path(:id => hidden_note_with_comment), :xhr => true
107     assert_response :not_found
108     assert_template "browse/not_found"
109     assert_template :layout => "xhr"
110
111     session_for(create(:moderator_user))
112
113     browse_check :note_path, hidden_note_with_comment.id, "notes/show"
114   end
115
116   def test_read_note_hidden_comments
117     note_with_hidden_comment = create(:note_with_comments, :comments_count => 2) do |note|
118       create(:note_comment, :note => note, :visible => false)
119     end
120
121     browse_check :note_path, note_with_hidden_comment.id, "notes/show"
122     assert_select "div.note-comments ul li", :count => 1
123
124     session_for(create(:moderator_user))
125
126     browse_check :note_path, note_with_hidden_comment.id, "notes/show"
127     assert_select "div.note-comments ul li", :count => 2
128   end
129
130   def test_read_note_hidden_user_comment
131     hidden_user = create(:user, :deleted)
132     note_with_hidden_user_comment = create(:note_with_comments, :comments_count => 2) do |note|
133       create(:note_comment, :note => note, :author => hidden_user)
134     end
135
136     browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
137     assert_select "div.note-comments ul li", :count => 1
138
139     session_for(create(:moderator_user))
140
141     browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
142     assert_select "div.note-comments ul li", :count => 1
143   end
144
145   def test_read_closed_note
146     user = create(:user)
147     closed_note = create(:note_with_comments, :status => "closed", :closed_at => Time.now.utc, :comments_count => 2) do |note|
148       create(:note_comment, :event => "closed", :note => note, :author => user)
149     end
150
151     browse_check :note_path, closed_note.id, "notes/show"
152     assert_select "div.note-comments ul li", :count => 2
153     assert_select "div.details", /Resolved by #{user.display_name}/
154
155     user.soft_destroy!
156
157     reset!
158
159     browse_check :note_path, closed_note.id, "notes/show"
160     assert_select "div.note-comments ul li", :count => 1
161     assert_select "div.details", /Resolved by deleted/
162   end
163
164   def test_new_note
165     get new_note_path
166     assert_response :success
167     assert_template "notes/new"
168   end
169
170   private
171
172   # This is a convenience method for most of the above checks
173   # First we check that when we don't have an id, it will correctly return a 404
174   # then we check that we get the correct 404 when a non-existant id is passed
175   # then we check that it will get a successful response, when we do pass an id
176   def browse_check(path, id, template)
177     path_method = method(path)
178
179     assert_raise ActionController::UrlGenerationError do
180       get path_method.call
181     end
182
183     # assert_raise ActionController::UrlGenerationError do
184     #   get path_method.call(:id => -10) # we won't have an id that's negative
185     # end
186
187     get path_method.call(:id => 0)
188     assert_response :not_found
189     assert_template "browse/not_found"
190     assert_template :layout => "map"
191
192     get path_method.call(:id => 0), :xhr => true
193     assert_response :not_found
194     assert_template "browse/not_found"
195     assert_template :layout => "xhr"
196
197     get path_method.call(:id => id)
198     assert_response :success
199     assert_template template
200     assert_template :layout => "map"
201
202     get path_method.call(:id => id), :xhr => true
203     assert_response :success
204     assert_template template
205     assert_template :layout => "xhr"
206   end
207 end