Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_authenticate_fails():
db = create_autospec(Database)
error = "command SON([('saslStart', 1), ('mechanism', 'SCRAM-SHA-1'), ('payload', Binary('n,,n=foo,r=OTI3MzA3MTEzMTIx', 0)), ('autoAuthorize', 1)]) on namespace admin.$cmd failed: Authentication failed."
db.authenticate.side_effect = OperationFailure(error)
assert auth.authenticate(db, sentinel.user, sentinel.password) is False
def __init__(self, db, host, port=27107, tz_aware=True, user=None, password=None, **kwargs):
"""
Create & open the connection - and authenticate.
"""
self.database = pymongo.database.Database(
pymongo.MongoClient(
host=host,
port=port,
tz_aware=tz_aware,
**kwargs
),
db
)
if user is not None and password is not None:
self.database.authenticate(user, password)
def __init__(self, db, host=DEFAULT_MONGO_HOST, port=DEFAULT_MONGO_PORT, tz_aware=True, user=None, password=None, **kwargs):
"""
Create & open the connection - and authenticate.
"""
self.database = pymongo.database.Database(
pymongo.MongoClient(
host=host,
port=port,
tz_aware=tz_aware,
w=0,
**kwargs
),
db
)
if user is not None and password is not None:
self.database.authenticate(user, password)
def __getattr__(self, name):
"""Get a database by name.
Raises :class:`~pymongo.errors.InvalidName` if an invalid
database name is used.
:Parameters:
- `name`: the name of the database to get
"""
return database.Database(self, name)
'state abbreviation': state_abbv,
'place name': place,
'places': result})
return (isFound, content) #Return True and JSON results
# PRESENT GLOBAL
ZIP = 'zip'
NEARBY = 'nearby'
with open(os.path.expanduser('~/environment.json')) as f:
env = json.load(f) # Extract environment variables
connection = Connection( env['DOTCLOUD_DB_MONGODB_URL'] ) # Connect to be w/ env info
#connection = Connection()
db = Database(connection,'zip') # Get handle to ZIP db
application = default_app() # WSGI application
default) the :attr:`codec_options` of this :class:`MongoClient` is
used.
- `read_preference` (optional): The read preference to use. If
``None`` (the default) the :attr:`read_preference` of this
:class:`MongoClient` is used. See :mod:`~pymongo.read_preferences`
for options.
- `write_concern` (optional): An instance of
:class:`~pymongo.write_concern.WriteConcern`. If ``None`` (the
default) the :attr:`write_concern` of this :class:`MongoClient` is
used.
- `read_concern` (optional): An instance of
:class:`~pymongo.read_concern.ReadConcern`. If ``None`` (the
default) the :attr:`read_concern` of this :class:`MongoClient` is
used.
"""
return database.Database(
self, name, codec_options, read_preference,
write_concern, read_concern)
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from pymongo.database import Database as PymongoDatabase
from bson.dbref import DBRef
from mongolite.document import Document
from collection import Collection
class Database(PymongoDatabase):
def __init__(self, *args, **kwargs):
self._collections = {}
super(Database, self).__init__(*args, **kwargs)
def __getattr__(self, key):
if key in self.connection._registered_documents:
document = self.connection._registered_documents[key]
return getattr(self[document.__collection__], key)
else:
if not key in self._collections:
self._collections[key] = Collection(self, key)
return self._collections[key]
def dereference(self, dbref, model = None):
if model is None:
def drop_database(self, name_or_database):
"""Drop a database.
Raises :class:`TypeError` if `name_or_database` is not an instance of
:class:`basestring` (:class:`str` in python 3) or Database.
:Parameters:
- `name_or_database`: the name of a database to drop, or a
:class:`~pymongo.database.Database` instance representing the
database to drop
"""
name = name_or_database
if isinstance(name, database.Database):
name = name.name
if not isinstance(name, basestring):
raise TypeError("name_or_database must be an instance of "
"%s or Database" % (basestring.__name__,))
self._purge_index(name)
self[name].command("dropDatabase")