web47core 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -14
  3. data/Gemfile.lock +30 -9
  4. data/README.md +27 -4
  5. data/lib/app/models/concerns/app47_logger.rb +175 -0
  6. data/lib/app/models/concerns/core_account.rb +51 -0
  7. data/lib/app/models/concerns/standard_model.rb +104 -6
  8. data/lib/app/models/email_notification.rb +253 -0
  9. data/lib/app/models/email_template.rb +6 -0
  10. data/lib/app/models/notification.rb +276 -0
  11. data/lib/app/models/notification_template.rb +20 -0
  12. data/lib/app/models/slack_notification.rb +89 -0
  13. data/lib/app/models/sms_notification.rb +56 -0
  14. data/lib/app/models/smtp_configuration.rb +148 -0
  15. data/lib/app/models/template.rb +21 -0
  16. data/lib/templates/email/notification_failure.liquid +10 -0
  17. data/lib/templates/email/notification_failure.subject.liquid +1 -0
  18. data/lib/templates/slack/error_message.liquid +1 -0
  19. data/lib/web47core.rb +10 -2
  20. data/test/factories/account_factories.rb +9 -0
  21. data/test/factories/notification_factories.rb +14 -0
  22. data/test/models/app47_logger_test.rb +88 -0
  23. data/test/models/concerns/{formable_test.rb → standard_model_test.rb} +24 -5
  24. data/test/models/email_notification_test.rb +297 -0
  25. data/test/models/notification_test.rb +127 -0
  26. data/test/models/slack_notification_test.rb +50 -0
  27. data/test/notification_test_helper.rb +146 -0
  28. data/test/rails_setup.rb +4 -0
  29. data/test/test_helper.rb +10 -4
  30. data/test/test_models_helper.rb +14 -0
  31. data/web47core.gemspec +5 -2
  32. metadata +87 -14
  33. data/lib/app/models/concerns/auto_clear_cache.rb +0 -34
  34. data/lib/app/models/concerns/formable.rb +0 -111
  35. data/test/models/concerns/auto_clear_cache_test.rb +0 -27
@@ -0,0 +1,148 @@
1
+ #
2
+ # SMTP Configuration for an account
3
+ #
4
+ class SmtpConfiguration
5
+ include StandardModel
6
+
7
+ field :email_address, type: String
8
+ field :reply_to_address, type: String
9
+ field :domain, type: String
10
+ field :port, type: Integer, default: 587
11
+ field :authentication_method, type: String
12
+ field :confirmation_token, type: String
13
+ field :confirmed, type: Boolean, default: false
14
+ field :verification_message, type: String
15
+ field :active, type: Boolean, default: false
16
+ field :ssl, type: Boolean, default: false
17
+ field :server_name, type: String
18
+ field :username, type: String
19
+ field :password, type: String
20
+ #
21
+ # Relationships
22
+ #
23
+ embedded_in :account
24
+ #
25
+ # Validations
26
+ validates :server_name, :username, :email_address, :port, presence: true
27
+ #
28
+ # Callbacks
29
+ #
30
+ before_save :update_token
31
+ #after_save :send_smtp_verification
32
+
33
+ #
34
+ # If we can use this SMTP configuration or not, it must be confirmed and
35
+ # activate. Otherwise we return false.
36
+ #
37
+ def use?
38
+ confirmed? && active?
39
+ end
40
+
41
+ def secure_fields
42
+ super + %i[password]
43
+ end
44
+
45
+ def validate_token(token)
46
+ valid = false
47
+ unless confirmation_token.nil?
48
+ if confirmation_token.eql? token
49
+ valid = true
50
+ unset(:confirmation_token)
51
+ unset(:verification_message)
52
+ end
53
+ end
54
+ set(confirmed: valid)
55
+ valid
56
+ end
57
+
58
+ #
59
+ # Update the token on save if we are active and the token is not already set.
60
+ def update_token
61
+ if active?
62
+ set(confirmation_token: Devise.friendly_token) if confirmation_token.nil?
63
+ set(confirmed: false)
64
+ set(verification_message: 'Sending SMTP verification email(s) to SMTP admins.')
65
+ else
66
+ set(verification_message: 'SMTP Configuration is not active, no verification email will be sent.')
67
+ end
68
+ end
69
+
70
+ def send_smtp_verification
71
+ if self.active?
72
+ sent_to= []
73
+ self.account.smtp_admins.each do |admin|
74
+ sent_to << admin.email if self.send_confirmation(admin)
75
+ end
76
+ if sent_to.empty?
77
+ set(verification_message: "No confirmations emails sent: (#{verification_message}).")
78
+ else
79
+ set(verification_message: "Confirmation emails sent to SMTP admins: #{sent_to.join(", ")}")
80
+ end
81
+ end
82
+ end
83
+
84
+ def send_confirmation(member)
85
+ sent = false
86
+ begin
87
+ mail = Mail.new
88
+ # Set the from line
89
+ mail.from = email_address
90
+
91
+ # Set the to address
92
+ mail.to = member.email
93
+
94
+ # Set the subject line
95
+ mail.subject = 'Verify SMTP configuration change'
96
+
97
+ # set the message body and send
98
+ html_message = build_message
99
+ mail.html_part do
100
+ content_type 'text/html; charset=UTF-8'
101
+ body html_message
102
+ end
103
+
104
+ # Setup the delivery method for this message only.
105
+ if Rails.env.test?
106
+ mail.delivery_method :test
107
+ else
108
+ config = { address: self.server_name,
109
+ port: self.port,
110
+ authentication: self.authentication_method.to_sym,
111
+ enable_starttls_auto: self.ssl.eql?(true) }
112
+ config[:domain] = self.domain unless self.domain.nil? or self.domain.empty?
113
+ config[:user_name] = self.username unless self.username.nil? or self.username.empty?
114
+ config[:password] = self.password unless self.password.nil? or self.password.empty?
115
+
116
+ mail.delivery_method :smtp, config
117
+ end
118
+
119
+ # Deliver it
120
+ mail.deliver
121
+ sent = true
122
+
123
+ rescue Exception=>e
124
+ App47::Logger.log_error "Unable to send SMTP confirmation email #{e.message}"
125
+ App47::Logger.log_error e.backtrace
126
+ set(verification_message: "Unable to send verification email to #{member.email}, error: #{e.message}")
127
+ end
128
+ sent
129
+ end
130
+
131
+ def build_message
132
+ template = File.read(Rails.root.join('app', 'assets', 'templates', 'email', "verify_smtp_configuration.liquid"))
133
+ engine = Liquid::Template.parse(template)
134
+ engine.render({
135
+ 'account_name' => account.name,
136
+ 'confirmation_url' => "#{SystemConfiguration.webui_url}/smtp_configuration/confirm_change?token=#{confirmation_token}"
137
+ })
138
+ end
139
+
140
+ def to_yaml_properties(wrap = true)
141
+ if wrap
142
+ self.class.new.to_yaml_properties(false)
143
+ else
144
+ super()
145
+ end
146
+ end
147
+
148
+ end
@@ -0,0 +1,21 @@
1
+ #
2
+ # Base template object for managing all communications templates
3
+ #
4
+ class Template
5
+ include StandardModel
6
+ #
7
+ # Fields
8
+ #
9
+ field :name, type: String
10
+ field :template, type: String
11
+ #
12
+ # Relationships
13
+ #
14
+ belongs_to :account, inverse_of: :templates
15
+ #
16
+ # Validations
17
+ #
18
+ validates :name, uniqueness: {scope: :account_id}
19
+ validates :name, presence: true
20
+ validates :template, presence: true
21
+ end
@@ -0,0 +1,10 @@
1
+ <html>
2
+ <meta content="text/html; charset=UTF-8"/>
3
+ <body style='margin:20px'>
4
+ <p>Failed to send the following notification due to the following error:</p>
5
+ <ul style='list-style-type:none; margin:25px 15px;'>
6
+ <li><b>Error:</b> {{ error_message }}</li>
7
+ <li><b>Notification:</b> {{ notification_url }}</li>
8
+ </ul>
9
+ </body>
10
+ </html>
@@ -0,0 +1 @@
1
+ Failed to send email
@@ -0,0 +1 @@
1
+ *ERROR:* `{{message}}`{% if exception != blank %} - `{{exception}}`{% endif %}
data/lib/web47core.rb CHANGED
@@ -1,5 +1,13 @@
1
- require 'app/models/concerns/auto_clear_cache'
1
+ require 'app/models/concerns/app47_logger'
2
2
  require 'app/models/concerns/cdn_url'
3
- require 'app/models/concerns/formable'
4
3
  require 'app/models/concerns/standard_model'
5
4
  require 'app/models/concerns/core_system_configuration'
5
+ require 'app/models/concerns/core_account'
6
+ require 'app/models/notification'
7
+ require 'app/models/template'
8
+ require 'app/models/notification_template'
9
+ require 'app/models/email_notification'
10
+ require 'app/models/email_template'
11
+ require 'app/models/slack_notification'
12
+ require 'app/models/smtp_configuration'
13
+ require 'app/models/sms_notification'
@@ -0,0 +1,9 @@
1
+ #
2
+ # Create basic accounts
3
+ #
4
+ FactoryBot.define do
5
+ # default account
6
+ factory :account, class: Account do
7
+ sequence(:name) { |n| "Account-#{n}" }
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ FactoryBot.define do
2
+
3
+ factory :notification, class: Notification do
4
+ to { "noone@app47.com" }
5
+ message { "None message" }
6
+ end
7
+
8
+ factory :email_notification, class: EmailNotification do
9
+ to { "noone@app47.com" }
10
+ sender { "someone@app47.com" }
11
+ subject { "here me roar!" }
12
+ message { "None message" }
13
+ end
14
+ end
@@ -0,0 +1,88 @@
1
+ require 'test_helper'
2
+
3
+ class App47LoggerTestCase < ActiveSupport::TestCase
4
+ include App47Logger
5
+ context 'as singleton' do
6
+ should 'log a debug message' do
7
+ App47Logger.expects(:log_message).once
8
+ App47Logger.log_debug('Foo')
9
+ end
10
+ should 'log a warn message with out exception' do
11
+ App47Logger.expects(:log_message).once
12
+ App47Logger.log_warn('Foo')
13
+ end
14
+ should 'log a warn message with exception' do
15
+ App47Logger.expects(:log_message).twice
16
+ App47Logger.log_warn('Foo', Exception.new)
17
+ end
18
+ should 'log a error message with out exception' do
19
+ App47Logger.expects(:log_message).once
20
+ App47Logger.log_error('Foo')
21
+ end
22
+ should 'log a error message with exception' do
23
+ App47Logger.expects(:log_message).twice
24
+ App47Logger.log_error('Foo', Exception.new)
25
+ end
26
+ end
27
+
28
+ context 'as class method' do
29
+ should 'log a debug message' do
30
+ App47Logger.expects(:log_message).once
31
+ log_debug('Foo')
32
+ end
33
+ should 'log a message' do
34
+ App47Logger.expects(:log_message).once
35
+ log_message(:debug, 'Foo')
36
+ end
37
+ should 'log a warn message with out exception' do
38
+ App47Logger.expects(:log_message).once
39
+ log_warn('Foo')
40
+ end
41
+ should 'log a warn message with exception' do
42
+ App47Logger.expects(:log_message).twice
43
+ log_warn('Foo', Exception.new)
44
+ end
45
+ should 'log a error message with out exception' do
46
+ App47Logger.expects(:log_message).once
47
+ SlackNotification.expects(:say)
48
+ log_error('Foo')
49
+ end
50
+ should 'log a error message with exception' do
51
+ App47Logger.expects(:log_message).twice
52
+ SlackNotification.expects(:say)
53
+ log_error('Foo', Exception.new)
54
+ end
55
+ end
56
+
57
+ context 'log message' do
58
+ should 'Log debug' do
59
+ App47Logger.expects(:puts).once
60
+ App47Logger.log_debug('Foo')
61
+ end
62
+ end
63
+
64
+ context 'as a controller' do
65
+ should 'log a controller' do
66
+ App47Logger.expects(:log_message).once
67
+ log_controller_error(nil)
68
+ end
69
+ should 'log a controller with exception' do
70
+ App47Logger.expects(:log_message).twice
71
+ log_controller_error(Exception.new('Message'))
72
+ end
73
+ end
74
+ #
75
+ # Support the testing of log_controller_error
76
+ #
77
+ def controller_name
78
+ 'tester'
79
+ end
80
+
81
+ def action_name
82
+ 'tester'
83
+ end
84
+
85
+ def params
86
+ {}
87
+ end
88
+ end
@@ -1,7 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
3
  # TODO: CMS add tests for clearing by account id as well as id
4
- class FormableTest < ActiveSupport::TestCase
4
+ class StandardModelTest < ActiveSupport::TestCase
5
5
 
6
6
  context 'allowed_params' do
7
7
  should 'return test form fields' do
@@ -52,11 +52,23 @@ class FormableTest < ActiveSupport::TestCase
52
52
  assert_equal 'abc123', @model.password
53
53
  end
54
54
  end
55
+
56
+ context 'Remove cache on save' do
57
+ setup do
58
+ @model = TestAutoClearCache.new
59
+ assert @model.save
60
+ @model.update_cache
61
+ end
62
+ should 'return the original url' do
63
+ assert Rails.cache.exist? @model.id.to_s
64
+ assert @model.save
65
+ refute Rails.cache.exist? @model.id.to_s
66
+ end
67
+ end
55
68
  end
56
69
 
57
70
  class TestForm
58
- include Mongoid::Document
59
- include Formable
71
+ include StandardModel
60
72
  field :name
61
73
  field :password
62
74
  has_many :test_inputs
@@ -67,8 +79,15 @@ class TestForm
67
79
  end
68
80
 
69
81
  class TestInput
70
- include Mongoid::Document
71
- include Formable
82
+ include StandardModel
72
83
  field :input_type
73
84
  belongs_to :test_form
74
85
  end
86
+
87
+ class TestAutoClearCache
88
+ include StandardModel
89
+
90
+ def update_cache(value = 'abc123')
91
+ Rails.cache.write id.to_s, value, expires_in: 60
92
+ end
93
+ end
@@ -0,0 +1,297 @@
1
+ require 'test_helper'
2
+ require 'notification_test_helper'
3
+
4
+ class EmailNotificationTest < ActiveSupport::TestCase
5
+
6
+ include Mail::Matchers
7
+ include NotificationTestHelper
8
+
9
+ setup do
10
+ @account = FactoryBot.create(:account)
11
+ assert_not_nil @account
12
+ assert @account.persisted?
13
+ end
14
+
15
+ context 'Create simple account notification' do
16
+ setup do
17
+ @email = EmailNotification.new
18
+ @email.account = @account
19
+ @email.to = 'a@app47.com'
20
+ @email.message = 'Now is the time for all men to come to the aid of their country'
21
+ @email.subject = 'Testing'
22
+ assert @email.save
23
+ assert @email.send_notification
24
+ end
25
+ should 'Have one email notification that is successfully sent' do
26
+ mails = Mail::TestMailer.deliveries
27
+ assert_equal 1, mails.count, 'Should only send one email'
28
+ mail = mails.first
29
+ assert_equal 'support@app47.com', mail.from.first, 'Should send from default mailer'
30
+ end
31
+
32
+ # MAM-8438 Hide the image better
33
+ should 'have the notifications url in the email to track it' do
34
+ mail_body = Mail::TestMailer.deliveries.first.all_parts.first.body
35
+ assert mail_body.include?("notifications/#{@email.id}/img"), mail_body.inspect
36
+ assert mail_body.include?("style='height:0;width:0;border:none;display:none;'"), mail_body.inspect
37
+ end
38
+ end
39
+ # context 'MAM-5971' do
40
+ # should 'have mailgun smtp header when mailgun configured' do
41
+ # email = EmailNotification.new
42
+ # email.account = @account
43
+ # email.to = 'a@app47.com'
44
+ # email.message = 'Now is the time for all men to come to the aid of their country'
45
+ # email.subject = 'Testing'
46
+ # assert email.save
47
+ # assert email.send_notification
48
+ # mail = Mail::TestMailer.deliveries.first
49
+ # assert_equal 'yes', mail.header['X-Mailgun-Native-Send'].value, mail.header.inspect
50
+ # end
51
+ # should 'NOT have mailgun smtp header when mailgun not configured' do
52
+ # config = SystemConfiguration.configuration
53
+ # config.mailgun_api_key = nil
54
+ # assert config.save
55
+ # email = EmailNotification.new
56
+ # email.account = @account
57
+ # email.to = 'a@app47.com'
58
+ # email.message = 'Now is the time for all men to come to the aid of their country'
59
+ # email.subject = 'Testing'
60
+ # assert email.save
61
+ # assert email.send_notification
62
+ # mail = Mail::TestMailer.deliveries.first
63
+ # assert_nil mail.header['X-Mailgun-Native-Send'], mail.header.inspect
64
+ # end
65
+ # end
66
+
67
+ context 'Create an account notification with an array of addresses' do
68
+ setup {
69
+ email = EmailNotification.new
70
+ email.account = @account
71
+ email.to = ['a@app47.com', 'b@app47.com', 'c@app47.com']
72
+ email.message = 'Now is the time for all men to come to the aid of their country'
73
+ email.subject = 'Testing'
74
+ assert email.save
75
+ assert email.send_notification
76
+ }
77
+ should 'send three emails' do
78
+ mails = Mail::TestMailer.deliveries
79
+ email = mails.first
80
+ assert email.to.include?('a@app47.com'), email.to.inspect
81
+ assert email.to.include?('b@app47.com'), email.to.inspect
82
+ assert email.to.include?('c@app47.com'), email.to.inspect
83
+ end
84
+ end
85
+
86
+ # context 'Create account notification with smtp configuration' do
87
+ # setup {
88
+ # smtp = @account.fetch_or_build_smtp_configuration
89
+ # smtp.email_address = "blue@red.com"
90
+ # smtp.active = true
91
+ # smtp.username = 'purple'
92
+ # smtp.server_name = 'lost.in.space'
93
+ # assert smtp.save!
94
+ # # Need to set the confirmation manually as it's reset every time the smtp
95
+ # # config is saved
96
+ # smtp.set(confirmed: true)
97
+ #
98
+ # email = EmailNotification.new
99
+ # email.account = @account
100
+ # email.to = 'a@app47.com'
101
+ # email.message = 'Now is the time for all men to come to the aid of their country'
102
+ # email.subject = 'Testing'
103
+ # assert email.save
104
+ # Mail::TestMailer.deliveries.clear
105
+ # assert email.deliver
106
+ # }
107
+ # should 'Have one email notification that is successfully sent' do
108
+ # mails = Mail::TestMailer.deliveries
109
+ # assert_equal 1, mails.count, "Should only send one email"
110
+ # mail = mails.first
111
+ # assert_equal "purple", mail.sender, "Should send from smtp settings"
112
+ # end
113
+ # end
114
+
115
+ # context 'Fail to send email via smtp ' do
116
+ # setup {
117
+ # # Kill the reporting of the error so it doesn't look like an error on the test run.
118
+ # EmailNotification.any_instance.expects(:log_error).at_least_once
119
+ # FactoryBot.create(:member_has_role_view, account: @account)
120
+ # @admin = FactoryBot.create(:member_has_role_admin, account: @account)
121
+ # @account.smtp_configuration = FactoryBot.build(:active_smtp_configuration)
122
+ # assert @account.save
123
+ # @account.fetch_or_build_smtp_configuration.set(confirmed: true)
124
+ # EmailNotification.destroy_all
125
+ # email = EmailNotification.new
126
+ # email.account = @account
127
+ # email.to = 'a@app47.com'
128
+ # email.subject = 'Fail' # This matches the exception below in the monkey patch
129
+ # email.message = "me"
130
+ # assert email.save
131
+ # assert email.deliver
132
+ # }
133
+ # should 'have an email to the admin that the notification failed' do
134
+ # mails = Mail::TestMailer.deliveries
135
+ # assert_equal 1, mails.count, "Email should be to admin though"
136
+ # mail = mails.first
137
+ # assert_equal @admin.email, mail.to.first, "Should send to the smtp admin"
138
+ # assert_equal "Failed to send email notification", mail.subject
139
+ #
140
+ # email = EmailNotification.excludes(account_id: nil).first
141
+ # assert_not_nil email
142
+ # assert email.state.eql? NotificationState::INVALID
143
+ # email = EmailNotification.where(account_id: nil).first
144
+ # assert_not_nil email
145
+ # assert email.state.eql? NotificationState::PROCESSED
146
+ # end
147
+ #
148
+ # end
149
+
150
+ context 'Set reply to and from addresses on email correctly' do
151
+ should 'No reply to and from based on system configuration' do
152
+ email = EmailNotification.new(account: @account)
153
+ email.to = 'a@app47.com'
154
+ email.from_template('notification_failure', test_values)
155
+ assert email.save
156
+ assert_nothing_raised { email.send_notification }
157
+
158
+ mails = Mail::TestMailer.deliveries
159
+ assert_equal 1, mails.count, "Should only send one email"
160
+ mail = mails.first
161
+
162
+ assert_equal SystemConfiguration.default_email, mail.from.first, "Should send from default mailer"
163
+ assert_nil mail.reply_to, "Reply to should be nil: #{mail.inspect}"
164
+ end
165
+
166
+ # should 'no reply to and from address from SMTP configuration' do
167
+ # @account.smtp_configuration = FactoryBot.build(:active_smtp_configuration)
168
+ # assert @account.save
169
+ # @account.smtp_configuration.set(confirmed: true)
170
+ # email = EmailNotification.new(account: @account)
171
+ # email.to = 'a@app47.com'
172
+ # email.from_template(AccountEmailTemplate::APP_STORE_USER_INVITE, test_values)
173
+ # assert email.save
174
+ # assert_nothing_raised { email.deliver }
175
+ #
176
+ # mails = Mail::TestMailer.deliveries
177
+ # assert_equal 1, mails.count, "Should only send one email"
178
+ # mail = mails.first
179
+ #
180
+ # assert_equal @account.smtp_configuration.email_address, mail.from.first, mail.inspect
181
+ # assert_nil mail.reply_to, "Reply to should be nil: #{mail.inspect}"
182
+ # end
183
+ # should 'no reply to and from address from system configuration because smtp is not confirmed' do
184
+ # @account.smtp_configuration = FactoryBot.build(:active_smtp_configuration)
185
+ # assert @account.save
186
+ # email = EmailNotification.new(account: @account)
187
+ # email.to = 'a@app47.com'
188
+ # email.from_template(AccountEmailTemplate::APP_STORE_USER_INVITE, test_values)
189
+ # assert email.save
190
+ # assert_nothing_raised { email.deliver }
191
+ #
192
+ # mails = Mail::TestMailer.deliveries
193
+ # assert_equal 1, mails.count, "Should only send one email"
194
+ # mail = mails.first
195
+ #
196
+ # assert_equal SystemConfiguration.default_email, mail.from.first, mail.inspect
197
+ # assert_nil mail.reply_to, "Reply to should be nil: #{mail.inspect}"
198
+ # end
199
+
200
+ end
201
+
202
+ def test_values
203
+ {
204
+ 'app_store_name' => 'Test app store name',
205
+ 'device_description' => 'Tom\'s iPad',
206
+ 'device_unique_identifier' => 'abc123def132afd1213afas',
207
+ 'device_model' => 'iPad 3',
208
+ 'app_name' => 'Test app name',
209
+ 'build_version' => '1.3',
210
+ 'profile_expiration_date' => 'July 31, 1968 20:45Z',
211
+ 'profile_name' => 'Test Ad Hoc Distro',
212
+ 'builds_url' => "#{SystemConfiguration.base_url}/builds",
213
+ 'show_branding' => true,
214
+ 'release_notes' => 'Fixed app bugs, software is perfect now!!',
215
+ 'invitation_message' => 'You are invited, join the fun!!',
216
+ 'registration_url' => "#{SystemConfiguration.base_url}/register",
217
+ 'invitation_url' => "#{SystemConfiguration.base_url}/invitation",
218
+ 'reset_password_url' => "#{SystemConfiguration.base_url}/reset_password",
219
+ 'require_authentication' => false,
220
+ 'passphrase' => 'letmein123',
221
+ 'passphrase_expiration' => '48 hours',
222
+ 'user_name' => 'Test User',
223
+ 'user_email' => 'test@abc.com',
224
+ 'app_names' => ['app1', 'app2', 'app3'],
225
+ 'user_url' => "#{SystemConfiguration.base_url}/users?search=test@abc.com"
226
+ }
227
+ end
228
+
229
+ # context 'email attachments' do
230
+ # should 'not have any attachments' do
231
+ # email = EmailNotification.new(account: @account)
232
+ # email.to = 'a@app47.com'
233
+ # email.from_template('notification_failure', test_values)
234
+ # assert email.save
235
+ # assert_nothing_raised { email.send_notification }
236
+ #
237
+ # assert_emails_attachment_count @account, 0
238
+ #
239
+ # mails = Mail::TestMailer.deliveries
240
+ # assert_equal 1, mails.count, 'Should only send one email'
241
+ # mail = mails.first
242
+ # assert_empty mail.attachments
243
+ # end
244
+ #
245
+ # should 'send one attachment' do
246
+ # email = EmailNotification.new(account: @account)
247
+ # email.to = 'a@app47.com'
248
+ # email.from_template('notification_failure', test_values)
249
+ # email.add_file 'test/etc/images/featured.jpg'
250
+ # assert email.save
251
+ # assert_nothing_raised { email.send_notification }
252
+ #
253
+ # assert_not_nil email.reload
254
+ # assert_equal Notification::STATE_PROCESSED, email.state
255
+ #
256
+ # assert_emails_attachment_count @account
257
+ #
258
+ # mails = Mail::TestMailer.deliveries
259
+ # assert_equal 1, mails.count, 'Should only send one email'
260
+ # mail = mails.first
261
+ # assert_equal 1, mail.attachments.count
262
+ # end
263
+ # should 'send two attachment' do
264
+ # email = EmailNotification.new(account: @account)
265
+ # email.to = 'a@app47.com'
266
+ # email.from_template('notification_failure', test_values)
267
+ # email.add_file 'test/etc/images/featured.jpg'
268
+ # email.add_file 'test/etc/images/featured.jpg'
269
+ # assert email.save
270
+ # assert_nothing_raised { email.send_notification }
271
+ #
272
+ # assert_not_nil email.reload
273
+ # assert_equal Notification::STATE_PROCESSED, email.state
274
+ #
275
+ # assert_emails_attachment_count @account, 2
276
+ #
277
+ # mails = Mail::TestMailer.deliveries
278
+ # assert_equal 1, mails.count, 'Should only send one email'
279
+ # mail = mails.first
280
+ # assert_equal 2, mail.attachments.count
281
+ # end
282
+ # end
283
+
284
+ end
285
+
286
+ # Override this method to thrown an exception if the subject is 'Fail'
287
+ module Mail
288
+ class TestMailer
289
+
290
+ def deliver!(mail)
291
+ throw Exception('Fail') if mail.subject.eql? 'Fail'
292
+ # check_delivery_params(mail)
293
+ Mail::TestMailer.deliveries << mail
294
+ end
295
+
296
+ end
297
+ end