push_subscriptions_controller_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. alerts: {
  21. follow: true,
  22. favourite: false,
  23. reblog: true,
  24. mention: false,
  25. }
  26. }
  27. }
  28. end
  29. describe 'POST #create' do
  30. it 'saves push subscriptions' do
  31. sign_in(user)
  32. stub_request(:post, create_payload[:subscription][:endpoint]).to_return(status: 200)
  33. post :create, format: :json, params: create_payload
  34. user.reload
  35. push_subscription = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])
  36. expect(push_subscription['endpoint']).to eq(create_payload[:subscription][:endpoint])
  37. expect(push_subscription['key_p256dh']).to eq(create_payload[:subscription][:keys][:p256dh])
  38. expect(push_subscription['key_auth']).to eq(create_payload[:subscription][:keys][:auth])
  39. end
  40. context 'with initial data' do
  41. it 'saves alert settings' do
  42. sign_in(user)
  43. stub_request(:post, create_payload[:subscription][:endpoint]).to_return(status: 200)
  44. post :create, format: :json, params: create_payload.merge(alerts_payload)
  45. push_subscription = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])
  46. expect(push_subscription.data['alerts']['follow']).to eq(alerts_payload[:data][:alerts][:follow].to_s)
  47. expect(push_subscription.data['alerts']['favourite']).to eq(alerts_payload[:data][:alerts][:favourite].to_s)
  48. expect(push_subscription.data['alerts']['reblog']).to eq(alerts_payload[:data][:alerts][:reblog].to_s)
  49. expect(push_subscription.data['alerts']['mention']).to eq(alerts_payload[:data][:alerts][:mention].to_s)
  50. end
  51. end
  52. end
  53. describe 'PUT #update' do
  54. it 'changes alert settings' do
  55. sign_in(user)
  56. stub_request(:post, create_payload[:subscription][:endpoint]).to_return(status: 200)
  57. post :create, format: :json, params: create_payload
  58. alerts_payload[:id] = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint]).id
  59. put :update, format: :json, params: alerts_payload
  60. push_subscription = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])
  61. expect(push_subscription.data['alerts']['follow']).to eq(alerts_payload[:data][:alerts][:follow].to_s)
  62. expect(push_subscription.data['alerts']['favourite']).to eq(alerts_payload[:data][:alerts][:favourite].to_s)
  63. expect(push_subscription.data['alerts']['reblog']).to eq(alerts_payload[:data][:alerts][:reblog].to_s)
  64. expect(push_subscription.data['alerts']['mention']).to eq(alerts_payload[:data][:alerts][:mention].to_s)
  65. end
  66. end
  67. end