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 get user_notes_path(:display_name => first_user.display_name)
44 assert_response :success
45 assert_select "table.note_list tbody tr", :count => 1
47 get user_notes_path(:display_name => second_user.display_name)
48 assert_response :success
49 assert_select "table.note_list tbody tr", :count => 1
51 get user_notes_path(:display_name => "non-existent")
52 assert_response :not_found
54 session_for(moderator_user)
56 get user_notes_path(:display_name => first_user.display_name)
57 assert_response :success
58 assert_select "table.note_list tbody tr", :count => 1
60 get user_notes_path(:display_name => second_user.display_name)
61 assert_response :success
62 assert_select "table.note_list tbody tr", :count => 2
64 get user_notes_path(:display_name => "non-existent")
65 assert_response :not_found
71 create_list(:note, 50) do |note|
72 create(:note_comment, :note => note, :author => user)
75 get user_notes_path(:display_name => user.display_name)
76 assert_response :success
77 assert_select "table.note_list tbody tr", :count => 10
79 get user_notes_path(:display_name => user.display_name, :page => 2)
80 assert_response :success
81 assert_select "table.note_list tbody tr", :count => 10
86 get user_notes_path(:display_name => user.display_name)
87 assert_response :success
88 assert_select "h4", :html => "No notes"
92 open_note = create(:note_with_comments)
94 browse_check :note_path, open_note.id, "notes/show"
97 def test_read_hidden_note
98 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
100 get note_path(:id => hidden_note_with_comment)
101 assert_response :not_found
102 assert_template "browse/not_found"
103 assert_template :layout => "map"
105 get note_path(:id => hidden_note_with_comment), :xhr => true
106 assert_response :not_found
107 assert_template "browse/not_found"
108 assert_template :layout => "xhr"
110 session_for(create(:moderator_user))
112 browse_check :note_path, hidden_note_with_comment.id, "notes/show"
115 def test_read_note_hidden_comments
116 note_with_hidden_comment = create(:note_with_comments, :comments_count => 2) do |note|
117 create(:note_comment, :note => note, :visible => false)
120 browse_check :note_path, note_with_hidden_comment.id, "notes/show"
121 assert_select "div.note-comments ul li", :count => 1
123 session_for(create(:moderator_user))
125 browse_check :note_path, note_with_hidden_comment.id, "notes/show"
126 assert_select "div.note-comments ul li", :count => 2
129 def test_read_note_hidden_user_comment
130 hidden_user = create(:user, :deleted)
131 note_with_hidden_user_comment = create(:note_with_comments, :comments_count => 2) do |note|
132 create(:note_comment, :note => note, :author => hidden_user)
135 browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
136 assert_select "div.note-comments ul li", :count => 1
138 session_for(create(:moderator_user))
140 browse_check :note_path, note_with_hidden_user_comment.id, "notes/show"
141 assert_select "div.note-comments ul li", :count => 1
144 def test_read_closed_note
146 closed_note = create(:note_with_comments, :closed, :closed_by => user, :comments_count => 2)
148 browse_check :note_path, closed_note.id, "notes/show"
149 assert_select "div.note-comments ul li", :count => 2
150 assert_select "div.details", /Resolved by #{user.display_name}/
156 browse_check :note_path, closed_note.id, "notes/show"
157 assert_select "div.note-comments ul li", :count => 1
158 assert_select "div.details", /Resolved by deleted/
163 assert_response :success
164 assert_template "notes/new"
169 # This is a convenience method for most of the above checks
170 # First we check that when we don't have an id, it will correctly return a 404
171 # then we check that we get the correct 404 when a non-existant id is passed
172 # then we check that it will get a successful response, when we do pass an id
173 def browse_check(path, id, template)
174 path_method = method(path)
176 assert_raise ActionController::UrlGenerationError do
180 # assert_raise ActionController::UrlGenerationError do
181 # get path_method.call(:id => -10) # we won't have an id that's negative
184 get path_method.call(:id => 0)
185 assert_response :not_found
186 assert_template "browse/not_found"
187 assert_template :layout => "map"
189 get path_method.call(:id => 0), :xhr => true
190 assert_response :not_found
191 assert_template "browse/not_found"
192 assert_template :layout => "xhr"
194 get path_method.call(:id => id)
195 assert_response :success
196 assert_template template
197 assert_template :layout => "map"
199 get path_method.call(:id => id), :xhr => true
200 assert_response :success
201 assert_template template
202 assert_template :layout => "xhr"