def test_order_not_recurrent_charge(request_mock, charge_mock, pk, assert_func): order = OrderFactory(id=pk, recurrent=False) result = OrderProcessing(request_mock, order).process() order.refresh_from_db() getattr(charge_mock, assert_func)() assert order.status == Order.SHIPPED assert 'error' not in result
def test_process_order_by_credits(request_mock, charge_mock): order = OrderFactory(recurrent=True, amount=20, customer__amount=25) result = OrderProcessing(request_mock, order).process() order.refresh_from_db() charge_mock.assert_not_called() assert order.status == Order.SHIPPED assert result['processed_by'] == 'credits' assert order.customer.amount == 5 assert order.amount == 0
def test_process_order_by_charge_unexpected_error(charge_mock, request_mock): charge_mock.side_effect = Exception('Error that will never occur ;)') order = OrderFactory(recurrent=True) result = OrderProcessing(request_mock, order).process() order.refresh_from_db() charge_mock.assert_called_once() assert order.status == Order.ERROR assert result.get('processed_by') is None assert result['error'] == ( "Critical Stripe error: Exception(u'Error that will never occur ;)',)")
def test_process_order_as_present(request_mock, charge_mock): order = OrderFactory(recurrent=True, customer__preferences__present_next=True) result = OrderProcessing(request_mock, order).process() order.refresh_from_db() charge_mock.assert_not_called() assert order.customer.preferences.present_next is False assert result['processed_by'] == 'present' assert order.status == Order.SHIPPED assert order.amount == 0
def test_api_unauthorized(mocker, json_client): order = OrderFactory() assert order.status == order.ACTIVE processing_mock = mocker.patch('manager.utils.OrderProcessing.process') resp = json_client.post('/manager/api/commands/processOrder/%d' % order.id, {'orderType': 'COFFEE'}) processing_mock.assert_not_called() order.refresh_from_db() assert order.status == order.ACTIVE assert resp.status_code == 302 assert '/manager?next=/' in resp.url
def test_process_order_by_charge(request_mock, charge_mock): order = OrderFactory(amount=14.15, recurrent=True) result = OrderProcessing(request_mock, order).process() order.refresh_from_db() charge_mock.assert_called_once_with(amount=1415, currency='SGD', customer=order.customer.stripe_id, description=str(order.coffee), metadata={'order_id': order.id}) assert order.status == Order.SHIPPED assert result['processed_by'] == 'card'
def test_api_one_coffee_order(charge_mock, admin_json_client): order = OrderFactory(id=18052) assert order.status == order.ACTIVE resp = admin_json_client.post( '/manager/api/commands/processOrder/%d' % order.id, {'orderType': 'COFFEE'}) charge_mock.assert_not_called() order.refresh_from_db() assert order.status == order.SHIPPED assert resp.status_code == 200 assert resp.content.startswith(b'%PDF')
def test_api_sub_coffee_order(charge_mock, admin_json_client): order = OrderFactory(recurrent=True, amount=14.25) assert order.status == order.ACTIVE resp = admin_json_client.post( '/manager/api/commands/processOrder/%d' % order.id, {'orderType': 'COFFEE'}) charge_mock.assert_called_once_with(amount=1425, currency='SGD', customer=order.customer.stripe_id, description=str(order.coffee), metadata={'order_id': order.id}) order.refresh_from_db() assert order.status == order.SHIPPED assert resp.status_code == 200 assert resp.content.startswith(b'%PDF')
def test_process_order_by_charge_and_credits_declined(request_mock, charge_mock): charge_mock.side_effect = _raise_card_was_declined order = OrderFactory(recurrent=True, amount=20, customer__amount=5) result = OrderProcessing(request_mock, order).process() order.refresh_from_db() order.customer.refresh_from_db() charge_mock.assert_called_once_with(amount=1500, currency='SGD', customer=order.customer.stripe_id, description=str(order.coffee), metadata={'order_id': order.id}) assert order.status == Order.DECLINED assert result.get('processed_by') is None # after a declined payment, the amount of credits on customer's account # and the order amount needs to be restored to its original value assert order.customer.amount == 5 assert order.amount == 20
def test_process_order_by_charge_declined(charge_mock, request_mock, mailoutbox): charge_mock.side_effect = _raise_card_was_declined order = OrderFactory(recurrent=True) result = OrderProcessing(request_mock, order).process() order.refresh_from_db() charge_mock.assert_called_once() assert order.status == Order.DECLINED assert result.get('processed_by') is None assert result['declined'] is True assert result['error'] == 'Your card was declined.' # check email which sent when card was declined assert len(mailoutbox) == 1 m = mailoutbox[0] assert m.subject == OrderProcessing.EMAIL_DECLINED_SUBJECT assert m.template_name == OrderProcessing.EMAIL_DECLINED_SUBJECT assert m.from_email == 'Hook Coffee <*****@*****.**>' assert list(m.to) == [order.customer.user.email]
def test_api_sub_coffee_order_declined(charge_mock, admin_json_client): charge_mock.side_effect = _raise_card_was_declined order = OrderFactory(recurrent=True, amount=14.25) assert order.status == order.ACTIVE resp = admin_json_client.post( '/manager/api/commands/processOrder/%d' % order.id, {'orderType': 'COFFEE'}) charge_mock.assert_called_once_with(amount=1425, currency='SGD', customer=order.customer.stripe_id, description=str(order.coffee), metadata={'order_id': order.id}) order.refresh_from_db() assert order.status == Order.DECLINED assert resp.status_code == 402 error = ('An error occurred while processing the order!\n\n' 'Order: {pk}\nUsername/Email: {name}\nAccount number: {acc}\n' 'Error: {err}').format(pk=order.pk, name=order.customer, acc=order.customer.stripe_id, err='Your card was declined.') assert resp.json() == {'error': error}
def test_api_sub_coffee_order_unexpected_error(charge_mock, admin_json_client): charge_mock.side_effect = Exception('Error that will never occur ;)') order = OrderFactory(recurrent=True, amount=14.25) assert order.status == order.ACTIVE resp = admin_json_client.post( '/manager/api/commands/processOrder/%d' % order.id, {'orderType': 'COFFEE'}) charge_mock.assert_called_once_with(amount=1425, currency='SGD', customer=order.customer.stripe_id, description=str(order.coffee), metadata={'order_id': order.id}) order.refresh_from_db() assert order.status == Order.ERROR assert resp.status_code == 406 error = ('An error occurred while processing the order!\n\n' 'Order: {pk}\nUsername/Email: {name}\nAccount number: {acc}\n' 'Error: {err}').format( pk=order.pk, name=order.customer, acc=order.customer.stripe_id, err=("Critical Stripe error: " "Exception(u'Error that will never occur ;)',)")) assert resp.json() == {'error': error}