push_notification_worker_spec.rb 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Web::PushNotificationWorker do
  4. subject { described_class.new }
  5. let(:p256dh) { 'BN4GvZtEZiZuqFxSKVZfSfluwKBD7UxHNBmWkfiZfCtgDE8Bwh-_MtLXbBxTBAWH9r7IPKL0lhdcaqtL1dfxU5E=' }
  6. let(:auth) { 'Q2BoAjC09xH3ywDLNJr-dA==' }
  7. let(:endpoint) { 'https://updates.push.services.mozilla.com/push/v1/subscription-id' }
  8. let(:user) { Fabricate(:user) }
  9. let(:notification) { Fabricate(:notification) }
  10. let(:subscription) { Fabricate(:web_push_subscription, user_id: user.id, key_p256dh: p256dh, key_auth: auth, endpoint: endpoint, data: { alerts: { notification.type => true } }) }
  11. let(:vapid_public_key) { 'BB37UCyc8LLX4PNQSe-04vSFvpUWGrENubUaslVFM_l5TxcGVMY0C3RXPeUJAQHKYlcOM2P4vTYmkoo0VZGZTM4=' }
  12. let(:vapid_private_key) { 'OPrw1Sum3gRoL4-DXfSCC266r-qfFSRZrnj8MgIhRHg=' }
  13. let(:vapid_key) { Webpush::VapidKey.from_keys(vapid_public_key, vapid_private_key) }
  14. let(:contact_email) { 'sender@example.com' }
  15. let(:ciphertext) { "+\xB8\xDBT}\x13\xB6\xDD.\xF9\xB0\xA7\xC8\xD2\x80\xFD\x99#\xF7\xAC\x83\xA4\xDB,\x1F\xB5\xB9w\x85>\xF7\xADr" }
  16. let(:salt) { "X\x97\x953\xE4X\xF8_w\xE7T\x95\xC51q\xFE" }
  17. let(:server_public_key) { "\x04\b-RK9w\xDD$\x16lFz\xF9=\xB4~\xC6\x12k\xF3\xF40t\xA9\xC1\fR\xC3\x81\x80\xAC\f\x7F\xE4\xCC\x8E\xC2\x88 n\x8BB\xF1\x9C\x14\a\xFA\x8D\xC9\x80\xA1\xDDyU\\&c\x01\x88#\x118Ua" }
  18. let(:shared_secret) { "\t\xA7&\x85\t\xC5m\b\xA8\xA7\xF8B{1\xADk\xE1y'm\xEDE\xEC\xDD\xEDj\xB3$s\xA9\xDA\xF0" }
  19. let(:payload) { { ciphertext: ciphertext, salt: salt, server_public_key: server_public_key, shared_secret: shared_secret } }
  20. describe 'perform' do
  21. around do |example|
  22. original_private = Rails.configuration.x.vapid_private_key
  23. original_public = Rails.configuration.x.vapid_public_key
  24. Rails.configuration.x.vapid_private_key = vapid_private_key
  25. Rails.configuration.x.vapid_public_key = vapid_public_key
  26. example.run
  27. Rails.configuration.x.vapid_private_key = original_private
  28. Rails.configuration.x.vapid_public_key = original_public
  29. end
  30. before do
  31. Setting.site_contact_email = contact_email
  32. allow(Webpush::Encryption).to receive(:encrypt).and_return(payload)
  33. allow(JWT).to receive(:encode).and_return('jwt.encoded.payload')
  34. stub_request(:post, endpoint).to_return(status: 201, body: '')
  35. end
  36. it 'calls the relevant service with the correct headers' do
  37. subject.perform(subscription.id, notification.id)
  38. expect(web_push_endpoint_request)
  39. .to have_been_made
  40. end
  41. def web_push_endpoint_request
  42. a_request(
  43. :post,
  44. endpoint
  45. ).with(
  46. headers: {
  47. 'Content-Encoding' => 'aesgcm',
  48. 'Content-Type' => 'application/octet-stream',
  49. 'Crypto-Key' => "dh=BAgtUks5d90kFmxGevk9tH7GEmvz9DB0qcEMUsOBgKwMf-TMjsKIIG6LQvGcFAf6jcmAod15VVwmYwGIIxE4VWE;p256ecdsa=#{vapid_public_key.delete('=')}",
  50. 'Encryption' => 'salt=WJeVM-RY-F9351SVxTFx_g',
  51. 'Ttl' => '172800',
  52. 'Urgency' => 'normal',
  53. 'Authorization' => 'WebPush jwt.encoded.payload',
  54. 'Unsubscribe-URL' => %r{/api/web/push_subscriptions/},
  55. },
  56. body: "+\xB8\xDBT}\u0013\xB6\xDD.\xF9\xB0\xA7\xC8Ҁ\xFD\x99#\xF7\xAC\x83\xA4\xDB,\u001F\xB5\xB9w\x85>\xF7\xADr"
  57. )
  58. end
  59. end
  60. end