def post(self, creditor_reservation_request): """Reserve an auto-generated creditor ID. **Note:** The reserved creditor ID will be a random valid creditor ID. """ for _ in range(100): creditor_id = procedures.generate_new_creditor_id() try: creditor = procedures.reserve_creditor( creditor_id, verify_correctness=False) break except procedures.CreditorExists: # pragma: no cover pass else: # pragma: no cover abort(500, message='Can not generate a valid creditor ID.') return creditor
def post(self, creditor_activation_request, creditorId): """Activate a creditor.""" reservation_id = creditor_activation_request.get( 'optional_reservation_id') try: if reservation_id is None: reservation_id = procedures.reserve_creditor( creditorId).reservation_id assert reservation_id is not None creditor = procedures.activate_creditor(creditorId, reservation_id) except procedures.CreditorExists: abort(409) except procedures.InvalidReservationId: abort(422, errors={'json': {'reservationId': ['Invalid ID.']}}) except procedures.InvalidCreditor: # pragma: no cover abort(500, message='The agent is not responsible for this creditor.') return creditor
def post(self, creditor_reservation_request, creditorId): """Try to reserve a specific creditor ID. **Note:** The reserved creditor ID will be the same as the `creditorId` specified in the path. --- Will fail if the creditor already exists. """ try: creditor = procedures.reserve_creditor(creditorId) except procedures.CreditorExists: abort(409) except procedures.InvalidCreditor: # pragma: no cover abort(500, message='The agent is not responsible for this creditor.') return creditor
def test_activate_new_creditor(db_session): with pytest.raises(p.InvalidCreditor): creditor = p.reserve_creditor(models.MAX_INT64 + 1) creditor = p.reserve_creditor(C_ID) assert creditor.creditor_id == C_ID assert not creditor.is_activated assert len(Creditor.query.all()) == 1 with pytest.raises(p.CreditorExists): p.reserve_creditor(C_ID) assert not p.get_active_creditor(C_ID) with pytest.raises(p.InvalidReservationId): p.activate_creditor(C_ID, -123) p.activate_creditor(C_ID, creditor.reservation_id) creditor = p.get_active_creditor(C_ID) assert creditor assert creditor.is_activated with pytest.raises(p.CreditorExists): p.reserve_creditor(C_ID)
def creditor(db_session): creditor = p.reserve_creditor(C_ID) p.activate_creditor(C_ID, creditor.reservation_id) return creditor
def _create_new_creditor(creditor_id: int, activate: bool = False): creditor = p.reserve_creditor(creditor_id) if activate: p.activate_creditor(creditor_id, creditor.reservation_id)