File: action_mailer_test.rb

package info (click to toggle)
ruby-mail-gpg 0.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 328 kB
  • sloc: ruby: 2,289; makefile: 6
file content (131 lines) | stat: -rw-r--r-- 4,325 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require 'test_helper'

class MyMailer < ActionMailer::Base
  default from: 'joe@foo.bar', to: 'jane@foo.bar'

  def unencrypted(bouncy=false)
    mail subject: 'unencrypted', body: 'unencrypted mail', return_path: (bouncy && bounce_address)
  end

  def encrypted(bouncy=false)
    mail subject: 'encrypted', body: 'encrypted mail', return_path: (bouncy && bounce_address), gpg: {encrypt: true}
  end

  def signed(bouncy=false)
    mail  from: 'jane@foo.bar',
          to: 'joe@foo.bar',
          subject: 'signed',
          body: 'signed mail',
          return_path: (bouncy && bounce_address),
          gpg: {
            sign: true,
            password: 'abc'
          }
  end

  private

  def bounce_address
    SecureRandom.uuid + "@bounces.example.com"
  end

end

class ActionMailerTest < MailGpgTestCase
  context 'without return_path' do
    setup do
      set_passphrase('abc')
      (@emails = ActionMailer::Base.deliveries).clear
    end

    context "with action mailer" do
      should "send unencrypted mail" do
        MyMailer.unencrypted.deliver
        assert_equal 1, @emails.size
        assert m = @emails.first
        assert_equal 'unencrypted', m.subject
      end


      should "send encrypted mail" do
        assert m = MyMailer.encrypted
        assert true == m.gpg[:encrypt]
        m.deliver
        assert_equal 1, @emails.size
        assert m = @emails.first
        assert_equal 'encrypted', m.subject
        assert_equal 2, m.parts.size
        assert encrypted = m.parts.detect{|p| p.content_type =~ /encrypted\.asc/}
        assert clear = GPGME::Crypto.new.decrypt(encrypted.body.to_s, password: 'abc')
        m = Mail.new clear
        assert_equal 'encrypted mail', m.body.to_s
      end

      should "send signed mail" do
        assert m = MyMailer.signed
        assert true == m.gpg[:sign]
        m.deliver
        assert_equal 1, @emails.size
        assert delivered = @emails.first
        assert_equal 'signed', delivered.subject
        assert_equal 2, delivered.parts.size
        assert sign_part = delivered.parts.detect{|p| p.content_type =~ /signature\.asc/}
        assert signed_part = delivered.parts.detect{|p| p.content_type !~ /signature\.asc/}
        GPGME::Crypto.new.verify(sign_part.body.to_s, signed_text: signed_part.encoded) do |sig|
          assert sig.valid?
        end
      end
    end
  end

  context 'with return_path' do
    setup do
      set_passphrase('abc')
      (@emails = ActionMailer::Base.deliveries).clear
    end

    context "with action mailer" do
      should "send unencrypted mail" do
        MyMailer.unencrypted(true).deliver
        assert_equal 1, @emails.size
        assert m = @emails.first
        assert_match /@bounces\.example\.com\z/, m.return_path
        assert_equal 'unencrypted', m.subject
      end

      # For unknown reasons this test can't decrypt the test-message if
      # it's the first one that's running. Therefore we misspelled
      # its name a little.
      should "zend encrypted mail" do
        assert m = MyMailer.encrypted(true)
        assert true == m.gpg[:encrypt]
        m.deliver
        assert_equal 1, @emails.size
        assert m = @emails.first
        assert_match /@bounces\.example\.com\z/, m.return_path
        assert_equal 'encrypted', m.subject
        assert_equal 2, m.parts.size
        assert encrypted = m.parts.detect{|p| p.content_type =~ /encrypted\.asc/}
        assert clear = GPGME::Crypto.new.decrypt(encrypted.body.to_s, password: 'abc')
        m = Mail.new clear
        assert_equal 'encrypted mail', m.body.to_s
      end

      should "send signed mail" do
        assert m = MyMailer.signed(true)
        assert true == m.gpg[:sign]
        m.deliver
        assert_equal 1, @emails.size
        assert delivered = @emails.first
        assert_match /@bounces\.example\.com\z/, delivered.return_path
        assert_equal 'signed', delivered.subject
        assert_equal 2, delivered.parts.size
        assert sign_part = delivered.parts.detect{|p| p.content_type =~ /signature\.asc/}
        assert signed_part = delivered.parts.detect{|p| p.content_type !~ /signature\.asc/}
        GPGME::Crypto.new.verify(sign_part.body.to_s, signed_text: signed_part.encoded) do |sig|
          assert sig.valid?
        end
      end
    end
  end
end