Skip to content

Instantly share code, notes, and snippets.

@InsilicoSoft
Created August 3, 2017 08:05
Show Gist options
  • Select an option

  • Save InsilicoSoft/c7e3990103c4c16cf04f1dece6271771 to your computer and use it in GitHub Desktop.

Select an option

Save InsilicoSoft/c7e3990103c4c16cf04f1dece6271771 to your computer and use it in GitHub Desktop.
Specs for payment model
describe Payment do
let(:payment) { create(:payment) }
let(:payment_with_order) { create(:payment, :with_future_order) }
context 'state machine' do
it 'is in pending state on creation' do
expect(payment.pending?).to eq true
end
it 'transfers to completed state with #complete' do
payment.complete!
expect(payment.completed?).to eq true
end
it 'creates balance replenishment after payment is complete' do
expect { payment.complete }.to change { payment.user.balance_transactions.replenishment.count }.by 1
end
it 'creates withdrawal if has future order' do
expect do
payment_with_order.complete
end.to change { payment_with_order.user.balance_transactions.withdrawal.count }.by 1
end
it 'sets replenishment to the amount of payment after completion' do
payment.complete
expect(payment.user.balance_transactions.replenishment.last.amount).to eq payment.amount
end
it 'sets replenishment and withdrawal to payment amount after completion if future order is present' do
payment_with_order.complete
expect(payment_with_order.user.balance_transactions.replenishment.last.amount).to eq payment_with_order.amount
expect(payment_with_order.user.balance_transactions.withdrawal.last.amount).to eq payment_with_order.amount
end
it 'transfers future order to paid_for state on completion' do
expect do
payment_with_order.complete
end.to change { payment_with_order.future_order.aasm_state }.from('pending').to('paid_for')
end
it 'transfers to canceled state with #cancel' do
payment.cancel!
expect(payment.canceled?).to eq true
end
it 'cancels future order if it is present' do
payment_with_order.cancel!
expect(payment_with_order.canceled?).to eq true
expect(payment_with_order.future_order.canceled?).to eq true
end
end
context 'payment service url' do
it 'raises error if payment is not persisted' do
payment = build(:payment)
expect do
payment.service_redirect_url
end.to raise_error { ActiveRecord::RecordNotSaved.new('Payment is not persisted') }
end
it 'returns payment url if payment is persisted' do
expect(payment.service_redirect_url).to be
end
it 'returns payment description as one of order if future order is present' do
expect(payment_with_order.send(:description)).to(
eq I18n.t('orders.payments.for_order', order_id: payment_with_order.future_order.id)
)
end
it 'returns payment description as one of balance if future order is not present' do
expect(payment.send(:description)).to(
eq I18n.t('orders.payments.for_balance')
)
end
end
context '#payment_service_redirect' do
it 'returns redirect url for balance if payment has no future order' do
result = :success
expect(payment.send(:payment_service_redirect, result)).to(
eq "#{Rails.configuration.payment_redirect_balance}?result=#{result}"
)
end
it 'returns redirect url for product if payment has future order' do
result = :success
expect(payment_with_order.send(:payment_service_redirect, result)).to(
eq "#{Rails.configuration.payment_redirect_product}/"\
"#{payment_with_order.future_order.product.friendly_id}?result=#{result}"
)
end
end
it 'successfully stores metadata' do
metadata = { hello: 'world', a: 23 }
payment.update(service_metadata: metadata)
expect(payment.reload.service_metadata).to eq metadata.stringify_keys
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment