コード例 #1
0
    def create_user(self):
        user = User.find_by_email(self.email)

        if user is None:
            user = User.create(self.first_name, self.last_name, self.email)

        return user
コード例 #2
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
コード例 #3
0
    def test_finds_by_email_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_email('*****@*****.**') is None
        assert User.find_by_email(self.email) == user
コード例 #4
0
    def test_find_all_by_user_returns_all_expected_wallets(self, database):
        user = User.create(first_name=self.first_name,
                           last_name=self.last_name,
                           email=self.email)

        user2 = User.create(first_name=self.first_name,
                            last_name=self.last_name,
                            email='*****@*****.**')

        wallet = Wallet.create(user, self.currency)

        assert Wallet.query.count() == 1
        assert Wallet.find_all_by_user(user2) == []
        assert Wallet.find_all_by_user(user) == [wallet]
コード例 #5
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)

        user2 = User.create(first_name=self.first_name,
                            last_name=self.last_name,
                            email='*****@*****.**')

        wallet = Wallet.create(user, self.currency)

        assert Wallet.query.count() == 1
        assert Wallet.find_by_user_and_currency(user2, self.currency) is None
        assert Wallet.find_by_user_and_currency(user, self.currency) == wallet
コード例 #6
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)
コード例 #7
0
    def _get_receiver(self, email: str):
        user = User.find_by_email(email)

        if user is None:
            raise InvalidUser

        return user
コード例 #8
0
    def test_creates_user_authentication_details_with_all_expected_attributes(
            self, database):
        user = User.create(first_name=self.first_name,
                           last_name=self.last_name,
                           email=self.email)

        user_authentication_detail = UserAuthenticationDetail.create(
            user=user, email=self.email, password=self.password)

        assert UserAuthenticationDetail.query.filter_by(
            email=self.email).first().id == user_authentication_detail.id

        assert user_authentication_detail.id is not None

        assert UserAuthenticationDetail.query.filter_by(
            email=self.email).first(
            ).created_at == user_authentication_detail.created_at
        assert user_authentication_detail.created_at is not None

        assert UserAuthenticationDetail.query.filter_by(
            email=self.email).first(
            ).updated_at == user_authentication_detail.updated_at
        assert user_authentication_detail.updated_at is not None

        assert user_authentication_detail.email == self.email
        assert user_authentication_detail.password != self.password
        assert user_authentication_detail.password == hashing.hash(
            self.password)
        assert user_authentication_detail.user_id == user.id
コード例 #9
0
    def test_creates_transaction_with_all_expected_attributes(self, database, transaction_type):
        user = User.create(
            first_name = self.first_name,
            last_name = self.last_name,
            email = self.email
        )

        wallet = Wallet.create(
            user,
            self.currency
        )

        transaction = Transaction.create(
            wallet,
            self.amount_cents,
            transaction_type

        )

        assert Transaction.query.filter_by(
            wallet_id = wallet.id
        ).count() == 1

        assert Transaction.query.filter_by(
            wallet_id = wallet.id
        ).first().id == transaction.id
        assert transaction.id is not None

        assert Transaction.query.filter_by(
            wallet_id = wallet.id
        ).first().created_at == transaction.created_at
        assert transaction.created_at is not None

        assert transaction.type == transaction_type.name
        assert transaction.amount_cents == self.amount_cents
コード例 #10
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
コード例 #11
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
コード例 #12
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)
コード例 #13
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
コード例 #14
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
コード例 #15
0
    def test_does_not_create_wallet_with_non_existing_user(self, database):
        user = User(id=uuid.uuid4(),
                    first_name=self.first_name,
                    last_name=self.last_name,
                    email=self.email)

        prev_count = Wallet.query.count()

        with pytest.raises(InvalidUser):
            wallet = Wallet.create(user, self.currency)

        assert Wallet.query.count() == prev_count
コード例 #16
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
コード例 #17
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
コード例 #18
0
    def test_finds_by_id_returns_the_expected_user_authentication_detail(
            self, database):
        user = User.create(first_name=self.first_name,
                           last_name=self.last_name,
                           email=self.email)

        user_authentication_detail = UserAuthenticationDetail.create(
            user=user, email=self.email, password=self.password)

        assert UserAuthenticationDetail.query.count() == 1
        assert UserAuthenticationDetail.find_by_id(uuid.uuid4()) is None
        assert UserAuthenticationDetail.find_by_id(
            user_authentication_detail.id) == user_authentication_detail
コード例 #19
0
    def test_does_not_create_wallet_with_duplicated_currency(self, database):
        user = User.create(first_name=self.first_name,
                           last_name=self.last_name,
                           email=self.email)

        wallet = Wallet.create(user, self.currency)

        prev_count = Wallet.query.filter_by(user_id=user.id).count()

        with pytest.raises(InvalidCurrency):
            wallet = Wallet.create(user, self.currency)

        assert Wallet.query.filter_by(user_id=user.id).count() == prev_count
コード例 #20
0
    def test_does_not_create_user_authentication_details_with_invalid_email(
            self, database):
        user = User.create(first_name=self.first_name,
                           last_name=self.last_name,
                           email=self.email)

        prev_count = UserAuthenticationDetail.query.count()

        with pytest.raises(InvalidUserAuthenticationDetails):
            user_authentication_detail = UserAuthenticationDetail.create(
                user=user, email=None, password=self.password)

        assert UserAuthenticationDetail.query.count() == prev_count
コード例 #21
0
    def test_add_operation_modifies_wallet_balance_accordingly(
            self, database, operation):
        user = User.create(first_name=self.first_name,
                           last_name=self.last_name,
                           email=self.email)

        wallet = Wallet.create(user, self.currency)

        assert wallet.balance_cents == 0

        wallet.add_operation(operation[0], operation[1])

        assert wallet.balance_cents == operation[2]
コード例 #22
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
コード例 #23
0
    def test_does_not_create_user_authentication_details_with_non_existing_user(
            self, database):
        user = User(id=uuid.uuid4(),
                    first_name=self.first_name,
                    last_name=self.last_name,
                    email=self.email)

        prev_count = UserAuthenticationDetail.query.count()

        with pytest.raises(InvalidUserAuthenticationDetails):
            user_authentication_detail = UserAuthenticationDetail.create(
                user=user, email=self.email, password=self.password)

        assert UserAuthenticationDetail.query.count() == prev_count
コード例 #24
0
    def test_converts_user_to_dict(self, database):
        user = User.create(first_name=self.first_name,
                           last_name=self.last_name,
                           email=self.email)

        expected_dict = {
            'first_name': user.first_name,
            'last_name': user.last_name,
            'email': user.email,
            'updated_at': int(user.created_at.timestamp()),
            'created_at': int(user.created_at.timestamp())
        }

        assert user.as_dict() == expected_dict
コード例 #25
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
コード例 #26
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']
コード例 #27
0
    def test_finds_by_token_returns_the_expected_user_authentication_detail(
            self, database):
        user = User.create(first_name=self.first_name,
                           last_name=self.last_name,
                           email=self.email)

        user_authentication_detail = UserAuthenticationDetail.create(
            user=user, email=self.email, password=self.password)

        user2 = User.create(first_name=self.first_name,
                            last_name=self.last_name,
                            email='*****@*****.**')

        UserAuthenticationDetail.create(user=user2,
                                        email='*****@*****.**',
                                        password=self.password)

        user_authentication_detail.generate_token()

        assert UserAuthenticationDetail.query.count() == 2
        assert UserAuthenticationDetail.find_by_token('') is None
        assert UserAuthenticationDetail.find_by_token(None) is None
        assert UserAuthenticationDetail.find_by_token(
            user_authentication_detail.token) == user_authentication_detail
コード例 #28
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
コード例 #29
0
    def test_does_not_create_user_with_empty_mandatory_details(
            self, database, invalid_detail):
        prev_count = User.query.count()

        with pytest.raises(InvalidUser):
            user = User.create(
                **{
                    **{
                        'first_name': self.first_name,
                        'last_name': self.last_name,
                        'email': self.email
                    },
                    **invalid_detail
                })

        assert User.query.count() == prev_count
コード例 #30
0
    def test_finds_by_email_and_password_returns_the_expected_user_authentication_detail(
            self, database):
        user = User.create(first_name=self.first_name,
                           last_name=self.last_name,
                           email=self.email)

        user_authentication_detail = UserAuthenticationDetail.create(
            user=user, email=self.email, password=self.password)

        assert UserAuthenticationDetail.query.count() == 1
        assert UserAuthenticationDetail.find_by_email_and_password(
            self.email, 'nonexistingpswd') is None
        assert UserAuthenticationDetail.find_by_email_and_password(
            '*****@*****.**', self.password) is None
        assert UserAuthenticationDetail.find_by_email_and_password(
            self.email, self.password) == user_authentication_detail