Skip to content

Commit

Permalink
Convert api element history actions to versions resources
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Feb 6, 2025
1 parent eb1ae8e commit 129031b
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 130 deletions.
3 changes: 1 addition & 2 deletions app/abilities/api_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def initialize(token)
can [:read, :download], Changeset
can :read, Tracepoint
can :read, User
can :read, [Node, Way, Relation]
can [:history, :read], [OldNode, OldWay, OldRelation]
can :read, [Node, Way, Relation, OldNode, OldWay, OldRelation]
can :read, UserBlock

if user&.active?
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/api/old_elements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
module Api
class OldElementsController < ApiController
before_action :check_api_writable, :only => [:redact]
before_action :setup_user_auth, :only => [:history, :show]
before_action :setup_user_auth, :only => [:index, :show]
before_action :authorize, :only => [:redact]

authorize_resource

before_action :lookup_old_element, :except => [:history]
before_action :lookup_old_element_versions, :only => [:history]
before_action :lookup_old_element, :except => [:index]
before_action :lookup_old_element_versions, :only => [:index]

before_action :set_request_formats, :except => [:redact]

def history
def index
# the .where() method used in the lookup_old_element_versions
# call won't throw an error if no records are found, so we have
# to do that ourselves.
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/old_nodes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def lookup_old_element
end

def lookup_old_element_versions
@elements = OldNode.where(:node_id => params[:id]).order(:version)
@elements = OldNode.where(:node_id => params[:node_id]).order(:version)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/api/old_relations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def lookup_old_element
end

def lookup_old_element_versions
@elements = OldRelation.where(:relation_id => params[:id]).order(:version)
@elements = OldRelation.where(:relation_id => params[:relation_id]).order(:version)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/api/old_ways_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def lookup_old_element
end

def lookup_old_element_versions
@elements = OldWay.where(:way_id => params[:id]).order(:version)
@elements = OldWay.where(:way_id => params[:way_id]).order(:version)
end
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion app/views/old_elements/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<%= render :partial => "browse/#{@type}", :collection => @feature.send(:"old_#{@type}s").reverse %>

<div class='secondary-actions'>
<%= link_to t("browse.download_xml"), :controller => "api/old_#{@type.pluralize}", :action => "history" %>
<%= link_to t("browse.download_xml"), send(:"api_#{@type}_versions_path", @feature.id) %>
&middot;
<%= link_to t("browse.view_details"), :controller => @type.pluralize, :action => :show %>
<% if params[:show_redactions] %>
Expand Down
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@
post "changeset/comment/:id/hide" => "changeset_comments#destroy", :as => :changeset_comment_hide, :id => /\d+/
post "changeset/comment/:id/unhide" => "changeset_comments#restore", :as => :changeset_comment_unhide, :id => /\d+/

get "node/:id/history" => "old_nodes#history", :as => :api_node_history, :id => /\d+/
post "node/:id/:version/redact" => "old_nodes#redact", :as => :node_version_redact, :version => /\d+/, :id => /\d+/
get "node/:id/:version" => "old_nodes#show", :as => :api_old_node, :id => /\d+/, :version => /\d+/

get "way/:id/history" => "old_ways#history", :as => :api_way_history, :id => /\d+/
post "way/:id/:version/redact" => "old_ways#redact", :as => :way_version_redact, :version => /\d+/, :id => /\d+/
get "way/:id/:version" => "old_ways#show", :as => :api_old_way, :id => /\d+/, :version => /\d+/

get "relation/:id/history" => "old_relations#history", :as => :api_relation_history, :id => /\d+/
post "relation/:id/:version/redact" => "old_relations#redact", :as => :relation_version_redact, :version => /\d+/, :id => /\d+/
get "relation/:id/:version" => "old_relations#show", :as => :api_old_relation, :id => /\d+/, :version => /\d+/
end
Expand All @@ -50,6 +47,7 @@
resources :ways, :only => :index
resources :relations, :only => :index
end
resources :versions, :path => "history", :controller => :old_nodes, :only => :index
end
put "node/create" => "nodes#create", :as => nil

Expand All @@ -61,6 +59,7 @@
scope :module => :ways do
resources :relations, :only => :index
end
resources :versions, :path => "history", :controller => :old_ways, :only => :index
end
put "way/create" => "ways#create", :as => nil

Expand All @@ -72,6 +71,7 @@
scope :module => :relations do
resources :relations, :only => :index
end
resources :versions, :path => "history", :controller => :old_relations, :only => :index
end
put "relation/create" => "relations#create", :as => nil

Expand Down
89 changes: 54 additions & 35 deletions test/controllers/api/old_nodes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@

module Api
class OldNodesControllerTest < ActionDispatch::IntegrationTest
#
# TODO: test history
#

##
# test all routes which lead to this controller
def test_routes
assert_routing(
{ :path => "/api/0.6/node/1/history", :method => :get },
{ :controller => "api/old_nodes", :action => "history", :id => "1" }
{ :controller => "api/old_nodes", :action => "index", :node_id => "1" }
)
assert_routing(
{ :path => "/api/0.6/node/1/2", :method => :get },
{ :controller => "api/old_nodes", :action => "show", :id => "1", :version => "2" }
{ :path => "/api/0.6/node/1/history.json", :method => :get },
{ :controller => "api/old_nodes", :action => "index", :node_id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/node/1/history.json", :method => :get },
{ :controller => "api/old_nodes", :action => "history", :id => "1", :format => "json" }
{ :path => "/api/0.6/node/1/2", :method => :get },
{ :controller => "api/old_nodes", :action => "show", :id => "1", :version => "2" }
)
assert_routing(
{ :path => "/api/0.6/node/1/2.json", :method => :get },
Expand All @@ -31,6 +27,49 @@ def test_routes
)
end

def test_index
node = create(:node, :version => 2)
create(:old_node, :node_id => node.id, :version => 1, :latitude => 60 * OldNode::SCALE, :longitude => 30 * OldNode::SCALE)
create(:old_node, :node_id => node.id, :version => 2, :latitude => 61 * OldNode::SCALE, :longitude => 31 * OldNode::SCALE)

get api_node_versions_path(node)

assert_response :success
assert_dom "osm:root", 1 do
assert_dom "> node", 2 do |dom_nodes|
assert_dom dom_nodes[0], "> @id", node.id.to_s
assert_dom dom_nodes[0], "> @version", "1"
assert_dom dom_nodes[0], "> @lat", "60.0000000"
assert_dom dom_nodes[0], "> @lon", "30.0000000"

assert_dom dom_nodes[1], "> @id", node.id.to_s
assert_dom dom_nodes[1], "> @version", "2"
assert_dom dom_nodes[1], "> @lat", "61.0000000"
assert_dom dom_nodes[1], "> @lon", "31.0000000"
end
end
end

##
# test that redacted nodes aren't visible in the history
def test_index_redacted
node = create(:node, :with_history, :version => 2)
node_v1 = node.old_nodes.find_by(:version => 1)
node_v1.redact!(create(:redaction))

get api_node_versions_path(node)
assert_response :success, "Redaction shouldn't have stopped history working."
assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 0,
"redacted node #{node_v1.node_id} version #{node_v1.version} shouldn't be present in the history."

# not even to a logged-in user
auth_header = bearer_authorization_header
get api_node_versions_path(node), :headers => auth_header
assert_response :success, "Redaction shouldn't have stopped history working."
assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 0,
"redacted node #{node_v1.node_id} version #{node_v1.version} shouldn't be present in the history, even when logged in."
end

##
# test the version call by submitting several revisions of a new node
# to the API and ensuring that later calls to version return the
Expand Down Expand Up @@ -191,7 +230,7 @@ def test_current_version
def test_lat_lon_xml_format
old_node = create(:old_node, :latitude => (0.00004 * OldNode::SCALE).to_i, :longitude => (0.00008 * OldNode::SCALE).to_i)

get api_node_history_path(old_node.node_id)
get api_node_versions_path(old_node.node_id)
assert_match(/lat="0.0000400"/, response.body)
assert_match(/lon="0.0000800"/, response.body)
end
Expand Down Expand Up @@ -279,26 +318,6 @@ def test_version_redacted
assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
end

##
# test that redacted nodes aren't visible in the history
def test_history_redacted
node = create(:node, :with_history, :version => 2)
node_v1 = node.old_nodes.find_by(:version => 1)
node_v1.redact!(create(:redaction))

get api_node_history_path(node)
assert_response :success, "Redaction shouldn't have stopped history working."
assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 0,
"redacted node #{node_v1.node_id} version #{node_v1.version} shouldn't be present in the history."

# not even to a logged-in user
auth_header = bearer_authorization_header
get api_node_history_path(node), :headers => auth_header
assert_response :success, "Redaction shouldn't have stopped history working."
assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 0,
"redacted node #{node_v1.node_id} version #{node_v1.version} shouldn't be present in the history, even when logged in."
end

##
# test the redaction of an old version of a node, while being
# authorised as a moderator.
Expand All @@ -318,11 +337,11 @@ def test_redact_node_moderator
assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."

# and when accessed via history
get api_node_history_path(node)
get api_node_versions_path(node)
assert_response :success, "Redaction shouldn't have stopped history working."
assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 0,
"node #{node_v3.node_id} version #{node_v3.version} should not be present in the history for moderators when not passing flag."
get api_node_history_path(node, :show_redactions => "true"), :headers => auth_header
get api_node_versions_path(node, :show_redactions => "true"), :headers => auth_header
assert_response :success, "Redaction shouldn't have stopped history working."
assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 1,
"node #{node_v3.node_id} version #{node_v3.version} should still be present in the history for moderators when passing flag."
Expand All @@ -346,7 +365,7 @@ def test_redact_node_is_redacted
assert_response :forbidden, "Redacted node shouldn't be visible via the version API."

# and when accessed via history
get api_node_history_path(node), :headers => auth_header
get api_node_versions_path(node), :headers => auth_header
assert_response :success, "Redaction shouldn't have stopped history working."
assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 0,
"redacted node #{node_v3.node_id} version #{node_v3.version} shouldn't be present in the history."
Expand Down Expand Up @@ -399,7 +418,7 @@ def test_unredact_node_moderator
assert_response :success, "After unredaction, node should not be gone for moderator."

# and when accessed via history
get api_node_history_path(node)
get api_node_versions_path(node)
assert_response :success, "Unredaction shouldn't have stopped history working."
assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1,
"node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for moderators without passing flag."
Expand All @@ -411,7 +430,7 @@ def test_unredact_node_moderator
assert_response :success, "After unredaction, node should be visible to normal users."

# and when accessed via history
get api_node_history_path(node)
get api_node_versions_path(node)
assert_response :success, "Unredaction shouldn't have stopped history working."
assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1,
"node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for normal users without passing flag."
Expand Down
Loading

0 comments on commit 129031b

Please sign in to comment.