Esempio n. 1
0
def test_context():
    '''
    Test Configuration
    '''
    app = create_app('testing')
    app.config[
        'SQLALCHEMY_DATABASE_URI'] = 'postgres://*****:*****@localhost:5432/verse_testing'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    app.app_context().push()

    test_client = app.test_client()

    with app.app_context():
        db.create_all()

        dummy_user = UserModel({
            'username': '******',
            'email': '*****@*****.**',
            'password': '******'
        })
        dummy_user.save()

        token = Auth.generate_token(dummy_user.id)
        yield test_client, dummy_user, token

        db.session.close()
        db.drop_all()
Esempio n. 2
0
 def test_can_get_dict_from_model(self):
     user_data = dict(id=3,
                      username='******',
                      email='*****@*****.**',
                      password='******',
                      active=True)
     user_model = UserModel(**user_data)
     self.assertEqual(user_model.to_dict(), user_data)
Esempio n. 3
0
 def test_can_instance_user_model_from_db_row(self):
     user_data = dict(id=3,
                      username='******',
                      email='*****@*****.**',
                      password='******',
                      active=True)
     user_model = UserModel(**user_data)
     user_model_from_db_row = UserModel.from_row(dict_to_db_row(user_data))
     self.assertEqual(user_model.__dict__, user_model_from_db_row.__dict__)
Esempio n. 4
0
def test_create_transaction_balance_conflict(test_context):
    test_client, dummy_user, token = test_context

    from_account = AccountModel({
        "user_id": dummy_user.id,
        "balance": 20,
        "account_number": "to_account_locking",
    })
    from_account.save()

    to_user = UserModel({
        "username": "******",
        "email": "from@account_locking.com",
        "password": "******"
    })
    to_user.save()

    to_account = AccountModel({
        "user_id": to_user.id,
        "balance": 0,
        "account_number": "from_account_locking"
    })
    to_account.save()

    transaction_details = {
        "to_account_id": to_account.id,
        "from_account_id": from_account.id,
        "amount": 20,
    }

    path = '/api/v1/transactions/'

    with ThreadPoolExecutor(2) as pool:
        first = pool.submit(make_post_request, (path), (test_client),
                            (transaction_details), (token))
        second = pool.submit(make_post_request, (path), (test_client),
                             (transaction_details), (token))

        assert first.result().status_code == 201
        assert second.result().status_code == 403

    db.session.expire(from_account)
    db.session.expire(to_account)

    updated_from_account = AccountModel.get_one_account(from_account.id)
    updated_to_account = AccountModel.get_one_account(to_account.id)

    assert updated_from_account.balance == 0
    assert updated_to_account.balance == 20

    transactions = TransactionModel.query.filter(
        TransactionModel.from_account_id == from_account.id,
        TransactionModel.to_account_id == to_account.id).all()
    assert len(transactions) == 1
Esempio n. 5
0
 def test_can_get_db_row_from_model(self):
     user_data = dict(id=3,
                      username='******',
                      email='*****@*****.**',
                      password='******',
                      active=True)
     user_model_from_dict = UserModel(**user_data)
     db_row = dict_to_db_row(user_data)
     user_model_from_db_row = UserModel.from_row(db_row)
     self.assertListEqual(sorted(list(user_model_from_dict.to_row())),
                          sorted(list(db_row)))
     self.assertListEqual(sorted(list(user_model_from_db_row.to_row())),
                          sorted(list(db_row)))
Esempio n. 6
0
    def post(cls):
        # claims = get_jwt_claims()
        # if not claims['isAdmin']:
        #      return {'message' : 'Admin priviledge required'} , 401
        data = _user_parser.parse_args()
        user = UserModel.find_by_username(data['username'])
        if user is not None:
            return {'message' : 'User with such username already exists'} , 400
        user = UserModel(**data)
        # user = user_schema.load(data)
        print(user)
        user.save_to_db()

        return ({'message' : 'User created successfully.'}), 201
Esempio n. 7
0
 def test_can_instance_user_model(self):
     user_data = dict(username='******',
                      email='*****@*****.**',
                      password='******',
                      active=True)
     user_model = UserModel(**user_data)
     self.assertGreaterEqual(user_model.__dict__.items(), user_data.items())
     self.assertIsInstance(user_model, IUserModel)
Esempio n. 8
0
def test_create_transaction_success(test_context):
    test_client, dummy_user, token = test_context

    from_account = AccountModel({
        "user_id": dummy_user.id,
        "balance": 20,
        "account_number": "to_account",
    })
    from_account.save()

    to_user = UserModel({
        "username": "******",
        "email": "*****@*****.**",
        "password": "******"
    })
    to_user.save()

    to_account = AccountModel({
        "user_id": to_user.id,
        "balance": 0,
        "account_number": "from_account"
    })
    to_account.save()

    transaction_details = {
        "to_account_id": to_account.id,
        "from_account_id": from_account.id,
        "amount": 20,
    }

    res = make_post_request('/api/v1/transactions/', test_client,
                            transaction_details, token)
    assert res.status_code == 201

    updated_from_account = AccountModel.get_one_account(from_account.id)
    updated_to_account = AccountModel.get_one_account(to_account.id)

    assert updated_from_account.balance == 0
    assert updated_to_account.balance == 20
Esempio n. 9
0
def test_create_transaction_permission_error(test_context):
    test_client, dummy_user, token = test_context

    user_with_permissions = UserModel({
        "email": "user@with_permissions.com",
        "password": "******",
        "username": "******"
    })
    user_with_permissions.save()

    from_account = AccountModel({
        "user_id": user_with_permissions.id,
        "balance": 20,
        "account_number": "to_account",
    })
    from_account.save()

    to_account = AccountModel({
        "user_id": dummy_user.id,
        "balance": 0,
        "account_number": "from_account"
    })
    to_account.save()

    transaction_details = {
        "to_account_id": dummy_user.id,
        "from_account_id": from_account.id,
        "amount": 20,
    }

    res = make_post_request('/api/v1/transactions/', test_client,
                            transaction_details, token)
    assert res.status_code == 403

    updated_from_account = AccountModel.get_one_account(from_account.id)
    updated_to_account = AccountModel.get_one_account(to_account.id)

    assert updated_from_account.balance == 20
    assert updated_to_account.balance == 0
Esempio n. 10
0
def test_create_user(test_context):
    test_client, dummy_user, token = test_context
    new_user = {
        'email': '*****@*****.**',
        'username': '******',
        'password': '******'
    }

    res = make_post_request('/api/v1/users/', test_client, new_user, token)

    created_user = UserModel.get_user_by_email('*****@*****.**')

    assert created_user is not None
    assert res.status_code == 201
Esempio n. 11
0
    def post(cls):
        data = _user_parser.parse_args()
        user = UserModel.find_by_username(data['username'])
       
        if user and user.check_hash(data['password']):
            print(1)
            expires = datetime.timedelta(days=1)
            access_token = create_access_token(identity=user.id, fresh = True,expires_delta=expires)
            print(2)
            refresh_token = create_refresh_token(user.id)
            print(3)
            # recents = UserModel.get_recent_accounts()
            return {
                'access_token' : access_token,
                'refresh_token' : refresh_token,
                'id' : user.id,
               

            }, 201
            print(4)
        return {'message' : 'Return invalid credentials'}, 401
Esempio n. 12
0
def _create_test_user(id):
    user = UserModel()
    user.id = id
    user.name = str(id)
    return user