]> git.openstreetmap.org Git - rails.git/blob - test/controllers/old_relations_controller_test.rb
66a960e6f74efa87377ed668d3ec76156da6723f
[rails.git] / test / controllers / old_relations_controller_test.rb
1 require "test_helper"
2
3 class OldRelationsControllerTest < ActionDispatch::IntegrationTest
4   def test_routes
5     assert_routing(
6       { :path => "/relation/1/history/2", :method => :get },
7       { :controller => "old_relations", :action => "show", :id => "1", :version => "2" }
8     )
9   end
10
11   def test_visible_with_one_version
12     relation = create(:relation, :with_history)
13     get old_relation_path(relation, 1)
14     assert_response :success
15     assert_template "old_relations/show"
16     assert_template :layout => "map"
17     assert_select "h4", /^Version/ do
18       assert_select "a[href='#{old_relation_path relation, 1}']", :count => 0
19     end
20     assert_select ".secondary-actions a[href='#{api_old_relation_path relation, 1}']", :count => 1
21     assert_select ".secondary-actions a[href='#{relation_path relation}']", :count => 1
22     assert_select ".secondary-actions a[href='#{old_relation_path relation, 1, :params => { :show_redactions => true }}']", :count => 0
23     assert_select ".secondary-actions a[href='#{relation_history_path relation}']", :count => 1
24   end
25
26   def test_visible_with_two_versions
27     relation = create(:relation, :with_history, :version => 2)
28     get old_relation_path(relation, 1)
29     assert_response :success
30     assert_template "old_relations/show"
31     assert_template :layout => "map"
32     assert_select "h4", /^Version/ do
33       assert_select "a[href='#{old_relation_path relation, 1}']", :count => 0
34     end
35     assert_select ".secondary-actions a[href='#{api_old_relation_path relation, 1}']", :count => 1
36     assert_select ".secondary-actions a[href='#{relation_path relation}']", :count => 1
37     assert_select ".secondary-actions a[href='#{relation_history_path relation}']", :count => 1
38     assert_select ".secondary-actions a[href='#{old_relation_path relation, 2}']", :count => 1
39
40     get old_relation_path(relation, 2)
41     assert_response :success
42     assert_template "old_relations/show"
43     assert_template :layout => "map"
44     assert_select "h4", /^Version/ do
45       assert_select "a[href='#{old_relation_path relation, 2}']", :count => 0
46     end
47     assert_select ".secondary-actions a[href='#{api_old_relation_path relation, 2}']", :count => 1
48     assert_select ".secondary-actions a[href='#{relation_path relation}']", :count => 1
49     assert_select ".secondary-actions a[href='#{relation_history_path relation}']", :count => 1
50     assert_select ".secondary-actions a[href='#{old_relation_path relation, 1}']", :count => 1
51   end
52
53   def test_visible_with_members
54     relation = create(:relation, :with_history)
55     create(:old_relation_member, :old_relation => relation.old_relations.first)
56     get old_relation_path(relation, 1)
57     assert_response :success
58     assert_template "old_relations/show"
59     assert_template :layout => "map"
60   end
61
62   test "show unrevealed redacted versions to anonymous users" do
63     relation = create_redacted_relation
64     get old_relation_path(relation, 1)
65     assert_response :success
66     assert_template "old_relations/show"
67     assert_template :layout => "map"
68     assert_select "td", :text => "TOP SECRET", :count => 0
69     assert_select ".secondary-actions a[href='#{relation_path relation}']", :count => 1
70     assert_select ".secondary-actions a[href='#{old_relation_path relation, 1, :params => { :show_redactions => true }}']", :count => 0
71     assert_select ".secondary-actions a[href='#{old_relation_path relation, 1}']", :count => 0
72     assert_select ".secondary-actions a[href='#{api_old_relation_path relation, 1}']", :count => 0
73   end
74
75   test "show unrevealed redacted versions to regular users" do
76     session_for(create(:user))
77     relation = create_redacted_relation
78     get old_relation_path(relation, 1)
79     assert_response :success
80     assert_template "old_relations/show"
81     assert_template :layout => "map"
82     assert_select "td", :text => "TOP SECRET", :count => 0
83     assert_select ".secondary-actions a[href='#{relation_path relation}']", :count => 1
84     assert_select ".secondary-actions a[href='#{old_relation_path relation, 1, :params => { :show_redactions => true }}']", :count => 0
85     assert_select ".secondary-actions a[href='#{old_relation_path relation, 1}']", :count => 0
86     assert_select ".secondary-actions a[href='#{api_old_relation_path relation, 1}']", :count => 0
87   end
88
89   test "show unrevealed redacted versions to moderators" do
90     session_for(create(:moderator_user))
91     relation = create_redacted_relation
92     get old_relation_path(relation, 1)
93     assert_response :success
94     assert_template "old_relations/show"
95     assert_template :layout => "map"
96     assert_select "td", :text => "TOP SECRET", :count => 0
97     assert_select ".secondary-actions a[href='#{relation_path relation}']", :count => 1
98     assert_select ".secondary-actions a[href='#{old_relation_path relation, 1, :params => { :show_redactions => true }}']", :count => 1
99     assert_select ".secondary-actions a[href='#{old_relation_path relation, 1}']", :count => 0
100     assert_select ".secondary-actions a[href='#{api_old_relation_path relation, 1}']", :count => 0
101   end
102
103   test "don't reveal redacted versions to anonymous users" do
104     relation = create_redacted_relation
105     get old_relation_path(relation, 1, :params => { :show_redactions => true })
106     assert_response :redirect
107   end
108
109   test "don't reveal redacted versions to regular users" do
110     session_for(create(:user))
111     relation = create_redacted_relation
112     get old_relation_path(relation, 1, :params => { :show_redactions => true })
113     assert_response :redirect
114   end
115
116   test "reveal redacted versions to moderators" do
117     session_for(create(:moderator_user))
118     relation = create_redacted_relation
119     get old_relation_path(relation, 1, :params => { :show_redactions => true })
120     assert_response :success
121     assert_select "td", :text => "TOP SECRET", :count => 1
122     assert_select ".secondary-actions a[href='#{old_relation_path relation, 1}']", :count => 1
123   end
124
125   def test_not_found
126     get old_relation_path(0, 0)
127     assert_response :not_found
128     assert_template "old_relations/not_found"
129     assert_template :layout => "map"
130     assert_select "#sidebar_content", /relation #0 version 0 could not be found/
131   end
132
133   private
134
135   def create_redacted_relation
136     create(:relation, :with_history, :version => 2) do |relation|
137       relation_v1 = relation.old_relations.find_by(:version => 1)
138       create(:old_relation_tag, :old_relation => relation_v1, :k => "name", :v => "TOP SECRET")
139       relation_v1.redact!(create(:redaction))
140     end
141   end
142 end