Example #1
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')

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

        # definitions

        definitions_update = HydrusNetwork.DefinitionsUpdate()

        if 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_string = definitions_update.DumpToNetworkString(
        )

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

        path = ServerFiles.GetExpectedFilePath(definitions_update_hash)

        with open(path, 'wb') as f:
            f.write(definitions_update_network_string)

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

        try:
            os.remove(path)
        except:
            pass

        self.assertEqual(response, definitions_update_network_string)

        # 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_string = content_update.DumpToNetworkString()

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

        path = ServerFiles.GetExpectedFilePath(content_update_hash)

        with open(path, 'wb') as f:
            f.write(content_update_network_string)

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

        try:
            os.remove(path)
        except:
            pass

        self.assertEqual(response, content_update_network_string)

        # 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()
        '''
Example #2
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.encode('hex')})

        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 = f.read()

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

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

        [(args, kwargs)] = written

        (written_service_key, written_account, written_file_dict) = args

        self.assertEqual(
            written_file_dict['hash'],
            '\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['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.encode('hex')})

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

        # 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.encode('hex')})

        self.assertEqual(response, EXAMPLE_THUMBNAIL)

        try:
            os.remove(path)
        except:
            pass