|
3 | 3 | let (:member_admin) { Fabricate(:member, organization: test_organization, manager: true) }
|
4 | 4 | let (:member_giver) { Fabricate(:member, organization: test_organization) }
|
5 | 5 | let (:member_taker) { Fabricate(:member, organization: test_organization) }
|
| 6 | + let (:second_organization) { Fabricate(:organization) } |
| 7 | + let (:second_member_taker) { Fabricate(:member, organization: second_organization) } |
6 | 8 |
|
7 | 9 | describe '#new' do
|
8 | 10 | let(:user) { member_giver.user }
|
|
51 | 53 | end
|
52 | 54 | end
|
53 | 55 |
|
| 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 | + |
54 | 78 | context 'when the offer is not specified' do
|
55 | 79 | it 'does not find any offer' do
|
56 | 80 | get :new, params: params
|
|
142 | 166 | } }
|
143 | 167 | end
|
144 | 168 |
|
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 | + } } |
149 | 175 | end
|
150 | 176 |
|
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 |
153 | 198 | end
|
154 | 199 |
|
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 |
165 | 231 | end
|
166 | 232 | end
|
167 | 233 |
|
|
173 | 239 | } }
|
174 | 240 | end
|
175 | 241 |
|
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 | + } } |
184 | 247 | end
|
185 | 248 |
|
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 |
196 | 274 | end
|
197 | 275 |
|
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 |
200 | 307 | end
|
201 | 308 | end
|
202 | 309 | end
|
203 | 310 |
|
204 | 311 | context 'with invalid params' do
|
205 | 312 | let(:user) { member_giver.user }
|
206 | 313 | 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}" } |
207 | 315 |
|
208 | 316 | before do
|
209 | 317 | request.env["HTTP_REFERER"] = referer
|
210 | 318 | end
|
211 | 319 |
|
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) |
216 | 336 |
|
217 |
| - expect(response).to redirect_to(referer) |
| 337 | + expect(response).to redirect_to(second_referer) |
| 338 | + end |
218 | 339 | end
|
219 | 340 | end
|
220 | 341 | end
|
|
0 commit comments