gitlab-mail_room 0.0.4 → 0.0.10

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 (41) hide show
  1. checksums.yaml +4 -4
  2. data/.gitlab-ci.yml +14 -5
  3. data/.gitlab/issue_templates/Release.md +7 -0
  4. data/.ruby-version +1 -1
  5. data/.travis.yml +3 -2
  6. data/README.md +16 -1
  7. data/lib/mail_room.rb +2 -0
  8. data/lib/mail_room/cli.rb +2 -2
  9. data/lib/mail_room/configuration.rb +11 -1
  10. data/lib/mail_room/connection.rb +7 -179
  11. data/lib/mail_room/coordinator.rb +8 -4
  12. data/lib/mail_room/crash_handler.rb +8 -12
  13. data/lib/mail_room/health_check.rb +60 -0
  14. data/lib/mail_room/imap.rb +8 -0
  15. data/lib/mail_room/imap/connection.rb +200 -0
  16. data/lib/mail_room/imap/message.rb +19 -0
  17. data/lib/mail_room/logger/structured.rb +15 -1
  18. data/lib/mail_room/mailbox.rb +7 -4
  19. data/lib/mail_room/mailbox_watcher.rb +9 -2
  20. data/lib/mail_room/message.rb +16 -0
  21. data/lib/mail_room/version.rb +2 -2
  22. data/mail_room.gemspec +5 -3
  23. data/spec/fixtures/test_config.yml +3 -0
  24. data/spec/lib/arbitration/redis_spec.rb +3 -2
  25. data/spec/lib/cli_spec.rb +30 -15
  26. data/spec/lib/configuration_spec.rb +9 -2
  27. data/spec/lib/coordinator_spec.rb +27 -11
  28. data/spec/lib/crash_handler_spec.rb +10 -9
  29. data/spec/lib/delivery/letter_opener_spec.rb +9 -5
  30. data/spec/lib/delivery/logger_spec.rb +7 -9
  31. data/spec/lib/delivery/postback_spec.rb +11 -27
  32. data/spec/lib/delivery/que_spec.rb +5 -8
  33. data/spec/lib/health_check_spec.rb +57 -0
  34. data/spec/lib/{connection_spec.rb → imap/connection_spec.rb} +13 -17
  35. data/spec/lib/imap/message_spec.rb +36 -0
  36. data/spec/lib/logger/structured_spec.rb +34 -2
  37. data/spec/lib/mailbox_spec.rb +14 -17
  38. data/spec/lib/mailbox_watcher_spec.rb +9 -12
  39. data/spec/lib/message_spec.rb +35 -0
  40. data/spec/spec_helper.rb +1 -1
  41. metadata +45 -19
@@ -5,7 +5,8 @@ describe MailRoom::Arbitration::Redis do
5
5
  let(:mailbox) {
6
6
  build_mailbox(
7
7
  arbitration_options: {
8
- namespace: "mail_room"
8
+ namespace: "mail_room",
9
+ redis_url: ENV['REDIS_URL']
9
10
  }
10
11
  )
11
12
  }
@@ -78,7 +79,7 @@ describe MailRoom::Arbitration::Redis do
78
79
 
79
80
  context 'redis client connection params' do
80
81
  context 'when only url is present' do
81
- let(:redis_url) { "redis://localhost:6379" }
82
+ let(:redis_url) { ENV.fetch('REDIS_URL', 'redis://localhost:6379') }
82
83
  let(:mailbox) {
83
84
  build_mailbox(
84
85
  arbitration_options: {
data/spec/lib/cli_spec.rb CHANGED
@@ -4,23 +4,35 @@ describe MailRoom::CLI do
4
4
  let(:config_path) {File.expand_path('../fixtures/test_config.yml', File.dirname(__FILE__))}
5
5
  let!(:configuration) {MailRoom::Configuration.new({:config_path => config_path})}
6
6
  let(:coordinator) {stub(:run => true, :quit => true)}
7
+ let(:configuration_args) { anything }
8
+ let(:coordinator_args) { [anything, anything] }
7
9
 
8
10
  describe '.new' do
9
11
  let(:args) {["-c", "a path"]}
10
12
 
11
13
  before :each do
12
- MailRoom::Configuration.stubs(:new).returns(configuration)
13
- MailRoom::Coordinator.stubs(:new).returns(coordinator)
14
+ MailRoom::Configuration.expects(:new).with(configuration_args).returns(configuration)
15
+ MailRoom::Coordinator.stubs(:new).with(*coordinator_args).returns(coordinator)
14
16
  end
15
17
 
16
- it 'parses arguments into configuration' do
17
- expect(MailRoom::CLI.new(args).configuration).to eq(configuration)
18
- expect(MailRoom::Configuration).to have_received(:new).with({:config_path => 'a path'})
18
+ context 'with configuration args' do
19
+ let(:configuration_args) do
20
+ {:config_path => 'a path'}
21
+ end
22
+
23
+ it 'parses arguments into configuration' do
24
+ expect(MailRoom::CLI.new(args).configuration).to eq configuration
25
+ end
19
26
  end
20
27
 
21
- it 'creates a new coordinator with configuration' do
22
- expect(MailRoom::CLI.new(args).coordinator).to eq(coordinator)
23
- expect(MailRoom::Coordinator).to have_received(:new).with(configuration.mailboxes)
28
+ context 'with coordinator args' do
29
+ let(:coordinator_args) do
30
+ [configuration.mailboxes, anything]
31
+ end
32
+
33
+ it 'creates a new coordinator with configuration' do
34
+ expect(MailRoom::CLI.new(args).coordinator).to eq(coordinator)
35
+ end
24
36
  end
25
37
  end
26
38
 
@@ -30,30 +42,33 @@ describe MailRoom::CLI do
30
42
  before :each do
31
43
  cli.configuration = configuration
32
44
  cli.coordinator = coordinator
45
+ cli.stubs(:exit)
33
46
  end
34
47
 
35
48
  it 'starts running the coordinator' do
36
- cli.start
49
+ coordinator.expects(:run)
37
50
 
38
- expect(coordinator).to have_received(:run)
51
+ cli.start
39
52
  end
40
53
 
41
54
  context 'on error' do
42
- let(:error_message) { "oh noes!" }
43
- let(:coordinator) { OpenStruct.new(run: true, quit: true) }
55
+ let(:error) { RuntimeError.new("oh noes!") }
56
+ let(:coordinator) { stub(run: true, quit: true) }
57
+ let(:crash_handler) { stub(handle: nil) }
44
58
 
45
59
  before do
46
60
  cli.instance_variable_set(:@options, {exit_error_format: error_format})
47
- coordinator.stubs(:run).raises(RuntimeError, error_message)
61
+ coordinator.stubs(:run).raises(error)
62
+ MailRoom::CrashHandler.stubs(:new).returns(crash_handler)
48
63
  end
49
64
 
50
65
  context 'json format provided' do
51
66
  let(:error_format) { 'json' }
52
67
 
53
68
  it 'passes onto CrashHandler' do
54
- cli.start
69
+ crash_handler.expects(:handle).with(error, error_format)
55
70
 
56
- expect(MailRoom::CrashHandler).to have_received(:new).with a_hash_including({format: error_format})
71
+ cli.start
57
72
  end
58
73
  end
59
74
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe MailRoom::Configuration do
4
4
  let(:config_path) {File.expand_path('../fixtures/test_config.yml', File.dirname(__FILE__))}
5
5
 
6
- describe 'set_mailboxes' do
6
+ describe '#initalize' do
7
7
  context 'with config_path' do
8
8
  let(:configuration) { MailRoom::Configuration.new(:config_path => config_path) }
9
9
 
@@ -12,6 +12,10 @@ describe MailRoom::Configuration do
12
12
 
13
13
  expect(configuration.mailboxes).to eq(['mailbox1', 'mailbox2'])
14
14
  end
15
+
16
+ it 'parses health check' do
17
+ expect(configuration.health_check).to be_a(MailRoom::HealthCheck)
18
+ end
15
19
  end
16
20
 
17
21
  context 'without config_path' do
@@ -19,10 +23,13 @@ describe MailRoom::Configuration do
19
23
 
20
24
  it 'sets mailboxes to an empty set' do
21
25
  MailRoom::Mailbox.stubs(:new)
26
+ MailRoom::Mailbox.expects(:new).never
22
27
 
23
28
  expect(configuration.mailboxes).to eq([])
29
+ end
24
30
 
25
- expect(MailRoom::Mailbox).to have_received(:new).never
31
+ it 'sets the health check to nil' do
32
+ expect(configuration.health_check).to be_nil
26
33
  end
27
34
  end
28
35
  end
@@ -3,20 +3,25 @@ require 'spec_helper'
3
3
  describe MailRoom::Coordinator do
4
4
  describe '#initialize' do
5
5
  it 'builds a watcher for each mailbox' do
6
- MailRoom::MailboxWatcher.stubs(:new).returns('watcher1', 'watcher2')
6
+ MailRoom::MailboxWatcher.expects(:new).with('mailbox1').returns('watcher1')
7
+ MailRoom::MailboxWatcher.expects(:new).with('mailbox2').returns('watcher2')
7
8
 
8
9
  coordinator = MailRoom::Coordinator.new(['mailbox1', 'mailbox2'])
9
10
 
10
11
  expect(coordinator.watchers).to eq(['watcher1', 'watcher2'])
11
-
12
- expect(MailRoom::MailboxWatcher).to have_received(:new).with('mailbox1')
13
- expect(MailRoom::MailboxWatcher).to have_received(:new).with('mailbox2')
14
12
  end
15
13
 
16
14
  it 'makes no watchers when mailboxes is empty' do
17
15
  coordinator = MailRoom::Coordinator.new([])
18
16
  expect(coordinator.watchers).to eq([])
19
17
  end
18
+
19
+ it 'sets the health check' do
20
+ health_check = MailRoom::HealthCheck.new({ address: '127.0.0.1', port: 8080})
21
+ coordinator = MailRoom::Coordinator.new([], health_check)
22
+
23
+ expect(coordinator.health_check).to eq(health_check)
24
+ end
20
25
  end
21
26
 
22
27
  describe '#run' do
@@ -24,27 +29,37 @@ describe MailRoom::Coordinator do
24
29
  watcher = stub
25
30
  watcher.stubs(:run)
26
31
  watcher.stubs(:quit)
32
+
33
+ health_check = stub
34
+ health_check.stubs(:run)
35
+ health_check.stubs(:quit)
36
+
27
37
  MailRoom::MailboxWatcher.stubs(:new).returns(watcher)
28
- coordinator = MailRoom::Coordinator.new(['mailbox1'])
38
+ coordinator = MailRoom::Coordinator.new(['mailbox1'], health_check)
29
39
  coordinator.stubs(:sleep_while_running)
40
+ watcher.expects(:run)
41
+ watcher.expects(:quit)
42
+ health_check.expects(:run)
43
+ health_check.expects(:quit)
44
+
30
45
  coordinator.run
31
- expect(watcher).to have_received(:run)
32
- expect(watcher).to have_received(:quit)
33
46
  end
34
-
47
+
35
48
  it 'should go to sleep after running watchers' do
36
49
  coordinator = MailRoom::Coordinator.new([])
37
50
  coordinator.stubs(:running=)
38
51
  coordinator.stubs(:running?).returns(false)
52
+ coordinator.expects(:running=).with(true)
53
+ coordinator.expects(:running?)
54
+
39
55
  coordinator.run
40
- expect(coordinator).to have_received(:running=).with(true)
41
- expect(coordinator).to have_received(:running?)
42
56
  end
43
57
 
44
58
  it 'should set attribute running to true' do
45
59
  coordinator = MailRoom::Coordinator.new([])
46
60
  coordinator.stubs(:sleep_while_running)
47
61
  coordinator.run
62
+
48
63
  expect(coordinator.running).to eq(true)
49
64
  end
50
65
  end
@@ -54,8 +69,9 @@ describe MailRoom::Coordinator do
54
69
  watcher = stub(:quit)
55
70
  MailRoom::MailboxWatcher.stubs(:new).returns(watcher)
56
71
  coordinator = MailRoom::Coordinator.new(['mailbox1'])
72
+ watcher.expects(:quit)
73
+
57
74
  coordinator.quit
58
- expect(watcher).to have_received(:quit)
59
75
  end
60
76
  end
61
77
  end
@@ -4,21 +4,22 @@ describe MailRoom::CrashHandler do
4
4
 
5
5
  let(:error_message) { "oh noes!" }
6
6
  let(:error) { RuntimeError.new(error_message) }
7
+ let(:stdout) { StringIO.new }
7
8
 
8
9
  describe '#handle' do
9
10
 
10
- subject{ described_class.new(error: error, format: format) }
11
+ subject{ described_class.new(stdout).handle(error, format) }
11
12
 
12
13
  context 'when given a json format' do
13
14
  let(:format) { 'json' }
14
- let(:fake_json) do
15
- { message: error_message }.to_json
16
- end
17
15
 
18
- it 'outputs the result of json to stdout' do
19
- subject.stubs(:json).returns(fake_json)
16
+ it 'writes a json message to stdout' do
17
+ subject
18
+ stdout.rewind
19
+ output = stdout.read
20
20
 
21
- expect{ subject.handle }.to output(/\"message\":\"#{error_message}\"/).to_stdout
21
+ expect(output).to end_with("\n")
22
+ expect(JSON.parse(output)['message']).to eq(error_message)
22
23
  end
23
24
  end
24
25
 
@@ -26,7 +27,7 @@ describe MailRoom::CrashHandler do
26
27
  let(:format) { "" }
27
28
 
28
29
  it 'raises an error as designed' do
29
- expect{ subject.handle }.to raise_error(error.class, error_message)
30
+ expect{ subject }.to raise_error(error.class, error_message)
30
31
  end
31
32
  end
32
33
 
@@ -34,7 +35,7 @@ describe MailRoom::CrashHandler do
34
35
  let(:format) { "nonsense" }
35
36
 
36
37
  it 'raises an error as designed' do
37
- expect{ subject.handle }.to raise_error(error.class, error_message)
38
+ expect{ subject }.to raise_error(error.class, error_message)
38
39
  end
39
40
  end
40
41
  end
@@ -10,20 +10,24 @@ describe MailRoom::Delivery::LetterOpener do
10
10
  before :each do
11
11
  Mail.stubs(:read_from_string).returns(mail)
12
12
  ::LetterOpener::DeliveryMethod.stubs(:new).returns(delivery_method)
13
-
14
- MailRoom::Delivery::LetterOpener.new(mailbox).deliver('a message')
15
13
  end
16
14
 
17
15
  it 'creates a new LetterOpener::DeliveryMethod' do
18
- expect(::LetterOpener::DeliveryMethod).to have_received(:new).with(:location => '/tmp/somewhere')
16
+ ::LetterOpener::DeliveryMethod.expects(:new).with(:location => '/tmp/somewhere').returns(delivery_method)
17
+
18
+ MailRoom::Delivery::LetterOpener.new(mailbox).deliver('a message')
19
19
  end
20
20
 
21
21
  it 'parses the message string with Mail' do
22
- expect(::Mail).to have_received(:read_from_string).with('a message')
22
+ ::Mail.expects(:read_from_string).with('a message')
23
+
24
+ MailRoom::Delivery::LetterOpener.new(mailbox).deliver('a message')
23
25
  end
24
26
 
25
27
  it 'delivers the mail message' do
26
- expect(delivery_method).to have_received(:deliver!).with(mail)
28
+ delivery_method.expects(:deliver!).with(mail)
29
+
30
+ MailRoom::Delivery::LetterOpener.new(mailbox).deliver('a message')
27
31
  end
28
32
  end
29
33
  end
@@ -9,9 +9,9 @@ describe MailRoom::Delivery::Logger do
9
9
  it 'creates a new ruby logger' do
10
10
  ::Logger.stubs(:new)
11
11
 
12
- MailRoom::Delivery::Logger.new(mailbox)
12
+ ::Logger.expects(:new).with(STDOUT)
13
13
 
14
- expect(::Logger).to have_received(:new).with(STDOUT)
14
+ MailRoom::Delivery::Logger.new(mailbox)
15
15
  end
16
16
  end
17
17
 
@@ -19,14 +19,12 @@ describe MailRoom::Delivery::Logger do
19
19
  let(:mailbox) {build_mailbox(:log_path => '/var/log/mail-room.log')}
20
20
 
21
21
  it 'creates a new file to append to' do
22
- ::Logger.stubs(:new)
23
22
  file = stub(:sync=)
24
- ::File.stubs(:open).returns(file)
25
23
 
26
- MailRoom::Delivery::Logger.new(mailbox)
24
+ File.expects(:open).with('/var/log/mail-room.log', 'a').returns(file)
25
+ ::Logger.stubs(:new).with(file)
27
26
 
28
- expect(File).to have_received(:open).with('/var/log/mail-room.log', 'a')
29
- expect(::Logger).to have_received(:new).with(file)
27
+ MailRoom::Delivery::Logger.new(mailbox)
30
28
  end
31
29
  end
32
30
  end
@@ -38,9 +36,9 @@ describe MailRoom::Delivery::Logger do
38
36
  logger = stub(:info)
39
37
  ::Logger.stubs(:new).returns(logger)
40
38
 
41
- MailRoom::Delivery::Logger.new(mailbox).deliver('a message')
39
+ logger.expects(:info).with('a message')
42
40
 
43
- expect(logger).to have_received(:info).with('a message')
41
+ MailRoom::Delivery::Logger.new(mailbox).deliver('a message')
44
42
  end
45
43
  end
46
44
  end
@@ -18,19 +18,13 @@ describe MailRoom::Delivery::Postback do
18
18
  request = stub
19
19
  Faraday.stubs(:new).returns(connection)
20
20
 
21
- connection.stubs(:token_auth)
22
- connection.stubs(:post).yields(request)
21
+ connection.expects(:token_auth).with('abcdefg')
22
+ connection.expects(:post).yields(request)
23
23
 
24
- request.stubs(:url)
25
- request.stubs(:body=)
24
+ request.expects(:url).with('http://localhost/inbox')
25
+ request.expects(:body=).with('a message')
26
26
 
27
27
  MailRoom::Delivery::Postback.new(delivery_options).deliver('a message')
28
-
29
- expect(connection).to have_received(:token_auth).with('abcdefg')
30
- expect(connection).to have_received(:post)
31
-
32
- expect(request).to have_received(:url).with('http://localhost/inbox')
33
- expect(request).to have_received(:body=).with('a message')
34
28
  end
35
29
  end
36
30
 
@@ -52,19 +46,13 @@ describe MailRoom::Delivery::Postback do
52
46
  request = stub
53
47
  Faraday.stubs(:new).returns(connection)
54
48
 
55
- connection.stubs(:basic_auth)
56
- connection.stubs(:post).yields(request)
49
+ connection.expects(:basic_auth).with('user1', 'password123abc')
50
+ connection.expects(:post).yields(request)
57
51
 
58
- request.stubs(:url)
59
- request.stubs(:body=)
52
+ request.expects(:url).with('http://localhost/inbox')
53
+ request.expects(:body=).with('a message')
60
54
 
61
55
  MailRoom::Delivery::Postback.new(delivery_options).deliver('a message')
62
-
63
- expect(connection).to have_received(:basic_auth).with('user1', 'password123abc')
64
- expect(connection).to have_received(:post)
65
-
66
- expect(request).to have_received(:url).with('http://localhost/inbox')
67
- expect(request).to have_received(:body=).with('a message')
68
56
  end
69
57
 
70
58
  context 'with content type in the delivery options' do
@@ -86,18 +74,14 @@ describe MailRoom::Delivery::Postback do
86
74
  connection = stub
87
75
  request = stub
88
76
  Faraday.stubs(:new).returns(connection)
89
-
90
- connection.stubs(:basic_auth)
91
- connection.stubs(:post).yields(request)
92
-
77
+
78
+ connection.expects(:post).yields(request)
93
79
  request.stubs(:url)
94
80
  request.stubs(:body=)
95
81
  request.stubs(:headers).returns({})
82
+ connection.expects(:basic_auth).with('user1', 'password123abc')
96
83
 
97
84
  MailRoom::Delivery::Postback.new(delivery_options).deliver('a message')
98
-
99
- expect(connection).to have_received(:basic_auth).with('user1', 'password123abc')
100
- expect(connection).to have_received(:post)
101
85
 
102
86
  expect(request.headers['Content-Type']).to eq('text/plain')
103
87
  end
@@ -18,20 +18,15 @@ describe MailRoom::Delivery::Que do
18
18
  let(:options) {MailRoom::Delivery::Que::Options.new(mailbox)}
19
19
 
20
20
  it 'stores the message in que_jobs table' do
21
- PG.stubs(:connect).returns(connection)
22
- connection.stubs(:exec)
23
-
24
- MailRoom::Delivery::Que.new(options).deliver('email')
25
-
26
- expect(PG).to have_received(:connect).with({
21
+ PG.expects(:connect).with({
27
22
  host: 'localhost',
28
23
  port: 5432,
29
24
  dbname: 'delivery_test',
30
25
  user: 'postgres',
31
26
  password: ''
32
- })
27
+ }).returns(connection)
33
28
 
34
- expect(connection).to have_received(:exec).with(
29
+ connection.expects(:exec).with(
35
30
  "INSERT INTO que_jobs (priority, job_class, queue, args) VALUES ($1, $2, $3, $4)",
36
31
  [
37
32
  5,
@@ -40,6 +35,8 @@ describe MailRoom::Delivery::Que do
40
35
  JSON.dump(['email'])
41
36
  ]
42
37
  )
38
+
39
+ MailRoom::Delivery::Que.new(options).deliver('email')
43
40
  end
44
41
  end
45
42
  end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe MailRoom::HealthCheck do
6
+ let(:address) { '127.0.0.1' }
7
+ let(:port) { 8000 }
8
+ let(:params) { { address: address, port: port } }
9
+ subject { described_class.new(params) }
10
+
11
+ describe '#initialize' do
12
+ context 'with valid parameters' do
13
+ it 'validates successfully' do
14
+ expect(subject).to be_a(described_class)
15
+ end
16
+ end
17
+
18
+ context 'with invalid address' do
19
+ let(:address) { nil }
20
+
21
+ it 'raises an error' do
22
+ expect { subject }.to raise_error('No health check address specified')
23
+ end
24
+ end
25
+
26
+ context 'with invalid port' do
27
+ let(:port) { nil }
28
+
29
+ it 'raises an error' do
30
+ expect { subject }.to raise_error('Health check port 0 is invalid')
31
+ end
32
+ end
33
+ end
34
+
35
+ describe '#run' do
36
+ it 'sets running to true' do
37
+ server = stub(start: true)
38
+ subject.stubs(:create_server).returns(server)
39
+
40
+ subject.run
41
+
42
+ expect(subject.running).to be true
43
+ end
44
+ end
45
+
46
+ describe '#quit' do
47
+ it 'sets running to false' do
48
+ server = stub(start: true, shutdown: true)
49
+ subject.stubs(:create_server).returns(server)
50
+
51
+ subject.run
52
+ subject.quit
53
+
54
+ expect(subject.running).to be false
55
+ end
56
+ end
57
+ end