Exemple #1
0
    def test_exchange_rate_success(self):
        ExchangeRateFactory.create(
            rate=Decimal('2.2'),
            currency=EUR
        )

        client = APIClient()
        response = client.get(reverse(
            'api_v1:exchange:rate',
            kwargs={'currency': EUR}
        ))

        assert response.status_code == 200
        for field in ['rate', 'created']:
            assert field in response.data
Exemple #2
0
    def test_make_payment_from_user_without_wallet(self):
        ExchangeRateFactory.create(rate=Decimal("2"), currency=EUR)
        from_user = UserFactory.create()
        wallet = WalletFactory.create(amount=Decimal("100"), currency=EUR)
        to_user = wallet.user

        serializer = PaymentSerializer(
            data={
                'to_user': to_user.get_username(),
                'amount': Decimal('10'),
                'currency': EUR
            })

        assert serializer.is_valid()
        with pytest.raises(ValueError):
            serializer.save(from_user)
Exemple #3
0
    def test_serializer_save(self):
        ExchangeRateFactory.create(rate=Decimal("2"), currency=EUR)

        wallet = WalletFactory.create(amount=Decimal("1"), currency=EUR)
        serializer = ChargeSerializer(data={
            'amount': Decimal("10"),
            'currency': USD
        })

        assert serializer.is_valid()
        serializer.save(wallet)

        wallet.refresh_from_db()

        assert wallet.amount == Decimal("21")
        assert wallet.currency == EUR
Exemple #4
0
    def test_wallet_charge_success(self):
        ExchangeRateFactory.create(rate=Decimal("2"), currency=EUR)
        ExchangeRateFactory.create(rate=Decimal("4"), currency=CAD)
        wallet = WalletFactory.create(amount=Decimal("10"), currency=EUR)
        user = wallet.user

        client = APIClient()
        client.force_login(user)

        response = client.put(reverse('api_v1:users:wallet'), {
            'amount': 2,
            'currency': CAD,
        })
        assert response.status_code == 200
        assert response.data['amount'] == "11.00"
        assert response.data['currency'] == EUR
Exemple #5
0
    def test_exchange_update_different_currencies(self):
        old_rate = ExchangeRateFactory.create(rate=Decimal("1.5"),
                                              currency=EUR)

        with pytest.raises(AssertionError):
            ExchangeRateSerializer(old_rate,
                                   data={'rate': Decimal("63")},
                                   currency=CAD)
Exemple #6
0
    def test_payment_user_without_wallet(self):
        ExchangeRateFactory.create(rate=Decimal("2"), currency=EUR)
        from_user = UserFactory.create()

        to_wallet = WalletFactory.create(amount=Decimal("100"), currency=EUR)
        to_user = to_wallet.user

        client = APIClient()
        client.force_login(from_user)
        response = client.post(reverse('api_v1:users:payment'), {
            'to_user': to_user.get_username(),
            'amount': 5,
            'currency': EUR,
        })

        assert response.status_code == 400
        assert api_settings.NON_FIELD_ERRORS_KEY in response.data
Exemple #7
0
    def test_payment_success(self):
        ExchangeRateFactory.create(rate=Decimal("2"), currency=EUR)
        from_wallet = WalletFactory.create(amount=Decimal("100"), currency=EUR)
        from_user = from_wallet.user

        to_wallet = WalletFactory.create(amount=Decimal("100"), currency=EUR)
        to_user = to_wallet.user

        client = APIClient()
        client.force_login(from_user)
        response = client.post(reverse('api_v1:users:payment'), {
            'to_user': to_user.get_username(),
            'amount': 5,
            'currency': EUR,
        })

        assert response.status_code == 201

        fields = ['to_user', 'amount', 'currency', 'from_user', 'created']

        for field in fields:
            assert field in response.data
Exemple #8
0
    def test_make_payment_success(self):
        ExchangeRateFactory.create(rate=Decimal("2"), currency=EUR)
        from_wallet = WalletFactory.create(amount=Decimal("60"), currency=EUR)
        from_user = from_wallet.user

        to_wallet = WalletFactory.create(amount=Decimal("100"), currency=EUR)
        to_user = to_wallet.user

        serializer = PaymentSerializer(
            data={
                'to_user': to_user.get_username(),
                'amount': Decimal('10'),
                'currency': EUR
            })

        assert serializer.is_valid()
        transaction = serializer.save(from_user)

        assert transaction.pk is not None
        assert transaction.amount == Decimal('10')
        assert transaction.currency == EUR
        assert transaction.from_wallet.pk == from_wallet.pk
        assert transaction.to_wallet.pk == to_wallet.pk
Exemple #9
0
    def test_exchange_update_new_value(self):
        old_rate = ExchangeRateFactory.create(rate=Decimal("1.5"),
                                              currency=EUR)

        serializer = ExchangeRateSerializer(instance=old_rate,
                                            data={'rate': Decimal("2")},
                                            currency=EUR)

        assert serializer.is_valid()

        rate = serializer.save()
        assert rate.currency == old_rate.currency
        assert rate.pk != old_rate.pk
        assert ExchangeRate.of(EUR) == rate.rate