inbox-sync 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/helper.rb ADDED
@@ -0,0 +1,87 @@
1
+ # this file is automatically required in when you require 'assert' in your tests
2
+ # put test helpers here
3
+
4
+ # add root dir to the load path
5
+ $LOAD_PATH.unshift(File.expand_path("../..", __FILE__))
6
+
7
+ require 'logger'
8
+ require 'net/imap'
9
+ require 'inbox-sync/sync'
10
+ require 'inbox-sync/mail_item'
11
+
12
+ class Assert::Context
13
+
14
+ def configured_sync
15
+ InboxSync::Sync.new.configure do
16
+ source.host 'imap.gmail.com'
17
+ source.port 993
18
+ source.ssl 'Yes'
19
+ source.login 'joetest@kellyredding.com', 'joetest1'
20
+
21
+ dest.host 'imap.gmail.com'
22
+ dest.port 993
23
+ dest.ssl 'Yes'
24
+ dest.login 'suetest@kellyredding.com', 'suetest1'
25
+
26
+ notify.host 'smtp.gmail.com'
27
+ notify.port 587
28
+ notify.tls 'Yes'
29
+ notify.helo 'gmail.com'
30
+ notify.login 'suetest@kellyredding.com', 'suetest1'
31
+ notify.from_addr 'suetest@kellyredding.com'
32
+ notify.to_addr 'suetest@kellyredding.com'
33
+
34
+ logger Logger.new('log/tests.log')
35
+ end
36
+ end
37
+
38
+ TEST_MAIL_DATA = {
39
+ 'RFC822' => "Delivered-To: joetest@kellyredding.com\r\nReceived: by 10.114.62.209 with SMTP id a17csp11732lds;\r\n Thu, 24 May 2012 08:34:46 -0700 (PDT)\r\nReceived: by 10.112.84.65 with SMTP id w1mr13705338lby.40.1337873686521;\r\n Thu, 24 May 2012 08:34:46 -0700 (PDT)\r\nReturn-Path: <suetest@kellyredding.com>\r\nReceived: from mail-lpp01m010-f67.google.com (mail-lpp01m010-f67.google.com [209.85.215.67])\r\n by mx.google.com with ESMTPS id a9si2217645lbh.1.2012.05.24.08.34.46\r\n (version=TLSv1/SSLv3 cipher=OTHER);\r\n Thu, 24 May 2012 08:34:46 -0700 (PDT)\r\nReceived-SPF: neutral (google.com: 209.85.215.67 is neither permitted nor denied by best guess record for domain of suetest@kellyredding.com) client-ip=209.85.215.67;\r\nAuthentication-Results: mx.google.com; spf=neutral (google.com: 209.85.215.67 is neither permitted nor denied by best guess record for domain of suetest@kellyredding.com) smtp.mail=suetest@kellyredding.com\r\nReceived: by lagv3 with SMTP id v3so751705lag.2\r\n for <joetest@kellyredding.com>; Thu, 24 May 2012 08:34:44 -0700 (PDT)\r\nX-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n d=google.com; s=20120113;\r\n h=mime-version:x-originating-ip:date:message-id:subject:from:to\r\n :content-type:x-gm-message-state;\r\n bh=jC/axn7WeCoH+0tXbYrcmVfGSuJ82WTAoccq2csJ8HA=;\r\n b=T5W+Ba0za3EsMrBgVM6Z2kJww/GvOIr5JzEMVwNnqJDMgFseumYcHw9f6OGZMuhvtH\r\n Ok3PCmg8uiDRj1Mm2YiE1O8dhQL7BgBd4UO5zBGcLzUDYHBLor+oSjh8pZOsNgHEJk9H\r\n dZSn0Af4q8RuCRNCyeD8xJt7rRF1EESAgqdQ35fd1GiTefGyhE1UwGkS/P4BiUga1wUr\r\n qG2fA7Sbj2XcTyCjvsXqgw/EoOuIgnOqeMHZ5CsCfE0FBhW24cIwAt59FIeTELcsb0QQ\r\n 4w76+RkaS4SxuNCEagsRN6DHnLB7SKqTRF6ZKt6xs/zbkz1Qz3mq4yrhrrYa3pNnK9Ez\r\n F2ug==\r\nMIME-Version: 1.0\r\nReceived: by 10.112.29.199 with SMTP id m7mr14024566lbh.31.1337873684699; Thu,\r\n 24 May 2012 08:34:44 -0700 (PDT)\r\nReceived: by 10.114.59.80 with HTTP; Thu, 24 May 2012 08:34:44 -0700 (PDT)\r\nX-Originating-IP: [209.133.30.126]\r\nDate: Thu, 24 May 2012 10:34:44 -0500\r\nMessage-ID: <CAA9i=SubBh6kAv=cxgGskY9cmS7gsT4m6YSGGdc417bM+DFk4Q@mail.gmail.com>\r\nSubject: test mail\r\nFrom: Sue Test <suetest@kellyredding.com>\r\nTo: joetest@kellyredding.com\r\nContent-Type: multipart/alternative; boundary=f46d04016b2dbb1bfe04c0c9fd9a\r\nX-Gm-Message-State: ALoCoQkFFPgZHO7zDsv7I5mvZqbwHdNjnyobpEyOYxxXgPySrU+OEAOtZSKX6G3vlddVo15J3A5s\r\n\r\n--f46d04016b2dbb1bfe04c0c9fd9a\r\nContent-Type: text/plain; charset=ISO-8859-1\r\n\r\nThis is a test mail\r\n\r\nWITH MARKUP!!!\r\n\r\n--f46d04016b2dbb1bfe04c0c9fd9a\r\nContent-Type: text/html; charset=ISO-8859-1\r\n\r\nThis is a test mail<div><br></div><div><font class=\"Apple-style-span\" color=\"#ff0000\">WITH MARKUP!!!</font></div>\r\n\r\n--f46d04016b2dbb1bfe04c0c9fd9a--\r\n",
40
+ 'INTERNALDATE' => "24-May-2012 15:34:46 +0000"
41
+ }
42
+
43
+ def test_mail_item
44
+ InboxSync::MailItem.new('12345', TEST_MAIL_DATA['RFC822'], TEST_MAIL_DATA['INTERNALDATE'])
45
+ end
46
+
47
+ def setup_sync_mail_item
48
+ @sync = Assert::Context.new.configured_sync
49
+ @sync.login
50
+ @mail_item = Assert::Context.new.test_mail_item
51
+ [@sync, @mail_item]
52
+ end
53
+
54
+ def empty_source_selected(sync)
55
+ sync.source_imap.uid_search(['ALL']).each do |uid|
56
+ sync.source_imap.uid_store(uid, "+FLAGS", [:Deleted])
57
+ end
58
+ sync.source_imap.expunge
59
+ end
60
+
61
+ def reset_source_inbox(sync)
62
+ sync.source_imap.select(sync.config.source.inbox)
63
+ empty_source_selected(sync)
64
+
65
+ # append the test mail on the source imap
66
+ inbox = sync.config.source.inbox
67
+ mail_s = test_mail_item.meta['RFC822']
68
+ flags = []
69
+ date = test_mail_item.meta['INTERNALDATE']
70
+
71
+ sync.source_imap.append(inbox, mail_s, flags, date)
72
+ end
73
+
74
+ def reset_source_archive(sync)
75
+ begin
76
+ sync.source_imap.select(sync.config.archive_folder)
77
+ rescue Net::IMAP::NoResponseError => err
78
+ # do nothing - archive folder not present - no action needed
79
+ else
80
+ empty_source_selected(sync)
81
+ sync.source_imap.delete(sync.config.archive_folder)
82
+ ensure
83
+ sync.source_imap.select(sync.config.source.inbox)
84
+ end
85
+ end
86
+
87
+ end
data/test/irb.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'assert/setup'
2
+
3
+ # this file is required in when the 'irb' rake test is run.
4
+ # b/c 'assert/setup' is required above, the test helper will be
5
+ # required in as well.
6
+
7
+ # put any IRB setup code here
8
+
9
+ require 'inbox-sync'
@@ -0,0 +1,62 @@
1
+ require 'assert'
2
+ require 'mail'
3
+ require 'inbox-sync/mail_item'
4
+
5
+ module InboxSync
6
+
7
+ class MailItemTests < Assert::Context
8
+ desc 'a mail item'
9
+ before do
10
+ @item = test_mail_item
11
+ end
12
+ subject { @item }
13
+
14
+ should have_readers :uid, :meta, :message
15
+ should have_class_method :find
16
+ should have_instance_method :name, :stripped
17
+
18
+ should "build a Mail Message from the raw IMAP attr data" do
19
+ assert_kind_of ::Mail::Message, subject.message
20
+ end
21
+
22
+ should "provide the raw IMAP attrs in the meta hash" do
23
+ assert_includes 'RFC822', subject.meta
24
+ assert_includes 'INTERNALDATE', subject.meta
25
+
26
+ assert_equal TEST_MAIL_DATA['RFC822'], subject.meta['RFC822']
27
+ assert_equal TEST_MAIL_DATA['INTERNALDATE'], subject.meta['INTERNALDATE']
28
+ end
29
+
30
+ should "be named by its uid, from, subject, and date" do
31
+ exp = "[12345] suetest@kellyredding.com: \"test mail\" (Thu May 24 2012, 10:34 AM)"
32
+ assert_equal exp, subject.name
33
+ end
34
+
35
+ end
36
+
37
+ class StrippedMailItemTests < MailItemTests
38
+ desc 'that has been stripped'
39
+ before do
40
+ @stripped = @item.stripped
41
+ end
42
+ subject { @stripped }
43
+
44
+ should "be single part plain text only" do
45
+ assert_equal 1, subject.message.parts.size
46
+ end
47
+
48
+ should "have no attachments" do
49
+ assert_equal 0, subject.message.attachments.size
50
+ end
51
+
52
+ should "prefix the body with a note saying that it is stripped down" do
53
+ pref = /\A\*\*\[inbox-sync\] stripped down to just plain text part\*\*/
54
+ assert_match pref, subject.message.parts.first.body.to_s
55
+ end
56
+
57
+ should "set its RFC822 to the stripped message string" do
58
+ assert_equal subject.meta['RFC822'], subject.message.to_s
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,40 @@
1
+ require 'assert'
2
+
3
+ require 'inbox-sync/sync'
4
+ require 'inbox-sync/runner'
5
+
6
+ module InboxSync
7
+
8
+ class RunnerTests < Assert::Context
9
+ desc "a sync runner"
10
+ before do
11
+ @syncs = [InboxSync::Sync.new, InboxSync::Sync.new]
12
+ @runner = InboxSync::Runner.new(@syncs, :interval => 20)
13
+ end
14
+ subject { @runner }
15
+
16
+ should have_readers :syncs, :interval, :logger
17
+
18
+ should "know its syncs" do
19
+ assert_equal @syncs, subject.syncs
20
+ end
21
+
22
+ should "know its interval seconds" do
23
+ assert_equal 20, subject.interval
24
+ end
25
+
26
+ should "default its interval to -1" do
27
+ assert_equal -1, InboxSync::Runner.new.interval
28
+ end
29
+
30
+ should "build from zero or many syncs" do
31
+ assert_equal @syncs, InboxSync::Runner.new(@syncs).syncs
32
+ assert_equal @syncs, InboxSync::Runner.new(*@syncs).syncs
33
+ assert_equal [@syncs.first], InboxSync::Runner.new(@syncs.first).syncs
34
+ assert_equal [], InboxSync::Runner.new(:interval => 5).syncs
35
+ assert_equal [], InboxSync::Runner.new().syncs
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,62 @@
1
+ require 'assert'
2
+
3
+ require 'inbox-sync/sync'
4
+ require 'inbox-sync/config'
5
+
6
+ module InboxSync
7
+
8
+ class BasicSyncTests < Assert::Context
9
+ desc "a sync"
10
+ before do
11
+ @sync = InboxSync::Sync.new
12
+ @raw_config = InboxSync::Config.new({
13
+ :source => { :host => 'imap.test.com'}
14
+ })
15
+ end
16
+ subject { @sync }
17
+
18
+ should have_readers :config, :source_imap, :notify_smtp
19
+ should have_instance_methods :uid, :name, :logger, :logged_in?
20
+ should have_instance_method :configure, :setup, :teardown, :run, :notify
21
+
22
+ should "configure using a block" do
23
+ subject.configure do
24
+ source.host 'imap.test.com'
25
+ end
26
+ assert_equal @raw_config.source, subject.config.source
27
+ end
28
+
29
+ should "configure passing in a settings hash" do
30
+ a_sync = InboxSync::Sync.new({
31
+ :source => {:host => 'imap.test.com'}
32
+ })
33
+
34
+ assert_equal a_sync.config.source, @raw_config.source
35
+ end
36
+
37
+ should "return itself when calling `configure`" do
38
+ assert_equal subject, subject.configure
39
+ end
40
+
41
+ end
42
+
43
+ class BasicConfiguredSyncTests < BasicSyncTests
44
+ desc 'that has the basic configs'
45
+ before do
46
+ subject.configure do
47
+ source.host 'imap.test.com'
48
+ source.login.user 'me@example.com'
49
+ end
50
+ end
51
+
52
+ should "have a unique id" do
53
+ assert_equal "me@example.com:imap.test.com", subject.uid
54
+ end
55
+
56
+ should "be named by its source" do
57
+ assert_equal "me@example.com (imap.test.com)", subject.name
58
+ end
59
+
60
+ end
61
+
62
+ end
@@ -0,0 +1,110 @@
1
+ require 'assert'
2
+
3
+ require 'inbox-sync/sync'
4
+
5
+ module InboxSync
6
+
7
+ class ConfiguredSyncTests < Assert::Context
8
+ desc "a sync that has been configured and setup"
9
+ before do
10
+ @sync = configured_sync
11
+ @sync.setup
12
+ end
13
+ after do
14
+ @sync.teardown
15
+ end
16
+ subject { @sync }
17
+
18
+ should "be logged in" do
19
+ assert subject.logged_in?
20
+ end
21
+
22
+ should "be logged out after teardown" do
23
+ subject.teardown
24
+
25
+ assert_not subject.logged_in?
26
+ end
27
+
28
+ should "set source IMAP and notify SMTP objects" do
29
+ assert_kind_of Net::IMAP, subject.source_imap
30
+ assert_kind_of Net::SMTP, subject.notify_smtp
31
+ end
32
+
33
+ should "remove source IMAP and notify SMTP objects after teardown" do
34
+ subject.teardown
35
+
36
+ assert_nil subject.source_imap
37
+ assert_nil subject.notify_smtp
38
+ end
39
+
40
+ should "validate the configs on setup" do
41
+ assert_raises ArgumentError do
42
+ InboxSync::Sync.new(:logger => '/dev/null').setup
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ class LoggedInTests < ConfiguredSyncTests
49
+ desc 'and is logged in and ready to run'
50
+ before do
51
+ reset_source_inbox(@sync)
52
+ reset_source_archive(@sync)
53
+
54
+ @sync_archive_folder = @sync.config.archive_folder
55
+ @mail_item = MailItem.find(@sync.source_imap).first
56
+ end
57
+
58
+ after do
59
+ @sync.config.archive_folder = @sync_archive_folder
60
+ @sync.source_imap.select(@sync.config.source.inbox)
61
+
62
+ reset_source_archive(@sync)
63
+ end
64
+
65
+ should "run without any errors" do
66
+ assert_nothing_raised do
67
+ subject.run
68
+ end
69
+ end
70
+
71
+ end
72
+
73
+ # class ArchiveTests < RunTests
74
+ # should "remove the message from the inbox" do
75
+ # @sync.archive_from_source(@mail_item)
76
+
77
+ # assert_not_included @mail_item.uid, @sync.source_imap.uid_search(['ALL'])
78
+ # assert_empty @sync.source_imap.uid_search(['ALL'])
79
+ # end
80
+
81
+ # should "move it to the archive folder" do
82
+ # @sync.archive_from_source(@mail_item)
83
+ # @sync.source_imap.select(@sync.config.archive_folder)
84
+
85
+ # assert_equal 1, @sync.source_imap.uid_search(['ALL']).size
86
+ # end
87
+
88
+ # should "not archive if no archive folder configured" do
89
+ # @sync.config.archive_folder = nil
90
+ # @sync.archive_from_source(@mail_item)
91
+ # @sync.source_imap.select(@sync.config.source.inbox)
92
+
93
+ # assert_not_included @mail_item.uid, @sync.source_imap.uid_search(['ALL'])
94
+ # end
95
+
96
+ # end
97
+
98
+ # class AppendTests < LoggedInTests
99
+ # should "append a source msg onto the dest" do
100
+ # assert_nothing_raised do
101
+ # @sync.append_to_dest(test_mail_item)
102
+ # end
103
+ # end
104
+
105
+ # should "parse the destination UID when appending mail items" do
106
+ # assert_match /\A\d+\Z/, @sync.append_to_dest(test_mail_item)
107
+ # end
108
+ # end
109
+
110
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inbox-sync
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Kelly Redding
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-06-05 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ type: :development
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ name: assert
32
+ version_requirements: *id001
33
+ prerelease: false
34
+ - !ruby/object:Gem::Dependency
35
+ type: :runtime
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ hash: 13
42
+ segments:
43
+ - 0
44
+ - 4
45
+ - 1
46
+ version: 0.4.1
47
+ name: ns-options
48
+ version_requirements: *id002
49
+ prerelease: false
50
+ - !ruby/object:Gem::Dependency
51
+ type: :runtime
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ hash: 11
58
+ segments:
59
+ - 2
60
+ - 4
61
+ version: "2.4"
62
+ name: mail
63
+ version_requirements: *id003
64
+ prerelease: false
65
+ description: Move messages from one inbox to another
66
+ email:
67
+ - kelly@kellyredding.com
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files: []
73
+
74
+ files:
75
+ - .gitignore
76
+ - Gemfile
77
+ - LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - inbox-sync.gemspec
81
+ - lib/inbox-sync.rb
82
+ - lib/inbox-sync/config.rb
83
+ - lib/inbox-sync/config/credentials.rb
84
+ - lib/inbox-sync/config/imap_config.rb
85
+ - lib/inbox-sync/config/smtp_config.rb
86
+ - lib/inbox-sync/mail_item.rb
87
+ - lib/inbox-sync/notice/base.rb
88
+ - lib/inbox-sync/notice/run_sync_error.rb
89
+ - lib/inbox-sync/notice/sync_mail_item_error.rb
90
+ - lib/inbox-sync/runner.rb
91
+ - lib/inbox-sync/sync.rb
92
+ - lib/inbox-sync/version.rb
93
+ - log/.gitkeep
94
+ - test/config_test.rb
95
+ - test/helper.rb
96
+ - test/irb.rb
97
+ - test/mail_item_test.rb
98
+ - test/runner_test.rb
99
+ - test/sync/basic_test.rb
100
+ - test/sync/login_test.rb
101
+ homepage: http://github.com/kellyredding/inbox-sync
102
+ licenses: []
103
+
104
+ post_install_message:
105
+ rdoc_options: []
106
+
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ hash: 3
124
+ segments:
125
+ - 0
126
+ version: "0"
127
+ requirements: []
128
+
129
+ rubyforge_project:
130
+ rubygems_version: 1.8.11
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: Move messages from one inbox to another
134
+ test_files:
135
+ - test/config_test.rb
136
+ - test/helper.rb
137
+ - test/irb.rb
138
+ - test/mail_item_test.rb
139
+ - test/runner_test.rb
140
+ - test/sync/basic_test.rb
141
+ - test/sync/login_test.rb