def _attach_file_to_resource(self, realm, name, data=None,
                                 filename=None, description=None,
                                 replace=False, content_type=None):
        """Attaches a file to a resource. Assumes the resource exists and
           has already been navigated to."""

        if data is None:
            data = random_page()
        if description is None:
            description = random_sentence()
        if filename is None:
            filename = random_word()

        tc.submit('attachfilebutton', 'attachfile')
        tc.url(self.url + r'/attachment/%s/%s/\?action=new$' % (realm, name))
        fp = StringIO(data)
        tc.formfile('attachment', 'attachment', filename,
                    content_type=content_type, fp=fp)
        tc.formvalue('attachment', 'description', description)
        if replace:
            tc.formvalue('attachment', 'replace', True)
        tc.submit()
        tc.url(self.url + r'/attachment/%s/%s/$' % (realm, name))

        return filename
dataset/ETHPy150Open edgewall/trac/trac/tests/functional/tester.py/FunctionalTester._attach_file_to_resource/original