subscriptions_controller_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Api::V1::Push::SubscriptionsController do
  4. render_views
  5. let(:user) { Fabricate(:user) }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'push') }
  7. before do
  8. allow(controller).to receive(:doorkeeper_token) { token }
  9. end
  10. let(:create_payload) do
  11. {
  12. subscription: {
  13. endpoint: 'https://fcm.googleapis.com/fcm/send/fiuH06a27qE:APA91bHnSiGcLwdaxdyqVXNDR9w1NlztsHb6lyt5WDKOC_Z_Q8BlFxQoR8tWFSXUIDdkyw0EdvxTu63iqamSaqVSevW5LfoFwojws8XYDXv_NRRLH6vo2CdgiN4jgHv5VLt2A8ah6lUX',
  14. keys: {
  15. p256dh: 'BEm_a0bdPDhf0SOsrnB2-ategf1hHoCnpXgQsFj5JCkcoMrMt2WHoPfEYOYPzOIs9mZE8ZUaD7VA5vouy0kEkr8=',
  16. auth: 'eH_C8rq2raXqlcBVDa1gLg==',
  17. },
  18. }
  19. }.with_indifferent_access
  20. end
  21. let(:alerts_payload) do
  22. {
  23. data: {
  24. policy: 'all',
  25. alerts: {
  26. follow: true,
  27. follow_request: true,
  28. favourite: false,
  29. reblog: true,
  30. mention: false,
  31. poll: true,
  32. status: false,
  33. }
  34. }
  35. }.with_indifferent_access
  36. end
  37. describe 'POST #create' do
  38. before do
  39. post :create, params: create_payload
  40. end
  41. it 'saves push subscriptions' do
  42. push_subscription = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])
  43. expect(push_subscription.endpoint).to eq(create_payload[:subscription][:endpoint])
  44. expect(push_subscription.key_p256dh).to eq(create_payload[:subscription][:keys][:p256dh])
  45. expect(push_subscription.key_auth).to eq(create_payload[:subscription][:keys][:auth])
  46. expect(push_subscription.user_id).to eq user.id
  47. expect(push_subscription.access_token_id).to eq token.id
  48. end
  49. it 'replaces old subscription on repeat calls' do
  50. post :create, params: create_payload
  51. expect(Web::PushSubscription.where(endpoint: create_payload[:subscription][:endpoint]).count).to eq 1
  52. end
  53. it 'returns the expected JSON' do
  54. expect(body_as_json.with_indifferent_access).to include({ endpoint: create_payload[:subscription][:endpoint], alerts: {}, policy: 'all' })
  55. end
  56. end
  57. describe 'PUT #update' do
  58. before do
  59. post :create, params: create_payload
  60. put :update, params: alerts_payload
  61. end
  62. it 'changes alert settings' do
  63. push_subscription = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])
  64. expect(push_subscription.data['policy']).to eq(alerts_payload[:data][:policy])
  65. %w(follow follow_request favourite reblog mention poll status).each do |type|
  66. expect(push_subscription.data['alerts'][type]).to eq(alerts_payload[:data][:alerts][type.to_sym].to_s)
  67. end
  68. end
  69. it 'returns the expected JSON' do
  70. expect(body_as_json.with_indifferent_access).to include({ endpoint: create_payload[:subscription][:endpoint], alerts: alerts_payload[:data][:alerts], policy: alerts_payload[:data][:policy] })
  71. end
  72. end
  73. describe 'DELETE #destroy' do
  74. before do
  75. post :create, params: create_payload
  76. delete :destroy
  77. end
  78. it 'removes the subscription' do
  79. expect(Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])).to be_nil
  80. end
  81. end
  82. end