maildis 0.0.1
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.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +2 -0
- data/Guardfile +24 -0
- data/LICENSE.txt +22 -0
- data/README.md +134 -0
- data/Rakefile +11 -0
- data/bin/maildis +5 -0
- data/lib/maildis/dispatcher.rb +43 -0
- data/lib/maildis/merge_field.rb +9 -0
- data/lib/maildis/multi_io.rb +15 -0
- data/lib/maildis/recipient.rb +22 -0
- data/lib/maildis/recipient_parser.rb +87 -0
- data/lib/maildis/sender.rb +17 -0
- data/lib/maildis/server_utils.rb +20 -0
- data/lib/maildis/smtp_server.rb +13 -0
- data/lib/maildis/template.rb +17 -0
- data/lib/maildis/template_loader.rb +18 -0
- data/lib/maildis/template_renderer.rb +20 -0
- data/lib/maildis/validation_error.rb +3 -0
- data/lib/maildis/validation_utils.rb +19 -0
- data/lib/maildis/validator.rb +50 -0
- data/lib/maildis/version.rb +3 -0
- data/lib/maildis.rb +118 -0
- data/maildis.gemspec +33 -0
- data/spec/lib/maildis/dispatcher_spec.rb +31 -0
- data/spec/lib/maildis/maildis_spec.rb +84 -0
- data/spec/lib/maildis/merge_field_spec.rb +15 -0
- data/spec/lib/maildis/recipient_parser_spec.rb +39 -0
- data/spec/lib/maildis/recipient_spec.rb +16 -0
- data/spec/lib/maildis/sender_spec.rb +15 -0
- data/spec/lib/maildis/smtp_server_spec.rb +17 -0
- data/spec/lib/maildis/template_loader_spec.rb +25 -0
- data/spec/lib/maildis/template_renderer_spec.rb +26 -0
- data/spec/lib/maildis/template_spec.rb +15 -0
- data/spec/lib/maildis/validator_spec.rb +132 -0
- data/spec/mailer/empty.csv +0 -0
- data/spec/mailer/invalid.csv +1 -0
- data/spec/mailer/mailer.yml +15 -0
- data/spec/mailer/mailer_invalid.yml +1 -0
- data/spec/mailer/recipients.csv +1 -0
- data/spec/mailer/recipients_empty.csv +1 -0
- data/spec/mailer/template.html +13 -0
- data/spec/mailer/template.txt +5 -0
- data/spec/spec_helper.rb +2 -0
- metadata +291 -0
data/lib/maildis.rb
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
require "thor"
|
2
|
+
require "logger"
|
3
|
+
require "yaml"
|
4
|
+
|
5
|
+
Dir[File.join(File.dirname(__FILE__), 'maildis', '*')].each { |file| require file }
|
6
|
+
|
7
|
+
module Maildis
|
8
|
+
class CLI < Thor
|
9
|
+
|
10
|
+
@@log_file_name = Dir.home + '/.maildis.log'
|
11
|
+
|
12
|
+
desc 'validate mailer', 'Validates mailer configuration file'
|
13
|
+
method_option :verbose, aliases: '-v', desc: 'Verbose', default: false
|
14
|
+
def validate(mailer_path)
|
15
|
+
init_logger options[:verbose]
|
16
|
+
begin
|
17
|
+
raise ValidationError, "File not found: #{mailer_path}" unless File.exists?(File.expand_path(mailer_path))
|
18
|
+
load_config mailer_path
|
19
|
+
$stdout.puts 'Ok'
|
20
|
+
exit
|
21
|
+
rescue ValidationError => e
|
22
|
+
error "Validation Error: #{e.message}"
|
23
|
+
rescue => e
|
24
|
+
fatal "Error: #{e.message}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
desc 'ping mailer', 'Attempts to connect to the SMTP server specified in the mailer configuration'
|
29
|
+
method_option :verbose, aliases: '-v', desc: 'Verbose', default: false
|
30
|
+
def ping(mailer_path)
|
31
|
+
init_logger options[:verbose]
|
32
|
+
begin
|
33
|
+
config = load_config mailer_path
|
34
|
+
$stdout.puts "SMTP server reachable" if ServerUtils.server_reachable? config['smtp']['host'], config['smtp']['port']
|
35
|
+
exit
|
36
|
+
rescue ValidationError => e
|
37
|
+
error "Validation Error: #{e.message}"
|
38
|
+
rescue => e
|
39
|
+
fatal "Error: #{e.message}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
desc 'dispatch mailer', 'Dispatches the mailer through the SMTP server specified in the mailer configuration.'
|
44
|
+
method_option :verbose, aliases: '-v', desc: 'Verbose', default: false
|
45
|
+
def dispatch(mailer_path)
|
46
|
+
init_logger options[:verbose]
|
47
|
+
begin
|
48
|
+
config = load_config mailer_path
|
49
|
+
subject = config['mailer']['subject']
|
50
|
+
recipients = load_recipients config['mailer']['recipients']
|
51
|
+
sender = load_sender config['mailer']['sender']
|
52
|
+
templates = load_templates config['mailer']['templates']
|
53
|
+
server = load_server config['smtp']
|
54
|
+
info "Dispatching to #{recipients.size} recipients"
|
55
|
+
result = Dispatcher.dispatch({subject: subject,
|
56
|
+
recipients: recipients,
|
57
|
+
sender: sender,
|
58
|
+
templates: templates,
|
59
|
+
server: server,
|
60
|
+
logger: @@logger})
|
61
|
+
info "Dispatch complete with errors" if result[:not_sent].size > 0
|
62
|
+
info "Dispatch complete without errors" if result[:not_sent].size == 0
|
63
|
+
exit
|
64
|
+
rescue => e
|
65
|
+
fatal e.message
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def init_logger(verbose)
|
72
|
+
if verbose
|
73
|
+
@@logger = Logger.new(MultiIO.new(STDOUT, File.open(@@log_file_name,'w+')))
|
74
|
+
else
|
75
|
+
@@logger = Logger.new(@@log_file_name)
|
76
|
+
end
|
77
|
+
@@logger.formatter = proc {|severity, datetime, progname, msg| "#{datetime} #{severity}: #{msg}\n"}
|
78
|
+
end
|
79
|
+
|
80
|
+
def info(msg); @@logger.info msg; end
|
81
|
+
def error(msg); @@logger.error msg; abort msg; end
|
82
|
+
def fatal(msg); @@logger.fatal msg; abort msg; end
|
83
|
+
|
84
|
+
def load_config(mailer_path)
|
85
|
+
info "Load #{File.expand_path(mailer_path)}"
|
86
|
+
config = YAML.load(File.open(File.expand_path(mailer_path)))
|
87
|
+
info "Validate configuration"
|
88
|
+
Validator.validate_config config
|
89
|
+
config
|
90
|
+
end
|
91
|
+
|
92
|
+
def load_recipients(hash)
|
93
|
+
info "Validate recipients csv"
|
94
|
+
Validator.validate_recipients hash
|
95
|
+
info "Extract recipients from csv"
|
96
|
+
RecipientParser.extract_recipients hash['csv']
|
97
|
+
end
|
98
|
+
|
99
|
+
def load_sender(hash)
|
100
|
+
info "Validate sender"
|
101
|
+
Validator.validate_sender hash
|
102
|
+
Sender.new hash['name'], hash['email']
|
103
|
+
end
|
104
|
+
|
105
|
+
def load_templates(hash)
|
106
|
+
info "Validate templates"
|
107
|
+
Validator.validate_templates hash
|
108
|
+
{html: TemplateLoader.load(hash['html']), plain: TemplateLoader.load(hash['plain'])}
|
109
|
+
end
|
110
|
+
|
111
|
+
def load_server(hash)
|
112
|
+
info "Validate SMTP server #{hash['host']}"
|
113
|
+
Validator.validate_smtp hash
|
114
|
+
SmtpServer.new hash['host'], hash['port'], hash['username'], hash['password']
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
end
|
data/maildis.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'maildis/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "maildis"
|
8
|
+
gem.version = Maildis::VERSION
|
9
|
+
gem.authors = ["Johnny Boursiquot"]
|
10
|
+
gem.email = ["jboursiquot@gmail.com"]
|
11
|
+
gem.description = %q{Maildis is a command line bulk email dispatching tool. It supports HTML and plain text templates and CSVs for recipients and merge fields.}
|
12
|
+
gem.summary = %q{Maildis is a command line bulk email dispatching tool. It supports HTML and plain text templates and CSVs for recipients and merge fields. It relies on SMTP information you provide through your own configuration file. Subject, sender, path to CSV and path to the templates are all configurable through YAML.}
|
13
|
+
gem.homepage = "https://github.com/jboursiquot/maildis"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_runtime_dependency 'thor'
|
21
|
+
gem.add_runtime_dependency 'pony'
|
22
|
+
|
23
|
+
gem.add_development_dependency 'rake'
|
24
|
+
gem.add_development_dependency 'rspec'
|
25
|
+
gem.add_development_dependency 'guard'
|
26
|
+
gem.add_development_dependency 'guard-rspec'
|
27
|
+
gem.add_development_dependency 'rb-fsevent','~> 0.9.1'
|
28
|
+
gem.add_development_dependency 'fivemat'
|
29
|
+
gem.add_development_dependency 'pry'
|
30
|
+
gem.add_development_dependency 'pry-debugger'
|
31
|
+
gem.add_development_dependency 'mailcatcher'
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe 'Maildis::Dispatcher' do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
@config = YAML.load(File.open('spec/mailer/mailer.yml'))
|
7
|
+
|
8
|
+
mailer = @config['mailer']
|
9
|
+
@subject = mailer['subject']
|
10
|
+
@recipients = [] << Maildis::RecipientParser.extract_recipients(mailer['recipients']['csv']).first
|
11
|
+
@sender = Maildis::Sender.new mailer['sender']['name'], mailer['sender']['email']
|
12
|
+
@templates = {html: Maildis::TemplateLoader.load(mailer['templates']['html']), plain: Maildis::TemplateLoader.load(mailer['templates']['plain'])}
|
13
|
+
|
14
|
+
smtp = @config['smtp']
|
15
|
+
@server = Maildis::SmtpServer.new smtp['host'], smtp['port'], smtp['username'], smtp['password']
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.dispatch' do
|
19
|
+
it 'should send emails based on the given subject, recipients, sender, templates and server' do
|
20
|
+
result = Maildis::Dispatcher.dispatch({ subject: @subject,
|
21
|
+
recipients: @recipients,
|
22
|
+
sender: @sender,
|
23
|
+
templates: @templates,
|
24
|
+
server: @server
|
25
|
+
})
|
26
|
+
result.should be_an_instance_of Hash
|
27
|
+
result[:sent].size.should_not == 0
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe 'Maildis' do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
@valid_mailer_path = 'spec/mailer/mailer.yml'
|
7
|
+
@invalid_mailer_path = 'spec/mailer/mailer_invalid.yml'
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'when invoked without any task' do
|
11
|
+
it 'should respond with a task list' do
|
12
|
+
output = `./bin/maildis`
|
13
|
+
output.should include 'validate'
|
14
|
+
output.should include 'ping'
|
15
|
+
output.should include 'dispatch'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when validating a mailer' do
|
20
|
+
|
21
|
+
describe '#validate' do
|
22
|
+
|
23
|
+
it "should respond with 'maildis validate requires at least 1 argument...' when no config file is passed in" do
|
24
|
+
output = `./bin/maildis validate 2>&1`
|
25
|
+
output.should include 'maildis validate requires at least 1 argument'
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should respond with 'File not found' if mailer config does not exist" do
|
29
|
+
output = `./bin/maildis validate no_file 2>&1`
|
30
|
+
output.should include 'File not found'
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should respond with a Validation Error when the mailer config passed in has a problem" do
|
34
|
+
output = `./bin/maildis validate #{@invalid_mailer_path} 2>&1`
|
35
|
+
output.should include 'Validation Error'
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should respond with 'Ok' when all validation checks pass" do
|
39
|
+
output = `./bin/maildis validate #{@valid_mailer_path} 2>&1`
|
40
|
+
output.should include 'Ok'
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when pinging the SMTP server specified in a mailer config' do
|
48
|
+
describe '#ping' do
|
49
|
+
it "should check the specified SMTP server is reachable" do
|
50
|
+
output = `./bin/maildis ping #{@valid_mailer_path} 2>&1`
|
51
|
+
output.should include 'SMTP server reachable'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'when dispatching a mailer' do
|
57
|
+
|
58
|
+
before(:all) do
|
59
|
+
@config = YAML.load(File.open(@valid_mailer_path))
|
60
|
+
|
61
|
+
mailer = @config['mailer']
|
62
|
+
@subject = mailer['subject']
|
63
|
+
@recipients = Maildis::RecipientParser.extract_recipients mailer['recipients']['csv']
|
64
|
+
@sender = Maildis::Sender.new mailer['sender']['name'], mailer['sender']['email']
|
65
|
+
@templates = {html: Maildis::TemplateLoader.load(mailer['templates']['html']), plain: Maildis::TemplateLoader.load(mailer['templates']['plain'])}
|
66
|
+
|
67
|
+
smtp = @config['smtp']
|
68
|
+
@server = Maildis::SmtpServer.new smtp['host'], smtp['port'], smtp['username'], smtp['password']
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#dispatch' do
|
72
|
+
it 'should send all emails through the designated SMTP server' do
|
73
|
+
result = Maildis::Dispatcher.dispatch({subject: @subject,
|
74
|
+
recipients: @recipients,
|
75
|
+
sender: @sender,
|
76
|
+
templates: @templates,
|
77
|
+
server: @server})
|
78
|
+
result.should be_an_instance_of Hash
|
79
|
+
result[:sent].size.should_not == 0
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe 'Maildis::MergeField' do
|
4
|
+
|
5
|
+
describe '#new' do
|
6
|
+
|
7
|
+
it 'should initialize with field and value' do
|
8
|
+
mf = Maildis::MergeField.new('field', 'value')
|
9
|
+
mf.field.should == 'field'
|
10
|
+
mf.value.should == 'value'
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe 'Maildis::RecipientParser' do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
@valid_csv_path = 'spec/mailer/recipients.csv'
|
7
|
+
@invalid_csv_path = 'spec/mailer/invalid.csv'
|
8
|
+
@empty_csv_path = 'spec/mailer/recipients_empty.csv'
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '.valid_csv?' do
|
12
|
+
it "should return true if CSV has the expected format" do
|
13
|
+
expect(Maildis::RecipientParser.valid_csv?(@valid_csv_path)).to be_true
|
14
|
+
end
|
15
|
+
it "should return false if CSV does not have the expected format" do
|
16
|
+
expect(Maildis::RecipientParser.valid_csv?(@invalid_csv_path)).to be_false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '.empty_csv?' do
|
21
|
+
it "should return true if CSV has the right format but is empty" do
|
22
|
+
expect(Maildis::RecipientParser.empty_csv?(@empty_csv_path)).to be_true
|
23
|
+
end
|
24
|
+
it "should return false if CSV has the right format and is not empty" do
|
25
|
+
expect(Maildis::RecipientParser.empty_csv?(@valid_csv_path)).to be_false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '.extract_recipients' do
|
30
|
+
it "should extract a list of recipients from the CSV" do
|
31
|
+
recipients = Maildis::RecipientParser.extract_recipients @valid_csv_path
|
32
|
+
recipients.size.should > 0
|
33
|
+
recipients.first.should be_an_instance_of Maildis::Recipient
|
34
|
+
recipients.first.merge_fields.should_not be_nil
|
35
|
+
recipients.first.merge_fields.should_not be_empty
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe 'Maildis::Recipient' do
|
4
|
+
|
5
|
+
describe '#new' do
|
6
|
+
|
7
|
+
it 'should initialize with name, email and merge fields' do
|
8
|
+
r = Maildis::Recipient.new('John Doe', 'jdoe@email.com', [Maildis::MergeField.new('field', 'value')])
|
9
|
+
r.name.should == 'John Doe'
|
10
|
+
r.email.should == 'jdoe@email.com'
|
11
|
+
r.merge_fields.size.should_not == 0
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe 'Maildis::Sender' do
|
4
|
+
|
5
|
+
describe '#new' do
|
6
|
+
|
7
|
+
it 'should initialize with name and email' do
|
8
|
+
r = Maildis::Sender.new('John Doe', 'jdoe@email.com')
|
9
|
+
r.name.should == 'John Doe'
|
10
|
+
r.email.should == 'jdoe@email.com'
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe 'Maildis::SmtpServer' do
|
4
|
+
|
5
|
+
describe '#new' do
|
6
|
+
|
7
|
+
it 'should initialize with host, port, username and password' do
|
8
|
+
s = Maildis::SmtpServer.new('localhost',1025,'username','password')
|
9
|
+
s.host.should == 'localhost'
|
10
|
+
s.port.should == 1025
|
11
|
+
s.username.should == 'username'
|
12
|
+
s.password.should == 'password'
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe 'Maildis::TemplateLoader' do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
@config = YAML.load(File.open('spec/mailer/mailer.yml'))
|
7
|
+
@html_template_path = @config['mailer']['templates']['html']
|
8
|
+
@plain_template_path = @config['mailer']['templates']['plain']
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '.load' do
|
12
|
+
|
13
|
+
it 'should load an html template given the template path' do
|
14
|
+
template = Maildis::TemplateLoader.load @html_template_path
|
15
|
+
template.type.should == Maildis::Template::HTML
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should load a plain text template given the template path' do
|
19
|
+
template = Maildis::TemplateLoader.load @plain_template_path
|
20
|
+
template.type.should == Maildis::Template::PLAIN
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe 'Maildis::TemplateRenderer' do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
@config = YAML.load(File.open('spec/mailer/mailer.yml'))
|
7
|
+
@html_template = @config['mailer']['templates']['html']
|
8
|
+
@plain_template = @config['mailer']['templates']['plain']
|
9
|
+
@merge_fields = [Maildis::MergeField.new('url','http://www.domain.com')]
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '.render' do
|
13
|
+
|
14
|
+
it 'should render an html template given the template and the merge fields' do
|
15
|
+
template = Maildis::TemplateLoader.load @html_template
|
16
|
+
expect(Maildis::TemplateRenderer.render template, @merge_fields).to include @merge_fields.first.value
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should render a plain text template given the template and the merge fields' do
|
20
|
+
template = Maildis::TemplateLoader.load @plain_template
|
21
|
+
expect(Maildis::TemplateRenderer.render template, @merge_fields).to include @merge_fields.first.value
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe 'Maildis::Template' do
|
4
|
+
|
5
|
+
describe '#new' do
|
6
|
+
|
7
|
+
it 'should initialize with type and content' do
|
8
|
+
t = Maildis::Template.new(Maildis::Template::HTML, '<html/>')
|
9
|
+
t.type.should == Maildis::Template::HTML
|
10
|
+
t.content.should == '<html/>'
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe 'Maildis::Validator' do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
@config = YAML.load(File.open('spec/mailer/mailer.yml'))
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '.validate_config' do
|
10
|
+
|
11
|
+
it 'should raise ValidationError when config does not have smtp settings' do
|
12
|
+
expect{Maildis::Validator.validate_config({'mailer' => {}})}.to raise_error Maildis::ValidationError, 'smtp settings missing'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should raise ValidationError when config does not have mailer settings' do
|
16
|
+
expect{Maildis::Validator.validate_config({'smtp' => {}})}.to raise_error Maildis::ValidationError, 'mailer settings missing'
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should return nil when no ValidationError is raised' do
|
20
|
+
expect(Maildis::Validator.validate_config @config).to be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '.validate_smtp' do
|
26
|
+
|
27
|
+
context 'when given missing or invalid parameters' do
|
28
|
+
|
29
|
+
it 'should raise ValidationError if smtp host is missing or invalid' do
|
30
|
+
smtp = {'port'=>1025, 'username'=>'', 'password'=>''}
|
31
|
+
expect{Maildis::Validator.validate_smtp smtp}.to raise_error Maildis::ValidationError, 'smtp::host missing or invalid'
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should raise ValidationError if smtp port is missing or invalid' do
|
35
|
+
smtp = {'host'=>'localhost', 'username'=>'', 'password'=>''}
|
36
|
+
expect{Maildis::Validator.validate_smtp smtp}.to raise_error Maildis::ValidationError, 'smtp::port missing or invalid'
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should raise ValidationError if smtp username is missing' do
|
40
|
+
smtp = {'host'=>'localhost', 'port'=>1025, 'password'=>''}
|
41
|
+
expect{Maildis::Validator.validate_smtp smtp}.to raise_error Maildis::ValidationError, 'smtp::username missing'
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should raise ValidationError if smtp password is missing' do
|
45
|
+
smtp = {'host'=>'localhost', 'port'=>1025, 'username'=>''}
|
46
|
+
expect{Maildis::Validator.validate_smtp smtp}.to raise_error Maildis::ValidationError, 'smtp::password missing'
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'when given valid parameters' do
|
52
|
+
it 'should not raise any ValidationError if smtp settings contain all required paramters' do
|
53
|
+
expect(Maildis::Validator.validate_config @config).to be_nil
|
54
|
+
expect(Maildis::Validator.validate_smtp @config['smtp']).to be_nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '.validate_sender' do
|
61
|
+
it 'should raise ValidationError if sender name is missing' do
|
62
|
+
sender = {'email'=>''}
|
63
|
+
expect{Maildis::Validator.validate_sender sender}.to raise_error Maildis::ValidationError, 'sender::name missing'
|
64
|
+
end
|
65
|
+
it 'should verify sender email is missing or invalid' do
|
66
|
+
sender = {'name'=>'', 'email'=>''}
|
67
|
+
expect{Maildis::Validator.validate_sender sender}.to raise_error Maildis::ValidationError, 'sender::email missing or invalid'
|
68
|
+
end
|
69
|
+
it 'should not raise any ValidationError if sender settings contains all required parameters' do
|
70
|
+
expect(Maildis::Validator.validate_sender @config['mailer']['sender']).to be_nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe '.validate_subject' do
|
75
|
+
it 'should raise ValidationError if subject is blank' do
|
76
|
+
expect {Maildis::Validator.validate_subject ''}.to raise_error Maildis::ValidationError, 'mailer::subject invalid'
|
77
|
+
end
|
78
|
+
it 'should raise ValidationError if subject is nil' do
|
79
|
+
expect {Maildis::Validator.validate_subject nil}.to raise_error Maildis::ValidationError, 'mailer::subject invalid'
|
80
|
+
end
|
81
|
+
it 'should not raise any ValidationError if subject is valid' do
|
82
|
+
expect(Maildis::Validator.validate_subject @config['mailer']['subject']).to be_nil
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe '.validate_recipients' do
|
87
|
+
|
88
|
+
it 'should raise ValidationError if csv is missing' do
|
89
|
+
expect {Maildis::Validator.validate_recipients Hash.new}.to raise_error Maildis::ValidationError, 'recipients::csv missing'
|
90
|
+
end
|
91
|
+
it 'should raise ValidationError if csv is invalid file' do
|
92
|
+
recipients = {'csv'=>'invalid filename'}
|
93
|
+
expect {Maildis::Validator.validate_recipients recipients}.to raise_error Maildis::ValidationError, 'recipients::csv invalid file path'
|
94
|
+
end
|
95
|
+
it 'should raise ValidationError if csv does not have minimum expected columns (name, email)' do
|
96
|
+
recipients = {'csv'=>'spec/mailer/invalid.csv'}
|
97
|
+
expect {Maildis::Validator.validate_recipients recipients}.to raise_error Maildis::ValidationError, 'recipients::csv invalid column headers'
|
98
|
+
end
|
99
|
+
it 'should not raise ValidationError if csv has minimum expected columns (name, email)' do
|
100
|
+
recipients = {'csv'=>'spec/mailer/recipients.csv'}
|
101
|
+
expect(Maildis::Validator.validate_recipients recipients).to be_nil
|
102
|
+
end
|
103
|
+
it 'should raise ValidationError if csv has no recipients' do
|
104
|
+
recipients = {'csv'=>'spec/mailer/recipients_empty.csv'}
|
105
|
+
expect {Maildis::Validator.validate_recipients recipients}.to raise_error Maildis::ValidationError, 'recipients::csv empty'
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe '.validate_templates' do
|
110
|
+
it 'should raise ValidationError if html template parameter is not specified' do
|
111
|
+
templates = {'plain'=>''}
|
112
|
+
expect {Maildis::Validator.validate_templates templates}.to raise_error Maildis::ValidationError, 'templates::html not specified'
|
113
|
+
end
|
114
|
+
it 'should raise ValidationError if plain template parameter is not specified' do
|
115
|
+
templates = {'html'=>''}
|
116
|
+
expect {Maildis::Validator.validate_templates templates}.to raise_error Maildis::ValidationError, 'templates::plain not specified'
|
117
|
+
end
|
118
|
+
it 'should raise ValidationError if html template is not found' do
|
119
|
+
templates = {'html'=>'bad_file_path', 'plain'=>'spec/mailer/template.txt'}
|
120
|
+
expect {Maildis::Validator.validate_templates templates}.to raise_error Maildis::ValidationError, 'templates::html not found'
|
121
|
+
end
|
122
|
+
it 'should raise ValidationError if plain template is not found' do
|
123
|
+
templates = {'html'=>'spec/mailer/template.html', 'plain'=>'bad_file_path'}
|
124
|
+
expect {Maildis::Validator.validate_templates templates}.to raise_error Maildis::ValidationError, 'templates::plain not found'
|
125
|
+
end
|
126
|
+
it 'should not raise ValidationError if html and plain templates are valid' do
|
127
|
+
templates = {'html'=>'spec/mailer/template.html', 'plain'=>'spec/mailer/template.txt'}
|
128
|
+
expect(Maildis::Validator.validate_templates templates).to be_nil
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
name_,email_,url_
|
@@ -0,0 +1,15 @@
|
|
1
|
+
mailer:
|
2
|
+
subject: "Test Mailer"
|
3
|
+
sender:
|
4
|
+
name: "Developer"
|
5
|
+
email: "developer@maark.com"
|
6
|
+
recipients:
|
7
|
+
csv: "spec/mailer/recipients.csv"
|
8
|
+
templates:
|
9
|
+
html: "spec/mailer/template.html"
|
10
|
+
plain: "spec/mailer/template.txt"
|
11
|
+
smtp:
|
12
|
+
host: "localhost"
|
13
|
+
port: 1025
|
14
|
+
username:
|
15
|
+
password:
|
@@ -0,0 +1 @@
|
|
1
|
+
invalid:
|
@@ -0,0 +1 @@
|
|
1
|
+
name,email,url
|
@@ -0,0 +1 @@
|
|
1
|
+
name,email,url
|
@@ -0,0 +1,13 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
3
|
+
<html>
|
4
|
+
<head>
|
5
|
+
<title>Test Mailer</title>
|
6
|
+
<style></style>
|
7
|
+
</head>
|
8
|
+
<body>
|
9
|
+
<p>This is a test mailer</p>
|
10
|
+
<p>The following merge field should be replaced with an actual URL: <a href="%url%">%url%</a></p>
|
11
|
+
<p>Organization | Address | Unsubscribe Link</p>
|
12
|
+
</body>
|
13
|
+
</html>
|
data/spec/spec_helper.rb
ADDED