def save(self, user, check_sync=True, old_format=True): if isinstance(user, DashboardLegacyUser): user = User(data = user._mongo_doc) UserDB.save(self, user, check_sync, old_format)
class MongoTestCase(unittest.TestCase): """TestCase with an embedded MongoDB temporary instance. Each test runs on a temporary instance of MongoDB. The instance will be listen in a random port between 40000 and 50000. A test can access the connection using the attribute `conn`. A test can access the port using the attribute `port` """ fixtures = [] MockedUserDB = MockedUserDB user = User(data=MOCKED_USER_STANDARD) mock_users_patches = [] def setUp(self, celery, get_attribute_manager, userdb_use_old_format=False): """ Test case initialization. To not get a circular dependency between eduid-userdb and eduid-am, celery and get_attribute_manager needs to be imported in the place where this module is called. Usage: from eduid_am.celery import celery, get_attribute_manager class MyTest(MongoTestCase): def setUp(self): super(MyTest, self).setUp(celery, get_attribute_manager) ... :param celery: module :param get_attribute_manager: callable :return: """ super(MongoTestCase, self).setUp() self.tmp_db = MongoTemporaryInstance.get_instance() self.conn = self.tmp_db.conn self.port = self.tmp_db.port if celery and get_attribute_manager: self.am_settings = { 'BROKER_TRANSPORT': 'memory', # Don't use AMQP bus when testing 'BROKER_URL': 'memory://', 'CELERY_EAGER_PROPAGATES_EXCEPTIONS': True, 'CELERY_ALWAYS_EAGER': True, 'CELERY_RESULT_BACKEND': "cache", 'CELERY_CACHE_BACKEND': 'memory', # Be sure to tell AttributeManager about the temporary mongodb instance. 'MONGO_URI': self.tmp_db.get_uri(''), } celery.conf.update(self.am_settings) self.am = get_attribute_manager(celery) self.amdb = self.am.userdb else: self.amdb = UserDB(self.tmp_db.get_uri(''), 'eduid_am') self.amdb._drop_whole_collection() mongo_settings = { 'mongo_replicaset': None, 'mongo_uri': self.tmp_db.get_uri(''), } if getattr(self, 'settings', None) is None: self.settings = mongo_settings else: self.settings.update(mongo_settings) for db_name in self.conn.database_names(): self.conn.drop_database(db_name) # Set up test users in the MongoDB. Read the users from MockedUserDB, which might # be overridden by subclasses. _foo_userdb = self.MockedUserDB(self.mock_users_patches) for userdoc in _foo_userdb.all_userdocs(): this = deepcopy( userdoc) # deep-copy to not have side effects between tests user = User(data=this) self.amdb.save(user, check_sync=False, old_format=userdb_use_old_format) def tearDown(self): super(MongoTestCase, self).tearDown() for userdoc in self.amdb._get_all_docs(): assert DashboardUser(data=userdoc) for db_name in self.conn.database_names(): if db_name == 'local': continue db = self.conn[db_name] for col_name in db.collection_names(): if 'system' not in col_name: db.drop_collection(col_name) del db self.conn.drop_database(db_name) self.amdb._drop_whole_collection() self.conn.disconnect() def mongodb_uri(self, dbname): self.assertIsNotNone(dbname) return self.tmp_db.get_uri(dbname=dbname)