def teardown_func(): # reset the temporary directory to system default shutil.rmtree(tempfile.tempdir) tempfile.tempdir = None # also remove any sessions created by the config file Session.remove()
def setup(self): Session.remove() self.tempdir = tempfile.mkdtemp() self.configfile = os.path.join(self.tempdir, 'config') self.datafile = os.path.join(self.tempdir, 'data') self.homedir = tempfile.mkdtemp(dir=self.tempdir) config = configparser.SafeConfigParser() config.add_section('settings') config.set('settings', 'dir.raw', os.path.join(self.tempdir, 'raw')) config.set('settings', 'dir.web', os.path.join(self.tempdir, 'web')) config.set('settings', 'sqlalchemy.url', 'sqlite:///' + os.path.join(self.tempdir, 'db')) config.set('settings', 'gpg.home', self.homedir) config.set('settings', 'gpg.passphrase', 'secret') config.set('settings', 'gpg.binary', 'gpg') config.set('settings', 'mail.on', 'True') config.set('settings', 'mail.message.author', 'proc@localhost') config.set('settings', 'notify.error', 'foo@localhost') config.set('settings', 'notify.999X.dhiv', 'foo@localhost') config.set('settings', 'notify.999X.dhcv', 'foo@localhost') config.set('settings', 'notify.999X.dhbv', 'foo@localhost') config.set('settings', 'notify.diagnostic', 'foo@localhost') self.config = config with open(self.configfile, 'w+') as fp: config.write(fp) sys.argv = ['', '-c', self.configfile] scripts.initdb.main() self.passphrase = 'secret', gpg = gnupg.GPG(gnupghome=self.homedir) self.gpg = gpg # persist the keys so the function properly reload them with tempfile.NamedTemporaryFile(dir=self.homedir) as fp: key = gpg.gen_key(gpg.gen_key_input()) fp.write(gpg.export_keys(str(key))) fp.write(gpg.export_keys(str(key), True)) self.key = key # The database will be reconfigured with the commannd, so make sure # it is not bound to a database Session.remove()
def test_duplicate(self): """ Ensure we get notified if a duplicate entry is attempted """ gpg = self.gpg datafile = os.path.join(self.tempdir, 'data') io = StringIO() print >> io, 'Has 1 result' print >> io, '076C 12345 P' io.flush() io.seek(0) gpg.encrypt_file(io, str(self.key), passphrase=self.passphrase, output=datafile) io.close() sys.argv = ['', '-c', self.configfile, datafile] scripts.parse.main() Session.remove() io = StringIO() print >> io, 'Has 1 result' print >> io, '076C 12345 N' io.flush() io.seek(0) gpg.encrypt_file(io, str(self.key), passphrase=self.passphrase, output=datafile) io.close() try: sys.argv = ['', '-c', self.configfile, datafile] scripts.parse.main() Session.remove() except: # the command will re-raise the exception, suppress it so we # can read the email pass emails = turbomail.interface.manager.transport.get_sent_mails() eq_(1, len(emails)) email_content = emails.pop() expected = 'Already exists: 76C12345' assert expected in email_content
def teardown(self): # reset the temporary directory to system default shutil.rmtree(self.tempdir) turbomail.interface.stop(force=True) turbomail.interface.config = {'mail.on': False} Session.remove()
def teardown_module(): Session.remove()