Exemplo n.º 1
0
    def test_user_auth(self):
        db_utils.create_entry(Users, **self.user_1_data_hashed)

        resp = self.client.post(url_for("auth"), json=self.user_1_credentials)

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.json, {"access_token": ANY})
Exemplo n.º 2
0
 def setUp(self):
     super().setUp()
     self.user_1 = db_utils.create_entry(Users, **self.user_1_data_hashed)
     self.user_2 = db_utils.create_entry(Users, **self.user_2_data_hashed)
     self.wallet = db_utils.create_entry(
         Wallets, **{
             **self.wallet_1_data, 'owner_uid': self.user_1.uid
         })
Exemplo n.º 3
0
    def setUp(self):
        super().setUp()

        self.user_1 = db_utils.create_entry(Users, **self.user_1_data_hashed)
        self.user_2 = db_utils.create_entry(Users, **self.user_2_data_hashed)

        self.wallet_1 = db_utils.create_entry(
            Wallets, **{
                **self.wallet_1_data, 'owner_uid': self.user_1.uid,
                "funds": 100
            })
        self.wallet_2 = db_utils.create_entry(
            Wallets, **{
                **self.wallet_2_data, 'owner_uid': self.user_2.uid
            })
        self.wallet_3 = db_utils.create_entry(
            Wallets, **{
                **self.wallet_3_data, 'owner_uid': self.user_1.uid
            })

        self.wallet_1_transaction_1_data = {
            "from_wallet_uid": self.wallet_1.uid,
            "to_wallet_uid": self.wallet_2.uid,
            "amount": 10,
            "datetime": datetime.now(),
        }
        self.wallet_1_transaction_1 = db_utils.create_entry(
            Transactions, **self.wallet_1_transaction_1_data)

        self.wallet_1_transaction_2_data = {
            "from_wallet_uid": self.wallet_1.uid,
            "to_wallet_uid": self.wallet_2.uid,
            "amount": 10,
            "datetime": datetime.now(),
        }
        self.wallet_1_transaction_2 = db_utils.create_entry(
            Transactions, **self.wallet_1_transaction_2_data)

        self.wallet_2_transaction_1_data = {
            "from_wallet_uid": self.wallet_2.uid,
            "to_wallet_uid": self.wallet_1.uid,
            "amount": 5,
            "datetime": datetime.now(),
        }
        self.wallet_2_transaction_1 = db_utils.create_entry(
            Transactions, **self.wallet_2_transaction_1_data)

        self.wallet_2_transaction_2_data = {
            "from_wallet_uid": self.wallet_2.uid,
            "to_wallet_uid": self.wallet_3.uid,
            "amount": 5,
            "datetime": datetime.now(),
        }
        self.wallet_2_transaction_2 = db_utils.create_entry(
            Transactions, **self.wallet_2_transaction_2_data)
Exemplo n.º 4
0
    def test_not_admin(self):
        db_utils.create_entry(Users, **self.user_1_data_hashed)

        resp = self.client.get(
            url_for("api.list_users"),
            headers=self.get_auth_headers(self.user_1_credentials),
        )

        self.assertEqual(resp.status_code, 401)
        self.assertEqual(resp.json, {
            'code': 401,
            'type': 'NOT_AUTHORIZED',
            'message': ANY
        })
Exemplo n.º 5
0
def create_wallet():
    wallet_data = WalletToCreate().load(request.json)
    wallet = db_utils.create_entry(Wallets,
                                   **wallet_data,
                                   owner_uid=current_identity.id,
                                   funds=0)
    return jsonify(WalletData().dump(wallet))
Exemplo n.º 6
0
def send_funds(wallet_id):
    transaction_data = FundsToSend().load(request.json)

    if wallet_id != 0 and not current_identity.is_admin:
        from_wallet = db_utils.get_entry_by_uid(Wallets,
                                                wallet_id,
                                                owner_uid=current_identity.id)
        assert from_wallet.funds > transaction_data[
            "amount"], "Not enough funds"
        db_utils.update_entry(
            from_wallet,
            funds=Wallets.funds - transaction_data["amount"],
            commit=False,
        )

    to_wallet = db_utils.get_entry_by_uid(Wallets,
                                          transaction_data["to_wallet"])
    db_utils.update_entry(
        to_wallet,
        funds=Wallets.funds + transaction_data["amount"],
        commit=False,
    )

    transaction = db_utils.create_entry(
        Transactions,
        from_wallet_uid=wallet_id or None,
        to_wallet_uid=transaction_data["to_wallet"],
        amount=transaction_data["amount"],
    )
    return jsonify(TransactionData().dump(transaction))
Exemplo n.º 7
0
    def test_not_admin(self):
        db_utils.create_entry(Users, **self.user_1_data_hashed)

        resp = self.client.post(
            url_for("api.create_user"),
            json=self.user_2_data,
            headers=self.get_auth_headers(self.user_1_credentials),
        )

        self.assertEqual(resp.status_code, 401)
        self.assertEqual(resp.json, {
            'code': 401,
            'type': 'NOT_AUTHORIZED',
            'message': ANY
        })
        self.assertFalse(Session().query(Users).filter_by(
            email=self.user_2_data['email']).one_or_none())
Exemplo n.º 8
0
    def test_list_users(self):
        db_utils.create_entry(Users, **self.user_1_data_hashed)
        db_utils.create_entry(Users, **self.user_2_data_hashed)

        resp = self.client.get(
            url_for("api.list_users"),
            headers=self.get_auth_headers(self.admin_credentials),
        )

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.json, [
            {
                'uid': ANY,
                'email': self.user_1_data['email'],
                'first_name': self.user_1_data['first_name'],
                'last_name': self.user_1_data['last_name'],
            },
            {
                'uid': ANY,
                'email': self.user_2_data['email'],
                'first_name': self.user_2_data['first_name'],
                'last_name': self.user_2_data['last_name'],
            },
        ])
Exemplo n.º 9
0
 def setUp(self):
     super().setUp()
     self.user_1 = db_utils.create_entry(Users, **self.user_1_data_hashed)
Exemplo n.º 10
0
def create_user():
    user_data = UserToCreate().load(request.json)
    user = db_utils.create_entry(Users, **user_data)
    return jsonify(UserData().dump(user))