Exemplo n.º 1
0
    def update_or_create(self, ctx, abstract_transaction: AbstractTransaction, id: Optional[int] = None) -> Tuple[Transaction, bool]:
        if abstract_transaction.src == abstract_transaction.dst:
            raise ValidationError('the source and destination accounts must not be the same')
        if abstract_transaction.value is None:
            raise ValidationError('the value field should not be None')
        if abstract_transaction.value <= 0:
            raise IntMustBePositive('value')

        if Roles.TRESO_WRITE.value not in ctx.get(CTX_ROLES):
            abstract_transaction.pending_validation = True

        transaction, created = super().update_or_create(ctx, abstract_transaction, id=id)

        if created:
            LOG.info('cashbox_update', extra=log_extra(
                ctx,
                value_modifier=abstract_transaction.value,
                transaction=transaction,
            ))
            if transaction.cashbox == "to":
                self.cashbox_repository.update(ctx, value_modifier=transaction.value, transaction=transaction)
            elif transaction.cashbox == "from":
                self.cashbox_repository.update(ctx, value_modifier=-transaction.value, transaction=transaction)
                
        return transaction, created
Exemplo n.º 2
0
 def test_update_readonly_field(self,
                     ctx,
                     transaction_manager: TransactionManager):
     req = AbstractTransaction(
         author=1,
     )
     with pytest.raises(ValidationError):
         transaction_manager.partially_update(ctx, req)
Exemplo n.º 3
0
    def test_happy_path(self,
                        ctx,
                        transaction_manager: TransactionManager):
        req = AbstractTransaction(
            name='test',
            attachments=None

        )
        with pytest.raises(NotImplementedError):
            transaction_manager.partially_update(ctx, req)
Exemplo n.º 4
0
    def test_happy_path(self,
                          ctx,
                          transaction_manager: TransactionManager):
        req = AbstractTransaction(
            src='1',
            dst='2',
            name='test',
            value=-1,
            payment_method='1',
            attachments=None

        )
        with raises(ValidationError):
            transaction_manager.update_or_create(ctx, req)
Exemplo n.º 5
0
    def test_happy_path_create(self,
                               ctx,
                               mock_transaction_repository: TransactionRepository,
                               transaction_manager: TransactionManager,
                               sample_transaction: Transaction):
        req = AbstractTransaction(
            src='1',
            dst='2',
            name='test',
            value=1,
            payment_method='1',
            attachments=None
        )
        mock_transaction_repository.create = MagicMock(return_value=(sample_transaction))

        _, c = transaction_manager.update_or_create(ctx, req)

        assert c is True
        mock_transaction_repository.create.assert_called_once_with(ctx, req)
Exemplo n.º 6
0
    def test_happy_path_create_only_admin(self,
                               ctx_only_admin,
                               mock_transaction_repository: TransactionRepository,
                               transaction_manager: TransactionManager,
                               sample_transaction_pending: Transaction):
        req = AbstractTransaction(
            src=sample_transaction_pending.src,
            dst=sample_transaction_pending.dst,
            name=sample_transaction_pending.name,
            value=sample_transaction_pending.value,
            payment_method=sample_transaction_pending.payment_method,
            attachments=sample_transaction_pending.attachments
        )
        mock_transaction_repository.create = MagicMock(return_value=(sample_transaction_pending))

        r, c = transaction_manager.update_or_create(ctx_only_admin, req)

        assert r.pending_validation
        assert c is True
        mock_transaction_repository.create.assert_called_once_with(ctx_only_admin, req)
Exemplo n.º 7
0
    def test_happy_path_update(self,
                               ctx,
                               mock_transaction_repository: TransactionRepository,
                               transaction_manager: TransactionManager,
                               sample_transaction: Transaction):
        req = AbstractTransaction(
            src=sample_transaction.src,
            dst=sample_transaction.dst,
            name=sample_transaction.name,
            value=sample_transaction.value,
            payment_method=sample_transaction.payment_method,
            attachments=sample_transaction.attachments
        )
        mock_transaction_repository.create = MagicMock(return_value=(sample_transaction))
        mock_transaction_repository.get_by_id = MagicMock(return_value=(sample_transaction))
        mock_transaction_repository.update = MagicMock(return_value=(sample_transaction))

        _, c = transaction_manager.update_or_create(ctx, req, sample_transaction.id)

        assert c is False
        mock_transaction_repository.create.assert_not_called()
        mock_transaction_repository.get_by_id.assert_called_once()
        mock_transaction_repository.update.assert_called_once()
Exemplo n.º 8
0
    def test_happy_path_create_add_cashbox(self,
                               ctx,
                               mock_transaction_repository: TransactionRepository,
                                           mock_cashbox_repository: CashboxRepository,
                               transaction_manager: TransactionManager,
                               sample_transaction: Transaction):
        sample_transaction.cashbox = "to"
        req = AbstractTransaction(
            src=sample_transaction.src,
            dst=sample_transaction.dst,
            name=sample_transaction.name,
            value=sample_transaction.value,
            payment_method=sample_transaction.payment_method,
            attachments=sample_transaction.attachments
        )
        mock_transaction_repository.create = MagicMock(return_value=(sample_transaction))
        mock_cashbox_repository.update = MagicMock(return_value=(None))

        _, c = transaction_manager.update_or_create(ctx, req)

        assert c is True
        mock_transaction_repository.create.assert_called_once_with(ctx, req)
        mock_cashbox_repository.update.assert_called_once_with(ctx, value_modifier=sample_transaction.value, transaction=sample_transaction)
Exemplo n.º 9
0
    def buy(self, ctx, member_id: int, payment_method_id: int, product_ids: List[int] = []) -> None:
        if not product_ids:
            raise ProductNotFoundError("None")

        payment_method = self.payment_method_repository.get_by_id(ctx, payment_method_id)
        dst_accounts, _ = self.account_repository.search_by(ctx, limit=1, filter_=AbstractAccount(name=KnownAccountExpense.TECHNICAL_EXPENSE.value))
        if not dst_accounts:
            raise AccountNotFoundError(KnownAccountExpense.TECHNICAL_EXPENSE.value)
        src_accounts, _ = self.account_repository.search_by(ctx, limit=1, filter_=AbstractAccount(member=member_id))
        if not src_accounts:
            raise AccountNotFoundError(KnownAccountExpense.TECHNICAL_EXPENSE.value)

        for i in product_ids:
            product = self.product_repository.get_by_id(ctx, i)

            _ = self.transaction_repository.create(
                ctx, 
                AbstractTransaction(
                    src=src_accounts[0].id,
                    dst=dst_accounts[0].id,
                    name=product.name,
                    value=product.selling_price,
                    payment_method=payment_method.id,
                ))