Skip to content

Commit ca70d10

Browse files
committed
tests: covered the new use case and other tests have been adapted
the transfer between users from different organisations has been carefully tested and tests like those of transfer_factory which required a parameter. Issue #638.
1 parent 11048ec commit ca70d10

File tree

3 files changed

+173
-45
lines changed

3 files changed

+173
-45
lines changed

spec/controllers/transfers_controller_spec.rb

+162-41
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
let (:member_admin) { Fabricate(:member, organization: test_organization, manager: true) }
44
let (:member_giver) { Fabricate(:member, organization: test_organization) }
55
let (:member_taker) { Fabricate(:member, organization: test_organization) }
6+
let (:second_organization) { Fabricate(:organization) }
7+
let (:second_member_taker) { Fabricate(:member, organization: second_organization) }
68

79
describe '#new' do
810
let(:user) { member_giver.user }
@@ -51,6 +53,28 @@
5153
end
5254
end
5355

56+
context 'when the offer is specified and belongs to another organization' do
57+
let(:other_offer) { Fabricate(:offer, organization: second_organization) }
58+
59+
it 'finds the transfer offer' do
60+
dest_acc_id = second_member_taker.user.accounts.first.id
61+
get :new, params: params.merge(offer: other_offer.id,
62+
organization_id: other_offer.organization.id,
63+
destination_account_id: dest_acc_id)
64+
65+
expect(response.body).to include("<h3>#{other_offer}</h3>")
66+
end
67+
68+
it 'builds a transfer with the offer as post' do
69+
dest_acc_id = second_member_taker.user.accounts.first.id
70+
get :new, params: params.merge(offer: other_offer.id,
71+
organization_id: other_offer.organization.id,
72+
destination_account_id: dest_acc_id)
73+
74+
expect(response.body).to include("<input class=\"form-control hidden form-control\" type=\"hidden\" value=\"#{other_offer.id}\" name=\"transfer[post_id]\" id=\"transfer_post_id\" />")
75+
end
76+
end
77+
5478
context 'when the offer is not specified' do
5579
it 'does not find any offer' do
5680
get :new, params: params
@@ -142,26 +166,68 @@
142166
} }
143167
end
144168

145-
let(:user) { member_admin.user }
146-
147-
it 'creates a new Transfer' do
148-
expect { post_create }.to change(Transfer, :count).by 1
169+
subject(:create_between_orgs) do
170+
post 'create', params: { transfer: {
171+
source: member_giver.account.id,
172+
destination: second_member_taker.account.id,
173+
amount: 5
174+
} }
149175
end
150176

151-
it 'creates two Movements' do
152-
expect { post_create }.to change { Movement.count }.by 2
177+
let(:user) { member_admin.user }
178+
context 'the transfer is within the same organisation' do
179+
it 'creates a new Transfer' do
180+
expect { post_create }.to change(Transfer, :count).by 1
181+
end
182+
183+
it 'creates two Movements' do
184+
expect { post_create }.to change { Movement.count }.by 2
185+
end
186+
187+
it 'updates the balance of both accounts' do
188+
expect do
189+
post_create
190+
member_giver.reload
191+
end.to change { member_giver.account.balance.to_i }.by -5
192+
193+
expect do
194+
post_create
195+
member_taker.reload
196+
end.to change { member_taker.account.balance.to_i }.by 5
197+
end
153198
end
154199

155-
it 'updates the balance of both accounts' do
156-
expect do
157-
post_create
158-
member_giver.reload
159-
end.to change { member_giver.account.balance.to_i }.by -5
160-
161-
expect do
162-
post_create
163-
member_taker.reload
164-
end.to change { member_taker.account.balance.to_i }.by 5
200+
context 'the transfer is between members of different organisations' do
201+
it 'creates three news Transfers' do
202+
expect { create_between_orgs }.to change(Transfer, :count).by 3
203+
end
204+
205+
it 'creates six Movements' do
206+
expect { create_between_orgs }.to change { Movement.count }.by 6
207+
end
208+
209+
it 'updates the balance of both accounts' do
210+
expect do
211+
create_between_orgs
212+
member_giver.reload
213+
end.to change { member_giver.account.balance.to_i }.by -5
214+
215+
expect do
216+
create_between_orgs
217+
second_member_taker.reload
218+
end.to change { second_member_taker.account.balance.to_i }.by 5
219+
end
220+
221+
it 'updates the global balance of both organizations' do
222+
create_between_orgs
223+
224+
expect(test_organization.global_balance).to equal -5
225+
expect(second_organization.global_balance).to equal 5
226+
end
227+
228+
it 'redirects to source user' do
229+
expect(create_between_orgs).to redirect_to(member_giver.user)
230+
end
165231
end
166232
end
167233

@@ -173,48 +239,103 @@
173239
} }
174240
end
175241

176-
let(:user) { member_giver.user }
177-
178-
it 'creates a new Transfer' do
179-
expect { post_create }.to change(Transfer, :count).by 1
180-
end
181-
182-
it 'creates two Movements' do
183-
expect { post_create }.to change { Movement.count }.by 2
242+
subject(:create_between_orgs) do
243+
post 'create', params: { transfer: {
244+
destination: second_member_taker.account.id,
245+
amount: 5
246+
} }
184247
end
185248

186-
it 'updates the balance of both accounts' do
187-
expect do
188-
post_create
189-
member_giver.reload
190-
end.to change { member_giver.account.balance.to_i }.by -5
191-
192-
expect do
193-
post_create
194-
member_taker.reload
195-
end.to change { member_taker.account.balance.to_i }.by 5
249+
let(:user) { member_giver.user }
250+
context 'the transfer is within the same organisation' do
251+
it 'creates a new Transfer' do
252+
expect { post_create }.to change(Transfer, :count).by 1
253+
end
254+
255+
it 'creates two Movements' do
256+
expect { post_create }.to change { Movement.count }.by 2
257+
end
258+
259+
it 'updates the balance of both accounts' do
260+
expect do
261+
post_create
262+
member_giver.reload
263+
end.to change { member_giver.account.balance.to_i }.by -5
264+
265+
expect do
266+
post_create
267+
member_taker.reload
268+
end.to change { member_taker.account.balance.to_i }.by 5
269+
end
270+
271+
it 'redirects to destination' do
272+
expect(post_create).to redirect_to(member_taker.user)
273+
end
196274
end
197275

198-
it 'redirects to destination' do
199-
expect(post_create).to redirect_to(member_taker.user)
276+
context 'the transfer is to a member of another organizations' do
277+
it 'creates three news Transfers' do
278+
expect { create_between_orgs }.to change(Transfer, :count).by 3
279+
end
280+
281+
it 'creates six Movements' do
282+
expect { create_between_orgs }.to change { Movement.count }.by 6
283+
end
284+
285+
it 'updates the balance of both accounts' do
286+
expect do
287+
create_between_orgs
288+
member_giver.reload
289+
end.to change { member_giver.account.balance.to_i }.by -5
290+
291+
expect do
292+
create_between_orgs
293+
second_member_taker.reload
294+
end.to change { second_member_taker.account.balance.to_i }.by 5
295+
end
296+
297+
it 'updates the global balance of both organizations' do
298+
create_between_orgs
299+
300+
expect(test_organization.global_balance).to equal -5
301+
expect(second_organization.global_balance).to equal 5
302+
end
303+
304+
it 'redirects to source' do
305+
expect(create_between_orgs).to redirect_to(member_giver.user)
306+
end
200307
end
201308
end
202309
end
203310

204311
context 'with invalid params' do
205312
let(:user) { member_giver.user }
206313
let(:referer) { "/transfers/new?destination_account_id=#{member_taker.account.id}" }
314+
let(:second_referer) { "/transfers/new?destination_account_id=#{member_taker.account.id}&organization_id=#{second_organization.id}" }
207315

208316
before do
209317
request.env["HTTP_REFERER"] = referer
210318
end
211319

212-
it 'does not create any Transfer and redirects to back if the amount is 0' do
213-
expect {
214-
post(:create, params: { transfer: { amount: 0, destination: member_taker.account.id } })
215-
}.not_to change(Transfer, :count)
320+
context 'the transfer is to a member of the same organization' do
321+
it 'does not create any Transfer and redirects to back if the amount is 0' do
322+
expect {
323+
post(:create, params: { transfer: { amount: 0, destination: member_taker.account.id } })
324+
}.not_to change(Transfer, :count)
325+
326+
expect(response).to redirect_to(referer)
327+
end
328+
end
329+
330+
context 'the transfer is to a member of another organization' do
331+
it 'does not create any Transfer and redirects to back if the amount is 0' do
332+
request.env["HTTP_REFERER"] = second_referer
333+
expect {
334+
post(:create, params: { transfer: { amount: 0, destination: second_member_taker.account.id } })
335+
}.not_to change(Transfer, :count)
216336

217-
expect(response).to redirect_to(referer)
337+
expect(response).to redirect_to(second_referer)
338+
end
218339
end
219340
end
220341
end

spec/models/transfer_factory_spec.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
organization,
55
current_user,
66
offer_id,
7-
destination_account_id
7+
destination_account_id,
8+
destination_organization_id
89
)
910
end
1011

1112
let(:organization) { Fabricate(:organization) }
1213
let(:current_user) { Fabricate(:user) }
1314
let(:organization_offer) { Fabricate(:offer, organization: organization) }
1415
let(:destination_account_id) { nil }
16+
let(:destination_organization_id) { organization.id }
1517

1618
describe '#offer' do
1719
subject { transfer_factory.offer }
@@ -32,6 +34,7 @@
3234

3335
let(:offer_id) { organization_offer.id }
3436
let(:destination_account_id) { destination_account.id }
37+
let(:destination_organization_id) { organization.id }
3538

3639
context 'when the destination account belongs to an organization' do
3740
let(:organization) { Fabricate(:organization) }
@@ -77,6 +80,7 @@
7780
subject { transfer_factory.transfer_sources }
7881

7982
let(:offer_id) { organization_offer.id }
83+
let(:destination_organization_id) { organization.id }
8084

8185
let!(:active_member) do
8286
Fabricate(:member, organization: organization, active: true)

spec/views/offers/show.html.erb_spec.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,18 @@
6565
allow(view).to receive(:current_organization) { another_organization }
6666
end
6767

68-
it 'doesn\'t render a link to the transfer page' do
68+
it 'render a link to the transfer page with the id of the destination organisation' do
6969
assign :offer, offer
70+
assign :destination_account, destination_account
7071
render template: 'offers/show'
7172

72-
expect(rendered).to_not have_link(
73+
expect(rendered).to have_link(
7374
t('offers.show.give_time_for'),
7475
href: new_transfer_path(
7576
id: offer.user.id,
7677
offer: offer.id,
77-
destination_account_id: destination_account.id
78+
destination_account_id: destination_account.id,
79+
organization_id: organization.id
7880
)
7981
)
8082
end
@@ -89,6 +91,7 @@
8991

9092
it 'displays the offer\'s organization' do
9193
assign :offer, offer
94+
assign :destination_account, destination_account
9295
render template: 'offers/show'
9396

9497
expect(rendered).to include(

0 commit comments

Comments
 (0)