1 # frozen_string_literal: true
 
   6   class NoteSubscriptionsControllerTest < ActionDispatch::IntegrationTest
 
   9         { :path => "/api/0.6/notes/1/subscription", :method => :post },
 
  10         { :controller => "api/note_subscriptions", :action => "create", :note_id => "1" }
 
  13         { :path => "/api/0.6/notes/1/subscription", :method => :delete },
 
  14         { :controller => "api/note_subscriptions", :action => "destroy", :note_id => "1" }
 
  20       auth_header = bearer_authorization_header user
 
  21       note = create(:note_with_comments)
 
  22       assert_empty note.subscribers
 
  24       assert_difference "NoteSubscription.count", 1 do
 
  25         assert_difference "note.subscribers.count", 1 do
 
  26           post api_note_subscription_path(note), :headers => auth_header
 
  27           assert_response :success
 
  30       assert_equal user, note.subscribers.last
 
  33     def test_create_fail_anonymous
 
  34       note = create(:note_with_comments)
 
  36       assert_no_difference "NoteSubscription.count" do
 
  37         assert_no_difference "note.subscribers.count" do
 
  38           post api_note_subscription_path(note)
 
  39           assert_response :unauthorized
 
  44     def test_create_fail_no_scope
 
  46       auth_header = bearer_authorization_header user, :scopes => %w[read_prefs]
 
  47       note = create(:note_with_comments)
 
  49       assert_no_difference "NoteSubscription.count" do
 
  50         assert_no_difference "note.subscribers.count" do
 
  51           post api_note_subscription_path(note), :headers => auth_header
 
  52           assert_response :forbidden
 
  57     def test_create_fail_note_not_found
 
  59       auth_header = bearer_authorization_header user
 
  61       assert_no_difference "NoteSubscription.count" do
 
  62         post api_note_subscription_path(999111), :headers => auth_header
 
  63         assert_response :not_found
 
  65       assert_match "not found", @response.body
 
  68     def test_create_fail_already_subscribed
 
  70       auth_header = bearer_authorization_header user
 
  71       note = create(:note_with_comments)
 
  72       create(:note_subscription, :user => user, :note => note)
 
  74       assert_no_difference "NoteSubscription.count" do
 
  75         assert_no_difference "note.subscribers.count" do
 
  76           post api_note_subscription_path(note), :headers => auth_header
 
  77           assert_response :conflict
 
  80       assert_match "already subscribed", @response.body
 
  85       auth_header = bearer_authorization_header user
 
  86       other_user = create(:user)
 
  87       note = create(:note_with_comments)
 
  88       other_note = create(:note_with_comments)
 
  89       create(:note_subscription, :user => user, :note => note)
 
  90       create(:note_subscription, :user => other_user, :note => note)
 
  91       create(:note_subscription, :user => user, :note => other_note)
 
  93       assert_difference "NoteSubscription.count", -1 do
 
  94         assert_difference "note.subscribers.count", -1 do
 
  95           delete api_note_subscription_path(note), :headers => auth_header
 
  96           assert_response :success
 
 100       assert_equal [other_user], note.subscribers
 
 101       assert_equal [user], other_note.subscribers
 
 104     def test_destroy_fail_anonymous
 
 105       note = create(:note_with_comments)
 
 107       delete api_note_subscription_path(note)
 
 108       assert_response :unauthorized
 
 111     def test_destroy_fail_no_scope
 
 113       auth_header = bearer_authorization_header user, :scopes => %w[read_prefs]
 
 114       note = create(:note_with_comments)
 
 115       create(:note_subscription, :user => user, :note => note)
 
 117       assert_no_difference "NoteSubscription.count" do
 
 118         assert_no_difference "note.subscribers.count" do
 
 119           delete api_note_subscription_path(note), :headers => auth_header
 
 120           assert_response :forbidden
 
 125     def test_destroy_fail_note_not_found
 
 127       auth_header = bearer_authorization_header user
 
 129       delete api_note_subscription_path(999111), :headers => auth_header
 
 130       assert_response :not_found
 
 131       assert_match "not found", @response.body
 
 134     def test_destroy_fail_not_subscribed
 
 136       auth_header = bearer_authorization_header user
 
 137       note = create(:note_with_comments)
 
 139       delete api_note_subscription_path(note), :headers => auth_header
 
 140       assert_response :not_found
 
 141       assert_match "not subscribed", @response.body