Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/pull/4240'
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Sep 14, 2023
2 parents 55b2ddc + 9442829 commit 85b17a1
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/abilities/api_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def initialize(user)
can [:history, :version], OldNode
can [:history, :version], OldWay
can [:history, :version], OldRelation
can [:show], UserBlock
end

if user&.active?
Expand Down
18 changes: 18 additions & 0 deletions app/controllers/api/user_blocks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Api
class UserBlocksController < ApiController
before_action :check_api_readable

authorize_resource

around_action :api_call_handle_error, :api_call_timeout
before_action :set_request_formats

def show
raise OSM::APIBadUserInput, "No id was given" unless params[:id]

@user_block = UserBlock.find(params[:id])
rescue ActiveRecord::RecordNotFound
raise OSM::APINotFoundError
end
end
end
13 changes: 13 additions & 0 deletions app/views/api/user_blocks/_user_block.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
json.user_block do
json.id user_block.id
json.created_at user_block.created_at.xmlschema
json.updated_at user_block.updated_at.xmlschema
json.ends_at user_block.ends_at.xmlschema
json.needs_view user_block.needs_view

json.user :uid => user_block.user_id, :user => user_block.user.display_name
json.creator :uid => user_block.creator_id, :user => user_block.creator.display_name
json.revoker :uid => user_block.revoker_id, :user => user_block.revoker.display_name if user_block.revoker

json.reason user_block.reason
end
14 changes: 14 additions & 0 deletions app/views/api/user_blocks/_user_block.xml.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
attrs = {
"id" => user_block.id,
"created_at" => user_block.created_at.xmlschema,
"updated_at" => user_block.updated_at.xmlschema,
"ends_at" => user_block.ends_at.xmlschema,
"needs_view" => user_block.needs_view
}

xml.user_block(attrs) do
xml.user :uid => user_block.user_id, :user => user_block.user.display_name
xml.creator :uid => user_block.creator_id, :user => user_block.creator.display_name
xml.revoker :uid => user_block.revoker_id, :user => user_block.revoker.display_name if user_block.revoker
xml.reason user_block.reason
end
3 changes: 3 additions & 0 deletions app/views/api/user_blocks/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json.partial! "api/root_attributes"

json.partial! @user_block
5 changes: 5 additions & 0 deletions app/views/api/user_blocks/show.xml.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
xml.instruct!

xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@user_block) || "")
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
post "notes/editPOIexec" => "api/notes#comment"
get "notes/getGPX" => "api/notes#index", :format => "gpx"
get "notes/getRSSfeed" => "api/notes#feed", :format => "rss"

resources :user_blocks, :only => [:show], :constraints => { :id => /\d+/ }, :controller => "api/user_blocks", :as => :api_user_blocks
end

# Data browsing
Expand Down
36 changes: 36 additions & 0 deletions test/controllers/api/user_blocks_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "test_helper"

module Api
class UserBlocksControllerTest < ActionDispatch::IntegrationTest
def test_routes
assert_routing(
{ :path => "/api/0.6/user_blocks/1", :method => :get },
{ :controller => "api/user_blocks", :action => "show", :id => "1" }
)
assert_routing(
{ :path => "/api/0.6/user_blocks/1.json", :method => :get },
{ :controller => "api/user_blocks", :action => "show", :id => "1", :format => "json" }
)
end

def test_show
block = create(:user_block)

get api_user_block_path(:id => block)
assert_response :success
assert_select "user_block[id='#{block.id}']", 1

get api_user_block_path(:id => block, :format => "json")
assert_response :success
js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js
assert_equal block.id, js["user_block"]["id"]
end

def test_show_not_found
get api_user_block_path(:id => 123)
assert_response :not_found
assert_equal "text/plain", @response.media_type
end
end
end

0 comments on commit 85b17a1

Please sign in to comment.