|
| 1 | +require 'spec_helper' |
| 2 | +require 'mail' |
| 3 | + |
| 4 | +RSpec.describe Userlist::DeliveryMethod do |
| 5 | + let(:push_client) { instance_double(Userlist::Push) } |
| 6 | + let(:messages) { instance_double('Messages') } |
| 7 | + let(:config) { { push_key: 'test-key' } } |
| 8 | + |
| 9 | + before do |
| 10 | + allow(Userlist::Push).to receive(:new).with(config).and_return(push_client) |
| 11 | + allow(push_client).to receive(:messages).and_return(messages) |
| 12 | + allow(messages).to receive(:push) |
| 13 | + end |
| 14 | + |
| 15 | + describe '#deliver!' do |
| 16 | + let(:delivery_method) { described_class.new(config) } |
| 17 | + |
| 18 | + context 'with plain text email' do |
| 19 | + let(:mail) do |
| 20 | + Mail.new( |
| 21 | + to: 'Example User <user@example.com>', |
| 22 | + from: 'Example Sender <sender@example.com>', |
| 23 | + subject: 'Test Subject', |
| 24 | + body: 'Hello world' |
| 25 | + ) |
| 26 | + end |
| 27 | + |
| 28 | + it 'delivers the message correctly' do |
| 29 | + expected_payload = { |
| 30 | + to: 'user@example.com', |
| 31 | + from: 'sender@example.com', |
| 32 | + subject: 'Test Subject', |
| 33 | + body: { type: :text, content: 'Hello world' }, |
| 34 | + theme: nil |
| 35 | + } |
| 36 | + |
| 37 | + expect(messages).to receive(:push).with(expected_payload) |
| 38 | + delivery_method.deliver!(mail) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + context 'with HTML email' do |
| 43 | + let(:mail) do |
| 44 | + Mail.new do |
| 45 | + to 'user@example.com' |
| 46 | + from 'sender@example.com' |
| 47 | + subject 'Test Subject' |
| 48 | + |
| 49 | + html_part do |
| 50 | + content_type 'text/html' |
| 51 | + body '<p>Hello world</p>' |
| 52 | + end |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + it 'delivers the message correctly' do |
| 57 | + expected_payload = { |
| 58 | + to: 'user@example.com', |
| 59 | + from: 'sender@example.com', |
| 60 | + subject: 'Test Subject', |
| 61 | + body: { type: :html, content: '<p>Hello world</p>' }, |
| 62 | + theme: nil |
| 63 | + } |
| 64 | + |
| 65 | + expect(messages).to receive(:push).with(expected_payload) |
| 66 | + delivery_method.deliver!(mail) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + context 'with multipart email' do |
| 71 | + let(:mail) do |
| 72 | + Mail.new do |
| 73 | + to 'user@example.com' |
| 74 | + from 'sender@example.com' |
| 75 | + subject 'Test Subject' |
| 76 | + |
| 77 | + text_part do |
| 78 | + body 'Hello world' |
| 79 | + end |
| 80 | + |
| 81 | + html_part do |
| 82 | + content_type 'text/html' |
| 83 | + body '<p>Hello world</p>' |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + it 'delivers the message correctly' do |
| 89 | + expected_payload = { |
| 90 | + to: 'user@example.com', |
| 91 | + from: 'sender@example.com', |
| 92 | + subject: 'Test Subject', |
| 93 | + body: { |
| 94 | + type: :multipart, |
| 95 | + content: [ |
| 96 | + { type: :text, content: 'Hello world' }, |
| 97 | + { type: :html, content: '<p>Hello world</p>' } |
| 98 | + ] |
| 99 | + }, |
| 100 | + theme: nil |
| 101 | + } |
| 102 | + |
| 103 | + expect(messages).to receive(:push).with(expected_payload) |
| 104 | + delivery_method.deliver!(mail) |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | +end |
0 commit comments