1# frozen_string_literal: true
2
3require 'mail/smtp_pool'
4
5# Original mockup from ActionMailer
6# Based on https://github.com/mikel/mail/blob/22a7afc23f253319965bf9228a0a430eec94e06d/spec/spec_helper.rb#L74-L138
7class MockSMTP
8  def self.deliveries
9    @@deliveries
10  end
11
12  def self.security
13    @@security
14  end
15
16  def initialize
17    @@deliveries = []
18    @@security = nil
19    @started = false
20  end
21
22  def sendmail(mail, from, to)
23    @@deliveries << [mail, from, to]
24    'OK'
25  end
26
27  def rset
28    Net::SMTP::Response.parse('250 OK')
29  end
30
31  def start(*args)
32    @started = true
33
34    if block_given?
35      result = yield(self)
36      @started = false
37
38      return result
39    else
40      return self
41    end
42  end
43
44  def started?
45    @started
46  end
47
48  def finish
49    @started = false
50    return true
51  end
52
53  def self.clear_deliveries
54    @@deliveries = []
55  end
56
57  def self.clear_security
58    @@security = nil
59  end
60
61  def enable_tls(context)
62    raise ArgumentError, "SMTPS and STARTTLS is exclusive" if @@security && @@security != :enable_tls
63    @@security = :enable_tls
64    context
65  end
66
67  def enable_starttls(context = nil)
68    raise ArgumentError, "SMTPS and STARTTLS is exclusive" if @@security == :enable_tls
69    @@security = :enable_starttls
70    context
71  end
72
73  def enable_starttls_auto(context)
74    raise ArgumentError, "SMTPS and STARTTLS is exclusive" if @@security == :enable_tls
75    @@security = :enable_starttls_auto
76    context
77  end
78end
79
80class Net::SMTP
81  def self.new(*args)
82    MockSMTP.new
83  end
84end
85