Ejemplo n.º 1
0
 def __init__(self, prompt, account, namespace='object', crecord_type=None):
     super(Browser, self).__init__(prompt)
     self.account = account
     self.namespace = namespace
     self.crecord_type = crecord_type
     self.storage = Storage(account,
                            namespace=namespace,
                            logging_level=INFO)
Ejemplo n.º 2
0
    def test_01_Init(self):
        global STORAGE
        STORAGE = Storage(self.user_account,
                          namespace='unittest',
                          logging_level=DEBUG)

        records = STORAGE.find(account=self.root_account)
        STORAGE.remove(records, account=self.root_account)
Ejemplo n.º 3
0
    def __init__(self, collections=None, *args, **kwargs):
        super(PurgeModule, self).__init__(*args, **kwargs)

        self.logger = Logger.get('migrationmodule', MigrationModule.LOG_PATH)
        self.config = Configuration.load(PurgeModule.CONF_PATH, Json)
        conf = self.config.get(self.CATEGORY, {})

        self.storage = Storage(account=Account(user='******', group='root'))

        if collections is not None:
            self.collections = collections
        else:
            self.collections = conf.get('collections', DEFAULT_COLLECTIONS)
Ejemplo n.º 4
0
            raise Exception('Invalid cryptedKey ... (%s)' % authkey)

    def test_04_authkey(self):
        ACCOUNT = Account(user="******", group="capensis")

        authkey = ACCOUNT.get_authkey()
        if not authkey:
            raise Exception('Invalid authkey ... (%s)' % authkey)

    def test_05_Store(self):
        ACCOUNT = Account(user="******", group="capensis")

        STORAGE.put(ACCOUNT)

    def test_09_Remove(self):
        # Anonymous cant remove account
        self.assertRaises(ValueError, STORAGE.remove, ACCOUNT, Account())

        # But root can ;)
        STORAGE.remove(ACCOUNT)

    def test_99_DropNamespace(self):
        STORAGE.drop_namespace('unittest')


if __name__ == "__main__":
    STORAGE = Storage(Account(user="******", group="root"), namespace='unittest')
    output = root_path + "/tmp/tests_report"
    unittest.main(testRunner=xmlrunner.XMLTestRunner(output=output),
                  verbosity=3)
Ejemplo n.º 5
0
    def sendmail(self, account, recipients, subject, body, attachments,
                 smtp_host, smtp_port, html):
        """
        :param account: Account or nothing for anon.
        :param recipients: str("*****@*****.**"), Account list of (Account
            or string).
        :param subject: str("My Subject").
        :param body: str("My Body").
        :param attachments: file or list of file.
        :param smtp_host: str("localhost").
        :param smtp_port: int(25).
        :param html: allow html into mail body (booleen).
        """

        charset.add_charset('utf-8', charset.SHORTEST, charset.QP)

        # Verify account
        account_firstname = account.firstname
        account_lastname = account.lastname
        account_mail = account.mail

        if not account_mail:
            self.logger.info(
                'No mail adress for this user (Fill the mail account field)')
            account_mail = '{0}@{1}'.format(account.user, socket.gethostname())

        if isinstance(account_mail, (list, tuple)):
            account_mail = account_mail[0]

        if not account_lastname and not account_firstname:
            account_full_mail = '"{0}" <{1}>'.format(
                account_mail.split('@')[0].title(), account_mail)

        else:
            account_full_mail = account.get_full_mail()

        if not re.match("^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+.([a-zA-Z]{2,6})?$",
                        str(account_mail)):
            return (
                2, 'Invalid Email format for sender: {0}'.format(account_mail))

        # Verify recipients
        if not recipients:
            return (2, 'No recipients configured')

        if not isinstance(recipients, list):
            recipients = [recipients]

        dests = []

        for dest in recipients:
            if isinstance(dest, basestring):
                if re.match(
                        "^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+.([a-zA-Z]{2,6})?$",
                        dest):
                    dest_mail = dest
                    dest_full_mail = '"{0}" <{1}>'.format(
                        dest_mail.split('@')[0].title(), dest_mail)
                    dests.append(dest_full_mail)

            else:
                self.logger.error(
                    'Ignoring invalid recipient: {0}'.format(dest))

        dests_str = ', '.join(dests)

        # Verify attachments
        if attachments:
            storage = Storage(account=account, namespace='object')

            if not isinstance(attachments, list):
                attachments = [attachments]

        # Send

        msg = MIMEMultipart()
        msg["From"] = account_full_mail
        msg["To"] = dests_str
        msg["Subject"] = subject

        if html:
            msg.attach(MIMEText(body, 'html', _charset='utf-8'))

        else:
            msg.attach(MIMEText(body, 'plain', _charset='utf-8'))

        msg['Date'] = formatdate(localtime=True)

        if attachments:
            for _file in attachments:
                part = MIMEBase('application', "octet-stream")

                #meta_file = _file.get(storage)
                content_file = _file.get(storage)
                part.set_payload(content_file)
                Encoders.encode_base64(part)
                part.add_header(
                    'Content-Disposition',
                    'attachment; filename="%s"' % _file.data['file_name'])
                part.add_header('Content-Type', _file.data['content_type'])
                msg.attach(part)

        sock = socket.socket()

        try:
            sock.connect((smtp_host, smtp_port))

        except Exception as err:
            return (2, 'Connection to SMTP <{0}:{1}> failed: {2}'.format(
                smtp_host, smtp_port, err))

        try:
            server = smtplib.SMTP(smtp_host, smtp_port)
            server.sendmail(account_full_mail, dests, msg.as_string())
            server.quit()

        except Exception as err:
            return (2, "Impossible to send mail: {0}".format(err))

        return (0, "Mail sent successfully")