]> git.openstreetmap.org Git - rails.git/blob - test/controllers/notes_controller_test.rb
Replace "Login" labels with "Log in" when used as a verb
[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, :closed, :closed_by => user, :comments_count => 2)
148
149     browse_check :note_path, closed_note.id, "notes/show"
150     assert_select "div.note-comments ul li", :count => 2
151     assert_select "div.details", /Resolved by #{user.display_name}/
152
153     user.soft_destroy!
154
155     reset!
156
157     browse_check :note_path, closed_note.id, "notes/show"
158     assert_select "div.note-comments ul li", :count => 1
159     assert_select "div.details", /Resolved by deleted/
160   end
161
162   def test_new_note
163     get new_note_path
164     assert_response :success
165     assert_template "notes/new"
166   end
167
168   private
169
170   # This is a convenience method for most of the above checks
171   # First we check that when we don't have an id, it will correctly return a 404
172   # then we check that we get the correct 404 when a non-existant id is passed
173   # then we check that it will get a successful response, when we do pass an id
174   def browse_check(path, id, template)
175     path_method = method(path)
176
177     assert_raise ActionController::UrlGenerationError do
178       get path_method.call
179     end
180
181     # assert_raise ActionController::UrlGenerationError do
182     #   get path_method.call(:id => -10) # we won't have an id that's negative
183     # end
184
185     get path_method.call(:id => 0)
186     assert_response :not_found
187     assert_template "browse/not_found"
188     assert_template :layout => "map"
189
190     get path_method.call(:id => 0), :xhr => true
191     assert_response :not_found
192     assert_template "browse/not_found"
193     assert_template :layout => "xhr"
194
195     get path_method.call(:id => id)
196     assert_response :success
197     assert_template template
198     assert_template :layout => "map"
199
200     get path_method.call(:id => id), :xhr => true
201     assert_response :success
202     assert_template template
203     assert_template :layout => "xhr"
204   end
205 end