예제 #1
0
    def test_finds_by_id_returns_the_expected_user(self, database):
        user = User.create(first_name=self.first_name,
                           last_name=self.last_name,
                           email=self.email)

        assert User.query.count() == 1
        assert User.find_by_id(uuid.uuid4()) is None
        assert User.find_by_id(user.id) == user
예제 #2
0
def wallets():
    user_authentication_details = flask_login.current_user
    user = User.find_by_id(user_authentication_details.user_id)

    wallets = [wallet.as_dict() for wallet in Wallet.find_all_by_user(user)]

    return jsonify(wallets), HTTPStatus.OK
예제 #3
0
def create_wallet_transfer(currency):
    user_authentication_details = flask_login.current_user
    user = User.find_by_id(user_authentication_details.user_id)
    wallet = helpers.get_wallet(user, currency)
    response_code = HTTPStatus.CREATED

    if type(wallet) is str:
        response = wallet
        response_code = HTTPStatus.BAD_REQUEST
    else:
        try:
            response = TransferCreate.call(user, wallet,
                                           request.json).as_dict()
        except InvalidTransaction:
            response = 'Invalid transaction. Please change transaction details.'
            response_code = HTTPStatus.BAD_REQUEST
        except InvalidTransfer:
            response = 'Invalid transfer. Please change transfer details.'
            response_code = HTTPStatus.BAD_REQUEST
        except InvalidUser:
            response = 'Invalid receiver. Please specify a valid receiver email.'
            response_code = HTTPStatus.BAD_REQUEST
        except (InvalidWallet, InvalidCurrency):
            response = "Invalid receiver wallet. Receiver can't accept transfer with specified currency."
            response_code = HTTPStatus.BAD_REQUEST

    return jsonify(response), response_code
예제 #4
0
    def _get_user(self, user_token: str):
        user_authentication_detail = UserAuthenticationDetail.find_by_token(
            user_token)

        if user_authentication_detail is None:
            raise InvalidUser

        return User.find_by_id(user_authentication_detail.user_id)
예제 #5
0
    def test_does_not_create_wallet_when_not_valid_token_provided(
            self, database, invalid_token):
        user_authentication_details = UserCreate.call(self.user_details)
        user = User.find_by_id(user_authentication_details.user_id)

        prev_count = Wallet.query.count()

        with pytest.raises(InvalidUser):
            wallet = WalletCreate.call(invalid_token, self.details)

        assert Wallet.query.count() == prev_count
예제 #6
0
    def test_creates_wallet_when_all_details_correct(self, database):
        user_authentication_details = UserCreate.call(self.user_details)
        user = User.find_by_id(user_authentication_details.user_id)

        assert Wallet.find_by_user_and_currency(user, self.currency) is None
        user_authentication_details.generate_token()

        wallet = WalletCreate.call(user_authentication_details.token,
                                   self.details)

        assert Wallet.find_by_user_and_currency(user, self.currency) == wallet
예제 #7
0
    def test_returns_existing_wallet_when_already_created(self, database):
        user_authentication_details = UserCreate.call(self.user_details)
        user = User.find_by_id(user_authentication_details.user_id)
        user_authentication_details.generate_token()

        existing_wallet = WalletCreate.call(user_authentication_details.token,
                                            self.details)

        wallet = WalletCreate.call(user_authentication_details.token,
                                   self.details)

        assert existing_wallet == wallet
예제 #8
0
def get_wallet(currency):
    user_authentication_details = flask_login.current_user
    user = User.find_by_id(user_authentication_details.user_id)
    response = helpers.get_wallet(user, currency)
    response_code = HTTPStatus.OK

    if type(response) is str:
        response_code = HTTPStatus.BAD_REQUEST
    else:
        response = response.as_dict()

    return jsonify(response), response_code
예제 #9
0
    def test_does_not_create_transaction_when_wallet_invalid(
            self, database, details):
        user_authentication_details = UserCreate.call(self.user_details)
        user = User.find_by_id(user_authentication_details.user_id)
        wallet = Wallet(user_id=user.id,
                        currency=self.wallet_details['currency'])

        prev_count = Transaction.query.count()

        with pytest.raises(InvalidWallet):
            transaction = TransactionCreate.call(wallet, details)

        assert Transaction.query.count() == prev_count
예제 #10
0
    def test_when_user_does_not_exists_and_all_details_correct_creates_user(
            self, database):
        assert User.find_by_email(self.details['email']) is None
        assert UserAuthenticationDetail.find_by_email_and_password(
            self.details['email'], self.details['password']) is None

        user_authentication_details = UserCreate.call(self.details)

        assert user_authentication_details is not None
        assert UserAuthenticationDetail.find_by_email_and_password(
            self.details['email'],
            self.details['password']) == user_authentication_details
        assert User.find_by_email(self.details['email']) == User.find_by_id(
            user_authentication_details.user_id)
예제 #11
0
    def test_does_not_create_transaction_when_details_invalid(
            self, database, invalid_details):
        user_authentication_details = UserCreate.call(self.user_details)
        user = User.find_by_id(user_authentication_details.user_id)
        user_authentication_details.generate_token()
        wallet = WalletCreate.call(user_authentication_details.token,
                                   self.wallet_details)

        prev_count = Transaction.query.count()

        with pytest.raises(InvalidTransaction):
            transaction = TransactionCreate.call(wallet, invalid_details)

        assert Transaction.query.count() == prev_count
        assert wallet.balance_cents == 0
예제 #12
0
    def test_creates_transaction_modifying_wallet_when_all_details_correct(
            self, database, details):
        user_authentication_details = UserCreate.call(self.user_details)
        user = User.find_by_id(user_authentication_details.user_id)
        user_authentication_details.generate_token()
        wallet = WalletCreate.call(user_authentication_details.token,
                                   self.wallet_details)

        assert wallet.balance_cents == 0
        assert Transaction.query.count() == 0

        transaction = TransactionCreate.call(wallet, details)

        assert Transaction.query.first() == transaction
        assert wallet.balance_cents == details['expected_output']
예제 #13
0
    def test_does_not_create_wallet_when_not_valid_details_provided(
            self, database, invalid_details):
        user_authentication_details = UserCreate.call(self.user_details)
        user = User.find_by_id(user_authentication_details.user_id)
        user_authentication_details.generate_token()

        prev_count = Wallet.query.count()

        with pytest.raises(InvalidCurrency):
            wallet = WalletCreate.call(user_authentication_details.token, {
                **self.details,
                **invalid_details
            })

        assert Wallet.query.count() == prev_count
예제 #14
0
def get_wallet_transactions(currency):
    user_authentication_details = flask_login.current_user
    user = User.find_by_id(user_authentication_details.user_id)
    wallet = helpers.get_wallet(user, currency)
    response_code = HTTPStatus.OK

    if type(wallet) is str:
        response_code = HTTPStatus.BAD_REQUEST
        response = wallet
    else:
        response = [
            transaction.as_dict()
            for transaction in Transaction.find_all_by_wallet(wallet)
        ]

    return jsonify(response), response_code
예제 #15
0
def create_wallet_transactions(currency):
    user_authentication_details = flask_login.current_user
    user = User.find_by_id(user_authentication_details.user_id)
    wallet = helpers.get_wallet(user, currency)
    response_code = HTTPStatus.CREATED

    if type(wallet) is str:
        response = wallet
        response_code = HTTPStatus.BAD_REQUEST
    else:
        try:
            response = TransactionCreate.call(wallet, request.json).as_dict()
        except InvalidTransaction:
            response = 'Invalid transaction. Please change transaction details.'
            response_code = HTTPStatus.BAD_REQUEST

    return jsonify(response), response_code