mail_room 0.10.0 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.gitlab-ci.yml +14 -12
  3. data/.rubocop.yml +5 -0
  4. data/.rubocop_todo.yml +501 -0
  5. data/.ruby-version +1 -1
  6. data/.travis.yml +12 -5
  7. data/CHANGELOG.md +4 -0
  8. data/README.md +99 -13
  9. data/Rakefile +1 -1
  10. data/lib/mail_room/arbitration/redis.rb +1 -1
  11. data/lib/mail_room/cli.rb +9 -2
  12. data/lib/mail_room/connection.rb +6 -180
  13. data/lib/mail_room/crash_handler.rb +26 -0
  14. data/lib/mail_room/delivery/letter_opener.rb +1 -1
  15. data/lib/mail_room/delivery/postback.rb +5 -3
  16. data/lib/mail_room/delivery/sidekiq.rb +4 -3
  17. data/lib/mail_room/imap/connection.rb +200 -0
  18. data/lib/mail_room/imap/message.rb +19 -0
  19. data/lib/mail_room/imap.rb +8 -0
  20. data/lib/mail_room/logger/structured.rb +15 -1
  21. data/lib/mail_room/mailbox.rb +77 -21
  22. data/lib/mail_room/mailbox_watcher.rb +9 -1
  23. data/lib/mail_room/message.rb +16 -0
  24. data/lib/mail_room/microsoft_graph/connection.rb +217 -0
  25. data/lib/mail_room/microsoft_graph.rb +7 -0
  26. data/lib/mail_room/version.rb +1 -1
  27. data/lib/mail_room.rb +2 -0
  28. data/mail_room.gemspec +7 -3
  29. data/spec/lib/arbitration/redis_spec.rb +3 -2
  30. data/spec/lib/cli_spec.rb +46 -11
  31. data/spec/lib/configuration_spec.rb +2 -3
  32. data/spec/lib/coordinator_spec.rb +11 -9
  33. data/spec/lib/crash_handler_spec.rb +42 -0
  34. data/spec/lib/delivery/letter_opener_spec.rb +10 -6
  35. data/spec/lib/delivery/logger_spec.rb +8 -10
  36. data/spec/lib/delivery/postback_spec.rb +45 -25
  37. data/spec/lib/delivery/que_spec.rb +5 -8
  38. data/spec/lib/delivery/sidekiq_spec.rb +29 -7
  39. data/spec/lib/{connection_spec.rb → imap/connection_spec.rb} +13 -17
  40. data/spec/lib/imap/message_spec.rb +36 -0
  41. data/spec/lib/logger/structured_spec.rb +35 -3
  42. data/spec/lib/mailbox_spec.rb +85 -34
  43. data/spec/lib/mailbox_watcher_spec.rb +54 -41
  44. data/spec/lib/message_spec.rb +35 -0
  45. data/spec/lib/microsoft_graph/connection_spec.rb +190 -0
  46. data/spec/spec_helper.rb +14 -4
  47. metadata +80 -21
data/lib/mail_room.rb CHANGED
@@ -9,7 +9,9 @@ require "mail_room/version"
9
9
  require "mail_room/configuration"
10
10
  require "mail_room/mailbox"
11
11
  require "mail_room/mailbox_watcher"
12
+ require "mail_room/message"
12
13
  require "mail_room/connection"
13
14
  require "mail_room/coordinator"
14
15
  require "mail_room/cli"
15
16
  require 'mail_room/logger/structured'
17
+ require 'mail_room/crash_handler'
data/mail_room.gemspec CHANGED
@@ -17,10 +17,13 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
+ gem.add_dependency "net-imap", ">= 0.2.1"
21
+ gem.add_dependency "oauth2", "~> 1.4.4"
22
+
20
23
  gem.add_development_dependency "rake"
21
- gem.add_development_dependency "rspec"
22
- gem.add_development_dependency "mocha"
23
- gem.add_development_dependency "bourne"
24
+ gem.add_development_dependency "rspec", "~> 3.9"
25
+ gem.add_development_dependency "rubocop", "~> 1.11"
26
+ gem.add_development_dependency "mocha", "~> 1.11"
24
27
  gem.add_development_dependency "simplecov"
25
28
 
26
29
  # for testing delivery methods
@@ -31,4 +34,5 @@ Gem::Specification.new do |gem|
31
34
  gem.add_development_dependency "redis-namespace"
32
35
  gem.add_development_dependency "pg"
33
36
  gem.add_development_dependency "charlock_holmes"
37
+ gem.add_development_dependency "webmock"
34
38
  end
@@ -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
@@ -2,25 +2,37 @@ require 'spec_helper'
2
2
 
3
3
  describe MailRoom::CLI do
4
4
  let(:config_path) {File.expand_path('../fixtures/test_config.yml', File.dirname(__FILE__))}
5
- let!(:configuration) {MailRoom::Configuration.new({:config_path => config_path})}
6
- let(:coordinator) {stub(:run => true, :quit => true)}
5
+ let!(:configuration) {MailRoom::Configuration.new({config_path: config_path})}
6
+ let(:coordinator) {stub(run: true, quit: true)}
7
+ let(:configuration_args) { anything }
8
+ let(:coordinator_args) { 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
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,12 +42,35 @@ 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
49
+ coordinator.expects(:run)
50
+
36
51
  cli.start
52
+ end
53
+
54
+ context 'on error' do
55
+ let(:error) { RuntimeError.new("oh noes!") }
56
+ let(:coordinator) { stub(run: true, quit: true) }
57
+ let(:crash_handler) { stub(handle: nil) }
58
+
59
+ before do
60
+ cli.instance_variable_set(:@options, {exit_error_format: error_format})
61
+ coordinator.stubs(:run).raises(error)
62
+ MailRoom::CrashHandler.stubs(:new).returns(crash_handler)
63
+ end
64
+
65
+ context 'json format provided' do
66
+ let(:error_format) { 'json' }
67
+
68
+ it 'passes onto CrashHandler' do
69
+ crash_handler.expects(:handle).with(error, error_format)
37
70
 
38
- expect(coordinator).to have_received(:run)
71
+ cli.start
72
+ end
73
+ end
39
74
  end
40
75
  end
41
76
  end
@@ -5,7 +5,7 @@ describe MailRoom::Configuration do
5
5
 
6
6
  describe 'set_mailboxes' do
7
7
  context 'with config_path' do
8
- let(:configuration) { MailRoom::Configuration.new(:config_path => config_path) }
8
+ let(:configuration) { MailRoom::Configuration.new(config_path: config_path) }
9
9
 
10
10
  it 'parses yaml into mailbox objects' do
11
11
  MailRoom::Mailbox.stubs(:new).returns('mailbox1', 'mailbox2')
@@ -19,10 +19,9 @@ describe MailRoom::Configuration do
19
19
 
20
20
  it 'sets mailboxes to an empty set' do
21
21
  MailRoom::Mailbox.stubs(:new)
22
+ MailRoom::Mailbox.expects(:new).never
22
23
 
23
24
  expect(configuration.mailboxes).to eq([])
24
-
25
- expect(MailRoom::Mailbox).to have_received(:new).never
26
25
  end
27
26
  end
28
27
  end
@@ -3,14 +3,12 @@ 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
@@ -27,24 +25,27 @@ describe MailRoom::Coordinator do
27
25
  MailRoom::MailboxWatcher.stubs(:new).returns(watcher)
28
26
  coordinator = MailRoom::Coordinator.new(['mailbox1'])
29
27
  coordinator.stubs(:sleep_while_running)
28
+ watcher.expects(:run)
29
+ watcher.expects(:quit)
30
+
30
31
  coordinator.run
31
- expect(watcher).to have_received(:run)
32
- expect(watcher).to have_received(:quit)
33
32
  end
34
33
 
35
34
  it 'should go to sleep after running watchers' do
36
35
  coordinator = MailRoom::Coordinator.new([])
37
36
  coordinator.stubs(:running=)
38
37
  coordinator.stubs(:running?).returns(false)
38
+ coordinator.expects(:running=).with(true)
39
+ coordinator.expects(:running?)
40
+
39
41
  coordinator.run
40
- expect(coordinator).to have_received(:running=).with(true)
41
- expect(coordinator).to have_received(:running?)
42
42
  end
43
43
 
44
44
  it 'should set attribute running to true' do
45
45
  coordinator = MailRoom::Coordinator.new([])
46
46
  coordinator.stubs(:sleep_while_running)
47
47
  coordinator.run
48
+
48
49
  expect(coordinator.running).to eq(true)
49
50
  end
50
51
  end
@@ -54,8 +55,9 @@ describe MailRoom::Coordinator do
54
55
  watcher = stub(:quit)
55
56
  MailRoom::MailboxWatcher.stubs(:new).returns(watcher)
56
57
  coordinator = MailRoom::Coordinator.new(['mailbox1'])
58
+ watcher.expects(:quit)
59
+
57
60
  coordinator.quit
58
- expect(watcher).to have_received(:quit)
59
61
  end
60
62
  end
61
63
  end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe MailRoom::CrashHandler do
4
+
5
+ let(:error_message) { "oh noes!" }
6
+ let(:error) { RuntimeError.new(error_message) }
7
+ let(:stdout) { StringIO.new }
8
+
9
+ describe '#handle' do
10
+
11
+ subject{ described_class.new(stdout).handle(error, format) }
12
+
13
+ context 'when given a json format' do
14
+ let(:format) { 'json' }
15
+
16
+ it 'writes a json message to stdout' do
17
+ subject
18
+ stdout.rewind
19
+ output = stdout.read
20
+
21
+ expect(output).to end_with("\n")
22
+ expect(JSON.parse(output)['message']).to eq(error_message)
23
+ end
24
+ end
25
+
26
+ context 'when given a blank format' do
27
+ let(:format) { "" }
28
+
29
+ it 'raises an error as designed' do
30
+ expect{ subject }.to raise_error(error.class, error_message)
31
+ end
32
+ end
33
+
34
+ context 'when given a nonexistent format' do
35
+ let(:format) { "nonsense" }
36
+
37
+ it 'raises an error as designed' do
38
+ expect{ subject }.to raise_error(error.class, error_message)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -3,27 +3,31 @@ require 'mail_room/delivery/letter_opener'
3
3
 
4
4
  describe MailRoom::Delivery::LetterOpener do
5
5
  describe '#deliver' do
6
- let(:mailbox) {build_mailbox(:location => '/tmp/somewhere')}
6
+ let(:mailbox) {build_mailbox(location: '/tmp/somewhere')}
7
7
  let(:delivery_method) {stub(:deliver!)}
8
8
  let(:mail) {stub}
9
9
 
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,24 +9,22 @@ 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
 
18
18
  context "with a log path" do
19
- let(:mailbox) {build_mailbox(:log_path => '/var/log/mail-room.log')}
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
@@ -5,8 +5,8 @@ describe MailRoom::Delivery::Postback do
5
5
  describe '#deliver' do
6
6
  context 'with token auth delivery' do
7
7
  let(:mailbox) {build_mailbox({
8
- :delivery_url => 'http://localhost/inbox',
9
- :delivery_token => 'abcdefg'
8
+ delivery_url: 'http://localhost/inbox',
9
+ delivery_token: 'abcdefg'
10
10
  })}
11
11
 
12
12
  let(:delivery_options) {
@@ -18,28 +18,22 @@ 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
 
37
31
  context 'with basic auth delivery options' do
38
32
  let(:mailbox) {build_mailbox({
39
- :delivery_options => {
40
- :url => 'http://localhost/inbox',
41
- :username => 'user1',
42
- :password => 'password123abc'
33
+ delivery_options: {
34
+ url: 'http://localhost/inbox',
35
+ username: 'user1',
36
+ password: 'password123abc'
43
37
  }
44
38
  })}
45
39
 
@@ -52,19 +46,45 @@ 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')
56
+ end
62
57
 
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')
58
+ context 'with content type in the delivery options' do
59
+ let(:mailbox) {build_mailbox({
60
+ delivery_options: {
61
+ url: 'http://localhost/inbox',
62
+ username: 'user1',
63
+ password: 'password123abc',
64
+ content_type: 'text/plain'
65
+ }
66
+ })}
67
+
68
+
69
+ let(:delivery_options) {
70
+ MailRoom::Delivery::Postback::Options.new(mailbox)
71
+ }
72
+
73
+ it 'posts the message with faraday' do
74
+ connection = stub
75
+ request = stub
76
+ Faraday.stubs(:new).returns(connection)
77
+
78
+ connection.expects(:post).yields(request)
79
+ request.stubs(:url)
80
+ request.stubs(:body=)
81
+ request.stubs(:headers).returns({})
82
+ connection.expects(:basic_auth).with('user1', 'password123abc')
83
+
84
+ MailRoom::Delivery::Postback.new(delivery_options).deliver('a message')
85
+
86
+ expect(request.headers['Content-Type']).to eq('text/plain')
87
+ end
68
88
  end
69
89
  end
70
90
  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
@@ -8,23 +8,45 @@ describe MailRoom::Delivery::Sidekiq do
8
8
 
9
9
  describe '#options' do
10
10
  let(:redis_url) { 'redis://localhost' }
11
+ let(:redis_options) { { redis_url: redis_url } }
11
12
 
12
13
  context 'when only redis_url is specified' do
13
14
  let(:mailbox) {
14
15
  build_mailbox(
15
16
  delivery_method: :sidekiq,
16
- delivery_options: {
17
- redis_url: redis_url
18
- }
17
+ delivery_options: redis_options
19
18
  )
20
19
  }
21
20
 
22
- it 'client has same specified redis_url' do
23
- expect(redis.client.options[:url]).to eq(redis_url)
21
+ context 'with simple redis url' do
22
+ it 'client has same specified redis_url' do
23
+ expect(redis.client.options[:url]).to eq(redis_url)
24
+ end
25
+
26
+ it 'client is a instance of RedisNamespace class' do
27
+ expect(redis).to be_a ::Redis
28
+ end
29
+
30
+ it 'connection has correct values' do
31
+ expect(redis.connection[:host]).to eq('localhost')
32
+ expect(redis.connection[:db]).to eq(0)
33
+ end
24
34
  end
25
35
 
26
- it 'client is a instance of RedisNamespace class' do
27
- expect(redis).to be_a ::Redis
36
+ context 'with redis_db specified in options' do
37
+ before do
38
+ redis_options[:redis_db] = 4
39
+ end
40
+
41
+ it 'client has correct redis_url' do
42
+ expect(redis.client.options[:url]).to eq(redis_url)
43
+ end
44
+
45
+
46
+ it 'connection has correct values' do
47
+ expect(redis.connection[:host]).to eq('localhost')
48
+ expect(redis.connection[:db]).to eq(4)
49
+ end
28
50
  end
29
51
  end
30
52
 
@@ -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
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal:true
2
+
3
+ require 'spec_helper'
4
+ require 'securerandom'
5
+
6
+ describe MailRoom::IMAP::Message do
7
+ let(:uid) { SecureRandom.hex }
8
+ let(:body) { 'hello world' }
9
+ let(:seqno) { 5 }
10
+
11
+ subject { described_class.new(uid: uid, body: body, seqno: seqno) }
12
+
13
+ describe '#initalize' do
14
+ it 'initializes with required parameters' do
15
+ subject
16
+
17
+ expect(subject.uid).to eq(uid)
18
+ expect(subject.body).to eq(body)
19
+ expect(subject.seqno).to eq(seqno)
20
+ end
21
+ end
22
+
23
+ describe '#==' do
24
+ let(:dup) { described_class.new(uid: uid, body: body, seqno: seqno) }
25
+ let(:base_msg) { MailRoom::Message.new(uid: uid, body: body) }
26
+
27
+ it 'matches an equivalent message' do
28
+ expect(dup == subject).to be true
29
+ end
30
+
31
+ it 'does not match a base message' do
32
+ expect(subject == base_msg).to be false
33
+ expect(base_msg == subject).to be false
34
+ end
35
+ end
36
+ end