spec/lib/myfinance/resources/payable_account_spec.rb in myfinance-0.7.0 vs spec/lib/myfinance/resources/payable_account_spec.rb in myfinance-1.0.0

- old
+ new

@@ -1,10 +1,86 @@ require "spec_helper" describe Myfinance::Resources::PayableAccount do let(:entity_id) { 3798 } + let(:pa_id) { 1235663 } + let(:page) { 2 } + let(:url) { subject.response.request.base_url } + let(:request_error) { Myfinance::RequestError } + describe "#find_all", vcr: true do + before :each do + subject.build + end + context "with pagination" do + subject { client.payable_accounts.find_all(entity_id, page) } + + it "returns 200 OK response code" do + expect(subject.response.code).to eq(200) + end + + it "returns a PayableAccount collection" do + expect(subject).to be_a(Myfinance::Entities::PayableAccountCollection) + end + + it "returns a paginated URL" do + expect(url).to include("?page=#{page}") + end + + context "with invalid page" do + subject { client.payable_accounts.find_all(entity_id, 42424242) } + + it "returns empty collection" do + expect(subject.collection).to be_empty + end + end + end + + context "without pagination" do + subject { client.payable_accounts.find_all(entity_id) } + + it "returns 200 OK response code" do + expect(subject.response.code).to eq(200) + end + + it "returns a PayableAccount collection" do + expect(subject).to be_a(Myfinance::Entities::PayableAccountCollection) + end + + it "returns a paginated URL" do + expect(url).not_to include("?page=#{page}") + end + + context "with invalid entity id" do + it "raises 404 not found error" do + expect { client.payable_accounts.find_all(nil) }.to raise_error(request_error) do |e| + expect(e.code).to eq(404) + expect(e.message).to eq("Not Found") + end + end + end + end + end + + describe "#find", vcr: true do + subject { client.payable_accounts.find(entity_id, pa_id) } + + it "returns a PayableAccount" do + expect(subject).to be_a(Myfinance::Entities::PayableAccount) + end + + context "with invalid ID" do + subject { client.payable_accounts.find(nil, nil) } + + it "raises 404 not found error" do + expect { subject }.to raise_error(request_error) do |e| + expect(e.code).to eq(404) + end + end + end + end + describe "#create", vcr: true do let(:params) { { due_date: '2015-08-15', amount: 150.99 } } subject { client.payable_accounts.create(entity_id, params) } context "with success" do @@ -59,30 +135,32 @@ end context "when any data is invalid" do let(:params) { { due_date: '', amount: 150.99 } } - it "raises Myfinance::RequestError" do - expect { subject }.to raise_error(Myfinance::RequestError) + it "raises request_error" do + expect { subject }.to raise_error(request_error) end it "adds information on request error object" do - expect(Myfinance::RequestError).to receive(:new).with(code: 422, message: "", body: { "competency_month" => ["não pode ser vazio"], "due_date" => ["não é uma data válida"] }).and_call_original - expect { subject }.to raise_error(Myfinance::RequestError) + body = { "competency_month" => ["não pode ser vazio"], "due_date" => ["não é uma data válida"] } + expect(request_error).to receive(:new).with(code: 422, message: "", body: body).and_call_original + expect { subject }.to raise_error(request_error) end end context "when entity does not exist" do subject { client.payable_accounts.create(555, params) } - it "raises Myfinance::RequestError" do - expect { subject }.to raise_error(Myfinance::RequestError) + it "raises request_error" do + expect { subject }.to raise_error(request_error) end it "adds information on request error object" do - expect(Myfinance::RequestError).to receive(:new).with(code: 403, message: "Forbidden", body: {"error" => "Você não tem permissão para acessar este recurso." }).and_call_original - expect { subject }.to raise_error(Myfinance::RequestError) + body = {"error" => "Você não tem permissão para acessar este recurso." } + expect(request_error).to receive(:new).with(code: 403, message: "Forbidden", body: body).and_call_original + expect { subject }.to raise_error(request_error) end end end describe "#pay", vcr: true do @@ -104,11 +182,11 @@ context "when any parameter is invalid" do let(:params) { { total_amount: nil, occurred_at: '2015-08-05', amount: 150.99 } } it "raises request error" do - expect { subject }.to raise_error(Myfinance::RequestError) + expect { subject }.to raise_error(request_error) end end end describe "#undo_payment", vcr: true do @@ -125,40 +203,78 @@ expect(subject.discount_amount).to be_nil end end describe "#update", vcr: true do - subject { client.payable_accounts.update(1235050, entity_id, { amount: 100.00 }) } + subject { client.payable_accounts.update(entity_id, 1235050, { amount: 100.00 }) } context "when payable account exists" do it "returns true" do expect(subject).to be_truthy end end context "when payable account does not exist" do - subject { client.payable_accounts.update(9999999, entity_id, { amount: 100.00 }) } + subject { client.payable_accounts.update(entity_id, 9999999, { amount: 100.00 }) } it "raises request error" do - expect { subject }.to raise_error(Myfinance::RequestError) + expect { subject }.to raise_error(request_error) end end end describe "#destroy", vcr: true do - subject { client.payable_accounts.destroy(1215631, entity_id) } + subject { client.payable_accounts.destroy(entity_id, 1215631) } context "when payable account exists" do it "returns true" do expect(subject).to be_truthy end end context "when payable account does not exist" do - subject { client.payable_accounts.destroy(1215631099, entity_id) } + subject { client.payable_accounts.destroy(entity_id, 1215631099) } it "raises request error" do - expect { subject }.to raise_error(Myfinance::RequestError) + expect { subject }.to raise_error(request_error) + end + end + end + + describe "#destroy_recurrence", vcr: true do + let(:params) { { due_date: "2015-08-15", amount: 150.99, create_as_recurrent: "annual" } } + let(:new_pa) { client.payable_accounts.create(entity_id, params) } + + before :each do + client.payable_accounts.destroy_recurrence(entity_id, new_pa.id) + end + + it "does not find recurrent payable accounts" do + expect { client.payable_accounts.find(entity_id, new_pa.id) }.to raise_error(request_error) do |e| + expect(e.code).to eq(404) + expect(e.message).to eq("Not Found") + end + + expect { client.payable_accounts.find(entity_id, new_pa.id + 1) }.to raise_error(request_error) do |e| + expect(e.code).to eq(404) + expect(e.message).to eq("Not Found") + end + end + end + + describe "#destroy_many", vcr: true do + let(:params) { { due_date: "2015-08-15", amount: 150.99 } } + let(:new_pa) { client.payable_accounts.create(entity_id, params) } + let(:new_pa2) { client.payable_accounts.create(entity_id, params) } + + before :each do + client.payable_accounts.destroy_many(entity_id, [new_pa.id, new_pa2.id]) + end + + it "does not find created PayableAccount" do + expect { client.payable_accounts.find(entity_id, new_pa.id) }.to raise_error(request_error) do |e| + expect(e.code).to eq(404) + expect(e.message).to eq("Not Found") end end end end