spec/unit/account/connection_spec.rb in imap-backup-1.0.3 vs spec/unit/account/connection_spec.rb in imap-backup-1.0.4
- old
+ new
@@ -1,116 +1,123 @@
# encoding: utf-8
require 'spec_helper'
describe Imap::Backup::Account::Connection do
- context '#initialize' do
- it 'should login to the imap server' do
- imap = stub('Net::IMAP')
- Net::IMAP.should_receive(:new).with('imap.gmail.com', 993, true).and_return(imap)
- imap.should_receive('login').with('myuser', 'secret')
+ def self.folder_config
+ {:name => 'backup_folder'}
+ end
- Imap::Backup::Account::Connection.new(:username => 'myuser', :password => 'secret')
- end
+ let(:imap) { double('Net::IMAP', :login => nil, :list => []) }
+ let(:options) do
+ {
+ :username => username,
+ :password => 'password',
+ :local_path => 'local_path',
+ :folders => [self.class.folder_config]
+ }
+ end
+ let(:username) { 'username@gmail.com' }
- context "with specific server" do
- it 'should login to the imap server' do
- imap = stub('Net::IMAP')
- Net::IMAP.should_receive(:new).with('my.imap.example.com', 993, true).and_return(imap)
- imap.should_receive('login').with('myuser', 'secret')
+ before do
+ allow(Net::IMAP).to receive(:new).and_return(imap)
+ end
- Imap::Backup::Account::Connection.new(:username => 'myuser', :password => 'secret', :server => 'my.imap.example.com')
- end
+ subject { Imap::Backup::Account::Connection.new(options) }
+
+ shared_examples 'connects to IMAP' do |username = 'username@gmail.com', server = 'imap.gmail.com'|
+ it 'sets up the IMAP connection' do
+ expect(Net::IMAP).to have_received(:new).with(server, {:port => 993, :ssl => true})
end
+
+ it 'logs in to the imap server' do
+ expect(imap).to have_received(:login).with(username, 'password')
+ end
end
- context 'instance methods' do
- before :each do
- @imap = stub('Net::IMAP', :login => nil)
- Net::IMAP.stub!(:new).and_return(@imap)
- @account = {
- :username => 'myuser',
- :password => 'secret',
- :folders => [{:name => 'my_folder'}],
- :local_path => '/base/path',
- }
+ context '#initialize' do
+ [
+ [:username, 'username@gmail.com'],
+ [:local_path, 'local_path'],
+ [:backup_folders, [folder_config]]
+ ].each do |attr, expected|
+ its(attr) { should eq(expected) }
end
+ end
- subject { Imap::Backup::Account::Connection.new(@account) }
+ [
+ ['GMail', 'user@gmail.com', 'imap.gmail.com'],
+ ['Fastmail', 'user@fastmail.fm', 'mail.messagingengine.com'],
+ ].each do |service, email_username, server|
+ context service do
+ let(:username) { email_username }
- context '#disconnect' do
- it 'should disconnect from the server' do
- @imap.should_receive(:disconnect)
+ before { allow(imap).to receive(:disconnect) }
+ before { subject.disconnect }
- subject.disconnect
- end
+ include_examples 'connects to IMAP', [email_username, server]
end
+ end
- context '#folders' do
- it 'should list all folders' do
- @imap.should_receive(:list).with('/', '*')
+ context '#folders' do
+ let(:folders) { 'folders' }
- subject.folders
- end
+ before { allow(imap).to receive(:list).and_return(folders) }
+
+ it 'returns the list of folders' do
+ expect(subject.folders).to eq(folders)
end
+ end
- context '#status' do
- before :each do
- @folder = stub('Imap::Backup::Account::Folder', :uids => [])
- Imap::Backup::Account::Folder.stub!(:new).with(subject, 'my_folder').and_return(@folder)
- @serializer = stub('Imap::Backup::Serializer', :uids => [])
- Imap::Backup::Serializer::Directory.stub!(:new).with('/base/path', 'my_folder').and_return(@serializer)
- end
+ context '#status' do
+ let(:folder) { double('folder', :uids => [remote_uid]) }
+ let(:local_uid) { 'local_uid' }
+ let(:serializer) { double('serializer', :uids => [local_uid]) }
+ let(:remote_uid) { 'remote_uid' }
- it 'should return the names of folders' do
- subject.status[0][:name].should == 'my_folder'
- end
+ before do
+ allow(Imap::Backup::Account::Folder).to receive(:new).and_return(folder)
+ allow(Imap::Backup::Serializer::Directory).to receive(:new).and_return(serializer)
+ end
- it 'should list local message uids' do
- @serializer.should_receive(:uids).and_return([321, 456])
+ it 'should return the names of folders' do
+ expect(subject.status[0][:name]).to eq('backup_folder')
+ end
- subject.status[0][:local].should == [321, 456]
- end
+ it 'returns local message uids' do
+ expect(subject.status[0][:local]).to eq([local_uid])
+ end
- it 'should retrieve the available uids' do
- @folder.should_receive(:uids).and_return([101, 234])
-
- subject.status[0][:remote].should == [101, 234]
- end
+ it 'should retrieve the available uids' do
+ expect(subject.status[0][:remote]).to eq([remote_uid])
end
+ end
- context '#run_backup' do
- before :each do
- @folder = stub('Imap::Backup::Account::Folder', :uids => [])
- Imap::Backup::Account::Folder.stub!(:new).with(subject, 'my_folder').and_return(@folder)
- @serializer = stub('Imap::Backup::Serializer')
- Imap::Backup::Serializer::Mbox.stub!(:new).with('/base/path', 'my_folder').and_return(@serializer)
- @downloader = stub('Imap::Backup::Downloader', :run => nil)
- Imap::Backup::Downloader.stub!(:new).with(@folder, @serializer).and_return(@downloader)
- end
+ context '#disconnect' do
+ before { allow(imap).to receive(:disconnect) }
+ before { subject.disconnect }
- it 'should instantiate folders' do
- Imap::Backup::Account::Folder.should_receive(:new).with(subject, 'my_folder').and_return(@folder)
+ it 'disconnects from the server' do
+ expect(imap).to have_received(:disconnect).with()
+ end
- subject.run_backup
- end
+ include_examples 'connects to IMAP'
+ end
- it 'should instantiate serializers' do
- Imap::Backup::Serializer::Mbox.should_receive(:new).with('/base/path', 'my_folder').and_return(@serializer)
+ context '#run_backup' do
+ let(:folder) { double('folder') }
+ let(:serializer) { double('serializer') }
+ let(:downloader) { double('downloader', run: nil) }
- subject.run_backup
- end
+ before do
+ allow(Imap::Backup::Account::Folder).to receive(:new).and_return(folder)
+ allow(Imap::Backup::Serializer::Mbox).to receive(:new).and_return(serializer)
+ allow(Imap::Backup::Downloader).to receive(:new).and_return(downloader)
+ end
- it 'should instantiate downloaders' do
- Imap::Backup::Downloader.should_receive(:new).with(@folder, @serializer).and_return(@downloader)
+ before { subject.run_backup }
- subject.run_backup
- end
-
- it 'should run downloaders' do
- @downloader.should_receive(:run)
-
- subject.run_backup
- end
+ it 'runs downloaders' do
+ expect(downloader).to have_received(:run)
end
end
end