def test_with_govuk_pay_erroring_when_refreshing(self, requests_mock): """ Test that if GOV.UK Pay cancels the payment but errors when refreshing the session, the session object is not updated (but the GOV.UK payment is still cancelled). This is okay as the session object will get refreshed at the next opportunity. """ session = PaymentGatewaySessionFactory() original_session_status = session.status requests_mock.post( govuk_url(f'payments/{session.govuk_payment_id}/cancel'), status_code=204, ) requests_mock.get( govuk_url(f'payments/{session.govuk_payment_id}'), status_code=500, ) with pytest.raises(GOVUKPayAPIException): session.cancel() session.refresh_from_db() assert session.status == original_session_status assert requests_mock.call_count == 2
def test_with_govuk_pay_erroring_when_cancelling(self, requests_mock): """ Test that if GOV.UK Pay errors when cancelling the payment, the session object is not updated. """ session = PaymentGatewaySessionFactory() original_session_status = session.status requests_mock.post( govuk_url(f'payments/{session.govuk_payment_id}/cancel'), status_code=500, ) with pytest.raises(GOVUKPayAPIException): session.cancel() session.refresh_from_db() assert session.status == original_session_status assert requests_mock.call_count == 1
def test_cancel_updates_session(self, requests_mock): """ Test that if GOV.UK Pay cancels and acknowledges the change, the session object is updated. """ session = PaymentGatewaySessionFactory() requests_mock.post( govuk_url(f'payments/{session.govuk_payment_id}/cancel'), status_code=204, ) requests_mock.get( govuk_url(f'payments/{session.govuk_payment_id}'), status_code=200, json={'state': {'status': 'cancelled'}}, ) session.cancel() session.refresh_from_db() assert session.status == PaymentGatewaySessionStatus.CANCELLED assert requests_mock.call_count == 2