Ejemplo n.º 1
0
def ParseHydrusNetworkGETArgs(requests_args):

    args = ParseTwistedRequestGETArgs(requests_args, INT_PARAMS, BYTE_PARAMS,
                                      STRING_PARAMS, JSON_PARAMS,
                                      JSON_BYTE_LIST_PARAMS)

    if 'subject_account_key' in args:

        args['subject_identifier'] = HydrusNetwork.AccountIdentifier(
            account_key=args['subject_account_key'])

    elif 'subject_hash' in args:

        hash = args['subject_hash']

        if 'subject_tag' in args:

            tag = args['subject_tag']

            content = HydrusNetwork.Content(HC.CONTENT_TYPE_MAPPING,
                                            (tag, hash))

        else:

            content = HydrusNetwork.Content(HC.CONTENT_TYPE_FILES, [hash])

        args['subject_identifier'] = HydrusNetwork.AccountIdentifier(
            content=content)

    return args
Ejemplo n.º 2
0
    def _test_content_creation(self):

        tag = 'character:samus aran'
        hash = HydrusData.GenerateKey()

        mappings_content = HydrusNetwork.Content(HC.CONTENT_TYPE_MAPPINGS,
                                                 (tag, (hash, )))
        mapping_content = HydrusNetwork.Content(HC.CONTENT_TYPE_MAPPING,
                                                (tag, hash))

        client_to_server_update = HydrusNetwork.ClientToServerUpdate()

        client_to_server_update.AddContent(HC.CONTENT_UPDATE_PEND,
                                           mappings_content)

        self._write('update', self._tag_service_key,
                    self._tag_service_regular_account, client_to_server_update,
                    HydrusData.GetNow())

        # can extend this to generate and fetch an actual update given a timespan

        #

        result = self._read('account_from_content', self._tag_service_key,
                            mapping_content)

        self.assertEqual(result.GetAccountKey(),
                         self._tag_service_regular_account.GetAccountKey())
Ejemplo n.º 3
0
    def _test_tag_repo(self, service):

        # account from tag

        test_tag = 'character:samus aran'
        test_hash = HydrusData.GenerateKey()

        subject_content = HydrusNetwork.Content(
            content_type=HC.CONTENT_TYPE_MAPPING,
            content_data=(test_tag, test_hash))

        subject_account_identifier = HydrusNetwork.AccountIdentifier(
            content=subject_content)

        HG.test_controller.SetRead('account', self._account)

        response = service.Request(
            HC.GET, 'other_account',
            {'subject_identifier': subject_account_identifier})

        self.assertEqual(repr(response['account']), repr(self._account))
Ejemplo n.º 4
0
    def _test_restricted(self, service):

        # access_key

        registration_key = HydrusData.GenerateKey()

        HG.test_controller.SetRead('access_key', self._access_key)

        response = service.Request(HC.GET, 'access_key',
                                   {'registration_key': registration_key})

        self.assertEqual(response['access_key'], self._access_key)

        # set up session

        last_error = 0

        account = self._account

        HG.test_controller.SetRead('service', service)

        HG.test_controller.SetRead('account_key_from_access_key',
                                   HydrusData.GenerateKey())
        HG.test_controller.SetRead('account', self._account)

        # account

        response = service.Request(HC.GET, 'account')

        self.assertEqual(repr(response['account']), repr(self._account))

        # account from access key

        HG.test_controller.SetRead('account', self._account)

        subject_account_identifier = HydrusNetwork.AccountIdentifier(
            account_key=self._account.GetAccountKey())

        response = service.Request(
            HC.GET, 'other_account',
            {'subject_identifier': subject_account_identifier})

        self.assertEqual(repr(response['account']), repr(self._account))

        # account from file

        HG.test_controller.SetRead('account_from_content', self._account)

        content = HydrusNetwork.Content(
            content_type=HC.CONTENT_TYPE_FILES,
            content_data=(HydrusData.GenerateKey(), ))

        subject_account_identifier = HydrusNetwork.AccountIdentifier(
            content=content)

        response = service.Request(
            HC.GET, 'other_account',
            {'subject_identifier': subject_account_identifier})

        self.assertEqual(repr(response['account']), repr(self._account))

        # account from mapping

        HG.test_controller.SetRead('account_from_content', self._account)

        content = HydrusNetwork.Content(
            content_type=HC.CONTENT_TYPE_MAPPING,
            content_data=('hello', HydrusData.GenerateKey()))

        subject_account_identifier = HydrusNetwork.AccountIdentifier(
            content=content)

        response = service.Request(
            HC.GET, 'other_account',
            {'subject_identifier': subject_account_identifier})

        self.assertEqual(repr(response['account']), repr(self._account))

        # account_info

        account_info = {'message': 'hello'}

        HG.test_controller.SetRead('account_info', account_info)

        subject_account_identifier = HydrusNetwork.AccountIdentifier(
            account_key=HydrusData.GenerateKey())

        response = service.Request(
            HC.GET, 'account_info',
            {'subject_identifier': subject_account_identifier})

        self.assertEqual(response['account_info'], account_info)

        #

        content = HydrusNetwork.Content(
            content_type=HC.CONTENT_TYPE_FILES,
            content_data=(HydrusData.GenerateKey(), ))

        subject_account_identifier = HydrusNetwork.AccountIdentifier(
            content=content)

        with self.assertRaises(HydrusExceptions.BadRequestException):

            # can only do it with an account id
            response = service.Request(
                HC.GET, 'account_info',
                {'subject_identifier': subject_account_identifier})

        content = HydrusNetwork.Content(
            content_type=HC.CONTENT_TYPE_MAPPING,
            content_data=('hello', HydrusData.GenerateKey()))

        subject_account_identifier = HydrusNetwork.AccountIdentifier(
            content=content)

        with self.assertRaises(HydrusExceptions.BadRequestException):

            # can only do it with an account id
            response = service.Request(
                HC.GET, 'account_info',
                {'subject_identifier': subject_account_identifier})

        # account_types

        account_types = [
            HydrusNetwork.AccountType.GenerateAdminAccountType(
                service.GetServiceType())
        ]

        HG.test_controller.SetRead('account_types', account_types)

        HG.test_controller.ClearWrites('account_types')

        response = service.Request(HC.GET, 'account_types')

        self.assertEqual(response['account_types'][0].GetAccountTypeKey(),
                         account_types[0].GetAccountTypeKey())

        empty_account_type = HydrusNetwork.AccountType.GenerateNewAccountType(
            'empty account', {}, HydrusNetworking.BandwidthRules())

        account_types.append(empty_account_type)

        service.Request(
            HC.POST, 'account_types', {
                'account_types': account_types,
                'deletee_account_type_keys_to_new_account_type_keys': {}
            })

        written = HG.test_controller.GetWrite('account_types')

        [(args, kwargs)] = written

        (written_service_key, written_account, written_account_types,
         written_deletee_account_type_keys_to_new_account_type_keys) = args

        self.assertEqual(
            {wat.GetAccountTypeKey()
             for wat in written_account_types},
            {at.GetAccountTypeKey()
             for at in account_types})
        self.assertEqual(
            written_deletee_account_type_keys_to_new_account_type_keys, {})

        # registration_keys

        registration_key = HydrusData.GenerateKey()

        HG.test_controller.SetRead('registration_keys', [registration_key])

        response = service.Request(
            HC.GET, 'registration_keys', {
                'num': 1,
                'account_type_key': os.urandom(32),
                'expires': HydrusData.GetNow() + 1200
            })

        self.assertEqual(response['registration_keys'], [registration_key])
Ejemplo n.º 5
0
    def _test_repo(self, service):

        service_key = service.GetServiceKey()

        # num_petitions

        num_petitions = [[
            HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_STATUS_PETITIONED, 23
        ], [HC.CONTENT_TYPE_TAG_PARENTS, HC.CONTENT_STATUS_PENDING, 0]]

        HG.test_controller.SetRead('num_petitions', num_petitions)

        response = service.Request(HC.GET, 'num_petitions')

        self.assertEqual(response['num_petitions'], num_petitions)

        # petition

        action = HC.CONTENT_UPDATE_PETITION
        petitioner_account = HydrusNetwork.Account.GenerateUnknownAccount()
        reason = 'it sucks'
        contents = [
            HydrusNetwork.Content(
                HC.CONTENT_TYPE_FILES,
                [HydrusData.GenerateKey() for i in range(10)])
        ]

        petition = HydrusNetwork.Petition(action, petitioner_account, reason,
                                          contents)

        HG.test_controller.SetRead('petition', petition)

        response = service.Request(
            HC.GET, 'petition', {
                'content_type': HC.CONTENT_TYPE_FILES,
                'status': HC.CONTENT_UPDATE_PETITION
            })

        self.assertEqual(response['petition'].GetSerialisableTuple(),
                         petition.GetSerialisableTuple())

        # definitions

        definitions_update = HydrusNetwork.DefinitionsUpdate()

        for i in range(100, 200):

            definitions_update.AddRow(
                (HC.DEFINITIONS_TYPE_TAGS, i, 'series:test ' + str(i)))
            definitions_update.AddRow((HC.DEFINITIONS_TYPE_HASHES, i + 500,
                                       HydrusData.GenerateKey()))

        definitions_update_network_bytes = definitions_update.DumpToNetworkBytes(
        )

        definitions_update_hash = hashlib.sha256(
            definitions_update_network_bytes).digest()

        path = ServerFiles.GetExpectedFilePath(definitions_update_hash)

        HydrusPaths.MakeSureDirectoryExists(path)

        with open(path, 'wb') as f:

            f.write(definitions_update_network_bytes)

        response = service.Request(HC.GET, 'update',
                                   {'update_hash': definitions_update_hash})

        try:
            os.remove(path)
        except:
            pass

        self.assertEqual(response, definitions_update_network_bytes)

        # content

        rows = [(random.randint(100, 1000),
                 [random.randint(100, 1000) for i in range(50)])
                for j in range(20)]

        content_update = HydrusNetwork.ContentUpdate()

        for row in rows:

            content_update.AddRow(
                (HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_ADD, row))

        content_update_network_bytes = content_update.DumpToNetworkBytes()

        content_update_hash = hashlib.sha256(
            content_update_network_bytes).digest()

        path = ServerFiles.GetExpectedFilePath(content_update_hash)

        with open(path, 'wb') as f:

            f.write(content_update_network_bytes)

        response = service.Request(HC.GET, 'update',
                                   {'update_hash': content_update_hash})

        try:
            os.remove(path)
        except:
            pass

        self.assertEqual(response, content_update_network_bytes)

        # metadata

        metadata = HydrusNetwork.Metadata()

        metadata.AppendUpdate([definitions_update_hash, content_update_hash],
                              HydrusData.GetNow() - 101000,
                              HydrusData.GetNow() - 1000,
                              HydrusData.GetNow() + 100000)

        service._metadata = metadata

        response = service.Request(HC.GET, 'metadata_slice', {'since': 0})

        self.assertEqual(response['metadata_slice'].GetSerialisableTuple(),
                         metadata.GetSerialisableTuple())

        # post content

        raise NotImplementedError()
        '''
Ejemplo n.º 6
0
    def _test_file_repo(self, service):

        # file

        path = ServerFiles.GetExpectedFilePath(self._file_hash)

        HydrusPaths.MakeSureDirectoryExists(os.path.dirname(path))

        with open(path, 'wb') as f:

            f.write(EXAMPLE_FILE)

        response = service.Request(HC.GET, 'file', {'hash': self._file_hash})

        self.assertEqual(response, EXAMPLE_FILE)

        #

        try:
            os.remove(path)
        except:
            pass

        path = os.path.join(HC.STATIC_DIR, 'hydrus.png')

        with open(path, 'rb') as f:

            file_bytes = f.read()

        HG.test_controller.ClearWrites('file')

        service.Request(HC.POST, 'file', {'file': file_bytes})

        written = HG.test_controller.GetWrite('file')

        [(args, kwargs)] = written

        (written_service_key, written_account, written_file_dict) = args

        hash = b'\xadm5\x99\xa6\xc4\x89\xa5u\xeb\x19\xc0&\xfa\xce\x97\xa9\xcdey\xe7G(\xb0\xce\x94\xa6\x01\xd22\xf3\xc3'

        self.assertEqual(written_file_dict['hash'], hash)
        self.assertEqual(written_file_dict['ip'], '127.0.0.1')
        self.assertEqual(written_file_dict['height'], 200)
        self.assertEqual(written_file_dict['width'], 200)
        self.assertEqual(written_file_dict['mime'], 2)
        self.assertEqual(written_file_dict['size'], 5270)

        # ip

        (ip, timestamp) = ('94.45.87.123', HydrusData.GetNow() - 100000)

        HG.test_controller.SetRead('ip', (ip, timestamp))

        response = service.Request(HC.GET, 'ip', {'hash': self._file_hash})

        self.assertEqual(response['ip'], ip)
        self.assertEqual(response['timestamp'], timestamp)

        # account from hash

        subject_content = HydrusNetwork.Content(
            content_type=HC.CONTENT_TYPE_FILES, content_data=hash)

        subject_account_identifier = HydrusNetwork.AccountIdentifier(
            content=subject_content)

        HG.test_controller.SetRead('account', self._account)

        response = service.Request(
            HC.GET, 'other_account',
            {'subject_identifier': subject_account_identifier})

        self.assertEqual(repr(response['account']), repr(self._account))

        # thumbnail

        path = ServerFiles.GetExpectedThumbnailPath(self._file_hash)

        HydrusPaths.MakeSureDirectoryExists(os.path.dirname(path))

        with open(path, 'wb') as f:

            f.write(EXAMPLE_THUMBNAIL)

        response = service.Request(HC.GET, 'thumbnail',
                                   {'hash': self._file_hash})

        self.assertEqual(response, EXAMPLE_THUMBNAIL)

        try:
            os.remove(path)
        except:
            pass