gitlab-mail_room 0.0.3 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe MailRoom::Connection do
3
+ describe MailRoom::IMAP::Connection do
4
4
  let(:imap) {stub}
5
5
  let(:mailbox) {build_mailbox(delete_after_delivery: true, expunge_deleted: true)}
6
6
 
@@ -9,7 +9,9 @@ describe MailRoom::Connection do
9
9
  end
10
10
 
11
11
  context "with imap set up" do
12
- let(:connection) {MailRoom::Connection.new(mailbox)}
12
+ let(:connection) {MailRoom::IMAP::Connection.new(mailbox)}
13
+ let(:uid) { 1 }
14
+ let(:seqno) { 8 }
13
15
 
14
16
  before :each do
15
17
  imap.stubs(:starttls)
@@ -36,30 +38,24 @@ describe MailRoom::Connection do
36
38
  end
37
39
 
38
40
  it "waits for a message to process" do
39
- new_message = 'a message'
40
- new_message.stubs(:seqno).returns(8)
41
+ new_message = MailRoom::IMAP::Message.new(uid: uid, body: 'a message', seqno: seqno)
41
42
 
42
43
  connection.on_new_message do |message|
43
44
  expect(message).to eq(new_message)
44
45
  true
45
46
  end
46
47
 
47
- mailbox.stubs(:deliver?).returns(true)
48
+ attr = { 'UID' => uid, 'RFC822' => new_message.body }
49
+ fetch_data = Net::IMAP::FetchData.new(seqno, attr)
48
50
 
49
- imap.stubs(:idle)
50
- imap.stubs(:uid_search).returns([]).then.returns([1])
51
- imap.stubs(:uid_fetch).returns([new_message])
52
- imap.stubs(:store)
53
- imap.stubs(:expunge)
51
+ imap.expects(:idle)
52
+ imap.stubs(:uid_search).with(mailbox.search_command).returns([], [uid])
53
+ imap.expects(:uid_fetch).with([uid], "RFC822").returns([fetch_data])
54
+ mailbox.expects(:deliver?).with(uid).returns(true)
55
+ imap.expects(:store).with(seqno, "+FLAGS", [Net::IMAP::DELETED])
56
+ imap.expects(:expunge).once
54
57
 
55
58
  connection.wait
56
-
57
- expect(imap).to have_received(:idle)
58
- expect(imap).to have_received(:uid_search).with(mailbox.search_command).twice
59
- expect(imap).to have_received(:uid_fetch).with([1], "RFC822")
60
- expect(mailbox).to have_received(:deliver?).with(1)
61
- expect(imap).to have_received(:store).with(8, "+FLAGS", [Net::IMAP::DELETED])
62
- expect(imap).to have_received(:expunge).once
63
59
  end
64
60
  end
65
61
  end