push_subscriptions_controller_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Api::Web::PushSubscriptionsController 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. }
  16. end
  17. let(:alerts_payload) do
  18. {
  19. data: {
  20. policy: 'all',
  21. alerts: {
  22. follow: true,
  23. follow_request: false,
  24. favourite: false,
  25. reblog: true,
  26. mention: false,
  27. poll: true,
  28. status: false,
  29. },
  30. },
  31. }
  32. end
  33. before do
  34. sign_in(user)
  35. stub_request(:post, create_payload[:subscription][:endpoint]).to_return(status: 200)
  36. end
  37. describe 'POST #create' do
  38. it 'saves push subscriptions' do
  39. post :create, format: :json, params: create_payload
  40. expect(response).to have_http_status(200)
  41. user.reload
  42. expect(created_push_subscription).to have_attributes(
  43. endpoint: eq(create_payload[:subscription][:endpoint]),
  44. key_p256dh: eq(create_payload[:subscription][:keys][:p256dh]),
  45. key_auth: eq(create_payload[:subscription][:keys][:auth])
  46. )
  47. expect(user.session_activations.first.web_push_subscription).to eq(created_push_subscription)
  48. end
  49. context 'with a user who has a session with a prior subscription' do
  50. let!(:prior_subscription) { Fabricate(:web_push_subscription, session_activation: user.session_activations.last) }
  51. it 'destroys prior subscription when creating new one' do
  52. post :create, format: :json, params: create_payload
  53. expect(response).to have_http_status(200)
  54. expect { prior_subscription.reload }.to raise_error(ActiveRecord::RecordNotFound)
  55. end
  56. end
  57. context 'with initial data' do
  58. it 'saves alert settings' do
  59. post :create, format: :json, params: create_payload.merge(alerts_payload)
  60. expect(response).to have_http_status(200)
  61. expect(created_push_subscription.data['policy']).to eq 'all'
  62. %w(follow follow_request favourite reblog mention poll status).each do |type|
  63. expect(created_push_subscription.data['alerts'][type]).to eq(alerts_payload[:data][:alerts][type.to_sym].to_s)
  64. end
  65. end
  66. end
  67. end
  68. describe 'PUT #update' do
  69. it 'changes alert settings' do
  70. post :create, format: :json, params: create_payload
  71. expect(response).to have_http_status(200)
  72. alerts_payload[:id] = created_push_subscription.id
  73. put :update, format: :json, params: alerts_payload
  74. expect(created_push_subscription.data['policy']).to eq 'all'
  75. %w(follow follow_request favourite reblog mention poll status).each do |type|
  76. expect(created_push_subscription.data['alerts'][type]).to eq(alerts_payload[:data][:alerts][type.to_sym].to_s)
  77. end
  78. end
  79. end
  80. def created_push_subscription
  81. Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])
  82. end
  83. end