From 4f59f9d458a1b6e7137621fbd30073af80a731d6 Mon Sep 17 00:00:00 2001 From: franpb14 Date: Sun, 16 May 2021 14:32:56 +0200 Subject: [PATCH] tests: added tests for the case where the organisation is the source. Issue #638 --- spec/controllers/transfers_controller_spec.rb | 97 +++++++++++++------ 1 file changed, 70 insertions(+), 27 deletions(-) diff --git a/spec/controllers/transfers_controller_spec.rb b/spec/controllers/transfers_controller_spec.rb index d3c8a7d6..7becc25f 100644 --- a/spec/controllers/transfers_controller_spec.rb +++ b/spec/controllers/transfers_controller_spec.rb @@ -174,6 +174,14 @@ } } end + subject(:create_between_src_org) do + post 'create', params: { transfer: { + source: test_organization.account.id, + destination: second_member_taker.account.id, + amount: 5 + } } + end + let(:user) { member_admin.user } context 'the transfer is within the same organisation' do it 'creates a new Transfer' do @@ -197,36 +205,71 @@ end end - context 'the transfer is between members of different organisations' do - it 'creates three news Transfers' do - expect { create_between_orgs }.to change(Transfer, :count).by 3 - end - - it 'creates six Movements' do - expect { create_between_orgs }.to change { Movement.count }.by 6 - end - - it 'updates the balance of both accounts' do - expect do - create_between_orgs - member_giver.reload - end.to change { member_giver.account.balance.to_i }.by -5 - - expect do + context 'the transfer is between different organisations' do + context 'the source is a member' do + it 'creates three news Transfers' do + expect { create_between_orgs }.to change(Transfer, :count).by 3 + end + + it 'creates six Movements' do + expect { create_between_orgs }.to change { Movement.count }.by 6 + end + + it 'updates the balance of both accounts' do + expect do + create_between_orgs + member_giver.reload + end.to change { member_giver.account.balance.to_i }.by -5 + + expect do + create_between_orgs + second_member_taker.reload + end.to change { second_member_taker.account.balance.to_i }.by 5 + end + + it 'updates the global balance of both organizations' do create_between_orgs - second_member_taker.reload - end.to change { second_member_taker.account.balance.to_i }.by 5 - end - it 'updates the global balance of both organizations' do - create_between_orgs + expect(test_organization.global_balance).to equal -5 + expect(second_organization.global_balance).to equal 5 + end - expect(test_organization.global_balance).to equal -5 - expect(second_organization.global_balance).to equal 5 + it 'redirects to destination organization' do + expect(create_between_orgs).to redirect_to(second_member_taker.organization) + end end - it 'redirects to source user' do - expect(create_between_orgs).to redirect_to(member_giver.user) + context 'the source is a organization' do + it 'creates two news Transfers' do + expect { create_between_src_org }.to change(Transfer, :count).by 2 + end + + it 'creates four Movements' do + expect { create_between_src_org }.to change { Movement.count }.by 4 + end + + it 'updates the balance of both accounts' do + expect do + create_between_src_org + test_organization.reload + end.to change { test_organization.account.balance.to_i }.by -5 + + expect do + create_between_src_org + second_member_taker.reload + end.to change { second_member_taker.account.balance.to_i }.by 5 + end + + it 'updates the global balance of both organizations' do + create_between_src_org + + expect(test_organization.global_balance).to equal -5 + expect(second_organization.global_balance).to equal 5 + end + + it 'redirects to destination organization' do + expect(create_between_src_org).to redirect_to(second_member_taker.organization) + end end end end @@ -301,8 +344,8 @@ expect(second_organization.global_balance).to equal 5 end - it 'redirects to source' do - expect(create_between_orgs).to redirect_to(member_giver.user) + it 'redirects to destination organization' do + expect(create_between_orgs).to redirect_to(second_member_taker.organization) end end end