push_subscriptions_controller_spec.rb 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. 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. describe 'POST #create' do
  34. it 'saves push subscriptions' do
  35. sign_in(user)
  36. stub_request(:post, create_payload[:subscription][:endpoint]).to_return(status: 200)
  37. post :create, format: :json, params: create_payload
  38. user.reload
  39. push_subscription = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])
  40. expect(push_subscription['endpoint']).to eq(create_payload[:subscription][:endpoint])
  41. expect(push_subscription['key_p256dh']).to eq(create_payload[:subscription][:keys][:p256dh])
  42. expect(push_subscription['key_auth']).to eq(create_payload[:subscription][:keys][:auth])
  43. end
  44. context 'with initial data' do
  45. it 'saves alert settings' do
  46. sign_in(user)
  47. stub_request(:post, create_payload[:subscription][:endpoint]).to_return(status: 200)
  48. post :create, format: :json, params: create_payload.merge(alerts_payload)
  49. push_subscription = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])
  50. expect(push_subscription.data['policy']).to eq 'all'
  51. %w(follow follow_request favourite reblog mention poll status).each do |type|
  52. expect(push_subscription.data['alerts'][type]).to eq(alerts_payload[:data][:alerts][type.to_sym].to_s)
  53. end
  54. end
  55. end
  56. end
  57. describe 'PUT #update' do
  58. it 'changes alert settings' do
  59. sign_in(user)
  60. stub_request(:post, create_payload[:subscription][:endpoint]).to_return(status: 200)
  61. post :create, format: :json, params: create_payload
  62. alerts_payload[:id] = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint]).id
  63. put :update, format: :json, params: alerts_payload
  64. push_subscription = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])
  65. expect(push_subscription.data['policy']).to eq 'all'
  66. %w(follow follow_request favourite reblog mention poll status).each do |type|
  67. expect(push_subscription.data['alerts'][type]).to eq(alerts_payload[:data][:alerts][type.to_sym].to_s)
  68. end
  69. end
  70. end
  71. end