]> git.openstreetmap.org Git - rails.git/blob - test/controllers/old_ways_controller_test.rb
Merge remote-tracking branch 'upstream/pull/4764'
[rails.git] / test / controllers / old_ways_controller_test.rb
1 require "test_helper"
2
3 class OldWaysControllerTest < ActionDispatch::IntegrationTest
4   def test_routes
5     assert_routing(
6       { :path => "/way/1/history", :method => :get },
7       { :controller => "old_ways", :action => "index", :id => "1" }
8     )
9     assert_routing(
10       { :path => "/way/1/history/2", :method => :get },
11       { :controller => "old_ways", :action => "show", :id => "1", :version => "2" }
12     )
13   end
14
15   def test_history
16     way = create(:way, :with_history)
17     sidebar_browse_check :way_history_path, way.id, "browse/history"
18     assert_select "h4", /^Version/ do
19       assert_select "a[href='#{old_way_path way, 1}']", :text => "1", :count => 1
20     end
21   end
22
23   def test_history_of_redacted
24     way = create(:way, :with_history, :version => 4)
25     way_v1 = way.old_ways.find_by(:version => 1)
26     way_v1.redact!(create(:redaction))
27     way_v3 = way.old_ways.find_by(:version => 3)
28     way_v3.redact!(create(:redaction))
29
30     get way_history_path(:id => way)
31     assert_response :success
32     assert_template "browse/history"
33
34     # there are 4 revisions of the redacted way, but only 2
35     # should be showing details here.
36     assert_select ".browse-section", 4
37     assert_select ".browse-section.browse-redacted", 2
38     assert_select ".browse-section.browse-way", 2
39   end
40
41   def test_unredacted_history_of_redacted
42     session_for(create(:moderator_user))
43     way = create(:way, :with_history, :version => 4)
44     way_v1 = way.old_ways.find_by(:version => 1)
45     way_v1.redact!(create(:redaction))
46     way_v3 = way.old_ways.find_by(:version => 3)
47     way_v3.redact!(create(:redaction))
48
49     get way_history_path(:id => way, :params => { :show_redactions => true })
50     assert_response :success
51     assert_template "browse/history"
52
53     assert_select ".browse-section", 4
54     assert_select ".browse-section.browse-redacted", 0
55     assert_select ".browse-section.browse-way", 4
56   end
57
58   def test_visible_with_one_version
59     way = create(:way, :with_history)
60     get old_way_path(way, 1)
61     assert_response :success
62     assert_template "old_ways/show"
63     assert_template :layout => "map"
64     assert_select "h4", /^Version/ do
65       assert_select "a[href='#{old_way_path way, 1}']", :count => 0
66     end
67     assert_select ".secondary-actions a[href='#{api_old_way_path way, 1}']", :count => 1
68     assert_select ".secondary-actions a[href='#{way_path way}']", :count => 1
69     assert_select ".secondary-actions a[href='#{old_way_path way, 1, :params => { :show_redactions => true }}']", :count => 0
70     assert_select ".secondary-actions a[href='#{way_history_path way}']", :count => 1
71   end
72
73   def test_visible_with_two_versions
74     way = create(:way, :with_history, :version => 2)
75     get old_way_path(way, 1)
76     assert_response :success
77     assert_template "old_ways/show"
78     assert_template :layout => "map"
79     assert_select "h4", /^Version/ do
80       assert_select "a[href='#{old_way_path way, 1}']", :count => 0
81     end
82     assert_select ".secondary-actions a[href='#{api_old_way_path way, 1}']", :count => 1
83     assert_select ".secondary-actions a[href='#{way_path way}']", :count => 1
84     assert_select ".secondary-actions a[href='#{way_history_path way}']", :count => 1
85     assert_select ".secondary-actions a[href='#{old_way_path way, 2}']", :count => 1
86
87     get old_way_path(way, 2)
88     assert_response :success
89     assert_template "old_ways/show"
90     assert_template :layout => "map"
91     assert_select "h4", /^Version/ do
92       assert_select "a[href='#{old_way_path way, 2}']", :count => 0
93     end
94     assert_select ".secondary-actions a[href='#{api_old_way_path way, 2}']", :count => 1
95     assert_select ".secondary-actions a[href='#{way_path way}']", :count => 1
96     assert_select ".secondary-actions a[href='#{way_history_path way}']", :count => 1
97     assert_select ".secondary-actions a[href='#{old_way_path way, 1}']", :count => 1
98   end
99
100   def test_visible_with_shared_nodes
101     node = create(:node, :with_history)
102     way = create(:way, :with_history)
103     create(:way_node, :way => way, :node => node)
104     create(:old_way_node, :old_way => way.old_ways.first, :node => node)
105     sharing_way = create(:way, :with_history)
106     create(:way_node, :way => sharing_way, :node => node)
107     create(:old_way_node, :old_way => sharing_way.old_ways.first, :node => node)
108     get old_way_path(way, 1)
109     assert_response :success
110     assert_template "old_ways/show"
111     assert_template :layout => "map"
112   end
113
114   test "show unrevealed redacted versions to anonymous users" do
115     way = create_redacted_way
116     get old_way_path(way, 1)
117     assert_response :success
118     assert_template "old_ways/show"
119     assert_template :layout => "map"
120     assert_select "td", :text => "TOP SECRET", :count => 0
121     assert_select ".secondary-actions a[href='#{way_path way}']", :count => 1
122     assert_select ".secondary-actions a[href='#{old_way_path way, 1, :params => { :show_redactions => true }}']", :count => 0
123     assert_select ".secondary-actions a[href='#{old_way_path way, 1}']", :count => 0
124     assert_select ".secondary-actions a[href='#{api_old_way_path way, 1}']", :count => 0
125   end
126
127   test "show unrevealed redacted versions to regular users" do
128     session_for(create(:user))
129     way = create_redacted_way
130     get old_way_path(way, 1)
131     assert_response :success
132     assert_template "old_ways/show"
133     assert_template :layout => "map"
134     assert_select "td", :text => "TOP SECRET", :count => 0
135     assert_select ".secondary-actions a[href='#{way_path way}']", :count => 1
136     assert_select ".secondary-actions a[href='#{old_way_path way, 1, :params => { :show_redactions => true }}']", :count => 0
137     assert_select ".secondary-actions a[href='#{old_way_path way, 1}']", :count => 0
138     assert_select ".secondary-actions a[href='#{api_old_way_path way, 1}']", :count => 0
139   end
140
141   test "show unrevealed redacted versions to moderators" do
142     session_for(create(:moderator_user))
143     way = create_redacted_way
144     get old_way_path(way, 1)
145     assert_response :success
146     assert_template "old_ways/show"
147     assert_template :layout => "map"
148     assert_select "td", :text => "TOP SECRET", :count => 0
149     assert_select ".secondary-actions a[href='#{way_path way}']", :count => 1
150     assert_select ".secondary-actions a[href='#{old_way_path way, 1, :params => { :show_redactions => true }}']", :count => 1
151     assert_select ".secondary-actions a[href='#{old_way_path way, 1}']", :count => 0
152     assert_select ".secondary-actions a[href='#{api_old_way_path way, 1}']", :count => 0
153   end
154
155   test "don't reveal redacted versions to anonymous users" do
156     way = create_redacted_way
157     get old_way_path(way, 1, :params => { :show_redactions => true })
158     assert_response :redirect
159   end
160
161   test "don't reveal redacted versions to regular users" do
162     session_for(create(:user))
163     way = create_redacted_way
164     get old_way_path(way, 1, :params => { :show_redactions => true })
165     assert_response :redirect
166   end
167
168   test "reveal redacted versions to moderators" do
169     session_for(create(:moderator_user))
170     way = create_redacted_way
171     get old_way_path(way, 1, :params => { :show_redactions => true })
172     assert_response :success
173     assert_select "td", :text => "TOP SECRET", :count => 1
174     assert_select ".secondary-actions a[href='#{old_way_path way, 1}']", :count => 1
175   end
176
177   def test_not_found
178     get old_way_path(0, 0)
179     assert_response :not_found
180     assert_template "old_ways/not_found"
181     assert_template :layout => "map"
182     assert_select "#sidebar_content", /way #0 version 0 could not be found/
183   end
184
185   private
186
187   def create_redacted_way
188     create(:way, :with_history, :version => 2) do |way|
189       way_v1 = way.old_ways.find_by(:version => 1)
190       create(:old_way_tag, :old_way => way_v1, :k => "name", :v => "TOP SECRET")
191       way_v1.redact!(create(:redaction))
192     end
193   end
194 end