subscriptions_controller_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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(:create_payload) do
  7. {
  8. subscription: {
  9. endpoint: 'https://fcm.googleapis.com/fcm/send/fiuH06a27qE:APA91bHnSiGcLwdaxdyqVXNDR9w1NlztsHb6lyt5WDKOC_Z_Q8BlFxQoR8tWFSXUIDdkyw0EdvxTu63iqamSaqVSevW5LfoFwojws8XYDXv_NRRLH6vo2CdgiN4jgHv5VLt2A8ah6lUX',
  10. keys: {
  11. p256dh: 'BEm_a0bdPDhf0SOsrnB2-ategf1hHoCnpXgQsFj5JCkcoMrMt2WHoPfEYOYPzOIs9mZE8ZUaD7VA5vouy0kEkr8=',
  12. auth: 'eH_C8rq2raXqlcBVDa1gLg==',
  13. },
  14. },
  15. }.with_indifferent_access
  16. end
  17. let(:alerts_payload) do
  18. {
  19. data: {
  20. policy: 'all',
  21. alerts: {
  22. follow: true,
  23. follow_request: true,
  24. favourite: false,
  25. reblog: true,
  26. mention: false,
  27. poll: true,
  28. status: false,
  29. },
  30. },
  31. }.with_indifferent_access
  32. end
  33. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'push') }
  34. before do
  35. allow(controller).to receive(:doorkeeper_token) { token }
  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