Beispiel #1
0
class WatchDogAPITest(unittest.TestCase):
    def setUp(self):
        self.conf_file = './fswatch.conf'
        self.config = FSConfig(self.conf_file)
        self.app_key = self.config.app_key()
        self.search_key = self.config.search_key()
        self.baseurl = self.config.base_url()
        self.api = SearchAppApi(self.search_key, self.app_key, self.baseurl)

    @patch('wd_api.SearchAppApi.init_schema')
    def test_mapping(self, *args):
        self.api.init_schema()
        self.api.init_schema.assert_called_once_with()

    @patch('wd_api.SearchAppApi.do_post')
    def test_post(self, *args):
        fname = './testfile.pdf'
        mimetype = "application/pdf"
        modified = datetime.datetime.now()
        owner = "test_user"
        perms = "06444"
        size = 1235
        self.api.do_post(
            fname,
            mimetype,
            owner,
            modified,
            perms,
            size
        )
        self.api.do_post.assert_called_once_with(
            fname, mimetype,
            owner, modified,
            perms, size
        )
Beispiel #2
0
    def do_post(self, filename, mimetype, owner, modified, perms, size):
        """
        Got yer meat and potatoes here
        :param filename: name of file to be uploaded
        :param mimetype: and its mimetype
        :return:
        """
        auth = self.get_auth()
        index_url = self.get_index_url()
        multipart_data = MultipartEncoder(
            fields={
                'file': (filename, open(filename, 'rb'), mimetype),
                'owner': owner,
                'modified': str(modified)
                # 'file_perms': perms,
                # 'file_size': "{0} bytes".format(size)
            })
        resp = requests.post(
                index_url, auth=auth, data=multipart_data, headers={'Content-Type': multipart_data.content_type}
        )
        logger.debug(resp.status_code)
        if resp.status_code != 200:
            raise SearchAppIndexException("{0} {1} {2}".format(resp.status_code, resp.reason, resp.url))
        resp.close()

# local sanity test
if __name__ == '__main__':
    api = SearchAppApi(config.search_key(), config.app_key(), config.base_url(), config.schema())
    api.init_schema()
    api.do_post("files/TestMe.pdf", 'application/pdf')