def test_clean_username(self): # A user with proto_user params does not exist yet. proto_user = UserFactory.build() form = UserCreationForm({ "username": proto_user.username, "password1": proto_user._password, "password2": proto_user._password, }) assert form.is_valid() assert form.clean_username() == proto_user.username # Creating a user. form.save() # The user with proto_user params already exists, # hence cannot be created. form = UserCreationForm({ "username": proto_user.username, "password1": proto_user._password, "password2": proto_user._password, }) assert not form.is_valid() assert len(form.errors) == 1 assert "username" in form.errors
def test_permission_without_wallet(self): user = UserFactory.create() client = APIClient() client.force_login(user) response = client.get(reverse('api_v1:users:wallet')) assert response.status_code == 404
def test_permission_succeeded(self): user = UserFactory.create() client = APIClient() client.force_login(user) response = client.get(reverse('api_v1:users:me')) assert response.status_code == 200
def test_exchange_rate_update_no_permission(self): user = UserFactory.create() client = APIClient() client.force_login(user) response = client.put( reverse('api_v1:exchange:rate', kwargs={'currency': EUR}), data={ "rate": 2 } ) assert response.status_code == 403
def test_exchange_rate_patch_method_not_allowed(self): user = UserFactory.create() user.is_superuser = True user.is_staff = True user.save() client = APIClient() client.force_login(user) response = client.patch( reverse('api_v1:exchange:rate', kwargs={'currency': EUR}), data={ 'rate': 3 } ) assert response.status_code == 405
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)
def test_exchange_rate_update_success(self): user = UserFactory.create() user.is_superuser = True user.is_staff = True user.save() client = APIClient() client.force_login(user) response = client.put( reverse('api_v1:exchange:rate', kwargs={'currency': EUR}), data={ "rate": '2.00' } ) assert response.status_code == 200 assert response.data['rate'] == '2.00'
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
def test_username_has_been_registerd_before(self): user = UserFactory.create() serializer = CreateUserSerializer( data={'username': user.get_username()}) assert not serializer.is_valid() assert 'username' in serializer.errors
def user() -> settings.AUTH_USER_MODEL: return UserFactory()
def test_charge_user_without_wallet(self): user = UserFactory.create() with pytest.raises(ValueError): charge(user, Decimal("100.0"), USD)
def test_user_without_wallet(self): user = UserFactory.create() with pytest.raises(ValueError): get_wallet(user)