def test_init_index_compatibility(self):
     collection = self.db['canaries']
     collection.create_indexes([IndexModel([('id', ASCENDING)])])
     existing_indexes = collection.index_information()
     self.assertNotIn('unique', existing_indexes['id_1'])
     MongoStore(self.db_hosts, self.db_name, None, None)
     new_existing_indexes = collection.index_information()
     self.assertTrue(new_existing_indexes['id_1']['unique'])
     # For code coverage, testing when is_unique is already True as it
     # should be.
     MongoStore(self.db_hosts, self.db_name, None, None)
 def test_init_ssl_cert_reqs(self):
     MongoStore(self.db_hosts,
                self.db_name,
                None,
                None,
                ssl_cert_reqs='NONE')
     with self.assertRaises(TypeError):
         MongoStore(self.db_hosts,
                    self.db_name,
                    None,
                    None,
                    ssl_cert_reqs='FROODLE')
Exemple #3
0
def main():  # pragma: no cover
    config = SafeConfigParser()
    dirs = ('.', '/etc', '/usr/local/etc')
    if not config.read([os.path.join(dir, config_file) for dir in dirs]):
        sys.exit('Could not find {} in {}'.format(config_file, dirs))

    try:
        logfile = config.get('logging', 'file')
        rotating = config.getboolean('logging', 'rotate', fallback=False)
        if rotating:
            max_size = config.get('logging', 'max_size', fallback=1048576)
            backup_count = config.get('logging', 'backup_count', fallback=5)
            handler = logbook.RotatingFileHandler(logfile,
                                                  max_size=max_size,
                                                  backup_count=backup_count)
        else:
            handler = logbook.FileHandler(logfile)
        handler.push_application()
    except Exception:
        logbook.StderrHandler().push_application()

    try:
        kwargs = dict(config.items('mongodb'))
    except NoSectionError:
        sys.exit('No "mongodb" section in config file')
    args = []
    for arg in ('hosts', 'database', 'username', 'password'):
        try:
            args.append(config.get('mongodb', arg))
        except NoOptionError:
            sys.exit(
                'No "{}" setting in "mongodb" section of config file'.format(
                    arg))
        kwargs.pop(arg)
    args[0] = [s.strip() for s in args[0].split(',')]
    store = MongoStore(*args, **kwargs)

    try:
        email_sender = config.get('email', 'sender')
    except NoSectionError:
        sys.exit('No "email" section in config file')
    except NoOptionError:
        sys.exit('No "sender" setting in "email" section of config file')

    business_logic = BusinessLogic(store, email_sender)

    try:
        listen_port = int(config.get('wsgi', 'port'))
        log.info('Binding to port {}'.format(listen_port))
    except Exception:
        listen_port = 80
        log.info('Binding to default port {}'.format(listen_port))

    try:
        auth_key = config.get('wsgi', 'auth_key')
        log.info('Server authentication enabled')
    except Exception:
        log.warning('Server authentication DISABLED')
        auth_key = None

    httpd = make_server('',
                        listen_port,
                        partial(application, business_logic, auth_key),
                        handler_class=LogbookWSGIRequestHandler)
    business_logic.schedule_next_deadline()
    httpd.serve_forever()
 def setUp(self):
     self.db_hosts = ['localhost']
     self.db_name = "coal-mine-test-" + str(uuid.uuid4())
     self.db_conn = MongoClient()
     self.db = self.db_conn[self.db_name]
     self.store = MongoStore(self.db_hosts, self.db_name, None, None)
class MongoStoreTests(TestCase):
    def setUp(self):
        self.db_hosts = ['localhost']
        self.db_name = "coal-mine-test-" + str(uuid.uuid4())
        self.db_conn = MongoClient()
        self.db = self.db_conn[self.db_name]
        self.store = MongoStore(self.db_hosts, self.db_name, None, None)

    def tearDown(self):
        self.db_conn.drop_database(self.db)

    def test_create(self):
        self.store.create({'id': 'abcdefgh'})

    def test_update_noop(self):
        self.store.create({'id': 'abcdefgh'})
        self.store.update('abcdefgh', {})

    def test_update_set(self):
        self.store.create({'id': 'abcdefgh'})
        self.store.update('abcdefgh', {'periodicity': 10})
        self.assertEqual(self.store.get('abcdefgh')['periodicity'], 10)

    def test_update_unset(self):
        self.store.create({'id': 'abcdefgh'})
        self.store.update('abcdefgh', {'periodicity': 10})
        self.store.update('abcdefgh', {'periodicity': None})
        self.assertNotIn('periodicity', self.store.get('abcdefgh'))

    def test_get_nonexistent(self):
        with self.assertRaises(KeyError):
            self.store.get('abcdefgh')

    def test_list(self):
        self.store.create({
            'id': 'abcdefgh',
            'name': 'freedle',
            'periodicity': 600,
            'paused': False,
            'late': False
        })
        iterator = self.store.list()
        next(iterator)
        with self.assertRaises(StopIteration):
            next(iterator)
        next(self.store.list(verbose=True))
        next(self.store.list(paused=False))
        next(self.store.list(late=False))
        next(self.store.list(order_by='deadline'))
        next(self.store.list(search=r'freedle'))

    def test_upcoming_deadlines(self):
        self.store.create({'id': 'abcdefgh', 'paused': False, 'late': False})
        next(self.store.upcoming_deadlines())

    def test_delete(self):
        self.store.create({'id': 'abcdefgh'})
        self.store.delete('abcdefgh')
        with self.assertRaises(KeyError):
            self.store.delete('abcdefgh')

    def test_find_identifier(self):
        self.store.create({'id': 'abcdefgh', 'slug': 'froodle'})
        self.assertEqual(self.store.find_identifier('froodle'), 'abcdefgh')
        with self.assertRaises(KeyError):
            self.store.find_identifier('freedle')
 def test_init_auth(self):
     with self.assertRaises(OperationFailure):
         MongoStore(self.db_hosts, self.db_name, 'nope', 'nope')
 def test_basic_init(self):
     MongoStore(self.db_hosts, self.db_name, None, None)
     pass
 def get_store(self):
     self.db_hosts = ['localhost']
     self.db_name = "coal-mine-test-" + str(uuid.uuid4())
     return MongoStore(self.db_hosts, self.db_name, None, None)