def _validate_cuenta(cls, v): if len(v) == 18: if not clabe.validate_clabe(v): raise ValueError('cuenta no es una válida CLABE') elif not len(v) in {10, 15, 16}: raise ValueError('cuenta no es válida') return v
def test_generate_new_clabes(): num_clabes = 10 prefix = '64618000011' clabes = generate_new_clabes(10, prefix) assert len(clabes) == num_clabes for clabe in clabes: assert clabe.startswith(prefix) assert validate_clabe(clabe)
def execute(order_val: dict): # Get version version = 0 if "version" in order_val: version = order_val['version'] if time_in_range(START_DOWNTIME, STOP_DOWNTIME, datetime.utcnow().time()): raise ScheduleError transaction = Transaction() try: input = factory.create(version, **order_val) transaction = input.transform() if not clabe.validate_clabe(transaction.cuenta_beneficiario) and ( not luhnmod10.valid(transaction.cuenta_beneficiario)): raise MalformedOrderException() except (MalformedOrderException, TypeError, ValueError): transaction.set_state(Estado.error) transaction.save() raise MalformedOrderException() try: prev_trx = Transaction.objects.get(speid_id=transaction.speid_id) # Si la transacción ya esta como succeeded termina # Puede suceder cuando se corre la misma tarea tiempo después # Y la transacción ya fue confirmada por stp assert prev_trx.estado != Estado.succeeded transaction = prev_trx transaction.events.append(Event(type=EventType.retry)) except DoesNotExist: transaction.events.append(Event(type=EventType.created)) transaction.save() pass except AssertionError: # Para evitar que se vuelva a mandar o regresar se manda la excepción raise ResendSuccessOrderException() if transaction.monto > MAX_AMOUNT: transaction.events.append(Event(type=EventType.error)) transaction.save() raise MalformedOrderException() now = datetime.utcnow() try: # Return transaction after 2 hours of creation assert (now - transaction.created_at) < timedelta(hours=2) transaction.create_order() except ( AssertionError, InvalidAccountType, InvalidAmount, InvalidInstitution, InvalidTrackingKey, PldRejected, ValidationError, ): transaction.set_state(Estado.failed) transaction.save()
def test_invalid_clabe(): invalid_clabe = '072691004495711490' assert not clabe.validate_clabe(invalid_clabe) with pytest.raises(ValidationError) as exc_info: create_orden(cuentaBeneficiario=invalid_clabe) errors = exc_info.value.errors() assert len(errors) == 1 error = errors[0] assert error['loc'] == ('cuentaBeneficiario', ) assert error['type'] == 'value_error'
def execute(order_val): # Get version version = 0 if "version" in order_val: version = order_val['version'] transaction = Transaction() try: input = factory.create(version, **order_val) transaction = input.transform() if not clabe.validate_clabe(transaction.cuenta_beneficiario) and ( not luhnmod10.valid(transaction.cuenta_beneficiario)): raise MalformedOrderException() except (MalformedOrderException, TypeError, ValueError): transaction.set_state(Estado.error) transaction.save() raise MalformedOrderException() try: prev_trx = Transaction.objects.get(speid_id=transaction.speid_id) transaction = prev_trx transaction.events.append(Event(type=EventType.retry)) except DoesNotExist: transaction.events.append(Event(type=EventType.created)) pass if transaction.monto > MAX_AMOUNT: transaction.events.append(Event(type=EventType.error)) transaction.save() raise MalformedOrderException() order = transaction.get_order() transaction.save() # Send order to STP order.monto = order.monto / 100 res = order.registra() if res is not None and res.id > 0: transaction.stp_id = res.id transaction.events.append( Event(type=EventType.completed, metadata=str(res))) else: transaction.events.append( Event(type=EventType.error, metadata=str(res))) transaction.save()
def test_validate_clabe(): assert clabe.validate_clabe(VALID_CLABE) assert not clabe.validate_clabe(INVALID_CLABE)
def test_validate_clabe(): assert validate_clabe(VALID_CLABE) assert not validate_clabe(INVALID_CLABE_BANK_CODE) assert not validate_clabe(INVALID_CLABE_CONTROL_DIGIT)