def test_0_register_auth_contract():
    class AuthDigitalCurrency(SimpleContract):
        accounts = {}

        @method_allow(Permissions.ROOT)
        def add_account(self, key: str):
            if key in self.accounts:
                raise Exception('Account already exists')

            self.accounts[key] = 0.0
            return key

        @method_allow(Permissions.ROOT)
        def increment(self, key: str, quantity: float):
            if key not in self.accounts:
                raise Exception('Account not found')

            self.accounts[key] += quantity

        @method_allow(Permissions.USER)
        def transfer(self, source: str, dest: str, quantity: float):
            if source not in self.accounts:
                raise Exception('Source account not found')
            if dest not in self.accounts:
                raise Exception('Destination account not found')
            if self.accounts[source] < quantity:
                raise Exception('Not enough funds in source account')
            if quantity < 0:
                raise Exception('You cannot transfer negative currency')

            self.accounts[source] -= quantity
            self.accounts[dest] += quantity

        @method_allow(Permissions.USER)
        def balance(self, key: str):
            if key not in self.accounts:
                print(self.accounts)
                raise Exception('Account not found')

            return str(self.accounts[key])

    register_contract(AuthDigitalCurrency())

    request = PyledgerRequest()
    response = PyledgerResponse()

    request.request = 'call'
    request.contract = 'AuthDigitalCurrency'
    request.call = 'add_account'
    request.data = pickle.dumps({'key': 'new_account'})

    byte_request = request.SerializeToString()
    byte_response = handle_request(byte_request)
    response.ParseFromString(byte_response)

    assert response.successful == False
    assert response.data == b'Not enough permissions'
Пример #2
0
def test_call_method():
    request = PyledgerRequest()
    response = PyledgerResponse()

    request.request = 'call'
    request.contract = 'somecontract'
    request.call = 'somemethod'

    byte_request = request.SerializeToString()
    byte_response = handle_request(byte_request)
    response.ParseFromString(byte_response)

    assert response.successful == False
    assert response.data == b'Contract somecontract not available'
Пример #3
0
def test_exception():
    request = PyledgerRequest()
    response = PyledgerResponse()

    request.request = 'call'
    request.contract = 'DigitalCurrency'
    request.call = 'increment'
    request.data = pickle.dumps({'key': 'another_account', 'quantity': 100.0})

    byte_request = request.SerializeToString()
    byte_response = handle_request(byte_request)
    response.ParseFromString(byte_response)

    assert response.successful == False
    assert response.data == b"Exception in user function: Exception('Account not found',)"
Пример #4
0
def test_simple_call():
    request = PyledgerRequest()
    response = PyledgerResponse()

    request.request = 'call'
    request.contract = 'DigitalCurrency'
    request.call = 'add_account'
    request.data = pickle.dumps({'key': 'new_account'})

    byte_request = request.SerializeToString()
    byte_response = handle_request(byte_request)
    response.ParseFromString(byte_response)

    response_data = pickle.loads(response.data)

    assert response.successful == True
    assert response_data == response_data
def test_access_contract_as_root():
    request = PyledgerRequest()
    response = PyledgerResponse()

    request.request = 'call'
    request.contract = 'AuthDigitalCurrency'
    request.call = 'add_account'
    request.user = '******'
    request.password = '******'
    request.data = pickle.dumps({'key': 'new_account'})

    byte_request = request.SerializeToString()
    byte_response = handle_request(byte_request)
    response.ParseFromString(byte_response)

    assert response.successful == True

    response_data = pickle.loads(response.data)
    assert response_data == 'new_account'
Пример #6
0
def call_request(**kwargs):
    request = PyledgerRequest()

    request.user, request.password = auth_info(kwargs)
    request.session_key = session_info(kwargs)
    request.request = 'call'

    if 'contract' not in kwargs:
        raise ValueError('Contract should be a keyword argument')
    request.contract = kwargs['contract']

    if 'call' not in kwargs:
        raise ValueError('Call should be a keyword argument')
    request.call = kwargs['call']

    if 'data' not in kwargs:
        raise ValueError('Data should be a keyword argument')
    request.data = pickle.dumps(kwargs['data'])

    return request.SerializeToString()