def test_signup_with_invalid_email(self): USER_REQ = OpMsg({ "find": "users", "filter": {"login": "******"}, "limit": 1, "singleBatch": True, "$db": "authservice", "$readPreference": {"mode": "primaryPreferred"}}, namespace="authservice") CLIENT_REQ = OpMsg({ "find": "clients", "filter": {"client_id": "midleware1", "client_secret": "1sfg135df1d32fsdf489d7q6sdq6s4d"}, "limit": 1, "singleBatch": True, "$db": "authservice", "$readPreference": {"mode": "primaryPreferred"}}, namespace="authservice" ) future = go(self.app.post, '/auth/signup', json={ "userID": '', "login": "******", "password": "******", "client_id": "midleware1", "client_secret": "1sfg135df1d32fsdf489d7q6sdq6s4d" }) self.assertEqual(future().status_code, 400)
def test_signup_create_existing_user(self): USER_REQ = OpMsg({ "find": "users", "filter": {"login": "******"}, "limit": 1, "singleBatch": True, "$db": "authservice", "$readPreference": {"mode": "primaryPreferred"}}, namespace="authservice") CLIENT_REQ = OpMsg({ "find": "clients", "filter": {"client_id": "midleware1", "client_secret": "1sfg135df1d32fsdf489d7q6sdq6s4d"}, "limit": 1, "singleBatch": True, "$db": "authservice", "$readPreference": {"mode": "primaryPreferred"}}, namespace="authservice" ) future = go(self.app.post, '/auth/signup', json={ "userID": '', "login": "******", "password": "******", "client_id": "midleware1", "client_secret": "1sfg135df1d32fsdf489d7q6sdq6s4d" }) request = self.server.receives(CLIENT_REQ) request.ok(cursor={'id': 0, 'firstBatch': [CLIENT]}) request = self.server.receives(USER_REQ) request.ok(cursor={'id': 0, 'firstBatch': [USER]})
def test_delete_many(self): with going(self.collection.delete_many, {}): self.server.receives( OpMsg(SON([('delete', 'collection'), ('writeConcern', { 'w': 0 })]), flags=2))
def test_delete_nonexisting_user(self): USER_REQ = OpMsg({ "find": "users", "filter": {"login": "******"}, "limit": 1, "singleBatch": True, "$db": "authservice", "$readPreference": {"mode": "primaryPreferred"}}, namespace="authservice") user = { "_id": "5b37fff8bbf300b7ef185042", "login": "******", "password": "******", "role": "admin" } access_token = generate_access_token( user=user, pivate_key=app.config['PRIVATE_KEY'], auth_host=app.config['AUTH_HOST'], token_ttl=app.config['TOKEN_TTL'], auth_algo=app.config['AUTH_ALGO'] ) headers = {'Authorization': 'Bearer {}'.format(access_token['access_token'])} future = go(self.app.delete, '/auth/signup', headers=headers, json={ "userID": '', "login": "******", "password": "******", "client_id": "midleware1", "client_secret": "1sfg135df1d32fsdf489d7q6sdq6s4d" }) request = self.server.receives(USER_REQ) request.ok(cursor={'id': 0, 'firstBatch': []}) self.assertEqual(future().status_code, 400)
def test_replace_one(self): with going(self.collection.replace_one, {}, {}): self.server.receives( OpMsg(SON([('update', 'collection'), ('writeConcern', { 'w': 0 })]), flags=2))
def test_aggregate(self): server = MockupDB() server.autoresponds('ismaster', ismaster=True, msg='isdbgrid', minWireVersion=2, maxWireVersion=6) self.addCleanup(server.stop) server.run() client = MongoClient(server.uri) self.addCleanup(client.close) collection = client.test.collection with going(collection.aggregate, []): command = server.receives(aggregate='collection', pipeline=[]) self.assertFalse(command.slave_ok, 'SlaveOkay set') command.ok(result=[{}]) secondary_collection = collection.with_options( read_preference=ReadPreference.SECONDARY) with going(secondary_collection.aggregate, []): command = server.receives(OpMsg({"aggregate": "collection", "pipeline": [], '$readPreference': {'mode': 'secondary'}})) command.ok(result=[{}]) self.assertTrue(command.slave_ok, 'SlaveOkay not set')
def mock_db_get(self, url=None): # dateStr = "2018-11-11T00:00:00.000Z" # date = dateutil.parser.parse(dateStr) request = self.server.receives( OpMsg('find', 'urls', filter={'shorturl': url})) request.ok(cursor={'id': 0, 'firstBatch': [mapping_dict[url]]})
def test_query_and_read_mode_sharded_op_msg(self): """Test OP_MSG sends non-primary $readPreference and never $query.""" server = MockupDB() server.autoresponds('ismaster', ismaster=True, msg='isdbgrid', minWireVersion=2, maxWireVersion=6) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) self.addCleanup(client.close) read_prefs = ( Primary(), SecondaryPreferred(), PrimaryPreferred(), Secondary(), Nearest(), SecondaryPreferred([{'tag': 'value'}]),) for query in ({'a': 1}, {'$query': {'a': 1}},): for mode in read_prefs: collection = client.db.get_collection('test', read_preference=mode) cursor = collection.find(query.copy()) with going(next, cursor): request = server.receives() # Command is not nested in $query. request.assert_matches(OpMsg( SON([('find', 'test'), ('filter', {'a': 1}), ('$readPreference', mode.document)]))) request.replies({'cursor': {'id': 0, 'firstBatch': [{}]}})
def test_signin_get_users(self): USER_REQ = OpMsg({ "find": "users", "filter": {}, "projection": {"password": 0}, "$db": "authservice", "$readPreference": {"mode": "primaryPreferred"}}, namespace="authservice") user = { "_id": "5b37fff8bbf300b7ef185042", "login": "******", "password": "******", "role": "admin" } access_token = generate_access_token( user=user, pivate_key=app.config['PRIVATE_KEY'], auth_host=app.config['AUTH_HOST'], token_ttl=app.config['TOKEN_TTL'], auth_algo=app.config['AUTH_ALGO'] ) headers = {'Authorization': 'Bearer {}'.format(access_token['access_token'])} future = go(self.app.get, '/auth/signin', headers=headers) request = self.server.receives(USER_REQ, timeout=60) request.ok(cursor={'id': 0, 'firstBatch': [USER]})
def test_update_many(self): with going(self.collection.update_many, {}, {'$unset': 'a'}): self.server.receives( OpMsg(SON([('update', 'collection'), ('ordered', True), ('writeConcern', { 'w': 0 })]), flags=2))
def test_datetime(self): server = MockupDB(auto_ismaster=True) server.run() client = MongoClient(server.uri) # Python datetimes have microsecond precision, BSON only millisecond. # Ensure this datetime matches itself despite the truncation. dt = datetime.datetime(2018, 12, 1, 6, 6, 6, 12345) doc = SON([('_id', 1), ('dt', dt)]) with going(client.db.collection.insert_one, doc): server.receives(OpMsg('insert', 'collection', documents=[doc])).ok()
def test_insert_many(self): collection = self.collection.with_options( write_concern=WriteConcern(0)) docs = [{'_id': 1}, {'_id': 2}] with going(collection.insert_many, docs, ordered=False): self.server.receives( OpMsg(SON([('insert', 'collection'), ('ordered', False), ('writeConcern', { 'w': 0 })]), flags=2))
def client(): server = MockupDB(auto_ismaster=True) server.run() server.autoresponds(OpMsg("find", "todos"), cursor={ "id": 0, "firstBatch": items.json_data }) mongo_uri = f"{server.uri}/test" # Create the new app. os.environ["SECRET_KEY"] = "SECRET_KEY" os.environ["LOGIN_DISABLED"] = "1" test_app = app.create_app(mongo_uri) # Use the app to create a test_client that can be used in our tests. with test_app.test_client() as client: yield client
def test_signup_with_invalid_client(self): CLIENT_REQ = OpMsg({ "find": "clients", "filter": {"client_id": "midleware1", "client_secret": "1sfg135df1d32fsdf489d7q6sdq6s4d"}, "limit": 1, "singleBatch": True, "$db": "authservice", "$readPreference": {"mode": "primaryPreferred"}}, namespace="authservice" ) future = go(self.app.post, '/auth/signup', json={ "userID": '', "login": "******", "password": "******", "client_id": "midleware1", "client_secret": "1sfg135df1d32fsdf489d7q6sdq6s4d" }) request = self.server.receives(CLIENT_REQ) request.ok(cursor={'id': 0, 'firstBatch': []}) self.assertEqual(future().status_code, 400)
from collections import namedtuple from mockupdb import MockupDB, going, OpMsg, OP_MSG_FLAGS from pymongo import MongoClient, WriteConcern, version_tuple from pymongo.operations import InsertOne, UpdateOne, DeleteOne from tests import unittest WriteOperation = namedtuple('WriteOperation', ['name', 'function', 'request', 'reply']) write_operations = [ WriteOperation('insert_one', lambda coll: coll.insert_one({}), request=OpMsg({"insert": "coll"}, flags=0), reply={ 'ok': 1, 'n': 1 }), WriteOperation('insert_one-w0', lambda coll: coll.with_options(write_concern=WriteConcern( w=0)).insert_one({}), request=OpMsg({"insert": "coll"}, flags=OP_MSG_FLAGS['moreToCome']), reply=None), WriteOperation('insert_many', lambda coll: coll.insert_many([{}, {}, {}]), request=OpMsg({"insert": "coll"}, flags=0), reply={ 'ok': 1,
from pymongo import MongoClient, WriteConcern from pymongo.operations import InsertOne, UpdateOne, DeleteOne from pymongo.cursor import CursorType import unittest Operation = namedtuple( 'Operation', ['name', 'function', 'request', 'reply']) operations = [ Operation( 'find_one', lambda coll: coll.find_one({}), request=OpMsg({"find": "coll"}, flags=0), reply={'ok': 1, 'cursor': {'firstBatch': [], 'id': 0}}), Operation( 'aggregate', lambda coll: coll.aggregate([]), request=OpMsg({"aggregate": "coll"}, flags=0), reply={'ok': 1, 'cursor': {'firstBatch': [], 'id': 0}}), Operation( 'insert_one', lambda coll: coll.insert_one({}), request=OpMsg({"insert": "coll"}, flags=0), reply={'ok': 1, 'n': 1}), Operation( 'insert_one-w0', lambda coll: coll.with_options( write_concern=WriteConcern(w=0)).insert_one({}),
def test_insert_one(self): with going(self.collection.insert_one, {'_id': 1}): # The moreToCome flag = 2. self.server.receives( OpMsg('insert', 'collection', writeConcern={'w': 0}, flags=2))
def test_op_insert_manipulate_false(self): # Test three aspects of legacy insert with manipulate=False: # 1. The return value is None, [None], or [None, None] as appropriate. # 2. _id is not set on the passed-in document object. # 3. _id is not sent to server. server = MockupDB(auto_ismaster=True) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) self.addCleanup(client.close) coll = client.db.get_collection('coll', write_concern=WriteConcern(w=0)) doc = {} with going(coll.insert, doc, manipulate=False) as future: if version_tuple >= (3, 7): server.receives( OpMsg(SON([("insert", coll.name), ("ordered", True), ("writeConcern", { "w": 0 }), ("documents", [{}])]), flags=OP_MSG_FLAGS['moreToCome'])) else: server.receives(OpInsert({'_id': absent})) self.assertFalse('_id' in doc) self.assertIsNone(future()) docs = [{}] # One doc in a list. with going(coll.insert, docs, manipulate=False) as future: if version_tuple >= (3, 7): # PyMongo 3.7 ordered bulk w:0 writes use implicit w:1. request = server.receives() request.assert_matches( OpMsg(SON([("insert", coll.name), ("ordered", True), ("documents", [{}])]), flags=0)) request.reply({"n": 1}) else: server.receives(OpInsert({'_id': absent})) self.assertFalse('_id' in docs[0]) self.assertEqual(future(), [None]) docs = [{}, {}] # Two docs. with going(coll.insert, docs, manipulate=False) as future: if version_tuple >= (3, 7): # PyMongo 3.7 ordered bulk w:0 writes use implicit w:1. request = server.receives() request.assert_matches( OpMsg(SON([("insert", coll.name), ("ordered", True), ("documents", [{}, {}])]), flags=0)) request.reply({"n": 2}) else: server.receives(OpInsert({'_id': absent}, {'_id': absent})) self.assertFalse('_id' in docs[0]) self.assertFalse('_id' in docs[1]) self.assertEqual(future(), [None, None])
def test_client_handshake_data(self): primary, secondary = MockupDB(), MockupDB() for server in primary, secondary: server.run() self.addCleanup(server.stop) hosts = [server.address_string for server in (primary, secondary)] primary_response = OpReply('ismaster', True, setName='rs', hosts=hosts, minWireVersion=2, maxWireVersion=6) error_response = OpReply( 0, errmsg='Cache Reader No keys found for HMAC ...', code=211) secondary_response = OpReply('ismaster', False, setName='rs', hosts=hosts, secondary=True, minWireVersion=2, maxWireVersion=6) client = MongoClient(primary.uri, replicaSet='rs', appname='my app', heartbeatFrequencyMS=500) # Speed up the test. self.addCleanup(client.close) # New monitoring sockets send data during handshake. heartbeat = primary.receives('ismaster') _check_handshake_data(heartbeat) heartbeat.ok(primary_response) heartbeat = secondary.receives('ismaster') _check_handshake_data(heartbeat) heartbeat.ok(secondary_response) # Subsequent heartbeats have no client data. primary.receives('ismaster', 1, client=absent).ok(error_response) secondary.receives('ismaster', 1, client=absent).ok(error_response) # The heartbeat retry has no client data after a command failure. primary.receives('ismaster', 1, client=absent).ok(error_response) secondary.receives('ismaster', 1, client=absent).ok(error_response) # Still no client data. primary.receives('ismaster', 1, client=absent).ok(primary_response) secondary.receives('ismaster', 1, client=absent).ok(secondary_response) # After a disconnect, next ismaster has client data again. primary.receives('ismaster', 1, client=absent).hangup() heartbeat = primary.receives('ismaster') _check_handshake_data(heartbeat) heartbeat.ok(primary_response) secondary.autoresponds('ismaster', secondary_response) # Start a command, so the client opens an application socket. future = go(client.db.command, 'whatever') for request in primary: if request.matches(Command('ismaster')): if request.client_port == heartbeat.client_port: # This is the monitor again, keep going. request.ok(primary_response) else: # Handshaking a new application socket. _check_handshake_data(request) request.ok(primary_response) else: # Command succeeds. if version_tuple >= (3, 7): request.assert_matches(OpMsg('whatever')) else: request.assert_matches(Command('whatever')) request.ok() assert future() return