]> git.openstreetmap.org Git - rails.git/blob - test/controllers/old_ways_controller_test.rb
Don't show unredacted element versions to non-moderators
[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/2", :method => :get },
7       { :controller => "old_ways", :action => "show", :id => "1", :version => "2" }
8     )
9   end
10
11   def test_visible_with_one_version
12     way = create(:way, :with_history)
13     get old_way_path(way, 1)
14     assert_response :success
15     assert_template "old_ways/show"
16     assert_template :layout => "map"
17     assert_select "h4", /^Version/ do
18       assert_select "a[href='#{old_way_path way, 1}']", :count => 0
19     end
20     assert_select ".secondary-actions a[href='#{way_version_path way, 1}']", :count => 1
21     assert_select ".secondary-actions a[href='#{way_path way}']", :count => 1
22     assert_select ".secondary-actions a[href='#{way_history_path way}']", :count => 1
23   end
24
25   def test_visible_with_two_versions
26     way = create(:way, :with_history, :version => 2)
27     get old_way_path(way, 1)
28     assert_response :success
29     assert_template "old_ways/show"
30     assert_template :layout => "map"
31     assert_select "h4", /^Version/ do
32       assert_select "a[href='#{old_way_path way, 1}']", :count => 0
33     end
34     assert_select ".secondary-actions a[href='#{way_version_path way, 1}']", :count => 1
35     assert_select ".secondary-actions a[href='#{way_path way}']", :count => 1
36     assert_select ".secondary-actions a[href='#{way_history_path way}']", :count => 1
37     assert_select ".secondary-actions a[href='#{old_way_path way, 2}']", :count => 1
38
39     get old_way_path(way, 2)
40     assert_response :success
41     assert_template "old_ways/show"
42     assert_template :layout => "map"
43     assert_select "h4", /^Version/ do
44       assert_select "a[href='#{old_way_path way, 2}']", :count => 0
45     end
46     assert_select ".secondary-actions a[href='#{way_version_path way, 2}']", :count => 1
47     assert_select ".secondary-actions a[href='#{way_path way}']", :count => 1
48     assert_select ".secondary-actions a[href='#{way_history_path way}']", :count => 1
49     assert_select ".secondary-actions a[href='#{old_way_path way, 1}']", :count => 1
50   end
51
52   def test_visible_with_shared_nodes
53     node = create(:node, :with_history)
54     way = create(:way, :with_history)
55     create(:way_node, :way => way, :node => node)
56     create(:old_way_node, :old_way => way.old_ways.first, :node => node)
57     sharing_way = create(:way, :with_history)
58     create(:way_node, :way => sharing_way, :node => node)
59     create(:old_way_node, :old_way => sharing_way.old_ways.first, :node => node)
60     get old_way_path(way, 1)
61     assert_response :success
62     assert_template "old_ways/show"
63     assert_template :layout => "map"
64   end
65
66   def test_redacted
67     way = create_redacted_way
68     get old_way_path(way, 1)
69     assert_response :success
70     assert_template "old_ways/show"
71     assert_template :layout => "map"
72     assert_select ".secondary-actions a[href='#{way_path way}']", :count => 1
73     assert_select ".secondary-actions a[href='#{old_way_path way, 1}']", :count => 0
74     assert_select ".secondary-actions a[href='#{way_version_path way, 1}']", :count => 0
75   end
76
77   test "don't show redacted versions to anonymous users" do
78     way = create_redacted_way
79     get old_way_path(way, 1, :params => { :show_redactions => true })
80     assert_response :redirect
81   end
82
83   test "don't show redacted versions to regular users" do
84     session_for(create(:user))
85     way = create_redacted_way
86     get old_way_path(way, 1, :params => { :show_redactions => true })
87     assert_response :redirect
88   end
89
90   test "show redacted versions to moderators" do
91     session_for(create(:moderator_user))
92     way = create_redacted_way
93     get old_way_path(way, 1, :params => { :show_redactions => true })
94     assert_response :success
95   end
96
97   def test_not_found
98     get old_way_path(0, 0)
99     assert_response :not_found
100     assert_template "old_ways/not_found"
101     assert_template :layout => "map"
102     assert_select "#sidebar_content", /way #0 version 0 could not be found/
103   end
104
105   private
106
107   def create_redacted_way
108     create(:way, :with_history, :deleted, :version => 2) do |way|
109       way_v1 = way.old_ways.find_by(:version => 1)
110       way_v1.redact!(create(:redaction))
111     end
112   end
113 end