예제 #1
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
예제 #2
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
예제 #3
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
예제 #4
0
def create_wallet():
    user_authentication_details = flask_login.current_user
    details = request.json
    response = helpers.get_currency(details.get('currency'))
    response_code = HTTPStatus.CREATED

    if type(response) is str:
        response_code = HTTPStatus.BAD_REQUEST
    else:
        response = WalletCreate.call(user_authentication_details.token,
                                     details).as_dict()

    return jsonify(response), response_code
예제 #5
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
예제 #6
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']
예제 #7
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
예제 #8
0
    def test_retrieves_wallets_for_user_when_authenticated(
            self, testapp, database):
        client = testapp.test_client()
        details = {'currency': Currency.SGD.name}

        client.post('/users', json=self.user_details)
        response = client.post('/login', json=self.user_details)

        wallet = WalletCreate.call(response.json, details)

        wallet_dict = wallet.as_dict()
        wallet_dict['id'] = str(wallet_dict['id'])

        headers = {'Authorization': f"Basic {response.json}"}

        response = client.get('/users/wallets', headers=headers)

        assert response.status_code == HTTPStatus.OK
        assert response.json == [wallet_dict]