Esempio n. 1
0
    def test_record_create_application(self):
        """
        Test if the supplychain processor can create a application.
        """
        validator = self.validator
        factory = self.factory

        record_addr = addressing.create_record_id("a")
        agent_addr = addressing.get_agent_id(self.factory.public_key)
        holder_addr = addressing.get_agent_id("holder")
        addrs = [record_addr, agent_addr, holder_addr]

        # 1. -> Send a set transaction
        #    <- Expect a state get request
        validator.send(
            factory.create_tp_process_request(
                {
                    'MessageType': 'Record',
                    'Action': 'CreateApplication',
                    'RecordId': record_addr,
                    'ApplicationType': 'test',
                    'ApplicationTerms': 'terms',
                }, addrs, addrs))

        # test for record existance
        received = validator.expect(factory.create_get_request(record_addr))
        validator.respond(
            factory.create_get_response(
                record_addr, {
                    'RecordInfo': {
                        'ApplicationFrom': None,
                        'ApplicationType': None,
                        'ApplicationTerms': None,
                        'ApplicationStatus': 'Open',
                        'CurrentHolder': holder_addr
                    }
                }), received)

        # AgentHandler.update_record_tracking
        self.expect_agent_value_update(agent_addr, 'OpenApplications',
                                       record_addr, 1)
        # AgentHandler.update_record_tracking
        self.expect_agent_value_update(holder_addr, 'HoldRecords', record_addr,
                                       1)

        received = validator.expect(
            factory.create_set_request(
                record_addr, {
                    'RecordInfo': {
                        'ApplicationFrom': agent_addr,
                        'ApplicationType': 'test',
                        'ApplicationTerms': 'terms',
                        'ApplicationStatus': 'Open',
                        'CurrentHolder': holder_addr,
                    }
                }))
        validator.respond(factory.create_set_response(record_addr), received)
        self.expect_ok()
Esempio n. 2
0
    def apply(cls, transaction, state):
        payload = json.loads(transaction.payload.decode())

        LOGGER.debug("AgentHandler.apply payload: %s", repr(payload))

        tnx_action = payload.get('Action', None)
        tnx_name = payload.get('Name', None)
        tnx_type = payload.get('Type', None)
        tnx_url = payload.get('Url', None)

        header = TransactionHeader()
        header.ParseFromString(transaction.header)
        uid = addressing.get_agent_id(header.signer_pubkey)

        if tnx_name is None or tnx_name == '':
            raise InvalidTransaction('Name not set')

        if tnx_action == "Create":
            LOGGER.debug("AgentHandler.apply CREATE")
            if state_get_single(state, uid) is not None:
                raise InvalidTransaction('Agent ID already registered')

            my_store = {}
            my_store['Name'] = tnx_name
            my_store['Type'] = tnx_type
            my_store['Url'] = tnx_url
            my_store['OwnRecords'] = {}
            my_store['HoldRecords'] = {}
            my_store['OpenApplications'] = {}
            my_store['AcceptedApplications'] = {}

            state_put_single(state, uid, my_store)
        else:
            raise InvalidTransaction(
                'Action {} is not valid'.format(tnx_action))
Esempio n. 3
0
    def test_record_accept_application(self):
        """
        Test if the supplychain processor can create a application.
        """
        validator = self.validator
        factory = self.factory

        record_addr = addressing.create_record_id("a")
        agent_addr = addressing.get_agent_id(self.factory.public_key)
        addrs = [record_addr, agent_addr]

        # 1. -> Send a set transaction
        #    <- Expect a state get request
        validator.send(
            factory.create_tp_process_request(
                {
                    'MessageType': 'Record',
                    'Action': 'AcceptApplication',
                    'RecordId': record_addr,
                }, addrs, addrs))

        # test for record existance
        received = validator.expect(factory.create_get_request(record_addr))
        validator.respond(
            factory.create_get_response(
                record_addr, {
                    'RecordInfo': {
                        'ApplicationFrom': agent_addr,
                        'ApplicationStatus': 'Open',
                        'CurrentHolder': agent_addr,
                        'Sensor': 'sensor'
                    }
                }), received)

        # AgentHandler.remove_open_application
        LOGGER.debug("1")
        self.expect_agent_value_delete(agent_addr, 'OpenApplications',
                                       record_addr, 1)
        LOGGER.debug("2")
        self.expect_agent_value_update(agent_addr, 'HoldRecords', record_addr,
                                       0)
        LOGGER.debug("3")
        # AgentHandler.add_accepted_application
        self.expect_agent_value_update(agent_addr, 'AcceptedApplications',
                                       record_addr, "sensor")
        LOGGER.debug("4")

        received = validator.expect(
            factory.create_set_request(
                record_addr, {
                    'RecordInfo': {
                        'ApplicationStatus': 'Accepted',
                        'ApplicationFrom': agent_addr,
                        'CurrentHolder': agent_addr,
                        'Sensor': 'sensor'
                    }
                }))
        validator.respond(factory.create_set_response(record_addr), received)
        self.expect_ok()
Esempio n. 4
0
    def test_record_finalize(self):
        """
        Test if the supplychain processor can cancel an application.
        """
        validator = self.validator
        factory = self.factory

        record_addr = addressing.create_record_id("a")
        agent_addr = addressing.get_agent_id(self.factory.public_key)
        addrs = [record_addr, agent_addr]

        # 1. -> Send a set transaction
        #    <- Expect a state get request
        validator.send(
            factory.create_tp_process_request(
                {
                    'MessageType': 'Record',
                    'Action': 'Finalize',
                    'RecordId': record_addr
                }, addrs, addrs))

        # test for record existance
        received = validator.expect(factory.create_get_request(record_addr))
        validator.respond(
            factory.create_get_response(
                record_addr, {
                    'RecordInfo': {
                        'Final': False,
                        'Owner': agent_addr,
                        'CurrentHolder': agent_addr
                    }
                }), received)

        # AgentHandler.remove_record_owner
        self.expect_agent_value_delete(agent_addr, 'OwnRecords', record_addr,
                                       1)

        # AgentHandler.remove_record_holder
        self.expect_agent_value_delete(agent_addr, 'HoldRecords', record_addr,
                                       1)

        received = validator.expect(
            factory.create_set_request(
                record_addr, {
                    'RecordInfo': {
                        'Final': True,
                        'Owner': agent_addr,
                        'CurrentHolder': agent_addr
                    },
                }))
        validator.respond(factory.create_set_response(record_addr), received)
        self.expect_ok()
Esempio n. 5
0
    def test_agent_create(self):
        """
        Test if the supplychain processor can create an agent.
        """
        validator = self.validator
        factory = self.factory

        agent_addr = addressing.get_agent_id(self.factory.public_key)

        # 1. -> Send a set transaction
        #    <- Expect a state get request
        validator.send(
            factory.create_tp_process_request({
                'MessageType': 'Agent',
                'Action': 'Create',
                'Name': 'test'
            }))

        # Expect test for agent existance
        received = validator.expect(factory.create_get_request(agent_addr))
        validator.respond(factory.create_get_response(agent_addr, None),
                          received)

        # Expect create the agent record
        received = validator.expect(
            factory.create_set_request(
                agent_addr, {
                    'Name': 'test',
                    'Type': None,
                    'Url': None,
                    'OwnRecords': {},
                    'HoldRecords': {},
                    'OpenApplications': {},
                    'AcceptedApplications': {}
                }))
        validator.respond(factory.create_set_response(agent_addr), received)

        self.expect_ok()
Esempio n. 6
0
    def apply(cls, transaction, state):
        payload = json.loads(transaction.payload.decode())

        LOGGER.debug("apply payload: %s", repr(payload))

        tnx_action = payload.get('Action', None)
        txnrecord_id = payload.get('RecordId', None)

        header = TransactionHeader()
        header.ParseFromString(transaction.header)
        tnx_originator = addressing.get_agent_id(header.signer_pubkey)

        # Retrieve the stored record data if an ID is provided.
        record_id = txnrecord_id
        record_store_key = record_id

        record_store = state_get_single(state, record_store_key)

        # Check Action
        if tnx_action == 'Create':
            if txnrecord_id is None:
                raise InvalidTransaction('Record id expected for CreateRecord')

            record_store = {}
            cls.create_record(tnx_originator, record_id, payload, state,
                              record_store)
        elif tnx_action == "CreateApplication":
            if txnrecord_id is None:
                raise InvalidTransaction(
                    'Record id expected for create_application')

            cls.create_application(tnx_originator, record_id, payload, state,
                                   record_store)
        elif tnx_action == "AcceptApplication":
            if txnrecord_id is None:
                raise InvalidTransaction(
                    'Record id expected for accept_application')

            cls.accept_application(tnx_originator, record_id, payload, state,
                                   record_store)
        elif tnx_action == "RejectApplication":
            if txnrecord_id is None:
                raise InvalidTransaction(
                    'Record id expected for reject_application')

            cls.reject_application(tnx_originator, record_id, payload, state,
                                   record_store)
        elif tnx_action == "CancelApplication":
            if txnrecord_id is None:
                raise InvalidTransaction(
                    'Record id expected for cancel_application')

            cls.cancel_application(tnx_originator, record_id, payload, state,
                                   record_store)
        elif tnx_action == "Finalize":
            if txnrecord_id is None:
                raise InvalidTransaction('Record id expected for Finalize')

            cls.finalize_record(tnx_originator, record_id, payload, state,
                                record_store)
        else:
            raise InvalidTransaction(
                'Action {} is not valid'.format(tnx_action))

        # Store the record data back
        state_put_single(state, record_store_key, record_store)
Esempio n. 7
0
    def test_record_create(self):
        """
        Test if the supplychain processor can create a record.
        """
        validator = self.validator
        factory = self.factory

        record_addr = addressing.create_record_id("a")
        agent_addr = addressing.get_agent_id(self.factory.public_key)
        addrs = [record_addr, agent_addr]
        time_stamp = time.time()

        # 1. -> Send a set transaction
        #    <- Expect a state get request
        validator.send(
            factory.create_tp_process_request(
                {
                    'MessageType': 'Record',
                    'Action': 'Create',
                    'RecordId': record_addr,
                    'Timestamp': time_stamp
                }, addrs, addrs))

        # test for record existance
        received = validator.expect(factory.create_get_request(record_addr))
        validator.respond(factory.create_get_response(record_addr, None),
                          received)

        # add_record_owner
        self.expect_agent_value_update(agent_addr, 'OwnRecords', record_addr,
                                       True)

        # Test for agent record
        # add_record_holder
        self.expect_agent_value_update(agent_addr, 'HoldRecords', record_addr,
                                       0)

        # Expect test for existance
        received = validator.expect(
            factory.create_set_request(
                record_addr, {
                    'RecordInfo': {
                        'CurrentHolder': agent_addr,
                        'Owner': agent_addr,
                        'Custodians': [],
                        'Parents': None,
                        'Timestamp': time_stamp,
                        'Sensor': None,
                        'Final': False,
                        'ApplicationFrom': None,
                        'ApplicationType': None,
                        'ApplicationTerms': None,
                        'ApplicationStatus': None,
                        'EncryptedConsumerAcccessible': None,
                        'EncryptedOwnerAccessible': None
                    },
                    'StoredTelemetry': {},
                    'DomainAttributes': {}
                }))
        validator.respond(factory.create_set_response(record_addr), received)

        self.expect_ok()