]> git.openstreetmap.org Git - rails.git/commitdiff
Add active blocks list api endpoint with empty response
authorAnton Khorev <tony29@yandex.ru>
Wed, 1 Jan 2025 01:40:34 +0000 (04:40 +0300)
committerAnton Khorev <tony29@yandex.ru>
Fri, 14 Feb 2025 11:16:48 +0000 (14:16 +0300)
app/abilities/api_ability.rb
app/controllers/api/user_blocks/active_lists_controller.rb [new file with mode: 0644]
config/routes.rb
test/controllers/api/user_blocks/active_lists_controller_test.rb [new file with mode: 0644]

index a0340c5cd6fc30c7d0d25122bf8da9c3af379cdb..7bbd9889ad53fbbb6d064b115f79eca7a54cad75 100644 (file)
@@ -30,6 +30,8 @@ class ApiAbility
         can [:read, :update, :destroy], Message if scopes.include?("consume_messages")
         can :create, Message if scopes.include?("send_messages")
 
+        can :read, :active_user_blocks_list if scopes.include?("read_prefs")
+
         if user.terms_agreed?
           can [:create, :update, :upload, :close, :subscribe, :unsubscribe], Changeset if scopes.include?("write_map")
           can :create, ChangesetComment if scopes.include?("write_changeset_comments")
diff --git a/app/controllers/api/user_blocks/active_lists_controller.rb b/app/controllers/api/user_blocks/active_lists_controller.rb
new file mode 100644 (file)
index 0000000..132d946
--- /dev/null
@@ -0,0 +1,13 @@
+module Api
+  module UserBlocks
+    class ActiveListsController < ApiController
+      before_action :authorize
+
+      authorize_resource :class => :active_user_blocks_list
+
+      before_action :set_request_formats
+
+      def show; end
+    end
+  end
+end
index 54bf037cf293984488572a901710283a02d9222b..45fc19f2c295f3ff3834fd92ed9430d83de23416 100644 (file)
@@ -122,6 +122,9 @@ OpenStreetMap::Application.routes.draw do
     end
 
     resources :user_blocks, :only => :show, :id => /\d+/, :controller => "user_blocks"
+    namespace :user_blocks, :path => "user/blocks" do
+      resource :active_list, :path => "active", :only => :show
+    end
   end
 
   # Data browsing
diff --git a/test/controllers/api/user_blocks/active_lists_controller_test.rb b/test/controllers/api/user_blocks/active_lists_controller_test.rb
new file mode 100644 (file)
index 0000000..98518e7
--- /dev/null
@@ -0,0 +1,43 @@
+require "test_helper"
+
+module Api
+  module UserBlocks
+    class ActiveListsControllerTest < ActionDispatch::IntegrationTest
+      ##
+      # test all routes which lead to this controller
+      def test_routes
+        assert_routing(
+          { :path => "/api/0.6/user/blocks/active", :method => :get },
+          { :controller => "api/user_blocks/active_lists", :action => "show" }
+        )
+        assert_routing(
+          { :path => "/api/0.6/user/blocks/active.json", :method => :get },
+          { :controller => "api/user_blocks/active_lists", :action => "show", :format => "json" }
+        )
+      end
+
+      def test_show_no_auth_header
+        get api_user_blocks_active_list_path
+        assert_response :unauthorized
+      end
+
+      def test_show_no_permission
+        user = create(:user)
+        user_auth_header = bearer_authorization_header(user, :scopes => %w[])
+
+        get api_user_blocks_active_list_path, :headers => user_auth_header
+        assert_response :forbidden
+      end
+
+      def test_show_empty
+        user = create(:user)
+        user_auth_header = bearer_authorization_header(user, :scopes => %w[read_prefs])
+        create(:user_block, :expired, :user => user)
+
+        get api_user_blocks_active_list_path, :headers => user_auth_header
+        assert_response :success
+        assert_dom "user_block", :count => 0
+      end
+    end
+  end
+end