logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

push_subscriptions_controller_spec.rb (3124B)


  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['follow']).to eq(alerts_payload[:data][:follow])
  47. expect(push_subscription.data['favourite']).to eq(alerts_payload[:data][:favourite])
  48. expect(push_subscription.data['reblog']).to eq(alerts_payload[:data][:reblog])
  49. expect(push_subscription.data['mention']).to eq(alerts_payload[:data][:mention])
  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['follow']).to eq(alerts_payload[:data][:follow])
  62. expect(push_subscription.data['favourite']).to eq(alerts_payload[:data][:favourite])
  63. expect(push_subscription.data['reblog']).to eq(alerts_payload[:data][:reblog])
  64. expect(push_subscription.data['mention']).to eq(alerts_payload[:data][:mention])
  65. end
  66. end
  67. end