]> git.openstreetmap.org Git - rails.git/commitdiff
Add destroy note subscription api endpoint
authorAnton Khorev <tony29@yandex.ru>
Thu, 24 Oct 2024 01:46:57 +0000 (04:46 +0300)
committerAnton Khorev <tony29@yandex.ru>
Wed, 20 Nov 2024 17:35:24 +0000 (20:35 +0300)
app/abilities/api_capability.rb
app/controllers/api/note_subscriptions_controller.rb
config/routes.rb
test/controllers/api/note_subscriptions_controller_test.rb

index dade7f6fec0b5cb287c0a27c52ef36baae94e295..0e953d50b11c95f2294bb2ec31f752b22a1966dc 100644 (file)
@@ -9,7 +9,7 @@ class ApiCapability
 
       if user&.active?
         can [:create, :comment, :close, :reopen], Note if scope?(token, :write_notes)
-        can :create, NoteSubscription if scope?(token, :write_notes)
+        can [:create, :destroy], NoteSubscription if scope?(token, :write_notes)
         can [:show, :data], Trace if scope?(token, :read_gpx)
         can [:create, :update, :destroy], Trace if scope?(token, :write_gpx)
         can [:details], User if scope?(token, :read_prefs)
index 348b428b6394e8122e3640ccee11b9e8d37421aa..c416dd803647112b337e22e137917c69206bd552 100644 (file)
@@ -14,5 +14,14 @@ module Api
     rescue ActiveRecord::RecordNotUnique
       report_error "You are already subscribed to note #{note_id}.", :conflict
     end
+
+    def destroy
+      note_id = params[:note_id].to_i
+      note = Note.find(note_id)
+      count = note.subscriptions.where(:user => current_user).delete_all
+      report_error "You are not subscribed to note #{note_id}.", :not_found if count.zero?
+    rescue ActiveRecord::RecordNotFound
+      report_error "Note #{note_id} not found.", :not_found
+    end
   end
 end
index 96b27c145d64b79d1f7110b613557f02a604cad2..f65042dd7d9ab1c9bf83e22b5c2f1c71c5503ff9 100644 (file)
@@ -109,7 +109,7 @@ OpenStreetMap::Application.routes.draw do
         post "reopen"
       end
 
-      resource :subscription, :only => :create, :controller => "note_subscriptions"
+      resource :subscription, :only => [:create, :destroy], :controller => "note_subscriptions"
     end
 
     resources :user_blocks, :only => :show, :id => /\d+/, :controller => "user_blocks"
index 45aa33ad16ff36c3e89150f0fee38af93ed217af..0e388697cc195861e3ed63b79ead6c7e062c13c8 100644 (file)
@@ -7,6 +7,10 @@ module Api
         { :path => "/api/0.6/notes/1/subscription", :method => :post },
         { :controller => "api/note_subscriptions", :action => "create", :note_id => "1" }
       )
+      assert_routing(
+        { :path => "/api/0.6/notes/1/subscription", :method => :delete },
+        { :controller => "api/note_subscriptions", :action => "destroy", :note_id => "1" }
+      )
     end
 
     def test_create
@@ -73,5 +77,66 @@ module Api
       end
       assert_match "already subscribed", @response.body
     end
+
+    def test_destroy
+      user = create(:user)
+      auth_header = bearer_authorization_header user
+      other_user = create(:user)
+      note = create(:note_with_comments)
+      other_note = create(:note_with_comments)
+      create(:note_subscription, :user => user, :note => note)
+      create(:note_subscription, :user => other_user, :note => note)
+      create(:note_subscription, :user => user, :note => other_note)
+
+      assert_difference "NoteSubscription.count", -1 do
+        assert_difference "note.subscribers.count", -1 do
+          delete api_note_subscription_path(note), :headers => auth_header
+          assert_response :success
+        end
+      end
+      note.reload
+      assert_equal [other_user], note.subscribers
+      assert_equal [user], other_note.subscribers
+    end
+
+    def test_destroy_fail_anonymous
+      note = create(:note_with_comments)
+
+      delete api_note_subscription_path(note)
+      assert_response :unauthorized
+    end
+
+    def test_destroy_fail_no_scope
+      user = create(:user)
+      auth_header = bearer_authorization_header user, :scopes => %w[read_prefs]
+      note = create(:note_with_comments)
+      create(:note_subscription, :user => user, :note => note)
+
+      assert_no_difference "NoteSubscription.count" do
+        assert_no_difference "note.subscribers.count" do
+          delete api_note_subscription_path(note), :headers => auth_header
+          assert_response :forbidden
+        end
+      end
+    end
+
+    def test_destroy_fail_note_not_found
+      user = create(:user)
+      auth_header = bearer_authorization_header user
+
+      delete api_note_subscription_path(999111), :headers => auth_header
+      assert_response :not_found
+      assert_match "not found", @response.body
+    end
+
+    def test_destroy_fail_not_subscribed
+      user = create(:user)
+      auth_header = bearer_authorization_header user
+      note = create(:note_with_comments)
+
+      delete api_note_subscription_path(note), :headers => auth_header
+      assert_response :not_found
+      assert_match "not subscribed", @response.body
+    end
   end
 end