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.

48 lines
2.8 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. 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. before do
  22. allow_any_instance_of(subscription.class).to receive(:contact_email).and_return(contact_email)
  23. allow_any_instance_of(subscription.class).to receive(:vapid_key).and_return(vapid_key)
  24. allow(Webpush::Encryption).to receive(:encrypt).and_return(payload)
  25. allow(JWT).to receive(:encode).and_return('jwt.encoded.payload')
  26. stub_request(:post, endpoint).to_return(status: 201, body: '')
  27. subject.perform(subscription.id, notification.id)
  28. end
  29. it 'calls the relevant service with the correct headers' do
  30. expect(a_request(:post, endpoint).with(headers: {
  31. 'Content-Encoding' => 'aesgcm',
  32. 'Content-Type' => 'application/octet-stream',
  33. 'Crypto-Key' => 'dh=BAgtUks5d90kFmxGevk9tH7GEmvz9DB0qcEMUsOBgKwMf-TMjsKIIG6LQvGcFAf6jcmAod15VVwmYwGIIxE4VWE;p256ecdsa=' + vapid_public_key.delete('='),
  34. 'Encryption' => 'salt=WJeVM-RY-F9351SVxTFx_g',
  35. 'Ttl' => '172800',
  36. 'Urgency' => 'normal',
  37. 'Authorization' => 'WebPush jwt.encoded.payload',
  38. }, body: "+\xB8\xDBT}\u0013\xB6\xDD.\xF9\xB0\xA7\xC8Ҁ\xFD\x99#\xF7\xAC\x83\xA4\xDB,\u001F\xB5\xB9w\x85>\xF7\xADr")).to have_been_made
  39. end
  40. end
  41. end