3 class NotesControllerTest < ActionDispatch::IntegrationTest
6 # Stub nominatim response for note locations
7 stub_request(:get, %r{^https://nominatim\.openstreetmap\.org/reverse\?})
8 .to_return(:status => 404)
12 # test all routes which lead to this controller
15 { :path => "/user/username/notes", :method => :get },
16 { :controller => "notes", :action => "index", :display_name => "username" }
19 { :path => "/note/1", :method => :get },
20 { :controller => "notes", :action => "show", :id => "1" }
23 { :path => "/note/new", :method => :get },
24 { :controller => "notes", :action => "new" }
28 def test_index_success
29 first_user = create(:user)
30 second_user = create(:user)
31 moderator_user = create(:moderator_user)
33 create(:note) do |note|
34 create(:note_comment, :note => note, :author => first_user)
36 create(:note) do |note|
37 create(:note_comment, :note => note, :author => second_user)
39 create(:note, :status => "hidden") do |note|
40 create(:note_comment, :note => note, :author => second_user)
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
48 get user_notes_path(:display_name => second_user.display_name)
49 assert_response :success
50 assert_select "table.note_list tr", :count => 2
52 get user_notes_path(:display_name => "non-existent")
53 assert_response :not_found
55 session_for(moderator_user)
57 get user_notes_path(:display_name => first_user.display_name)
58 assert_response :success
59 assert_select "table.note_list tr", :count => 2
61 get user_notes_path(:display_name => second_user.display_name)
62 assert_response :success
63 assert_select "table.note_list tr", :count => 3
65 get user_notes_path(:display_name => "non-existent")
66 assert_response :not_found
72 create_list(:note, 50) do |note|
73 create(:note_comment, :note => note, :author => user)
76 get user_notes_path(:display_name => user.display_name)
77 assert_response :success
78 assert_select "table.note_list tr", :count => 11
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
87 get user_notes_path(:display_name => user.display_name)
88 assert_response :success
89 assert_select "h4", :html => "No notes"
93 open_note = create(:note_with_comments)
95 browse_check :note_path, open_note.id, "notes/show"
98 def test_read_hidden_note
99 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
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"
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"
111 session_for(create(:moderator_user))
113 browse_check :note_path, hidden_note_with_comment.id, "notes/show"
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)
121 browse_check :note_path, note_with_hidden_comment.id, "notes/show"
122 assert_select "div.note-comments ul li", :count => 1
124 session_for(create(:moderator_user))
126 browse_check :note_path, note_with_hidden_comment.id, "notes/show"
127 assert_select "div.note-comments ul li", :count => 2
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)
136 browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
137 assert_select "div.note-comments ul li", :count => 1
139 session_for(create(:moderator_user))
141 browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
142 assert_select "div.note-comments ul li", :count => 1
145 def test_read_closed_note
147 closed_note = create(:note_with_comments, :closed, :closed_by => user, :comments_count => 2)
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}/
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/
164 assert_response :success
165 assert_template "notes/new"
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)
177 assert_raise ActionController::UrlGenerationError do
181 # assert_raise ActionController::UrlGenerationError do
182 # get path_method.call(:id => -10) # we won't have an id that's negative
185 get path_method.call(:id => 0)
186 assert_response :not_found
187 assert_template "browse/not_found"
188 assert_template :layout => "map"
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"
195 get path_method.call(:id => id)
196 assert_response :success
197 assert_template template
198 assert_template :layout => "map"
200 get path_method.call(:id => id), :xhr => true
201 assert_response :success
202 assert_template template
203 assert_template :layout => "xhr"