You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.9 KiB

  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(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'push') }
  7. before do
  8. allow(controller).to receive(:doorkeeper_token) { token }
  9. end
  10. let(:create_payload) do
  11. {
  12. subscription: {
  13. endpoint: 'https://fcm.googleapis.com/fcm/send/fiuH06a27qE:APA91bHnSiGcLwdaxdyqVXNDR9w1NlztsHb6lyt5WDKOC_Z_Q8BlFxQoR8tWFSXUIDdkyw0EdvxTu63iqamSaqVSevW5LfoFwojws8XYDXv_NRRLH6vo2CdgiN4jgHv5VLt2A8ah6lUX',
  14. keys: {
  15. p256dh: 'BEm_a0bdPDhf0SOsrnB2-ategf1hHoCnpXgQsFj5JCkcoMrMt2WHoPfEYOYPzOIs9mZE8ZUaD7VA5vouy0kEkr8=',
  16. auth: 'eH_C8rq2raXqlcBVDa1gLg==',
  17. },
  18. }
  19. }.with_indifferent_access
  20. end
  21. let(:alerts_payload) do
  22. {
  23. data: {
  24. alerts: {
  25. follow: true,
  26. favourite: false,
  27. reblog: true,
  28. mention: false,
  29. }
  30. }
  31. }.with_indifferent_access
  32. end
  33. describe 'POST #create' do
  34. it 'saves push subscriptions' do
  35. post :create, params: create_payload
  36. push_subscription = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])
  37. expect(push_subscription.endpoint).to eq(create_payload[:subscription][:endpoint])
  38. expect(push_subscription.key_p256dh).to eq(create_payload[:subscription][:keys][:p256dh])
  39. expect(push_subscription.key_auth).to eq(create_payload[:subscription][:keys][:auth])
  40. expect(push_subscription.user_id).to eq user.id
  41. expect(push_subscription.access_token_id).to eq token.id
  42. end
  43. it 'replaces old subscription on repeat calls' do
  44. post :create, params: create_payload
  45. post :create, params: create_payload
  46. expect(Web::PushSubscription.where(endpoint: create_payload[:subscription][:endpoint]).count).to eq 1
  47. end
  48. end
  49. describe 'PUT #update' do
  50. it 'changes alert settings' do
  51. post :create, params: create_payload
  52. put :update, params: alerts_payload
  53. push_subscription = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])
  54. expect(push_subscription.data.dig('alerts', 'follow')).to eq(alerts_payload[:data][:alerts][:follow].to_s)
  55. expect(push_subscription.data.dig('alerts', 'favourite')).to eq(alerts_payload[:data][:alerts][:favourite].to_s)
  56. expect(push_subscription.data.dig('alerts', 'reblog')).to eq(alerts_payload[:data][:alerts][:reblog].to_s)
  57. expect(push_subscription.data.dig('alerts', 'mention')).to eq(alerts_payload[:data][:alerts][:mention].to_s)
  58. end
  59. end
  60. describe 'DELETE #destroy' do
  61. it 'removes the subscription' do
  62. post :create, params: create_payload
  63. delete :destroy
  64. expect(Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])).to be_nil
  65. end
  66. end
  67. end