imap-backup 2.1.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +0 -1
  3. data/.rubocop.yml +5 -2
  4. data/.rubocop_todo.yml +24 -0
  5. data/.travis.yml +13 -1
  6. data/README.md +9 -21
  7. data/Rakefile +6 -2
  8. data/imap-backup.gemspec +5 -1
  9. data/lib/email/mboxrd/message.rb +13 -12
  10. data/lib/imap/backup/account/connection.rb +3 -2
  11. data/lib/imap/backup/account/folder.rb +2 -0
  12. data/lib/imap/backup/configuration/account.rb +20 -16
  13. data/lib/imap/backup/configuration/asker.rb +1 -1
  14. data/lib/imap/backup/serializer/mbox.rb +39 -25
  15. data/lib/imap/backup/serializer/mbox_enumerator.rb +31 -0
  16. data/lib/imap/backup/serializer/mbox_store.rb +6 -27
  17. data/lib/imap/backup/version.rb +1 -1
  18. data/spec/features/support/email_server.rb +3 -0
  19. data/spec/support/fixtures.rb +1 -1
  20. data/spec/unit/email/mboxrd/message_spec.rb +20 -3
  21. data/spec/unit/email/provider_spec.rb +1 -1
  22. data/spec/unit/imap/backup/account/connection_spec.rb +10 -9
  23. data/spec/unit/imap/backup/account/folder_spec.rb +24 -10
  24. data/spec/unit/imap/backup/configuration/account_spec.rb +47 -22
  25. data/spec/unit/imap/backup/configuration/asker_spec.rb +5 -9
  26. data/spec/unit/imap/backup/configuration/connection_tester_spec.rb +6 -6
  27. data/spec/unit/imap/backup/configuration/folder_chooser_spec.rb +6 -6
  28. data/spec/unit/imap/backup/configuration/list_spec.rb +24 -1
  29. data/spec/unit/imap/backup/configuration/setup_spec.rb +29 -9
  30. data/spec/unit/imap/backup/configuration/store_spec.rb +5 -5
  31. data/spec/unit/imap/backup/downloader_spec.rb +11 -13
  32. data/spec/unit/imap/backup/serializer/mbox_enumerator_spec.rb +40 -0
  33. data/spec/unit/imap/backup/serializer/mbox_spec.rb +63 -24
  34. data/spec/unit/imap/backup/serializer/mbox_store_spec.rb +162 -9
  35. data/spec/unit/imap/backup/utils_spec.rb +3 -3
  36. data/spec/unit/imap/backup_spec.rb +28 -0
  37. metadata +8 -2
@@ -14,10 +14,11 @@ describe Imap::Backup::Serializer::MboxStore do
14
14
  let(:imap_content) do
15
15
  {
16
16
  version: Imap::Backup::Serializer::MboxStore::CURRENT_VERSION,
17
- uid_validity: 123,
18
- uids: uids.sort
17
+ uid_validity: uid_validity,
18
+ uids: uids
19
19
  }.to_json
20
20
  end
21
+ let(:uid_validity) { 123 }
21
22
 
22
23
  before do
23
24
  allow(File).to receive(:exist?).and_call_original
@@ -40,9 +41,30 @@ describe Imap::Backup::Serializer::MboxStore do
40
41
  allow(FileUtils).to receive(:chmod)
41
42
  end
42
43
 
43
- context "#uids" do
44
- it "returns the backed-up uids as sorted integers" do
45
- expect(subject.uids).to eq(uids.map(&:to_i).sort)
44
+ describe "#uid_validity=" do
45
+ let(:new_uid_validity) { "13" }
46
+ let(:updated_imap_content) do
47
+ {
48
+ version: Imap::Backup::Serializer::MboxStore::CURRENT_VERSION,
49
+ uid_validity: new_uid_validity,
50
+ uids: uids
51
+ }.to_json
52
+ end
53
+
54
+ before { subject.uid_validity = new_uid_validity }
55
+
56
+ it "sets uid_validity" do
57
+ expect(subject.uid_validity).to eq(new_uid_validity)
58
+ end
59
+
60
+ it "writes the imap file" do
61
+ expect(imap_file).to have_received(:write).with(updated_imap_content)
62
+ end
63
+ end
64
+
65
+ describe "#uids" do
66
+ it "returns the backed-up uids as integers" do
67
+ expect(subject.uids).to eq(uids.map(&:to_i))
46
68
  end
47
69
 
48
70
  context "when the imap file does not exist" do
@@ -53,6 +75,30 @@ describe Imap::Backup::Serializer::MboxStore do
53
75
  end
54
76
  end
55
77
 
78
+ context "when the imap file is malformed" do
79
+ before do
80
+ allow(JSON).to receive(:parse).and_raise(JSON::ParserError)
81
+ end
82
+
83
+ let!(:result) { subject.uids }
84
+
85
+ it "returns an empty Array" do
86
+ expect(result).to eq([])
87
+ end
88
+
89
+ it "deletes the imap file" do
90
+ expect(File).to have_received(:unlink).with(imap_pathname)
91
+ end
92
+
93
+ it "deletes the mbox file" do
94
+ expect(File).to have_received(:unlink).with(mbox_pathname)
95
+ end
96
+
97
+ it "writes a blank mbox file" do
98
+ expect(mbox_file).to have_received(:write).with("")
99
+ end
100
+ end
101
+
56
102
  context "when the mbox does not exist" do
57
103
  let(:mbox_exists) { false }
58
104
 
@@ -62,7 +108,7 @@ describe Imap::Backup::Serializer::MboxStore do
62
108
  end
63
109
  end
64
110
 
65
- context "#add" do
111
+ describe "#add" do
66
112
  let(:mbox_formatted_message) { "message in mbox format" }
67
113
  let(:message_uid) { "999" }
68
114
  let(:message) do
@@ -74,15 +120,14 @@ describe Imap::Backup::Serializer::MboxStore do
74
120
  let(:updated_imap_content) do
75
121
  {
76
122
  version: Imap::Backup::Serializer::MboxStore::CURRENT_VERSION,
77
- uid_validity: 123,
78
- uids: (uids + [999]).sort
123
+ uid_validity: uid_validity,
124
+ uids: uids + [999]
79
125
  }.to_json
80
126
  end
81
127
 
82
128
  before do
83
129
  allow(Email::Mboxrd::Message).to receive(:new).and_return(message)
84
130
  allow(File).to receive(:open).with(mbox_pathname, "ab") { mbox_file }
85
- subject.uid_validity = 123
86
131
  end
87
132
 
88
133
  it "saves the message to the mbox" do
@@ -97,6 +142,15 @@ describe Imap::Backup::Serializer::MboxStore do
97
142
  expect(imap_file).to have_received(:write).with(updated_imap_content)
98
143
  end
99
144
 
145
+ context "when the message is already downloaded" do
146
+ let(:uids) { [999] }
147
+
148
+ it "skips the message" do
149
+ subject.add(message_uid, "The\nemail\n")
150
+ expect(mbox_file).to_not have_received(:write)
151
+ end
152
+ end
153
+
100
154
  context "when the message causes parsing errors" do
101
155
  before do
102
156
  allow(message).to receive(:to_serialized).and_raise(ArgumentError)
@@ -114,4 +168,103 @@ describe Imap::Backup::Serializer::MboxStore do
114
168
  end
115
169
  end
116
170
  end
171
+
172
+ describe "#load" do
173
+ let(:uid) { "1" }
174
+ let(:result) { subject.load(uid) }
175
+ let(:enumerator) do
176
+ instance_double(Imap::Backup::Serializer::MboxEnumerator)
177
+ end
178
+ let(:enumeration) { instance_double(Enumerator) }
179
+
180
+ before do
181
+ allow(Imap::Backup::Serializer::MboxEnumerator).
182
+ to receive(:new) { enumerator }
183
+ allow(enumerator).to receive(:each) { enumeration }
184
+ allow(enumeration).
185
+ to receive(:with_index).
186
+ and_yield("", 0).
187
+ and_yield("", 1).
188
+ and_yield("ciao", 2)
189
+ end
190
+
191
+ it "returns the message" do
192
+ expect(result.supplied_body).to eq("ciao")
193
+ end
194
+
195
+ context "when the UID is unknown" do
196
+ let(:uid) { "99" }
197
+
198
+ it "returns nil" do
199
+ expect(result).to be_nil
200
+ end
201
+ end
202
+ end
203
+
204
+ describe "#update_uid" do
205
+ let(:old_uid) { "2" }
206
+ let(:updated_imap_content) do
207
+ {
208
+ version: Imap::Backup::Serializer::MboxStore::CURRENT_VERSION,
209
+ uid_validity: uid_validity,
210
+ uids: [3, 999, 1]
211
+ }.to_json
212
+ end
213
+
214
+ before { subject.update_uid(old_uid, "999") }
215
+
216
+ it "updates the stored UID" do
217
+ expect(imap_file).to have_received(:write).with(updated_imap_content)
218
+ end
219
+
220
+ context "when the UID is unknown" do
221
+ let(:old_uid) { "42" }
222
+
223
+ it "does nothing" do
224
+ expect(imap_file).to_not have_received(:write)
225
+ end
226
+ end
227
+ end
228
+
229
+ describe "#reset" do
230
+ before { subject.reset }
231
+
232
+ it "deletes the imap file" do
233
+ expect(File).to have_received(:unlink).with(imap_pathname)
234
+ end
235
+
236
+ it "deletes the mbox file" do
237
+ expect(File).to have_received(:unlink).with(mbox_pathname)
238
+ end
239
+
240
+ it "writes a blank mbox file" do
241
+ expect(mbox_file).to have_received(:write).with("")
242
+ end
243
+ end
244
+
245
+ describe "#rename" do
246
+ let(:new_name) { "new_name" }
247
+ let(:new_folder_path) { File.join(base_path, new_name) }
248
+ let(:new_imap_name) { new_folder_path + ".imap" }
249
+ let(:new_mbox_name) { new_folder_path + ".mbox" }
250
+
251
+ before do
252
+ allow(File).to receive(:rename).and_call_original
253
+ allow(File).to receive(:rename).with(imap_pathname, new_imap_name)
254
+ allow(File).to receive(:rename).with(mbox_pathname, new_mbox_name)
255
+ subject.rename(new_name)
256
+ end
257
+
258
+ it "renames the imap file" do
259
+ expect(File).to have_received(:rename).with(imap_pathname, new_imap_name)
260
+ end
261
+
262
+ it "renames the mbox file" do
263
+ expect(File).to have_received(:rename).with(mbox_pathname, new_mbox_name)
264
+ end
265
+
266
+ it "updates the folder name" do
267
+ expect(subject.folder).to eq(new_name)
268
+ end
269
+ end
117
270
  end
@@ -9,7 +9,7 @@ describe Imap::Backup::Utils do
9
9
  allow(File).to receive(:exist?).with(filename).and_return(exists)
10
10
  end
11
11
 
12
- context ".check_permissions" do
12
+ describe ".check_permissions" do
13
13
  let(:requested) { 0o345 }
14
14
 
15
15
  context "with existing files" do
@@ -47,7 +47,7 @@ describe Imap::Backup::Utils do
47
47
  end
48
48
  end
49
49
 
50
- context ".mode" do
50
+ describe ".mode" do
51
51
  context "with existing files" do
52
52
  let(:mode) { 0o2345 }
53
53
 
@@ -65,7 +65,7 @@ describe Imap::Backup::Utils do
65
65
  end
66
66
  end
67
67
 
68
- context ".make_folder" do
68
+ describe ".make_folder" do
69
69
  before do
70
70
  allow(FileUtils).to receive(:mkdir_p)
71
71
  allow(FileUtils).to receive(:chmod)
@@ -0,0 +1,28 @@
1
+ require "ostruct"
2
+ require "imap/backup"
3
+
4
+ describe Imap::Backup do
5
+ describe ".setup_logging" do
6
+ let!(:previous) { described_class.logger.level }
7
+
8
+ before { described_class.setup_logging(config) }
9
+
10
+ after { described_class.logger.level = previous }
11
+
12
+ context "when config.debug?" do
13
+ let(:config) { OpenStruct.new(debug?: true) }
14
+
15
+ it "sets logger level to debug" do
16
+ expect(described_class.logger.level).to eq(::Logger::Severity::DEBUG)
17
+ end
18
+ end
19
+
20
+ context "when not config.debug?" do
21
+ let(:config) { OpenStruct.new(debug?: false) }
22
+
23
+ it "sets logger level to debug" do
24
+ expect(described_class.logger.level).to eq(::Logger::Severity::ERROR)
25
+ end
26
+ end
27
+ end
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imap-backup
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Yates
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-22 00:00:00.000000000 Z
11
+ date: 2019-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: highline
@@ -134,6 +134,7 @@ files:
134
134
  - ".rspec"
135
135
  - ".rspec-all"
136
136
  - ".rubocop.yml"
137
+ - ".rubocop_todo.yml"
137
138
  - ".travis.yml"
138
139
  - Gemfile
139
140
  - LICENSE
@@ -158,6 +159,7 @@ files:
158
159
  - lib/imap/backup/downloader.rb
159
160
  - lib/imap/backup/serializer.rb
160
161
  - lib/imap/backup/serializer/mbox.rb
162
+ - lib/imap/backup/serializer/mbox_enumerator.rb
161
163
  - lib/imap/backup/serializer/mbox_store.rb
162
164
  - lib/imap/backup/uploader.rb
163
165
  - lib/imap/backup/utils.rb
@@ -188,10 +190,12 @@ files:
188
190
  - spec/unit/imap/backup/configuration/setup_spec.rb
189
191
  - spec/unit/imap/backup/configuration/store_spec.rb
190
192
  - spec/unit/imap/backup/downloader_spec.rb
193
+ - spec/unit/imap/backup/serializer/mbox_enumerator_spec.rb
191
194
  - spec/unit/imap/backup/serializer/mbox_spec.rb
192
195
  - spec/unit/imap/backup/serializer/mbox_store_spec.rb
193
196
  - spec/unit/imap/backup/uploader_spec.rb
194
197
  - spec/unit/imap/backup/utils_spec.rb
198
+ - spec/unit/imap/backup_spec.rb
195
199
  - tmp/.gitkeep
196
200
  homepage: https://github.com/joeyates/imap-backup
197
201
  licenses: []
@@ -246,7 +250,9 @@ test_files:
246
250
  - spec/unit/imap/backup/configuration/setup_spec.rb
247
251
  - spec/unit/imap/backup/configuration/store_spec.rb
248
252
  - spec/unit/imap/backup/downloader_spec.rb
253
+ - spec/unit/imap/backup/serializer/mbox_enumerator_spec.rb
249
254
  - spec/unit/imap/backup/serializer/mbox_spec.rb
250
255
  - spec/unit/imap/backup/serializer/mbox_store_spec.rb
251
256
  - spec/unit/imap/backup/uploader_spec.rb
252
257
  - spec/unit/imap/backup/utils_spec.rb
258
+ - spec/unit/imap/backup_spec.rb