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
@@ -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
@@ -5,6 +5,7 @@ describe MailRoom::Logger::Structured do
5
5
  subject { described_class.new $stdout }
6
6
 
7
7
  let!(:now) { Time.now }
8
+ let(:timestamp) { now.to_datetime.iso8601(3) }
8
9
  let(:message) { { action: 'exciting development', message: 'testing 123' } }
9
10
 
10
11
  before do
@@ -32,7 +33,7 @@ describe MailRoom::Logger::Structured do
32
33
  }
33
34
  expected = {
34
35
  severity: 'DEBUG',
35
- time: now,
36
+ time: timestamp,
36
37
  additional_field: "some value"
37
38
  }
38
39
 
@@ -40,10 +41,41 @@ describe MailRoom::Logger::Structured do
40
41
  end
41
42
  end
42
43
 
44
+ describe '#format_message' do
45
+ shared_examples 'timestamp formatting' do
46
+ it 'outputs ISO8601 timestamps' do
47
+ data = JSON.parse(subject.format_message('debug', input_timestamp, 'test', { message: 'hello' } ))
48
+
49
+ expect(data['time']).to eq(expected_timestamp)
50
+ end
51
+ end
52
+
53
+ context 'with no timestamp' do
54
+ let(:input_timestamp) { nil }
55
+ let(:expected_timestamp) { timestamp }
56
+
57
+ it_behaves_like 'timestamp formatting'
58
+ end
59
+
60
+ context 'with DateTime' do
61
+ let(:input_timestamp) { now.to_datetime }
62
+ let(:expected_timestamp) { timestamp }
63
+
64
+ it_behaves_like 'timestamp formatting'
65
+ end
66
+
67
+ context 'with string' do
68
+ let(:input_timestamp) { now.to_s }
69
+ let(:expected_timestamp) { input_timestamp }
70
+
71
+ it_behaves_like 'timestamp formatting'
72
+ end
73
+ end
74
+
43
75
  def json_matching(level, message)
44
76
  contents = {
45
77
  severity: level,
46
- time: now
78
+ time: timestamp
47
79
  }.merge(message)
48
80
 
49
81
  as_regex(contents)
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe MailRoom::Mailbox do
4
- let(:sample_message) { {'RFC822' => 'a message', 'UID' => 123} }
4
+ let(:sample_message) { MailRoom::Message.new(uid: 123, body: 'a message') }
5
5
 
6
6
  describe "#deliver" do
7
7
  context "with arbitration_method of noop" do
@@ -12,9 +12,9 @@ describe MailRoom::Mailbox do
12
12
 
13
13
  uid = 123
14
14
 
15
- mailbox.deliver?(uid)
15
+ noop.expects(:deliver?).with(uid)
16
16
 
17
- expect(noop).to have_received(:deliver?).with(uid)
17
+ mailbox.deliver?(uid)
18
18
  end
19
19
  end
20
20
 
@@ -23,12 +23,10 @@ describe MailRoom::Mailbox do
23
23
  mailbox = build_mailbox({:arbitration_method => 'redis'})
24
24
  redis = stub(:deliver?)
25
25
  MailRoom::Arbitration['redis'].stubs(:new => redis)
26
-
27
26
  uid = 123
27
+ redis.expects(:deliver?).with(uid)
28
28
 
29
29
  mailbox.deliver?(uid)
30
-
31
- expect(redis).to have_received(:deliver?).with(uid)
32
30
  end
33
31
  end
34
32
 
@@ -38,9 +36,9 @@ describe MailRoom::Mailbox do
38
36
  noop = stub(:deliver)
39
37
  MailRoom::Delivery['noop'].stubs(:new => noop)
40
38
 
41
- mailbox.deliver(stub(:attr => sample_message))
39
+ noop.expects(:deliver).with(sample_message.body)
42
40
 
43
- expect(noop).to have_received(:deliver).with('a message')
41
+ mailbox.deliver(sample_message)
44
42
  end
45
43
  end
46
44
 
@@ -50,9 +48,9 @@ describe MailRoom::Mailbox do
50
48
  logger = stub(:deliver)
51
49
  MailRoom::Delivery['logger'].stubs(:new => logger)
52
50
 
53
- mailbox.deliver(stub(:attr => sample_message))
51
+ logger.expects(:deliver).with(sample_message.body)
54
52
 
55
- expect(logger).to have_received(:deliver).with('a message')
53
+ mailbox.deliver(sample_message)
56
54
  end
57
55
  end
58
56
 
@@ -62,9 +60,9 @@ describe MailRoom::Mailbox do
62
60
  postback = stub(:deliver)
63
61
  MailRoom::Delivery['postback'].stubs(:new => postback)
64
62
 
65
- mailbox.deliver(stub(:attr => sample_message))
63
+ postback.expects(:deliver).with(sample_message.body)
66
64
 
67
- expect(postback).to have_received(:deliver).with('a message')
65
+ mailbox.deliver(sample_message)
68
66
  end
69
67
  end
70
68
 
@@ -74,9 +72,9 @@ describe MailRoom::Mailbox do
74
72
  letter_opener = stub(:deliver)
75
73
  MailRoom::Delivery['letter_opener'].stubs(:new => letter_opener)
76
74
 
77
- mailbox.deliver(stub(:attr => sample_message))
75
+ letter_opener.expects(:deliver).with(sample_message.body)
78
76
 
79
- expect(letter_opener).to have_received(:deliver).with('a message')
77
+ mailbox.deliver(sample_message)
80
78
  end
81
79
  end
82
80
 
@@ -85,10 +83,9 @@ describe MailRoom::Mailbox do
85
83
  mailbox = build_mailbox({ name: "magic mailbox", delivery_method: 'noop' })
86
84
  noop = stub(:deliver)
87
85
  MailRoom::Delivery['noop'].stubs(:new => noop)
86
+ noop.expects(:deliver).never
88
87
 
89
- mailbox.deliver(stub(:attr => {'FLAGS' => [:Seen, :Recent]}))
90
-
91
- expect(noop).to have_received(:deliver).never
88
+ mailbox.deliver(MailRoom::Message.new(uid: 1234, body: nil))
92
89
  end
93
90
  end
94
91
 
@@ -19,20 +19,16 @@ describe MailRoom::MailboxWatcher do
19
19
  end
20
20
 
21
21
  it 'loops over wait while running' do
22
- connection = MailRoom::Connection.new(mailbox)
23
- connection.stubs(:on_new_message)
24
- connection.stubs(:wait)
22
+ connection = MailRoom::IMAP::Connection.new(mailbox)
25
23
 
26
- MailRoom::Connection.stubs(:new).returns(connection)
24
+ MailRoom::IMAP::Connection.stubs(:new).returns(connection)
27
25
 
28
- watcher.stubs(:running?).returns(true).then.returns(false)
26
+ watcher.expects(:running?).twice.returns(true, false)
27
+ connection.expects(:wait).once
28
+ connection.expects(:on_new_message).once
29
29
 
30
30
  watcher.run
31
31
  watcher.watching_thread.join # wait for finishing run
32
-
33
- expect(watcher).to have_received(:running?).times(2)
34
- expect(connection).to have_received(:wait).once
35
- expect(connection).to have_received(:on_new_message).once
36
32
  end
37
33
  end
38
34
 
@@ -45,19 +41,20 @@ describe MailRoom::MailboxWatcher do
45
41
  end
46
42
 
47
43
  it 'closes and waits for the connection' do
48
- connection = MailRoom::Connection.new(mailbox)
44
+ connection = MailRoom::IMAP::Connection.new(mailbox)
49
45
  connection.stubs(:wait)
50
46
  connection.stubs(:quit)
51
47
 
52
- MailRoom::Connection.stubs(:new).returns(connection)
48
+ MailRoom::IMAP::Connection.stubs(:new).returns(connection)
53
49
 
54
50
  watcher.run
55
51
 
56
52
  expect(watcher.running?).to eq(true)
57
53
 
54
+ connection.expects(:quit)
55
+
58
56
  watcher.quit
59
57
 
60
- expect(connection).to have_received(:quit)
61
58
  expect(watcher.running?).to eq(false)
62
59
  end
63
60
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal:true
2
+
3
+ require 'spec_helper'
4
+ require 'securerandom'
5
+
6
+ describe MailRoom::Message do
7
+ let(:uid) { SecureRandom.hex }
8
+ let(:body) { 'hello world' }
9
+
10
+ subject { described_class.new(uid: uid, body: body) }
11
+
12
+ describe '#initalize' do
13
+ it 'initializes with required parameters' do
14
+ subject
15
+
16
+ expect(subject.uid).to eq(uid)
17
+ expect(subject.body).to eq(body)
18
+ end
19
+ end
20
+
21
+ describe '#==' do
22
+ let(:dup) { described_class.new(uid: uid, body: body) }
23
+
24
+ it 'matches an equivalent message' do
25
+ expect(dup == subject).to be true
26
+ end
27
+
28
+ it 'does not match a message with a different UID' do
29
+ msg = described_class.new(uid: '12345', body: body)
30
+
31
+ expect(subject == msg).to be false
32
+ expect(msg == subject).to be false
33
+ end
34
+ end
35
+ end
data/spec/spec_helper.rb CHANGED
@@ -2,10 +2,10 @@ require 'simplecov'
2
2
  SimpleCov.start
3
3
 
4
4
  require 'bundler/setup'
5
+ require 'date'
5
6
 
6
7
  require 'rspec'
7
8
  require 'mocha/api'
8
- require 'bourne'
9
9
 
10
10
  require File.expand_path('../../lib/mail_room', __FILE__)
11
11
 
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-mail_room
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Pitale
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-06 00:00:00.000000000 Z
11
+ date: 2021-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-imap
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.1
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rake
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -28,32 +42,32 @@ dependencies:
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - ">="
45
+ - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '0'
47
+ version: '3.9'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - ">="
52
+ - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: '0'
54
+ version: '3.9'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: mocha
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - ">="
59
+ - - "~>"
46
60
  - !ruby/object:Gem::Version
47
- version: '0'
61
+ version: '1.11'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - ">="
66
+ - - "~>"
53
67
  - !ruby/object:Gem::Version
54
- version: '0'
68
+ version: '1.11'
55
69
  - !ruby/object:Gem::Dependency
56
- name: bourne
70
+ name: simplecov
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
73
  - - ">="
@@ -67,19 +81,19 @@ dependencies:
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
- name: simplecov
84
+ name: webrick
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
- - - ">="
87
+ - - "~>"
74
88
  - !ruby/object:Gem::Version
75
- version: '0'
89
+ version: '1.6'
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
- - - ">="
94
+ - - "~>"
81
95
  - !ruby/object:Gem::Version
82
- version: '0'
96
+ version: '1.6'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: faraday
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -188,6 +202,7 @@ extra_rdoc_files: []
188
202
  files:
189
203
  - ".gitignore"
190
204
  - ".gitlab-ci.yml"
205
+ - ".gitlab/issue_templates/Release.md"
191
206
  - ".ruby-version"
192
207
  - ".travis.yml"
193
208
  - CHANGELOG.md
@@ -213,9 +228,14 @@ files:
213
228
  - lib/mail_room/delivery/postback.rb
214
229
  - lib/mail_room/delivery/que.rb
215
230
  - lib/mail_room/delivery/sidekiq.rb
231
+ - lib/mail_room/health_check.rb
232
+ - lib/mail_room/imap.rb
233
+ - lib/mail_room/imap/connection.rb
234
+ - lib/mail_room/imap/message.rb
216
235
  - lib/mail_room/logger/structured.rb
217
236
  - lib/mail_room/mailbox.rb
218
237
  - lib/mail_room/mailbox_watcher.rb
238
+ - lib/mail_room/message.rb
219
239
  - lib/mail_room/version.rb
220
240
  - logfile.log
221
241
  - mail_room.gemspec
@@ -223,7 +243,6 @@ files:
223
243
  - spec/lib/arbitration/redis_spec.rb
224
244
  - spec/lib/cli_spec.rb
225
245
  - spec/lib/configuration_spec.rb
226
- - spec/lib/connection_spec.rb
227
246
  - spec/lib/coordinator_spec.rb
228
247
  - spec/lib/crash_handler_spec.rb
229
248
  - spec/lib/delivery/letter_opener_spec.rb
@@ -231,9 +250,13 @@ files:
231
250
  - spec/lib/delivery/postback_spec.rb
232
251
  - spec/lib/delivery/que_spec.rb
233
252
  - spec/lib/delivery/sidekiq_spec.rb
253
+ - spec/lib/health_check_spec.rb
254
+ - spec/lib/imap/connection_spec.rb
255
+ - spec/lib/imap/message_spec.rb
234
256
  - spec/lib/logger/structured_spec.rb
235
257
  - spec/lib/mailbox_spec.rb
236
258
  - spec/lib/mailbox_watcher_spec.rb
259
+ - spec/lib/message_spec.rb
237
260
  - spec/spec_helper.rb
238
261
  homepage: http://github.com/tpitale/mail_room
239
262
  licenses: []
@@ -253,7 +276,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
253
276
  - !ruby/object:Gem::Version
254
277
  version: '0'
255
278
  requirements: []
256
- rubygems_version: 3.1.2
279
+ rubygems_version: 3.1.4
257
280
  signing_key:
258
281
  specification_version: 4
259
282
  summary: mail_room will proxy email (gmail) from IMAP to a callback URL, logger, or
@@ -263,7 +286,6 @@ test_files:
263
286
  - spec/lib/arbitration/redis_spec.rb
264
287
  - spec/lib/cli_spec.rb
265
288
  - spec/lib/configuration_spec.rb
266
- - spec/lib/connection_spec.rb
267
289
  - spec/lib/coordinator_spec.rb
268
290
  - spec/lib/crash_handler_spec.rb
269
291
  - spec/lib/delivery/letter_opener_spec.rb
@@ -271,7 +293,11 @@ test_files:
271
293
  - spec/lib/delivery/postback_spec.rb
272
294
  - spec/lib/delivery/que_spec.rb
273
295
  - spec/lib/delivery/sidekiq_spec.rb
296
+ - spec/lib/health_check_spec.rb
297
+ - spec/lib/imap/connection_spec.rb
298
+ - spec/lib/imap/message_spec.rb
274
299
  - spec/lib/logger/structured_spec.rb
275
300
  - spec/lib/mailbox_spec.rb
276
301
  - spec/lib/mailbox_watcher_spec.rb
302
+ - spec/lib/message_spec.rb
277
303
  - spec/spec_helper.rb