mail_runner 0.1.0

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.
@@ -0,0 +1,158 @@
1
+ require_relative 'helper'
2
+
3
+ class TestBotHelpers < Minitest::Test
4
+
5
+ ####### BotHelpers::Tests ###########
6
+ describe "test BotHelpers:: Tests" do
7
+ before do
8
+ @opts ={
9
+ :mailbox => "/var/mail/root", #app
10
+ :webhook => "http://127.0.0.1:4000/talkpost"
11
+ }
12
+ @test = BotHelpers::Tests
13
+ end
14
+
15
+ #all_args?
16
+ it "Error - .all_args_included? if arguments less than 2" do
17
+ @opts.shift
18
+ assert_raises(ArgumentError) {@test.all_args_included?(@opts)}
19
+ end
20
+
21
+ #Create test for other arguments like daemonize, log, etc
22
+
23
+ it "Passes - .all_args_included? if 2 or 3 arguments." do
24
+ assert_silent {@test.all_args_included?(@opts)}
25
+ @opts[:archive]=true
26
+ assert_silent {@test.all_args_included?(@opts)}
27
+ end
28
+
29
+ #test_mailbox
30
+ it "error - .test_mailbox if doesn't exist" do
31
+ assert_raises(ArgumentError) {@test.test_mailbox("no_mailbox")}
32
+ end
33
+
34
+ it "Passes - .test_mailbox if exists" do
35
+ assert_silent {@test.test_mailbox(@opts[:mailbox])}
36
+ end
37
+
38
+ #test_webhook
39
+ it "Passes - .test_webook if returns 200" do
40
+ assert_silent {@test.test_webhook(@opts[:webhook])}
41
+ end
42
+
43
+ it "fails - .test_webook if returns not 200" do
44
+ assert_raises(ArgumentError) {@test.test_webhook("http://127.0.0.1:4000/faulty_hook")}
45
+ end
46
+ end
47
+ ########################################
48
+
49
+
50
+ ####### BotHelpers::Runner ###########
51
+ describe "test BotHelpers:: Runner" do
52
+ context "method : get_contents_from_email" do
53
+ before do
54
+ @mailbox = "/var/mail/talkpost" #app
55
+ @webhook = "http://127.0.0.1:4000/talkpost"
56
+ @runner = BotHelpers::Runner
57
+ end
58
+
59
+ #get_contents_from_email
60
+ context "when there is mail" do
61
+
62
+ it "returns an object of type mail & empties mailbox after reading" do
63
+
64
+ #insert test content into mailbox for test.
65
+ `cp #{Dir[File.dirname(__FILE__)][0] + "/test_assets/test.email"} /var/mail/talkpost`
66
+
67
+ object_returned = @runner.get_contents_from_mailbox(@mailbox)
68
+ assert_instance_of Array, object_returned
69
+ assert_equal 1, object_returned.length #confirms scrubs first empty item in array.
70
+ assert_equal true, File.zero?(@mailbox)
71
+ end
72
+ end
73
+
74
+ context "when there is no mail" do
75
+ it "does nothing if mailbox empty" do
76
+ assert_silent {@runner.get_contents_from_mailbox(@mailbox)}
77
+ end
78
+ end
79
+
80
+ end
81
+ context "method : Post_to_hook" do
82
+ before do
83
+ @webhook = "http://127.0.0.1:4000/talkpost"
84
+ @runner = BotHelpers::Runner
85
+ end
86
+ it "successfully posts if packet has contents to webhook" do
87
+ parcel = File.read(("#{Dir[File.dirname(__FILE__)][0] + '/test_assets/test.json'}"))
88
+ assert_equal 200, @runner.post_to_hook(@webhook, parcel).code
89
+ end
90
+
91
+ it "Returns an error if server down or response not 200" do
92
+ @faulty_webhook = "http://127.0.0.1:4000/faulty_hook"
93
+ parcel = File.read(("#{Dir[File.dirname(__FILE__)][0] + '/test_assets/empty.txt'}"))
94
+ assert_raises(ArgumentError) {@runner.post_to_hook(@faulty_webhook, parcel)}
95
+ end
96
+ end
97
+
98
+ context "method: add mail to queue" do
99
+ it "do we really need to test this one?" do
100
+ end
101
+ end
102
+
103
+
104
+ end
105
+
106
+ ########################################
107
+
108
+
109
+ ####### BotHelpers::Helpers ###########
110
+ describe "test BotHelpers:: Helpers" do
111
+ before do
112
+ @helper = BotHelpers::Helpers
113
+ end
114
+
115
+ context "method : convert_raw_mail_to_json" do
116
+ before do
117
+ @mail = Mail.read("#{Dir[File.dirname(__FILE__)][0] + "/test_assets/test.email"}")
118
+ end
119
+
120
+ it "returns valid json " do
121
+ def valid_json?(j)
122
+ JSON.parse(j)
123
+ return true
124
+ rescue JSON::ParserError
125
+ return false
126
+ end
127
+
128
+ assert_equal true, valid_json?(@helper.convert_raw_mail_to_json(@mail))
129
+ end
130
+ end
131
+
132
+ context "method : parse_attachments" do
133
+ before do
134
+ @mail = Mail.read("#{Dir[File.dirname(__FILE__)][0] + "/test_assets/with_attachments.email"}")
135
+ @inline_mail = Mail.read("#{Dir[File.dirname(__FILE__)][0] + "/test_assets/inline_attachments.email"}")
136
+ end
137
+
138
+ it "returns a hash of 2 arrays" do
139
+ assert_instance_of Hash, @helper.parse_attachments(@mail.attachments)
140
+ assert_equal 2, @helper.parse_attachments(@mail.attachments).size
141
+ end
142
+
143
+ it "returns content in i_array if inline attachment" do
144
+ assert_equal 1, @helper.parse_attachments(@inline_mail.attachments)[:i_array].size
145
+ assert_equal 0, @helper.parse_attachments(@inline_mail.attachments)[:a_array].size
146
+ end
147
+
148
+ it "returns conent in a_array if not inline attachment" do
149
+ assert_equal 0, @helper.parse_attachments(@mail.attachments)[:i_array].size
150
+ assert_equal 1, @helper.parse_attachments(@mail.attachments)[:a_array].size
151
+ end
152
+ end
153
+
154
+
155
+ end
156
+ ########################################
157
+
158
+ end
data/test/test_cli.rb ADDED
@@ -0,0 +1,139 @@
1
+ require_relative 'helper'
2
+
3
+ class TestCLI< Minitest::Test
4
+ describe "test #parse_options" do
5
+ before do
6
+ @cli = MailRunner::CLI
7
+ end
8
+
9
+ it "returns a hash object when valid args entered" do
10
+ assert_instance_of Hash, @cli.parse_options(["-m", "talkpost", "-w", "http://127.0.0.1:4000/talkpost"])
11
+ assert_equal 2, @cli.parse_options(["-m", "talkpost", "-w", "http://127.0.0.1:4000/talkpost"]).size
12
+ end
13
+
14
+ it "Seems a worthless test to me..." do
15
+ @cli.stub :daemonize, true do
16
+ assert_equal true, @cli.daemonize
17
+ end
18
+ end
19
+ end
20
+ describe "demonization upon start" do
21
+
22
+ #create some set up where it makes sure server is down and test -d flag
23
+ it "does not daemonize if the mailbox or webhook is invalid" do
24
+
25
+ end
26
+ end
27
+
28
+ describe 'with logfile' do
29
+ before do
30
+ @cli = MailRunner::CLI
31
+ @tmp_log_path = '/tmp/mailrunner.log'
32
+ @options = @cli.parse_options(['mailrunner', '-L', @tmp_log_path])
33
+ @logger = Logger.new(@options[:logfile]) #Bypass logger in helper file so can create new one with tmp path
34
+ end
35
+ after do
36
+ File.unlink @tmp_log_path if File.exist?(@tmp_log_path)
37
+ end
38
+
39
+ it 'sets the logfile path' do
40
+ assert_equal @tmp_log_path, @options[:logfile]
41
+ end
42
+
43
+ it 'creates and writes to a logfile' do
44
+ @logger.info('test message')
45
+ assert_match(/test message/, File.read(@tmp_log_path), "didn't include the log message")
46
+ end
47
+
48
+ it 'appends messages to a logfile' do
49
+ File.open(@tmp_log_path, 'w') do |f|
50
+ f.puts 'Existing log message'
51
+ end
52
+ @logger.info('test message')
53
+
54
+ log_file_content = File.read(@tmp_log_path)
55
+ assert_match(/Existing log message/, log_file_content, "didn't include the old message")
56
+ assert_match(/test message/, log_file_content, "didn't include the new message")
57
+ end
58
+ end
59
+
60
+ describe 'launch with config file' do
61
+ before do
62
+ @cli = MailRunner::CLI
63
+ @options = @cli.parse_options(['mailrunner', '-c', './test/test_assets/test_config.yml'])
64
+ end
65
+
66
+ it 'takes a config path' do
67
+ assert_equal './test/test_assets/test_config.yml', @options[:config]
68
+ end
69
+
70
+ it 'sets mailbox' do
71
+ assert_equal 'test_mailbox', @options[:mailbox]
72
+ end
73
+
74
+ it 'sets webhook' do
75
+ assert_equal 'localhost:4000/test_hook', @options[:webhook]
76
+ end
77
+
78
+ it 'sets daemon false by default' do
79
+ assert_equal false, @options[:daemon]
80
+ end
81
+
82
+ it 'sets archive' do
83
+ refute_nil @options[:archive]
84
+ end
85
+ #Add subsections once archive complete
86
+
87
+ it 'Does not set verbose' do
88
+ refute @options[:verbose]
89
+ end
90
+
91
+ it 'sets logfile' do
92
+ assert_equal '/home/user/mailrunner.log', @options[:logfile]
93
+ end
94
+ end
95
+
96
+ describe "individual flags override config file" do
97
+ before do
98
+ @cli = MailRunner::CLI
99
+ @options = @cli.parse_options(['mailrunner',
100
+ '-m', 'flag_mailbox',
101
+ '-w', 'localhost:4000/flag_hook',
102
+ '-d',
103
+ '-a', true,
104
+ '-L', '/home/user/flag.log',
105
+ '-c', './test/test_assets/test_config.yml',
106
+ '-v', true])
107
+ end
108
+
109
+ it ' takes the config path' do
110
+ assert_equal './test/test_assets/test_config.yml', @options[:config]
111
+ end
112
+
113
+ it 'uses flag mailbox' do
114
+ assert_equal 'flag_mailbox', @options[:mailbox]
115
+ end
116
+
117
+ it 'uses flag webhook' do
118
+ assert_equal 'localhost:4000/flag_hook', @options[:webhook]
119
+ end
120
+
121
+ it 'uses flag daemon setting' do
122
+ assert_equal true, @options[:daemon]
123
+ end
124
+
125
+ it 'uses flag archive' do
126
+ assert_equal true, @options[:archive]
127
+ end
128
+ #Add subsections once archive complete
129
+
130
+ it 'uses flag verbose' do
131
+ assert_equal true, @options[:verbose]
132
+ end
133
+
134
+ it 'uses flag logfile' do
135
+ assert_equal '/home/user/flag.log', @options[:logfile]
136
+ end
137
+ end
138
+
139
+ end
@@ -0,0 +1,70 @@
1
+ require_relative 'helper'
2
+
3
+ class TestInboundManager < Minitest::Test
4
+
5
+ #describe "Test Process Inbound Method" do
6
+
7
+ #ADD AND FIND EXCEPTION! e.g. what happens if can convert to json? is mail lost?
8
+ #end
9
+
10
+ describe "Inbound Manager::get_mail" do
11
+ before do
12
+ @inboundManager = MailRunner::InboundManagerBot
13
+ @mailbox = "/var/mail/talkpost" #app
14
+ @webhook = "http://127.0.0.1:4000/talkpost"
15
+ end
16
+ context "when there is mail" do
17
+
18
+ it "returns an object of type mail & empties mailbox after reading" do
19
+
20
+ #insert test content into mailbox for test.
21
+ `cp #{Dir[File.dirname(__FILE__)][0] + "/test_assets/test.email"} /var/mail/talkpost`
22
+
23
+ object_returned = @inboundManager.get_mail(@mailbox)
24
+ assert_instance_of Array, object_returned
25
+ assert_equal 1, object_returned.length #confirms scrubs first empty item in array.
26
+ assert_equal true, File.zero?(@mailbox)
27
+ end
28
+ end
29
+
30
+ context "when there is no mail" do
31
+ it "does nothing if mailbox empty" do
32
+ assert_silent {@inboundManager.get_mail(@mailbox)}
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "Inbound Manager::read_mail" do
38
+ before do
39
+ @inboundManager = MailRunner::InboundManagerBot
40
+ @raw_msg = File.read("#{Dir[File.dirname(__FILE__)][0] + "/test_assets/test.email"}")
41
+ end
42
+ it "takes raw message & returns an object of type mail" do
43
+ assert_instance_of Mail::Message, @inboundManager.read_mail(@raw_msg)
44
+ end
45
+ end
46
+
47
+ describe "Inbound Manager::deliver_mail" do
48
+ before do
49
+ @inboundManager = MailRunner::InboundManagerBot
50
+ @webhook = "http://127.0.0.1:4000/talkpost"
51
+ end
52
+ context "When webhook is valid" do
53
+ it "successfully delivers mail " do
54
+ parcel = File.read(("#{Dir[File.dirname(__FILE__)][0] + '/test_assets/test.json'}"))
55
+ assert_equal 200, @inboundManager.deliver_mail(@webhook, parcel).code
56
+ end
57
+ end
58
+
59
+ context "when webhook is invalid" do
60
+ it "Returns true = added to queue" do
61
+ @faulty_webhook = "http://127.0.0.1:4000/faulty_hook"
62
+ @dead_webhook = "http://127.0.0.1:4000/faulty_hook"
63
+ parcel = File.read(("#{Dir[File.dirname(__FILE__)][0] + '/test_assets/empty.txt'}"))
64
+ #Below
65
+ assert_equal true, @inboundManager.deliver_mail(@faulty_webhook, parcel)
66
+ assert_equal true, @inboundManager.deliver_mail(@dead_webhook, parcel)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,59 @@
1
+ #####################
2
+ # Several tests require a live server behind the test webhook.
3
+ # will cause sveral fails if not. Must be Post method.
4
+
5
+
6
+
7
+ ######################
8
+ require_relative 'helper'
9
+
10
+ class TestMailRunnerManager < Minitest::Test
11
+
12
+ ####### Top Level Call Tests ##########
13
+ describe 'MailRunner Manager initialize' do
14
+ before do
15
+ @bot = MailRunner.initialize_manager_bot
16
+ end
17
+
18
+ it "MailRunner Manager & atributes exist" do
19
+ assert_instance_of MailRunner::ManagerBot, @bot
20
+ assert_equal nil, @bot.mailbox
21
+ assert_equal nil, @bot.webhook
22
+ assert_equal false, @bot.archive
23
+ end
24
+
25
+ it "includes BotHelpers" do
26
+ end
27
+ end
28
+
29
+ describe "Parse Options method" do
30
+ before do
31
+ @bot = MailRunner.initialize_manager_bot
32
+ @opts = {:mailbox => "box_name", :webhook => "webhook/path"}
33
+ end
34
+
35
+ it "assigns passed arguments as getter bot attributes" do
36
+ @bot.parse_options(@opts)
37
+ assert_equal "/var/mail/box_name", @bot.mailbox
38
+ assert_equal "webhook/path", @bot.webhook
39
+ assert_equal false, @bot.archive
40
+ end
41
+
42
+ it "archive = true if passed archive argument" do
43
+ @opts[:archive] = "true"
44
+ @bot.parse_options(@opts)
45
+ assert_equal true, @bot.archive
46
+ end
47
+ end
48
+
49
+ describe "Run Method" do
50
+ before do
51
+ @bot = MailRunner.initialize_manager_bot
52
+ @bot.mailbox = "/var/mail/root" #app
53
+ @bot.webhook = "http://127.0.0.1:4000/talkpost"
54
+ end
55
+
56
+ end
57
+ ########################################
58
+
59
+ end
@@ -0,0 +1,76 @@
1
+ require_relative 'helper'
2
+
3
+
4
+ class TestQueueManager < Minitest::Test
5
+
6
+ describe "Queue Manager::add_mail_to_queue" do
7
+ before do
8
+ $redis.del("mail_room") # make sure starting with empty list
9
+ @queueManager = MailRunner::QueueManagerBot
10
+ @parcel = File.read(("#{Dir[File.dirname(__FILE__)][0] + '/test_assets/empty.txt'}"))
11
+ @webhook = "http://127.0.0.1:4000/talkpost"
12
+
13
+ end
14
+ it 'successfully adds web hook & parcel to redis queue' do
15
+ assert_equal true, @queueManager.add_to_mail_queue(@webhook, @parcel)
16
+ assert_equal 1, $redis.llen("mail_room")
17
+ end
18
+
19
+ #it "preserves the content ty"
20
+ end
21
+
22
+ describe "Queue Manager::pop_packet_from_queue" do
23
+ before do
24
+ $redis.del("mail_room") # make sure starting with empty list
25
+ @queueManager = MailRunner::QueueManagerBot
26
+
27
+ webhook = "http://127.0.0.1:4000/talkpost"
28
+ mail = Mail.read("#{Dir[File.dirname(__FILE__)][0] + "/test_assets/test.email"}")
29
+ @helper = BotHelpers::Helpers
30
+ que_packet = [webhook, @helper.convert_raw_mail_to_json(mail)]
31
+
32
+ $redis.lpush("mail_room", que_packet.to_json)
33
+ end
34
+
35
+
36
+ it "returns an array with the hook & json email packet" do
37
+ def valid_json? json_
38
+ JSON.parse(json_)
39
+ return true
40
+ rescue JSON::ParserError
41
+ return false
42
+ end
43
+
44
+ returned_data = @queueManager.pop_packet_from_queue
45
+ assert_equal 2, returned_data.size
46
+ assert_equal "http://127.0.0.1:4000/talkpost", returned_data[0]
47
+ assert_equal true, valid_json?(returned_data[1])
48
+ end
49
+
50
+ end
51
+
52
+ describe "Queue Manager::deliver_mail" do
53
+ before do
54
+ @queueManager = MailRunner::QueueManagerBot
55
+ @webhook = "http://127.0.0.1:4000/talkpost"
56
+ end
57
+ context "When webhook is valid" do
58
+ it "successfully delivers mail " do
59
+ parcel = File.read(("#{Dir[File.dirname(__FILE__)][0] + '/test_assets/test.json'}"))
60
+ assert_equal 200, @queueManager.deliver_mail(@webhook, parcel).code
61
+ end
62
+ end
63
+
64
+ context "when webhook is invalid" do
65
+ it "Returns true = added to queue" do
66
+ @faulty_webhook = "http://127.0.0.1:4000/faulty_hook"
67
+ @dead_webhook = "http://127.0.0.1:4000/faulty_hook"
68
+ parcel = File.read(("#{Dir[File.dirname(__FILE__)][0] + '/test_assets/empty.txt'}"))
69
+ #Below
70
+ assert_raises(ArgumentError) {@queueManager.deliver_mail(@faulty_webhook, parcel)}
71
+ assert_raises(ArgumentError) {@queueManager.deliver_mail(@dead_webhook, parcel)}
72
+ end
73
+ end
74
+ end
75
+
76
+ end