spec/unit/imap/backup/account/folder_spec.rb in imap-backup-4.0.2 vs spec/unit/imap/backup/account/folder_spec.rb in imap-backup-4.0.3

- old
+ new

@@ -4,21 +4,21 @@ FOLDER_NAME = "Gelöscht".freeze ENCODED_FOLDER_NAME = "Gel&APY-scht".freeze subject { described_class.new(connection, FOLDER_NAME) } - let(:imap) do + let(:client) do instance_double( - Net::IMAP, + Imap::Backup::Client::Default, append: append_response, create: nil, examine: nil, responses: responses ) end let(:connection) do - instance_double(Imap::Backup::Account::Connection, imap: imap) + instance_double(Imap::Backup::Account::Connection, client: client) end let(:missing_mailbox_data) do OpenStruct.new(text: "Unknown Mailbox: #{FOLDER_NAME}") end let(:missing_mailbox_response) { OpenStruct.new(data: missing_mailbox_data) } @@ -29,19 +29,19 @@ let(:append_response) { nil } describe "#uids" do let(:uids) { [5678, 123] } - before { allow(imap).to receive(:uid_search) { uids } } + before { allow(client).to receive(:uid_search) { uids } } it "lists available messages" do expect(subject.uids).to eq(uids.reverse) end context "with missing mailboxes" do before do - allow(imap).to receive(:examine). + allow(client).to receive(:examine). with(ENCODED_FOLDER_NAME).and_raise(missing_mailbox_error) end it "returns an empty array" do expect(subject.uids).to eq([]) @@ -52,11 +52,11 @@ let(:no_method_error) do NoMethodError.new("Somethimes SEARCH responses come out undefined") end before do - allow(imap).to receive(:examine). + allow(client).to receive(:examine). with(ENCODED_FOLDER_NAME).and_raise(missing_mailbox_error) end it "returns an empty array" do expect(subject.uids).to eq([]) @@ -64,58 +64,58 @@ end end describe "#fetch" do let(:message_body) { instance_double(String, force_encoding: nil) } - let(:attributes) { {"RFC822" => message_body, "other" => "xxx"} } + let(:attributes) { {"BODY[]" => message_body, "other" => "xxx"} } let(:fetch_data_item) do instance_double(Net::IMAP::FetchData, attr: attributes) end - before { allow(imap).to receive(:uid_fetch) { [fetch_data_item] } } + before { allow(client).to receive(:uid_fetch) { [fetch_data_item] } } it "returns the message" do - expect(subject.fetch(123)).to eq(attributes) + expect(subject.fetch(123)).to eq(message_body) end context "when the server responds with nothing" do - before { allow(imap).to receive(:uid_fetch) { nil } } + before { allow(client).to receive(:uid_fetch) { nil } } it "is nil" do expect(subject.fetch(123)).to be_nil end end context "when the mailbox doesn't exist" do before do - allow(imap).to receive(:examine). + allow(client).to receive(:examine). with(ENCODED_FOLDER_NAME).and_raise(missing_mailbox_error) end it "is nil" do expect(subject.fetch(123)).to be_nil end end - context "when the response doesn't have RFC822" do + context "when the response doesn't include 'BODY[]'" do let(:attributes) { {} } it "is nil" do expect(subject.fetch(123)).to be_nil end end context "when the first fetch_uid attempts fail" do before do outcomes = [-> { raise EOFError }, -> { [fetch_data_item] }] - allow(imap).to receive(:uid_fetch) { outcomes.shift.call } + allow(client).to receive(:uid_fetch) { outcomes.shift.call } end it "retries" do subject.fetch(123) - expect(imap).to have_received(:uid_fetch).twice + expect(client).to have_received(:uid_fetch).twice end it "succeeds" do subject.fetch(123) end @@ -135,11 +135,11 @@ end end context "when the folder doesn't exist" do before do - allow(imap).to receive(:examine). + allow(client).to receive(:examine). with(ENCODED_FOLDER_NAME).and_raise(missing_mailbox_error) end it "is false" do expect(subject.exist?).to be_falsey @@ -148,30 +148,30 @@ end describe "#create" do context "when the folder exists" do it "is does not create the folder" do - expect(imap).to_not receive(:create) + expect(client).to_not receive(:create) subject.create end end context "when the folder doesn't exist" do before do - allow(imap).to receive(:examine). + allow(client).to receive(:examine). with(ENCODED_FOLDER_NAME).and_raise(missing_mailbox_error) end it "creates the folder" do - expect(imap).to receive(:create) + expect(client).to receive(:create) subject.create end it "encodes the folder name" do - expect(imap).to receive(:create).with(ENCODED_FOLDER_NAME) + expect(client).to receive(:create).with(ENCODED_FOLDER_NAME) subject.create end end end @@ -183,11 +183,11 @@ expect(subject.uid_validity).to eq("uid validity") end context "when the folder doesn't exist" do before do - allow(imap).to receive(:examine). + allow(client).to receive(:examine). with(ENCODED_FOLDER_NAME).and_raise(missing_mailbox_error) end it "raises an error" do expect do @@ -209,16 +209,16 @@ let(:append_response) do OpenStruct.new(data: OpenStruct.new(code: OpenStruct.new(data: "1 2"))) end it "appends the message" do - expect(imap).to receive(:append) + expect(client).to receive(:append) subject.append(message) end it "sets the date and time" do - expect(imap).to receive(:append). + expect(client).to receive(:append). with(anything, anything, anything, message_date) subject.append(message) end