예제 #1
0
class LoggedInRequestTests(MongoTestCase):
    """Base TestCase for those tests that need a logged in environment setup"""

    def setUp(self, settings={}, skip_on_fail=False, std_user='******'):

        self.settings = deepcopy(SETTINGS)
        self.settings.update(settings)
        super(LoggedInRequestTests, self).setUp(celery, get_attribute_manager, userdb_use_old_format=True)

        self.redis_instance = RedisTemporaryInstance.get_instance()
        self.settings['redis_host'] = 'localhost'
        self.settings['redis_port'] = self.redis_instance._port
        self.settings['redis_db'] = '0'

        self.settings['mongo_uri'] = self.mongodb_uri('')
        try:
            app = eduiddashboard_main({}, **self.settings)
        except pymongo.errors.ConnectionFailure:
            if skip_on_fail:
                raise unittest.SkipTest("requires accessible MongoDB server on {!r}".format(
                    self.settings['mongo_uri']))
            raise

        self.testapp = TestApp(app)

        self.config = testing.setUp()
        self.config.registry.settings = self.settings
        self.config.registry.registerUtility(self, IDebugLogger)

        #self.db = get_db(self.settings)
        self.db = app.registry.settings['mongodb'].get_database('eduid_dashboard')    # central userdb, raw mongodb
        self.userdb_new = UserDB(self.mongodb_uri(''), 'eduid_am')   # central userdb in new format (User)
        self.dashboard_db = DashboardUserDB(self.mongodb_uri('eduid_dashboard'))
        # Clean up the dashboards private database collections
        logger.debug("Dropping profiles, verifications and reset_passwords from {!s}".format(self.db))
        self.db.profiles.drop()
        self.db.verifications.drop()
        self.db.reset_passwords.drop()

        self.user = self.userdb_new.get_user_by_mail(std_user)
        self.assertIsNotNone(self.user, "Could not load the standard test user {!r} from the database {!s}".format(
            std_user, self.userdb_new))
        self.logged_in_user = None

        # Copy all the users from the eduid userdb into the dashboard applications userdb
        # since otherwise the out-of-sync check will trigger on every save to the dashboard
        # applications database because there is no document there with the right modified_ts
        for userdoc in self.userdb_new._get_all_docs():
            logger.debug("COPYING USER INTO PROFILES:\n{!s}".format(userdoc))
            self.db.profiles.insert(userdoc)

        self.initial_verifications = (getattr(self, 'initial_verifications', None)
                                      or INITIAL_VERIFICATIONS)

        for verification_data in self.initial_verifications:
            self.db.verifications.insert(verification_data)

        logger.debug("setUp finished\n\n" + ('-=-' * 30) + "\n\n")

    def tearDown(self):
        super(LoggedInRequestTests, self).tearDown()
        logger.debug("tearDown: Dropping profiles, verifications and reset_passwords from {!s}".format(self.db))
        for userdoc in self.db.profiles.find({}):
            assert DashboardUser(data=userdoc)
        self.db.profiles.drop()
        self.db.verifications.drop()
        self.db.reset_passwords.drop()
        self.testapp.reset()

    def dummy_get_user(self, userid=''):
        return self.user

    def set_mocked_get_user(self):
        patcher = patch('eduid_userdb.dashboard.UserDBWrapper.get_user_by_eppn',
                        self.dummy_get_user)
        patcher.start()

    def dummy_request(self, cookies={}):
        request = DummyRequest()
        request.context = DummyResource()
        request.context.request = request
        request.userdb_new = self.userdb_new
        request.db = self.db
        request.dashboard_userdb = self.dashboard_db
        request.registry.settings = self.settings

        def propagate_user_changes(user):
            """
            Make sure there is a request.context.propagate_user_changes in testing too.
            """
            logger.debug('FREDRIK: Testing dummy_request.context propagate_user_changes')
            return self.request.amrelay.request_sync(user)

        request.context.propagate_user_changes = propagate_user_changes

        return request

    def set_logged(self, email='*****@*****.**', extra_session_data={}):
        request = self.set_user_cookie(email)
        user_obj = self.userdb_new.get_user_by_mail(email, raise_on_missing=True)
        if not user_obj:
            logging.error("User {!s} not found in database {!r}. Users:".format(email, self.userdb))
            for this in self.userdb_new._get_all_userdocs():
                this_user = DashboardUser(this)
                logging.debug("  User: {!s}".format(this_user))
        # user only exists in eduid-userdb, so need to clear modified-ts to be able
        # to save it to eduid-dashboard.profiles
        user_obj.modified_ts = None
        dummy = DummyRequest()
        dummy.session = {
            'eduPersonAssurance': loa(3),
            'eduPersonIdentityProofing': loa(3),
            'eduPersonPrincipalName': 'hubba-bubba',
            'user_eppn': 'hubba-bubba',
        }
        store_session_user(dummy, user_obj)
        # XXX ought to set self.user = user_obj
        self.logged_in_user = self.userdb_new.get_user_by_id(user_obj.user_id)
        dummy.session.update(extra_session_data)
        request = self.add_to_session(dummy.session)
        return request

    def set_user_cookie(self, user_id):
        request = TestRequest.blank('', {})
        request.registry = self.testapp.app.registry
        remember_headers = remember(request, user_id)
        cookie_value = remember_headers[0][1].split('"')[1]
        self.testapp.set_cookie('auth_tkt', cookie_value)
        return request

    def add_to_session(self, data):
        # Log warning since we're moving away from direct request.session access
        logger.warning('Add to session called with data: {!r}'.format(data))
        queryUtility = self.testapp.app.registry.queryUtility
        session_factory = queryUtility(ISessionFactory)
        request = self.dummy_request()
        session = session_factory(request)
        for key, value in data.items():
            session[key] = value
        session.persist()
        self.testapp.set_cookie(self.settings['session.key'], session._session.token)
        return request

    def check_values(self, fields, values, ignore_not_found=[]):
        for field in fields:
            if field.attrs['type'] == 'checkbox':
                # A webtest.app.Checkbox only has a value if it is checked (!)
                field.checked = True
                if field.value in values:
                    logger.debug("Checked checkbox {!r} (value {!r})".format(field.id, field.value))
                    values.remove(field.value)
                else:
                    # restore the checkbox whose value was not found in values
                    field.checked = False
        values = [x for x in values if x not in ignore_not_found]
        self.assertEqual(values, [], "Failed checking one or more checkboxes: {!r}".format(values))

    def values_are_checked(self, fields, values):
        checked = [f.value for f in fields if f.value is not None]

        self.assertEqual(values, checked)

    def sync_user_from_dashboard_to_userdb(self, user_id, old_format = False):
        """
        When there is no eduid-dashboard-amp Attribute Manager plugin loaded to
        sync users from dashboard to userdb, this crude function can do it.

        :param user_id: User id
        :param old_format: Write in old format to userdb

        :type user_id: ObjectId
        :type old_format: bool
        :return:
        """
        user = self.dashboard_db.get_user_by_id(user_id)
        logger.debug('Syncing user {!s} from dashboard to userdb'.format(user))
        test_doc = {'_id': user_id}
        user_doc = user.to_dict(old_userdb_format=old_format)
        # Fixups to turn the DashboardUser into a User
        user_doc['terminated'] = True
        self.userdb_new._coll.update(test_doc, user_doc, upsert=False)
예제 #2
0
class LoggedInRequestTests(MongoTestCase):
    """Base TestCase for those tests that need a logged in environment setup"""
    def setUp(self,
              settings={},
              skip_on_fail=False,
              std_user='******'):

        self.settings = deepcopy(SETTINGS)
        self.settings.update(settings)
        super(LoggedInRequestTests, self).setUp(celery,
                                                get_attribute_manager,
                                                userdb_use_old_format=True)

        self.redis_instance = RedisTemporaryInstance.get_instance()
        self.settings['redis_host'] = 'localhost'
        self.settings['redis_port'] = self.redis_instance._port
        self.settings['redis_db'] = '0'

        self.settings['mongo_uri'] = self.mongodb_uri('')
        try:
            app = eduiddashboard_main({}, **self.settings)
        except pymongo.errors.ConnectionFailure:
            if skip_on_fail:
                raise unittest.SkipTest(
                    "requires accessible MongoDB server on {!r}".format(
                        self.settings['mongo_uri']))
            raise

        self.testapp = TestApp(app)

        self.config = testing.setUp()
        self.config.registry.settings = self.settings
        self.config.registry.registerUtility(self, IDebugLogger)

        #self.db = get_db(self.settings)
        self.db = app.registry.settings['mongodb'].get_database(
            'eduid_dashboard')  # central userdb, raw mongodb
        self.userdb_new = UserDB(
            self.mongodb_uri(''),
            'eduid_am')  # central userdb in new format (User)
        self.dashboard_db = DashboardUserDB(
            self.mongodb_uri('eduid_dashboard'))
        # Clean up the dashboards private database collections
        logger.debug(
            "Dropping profiles, verifications and reset_passwords from {!s}".
            format(self.db))
        self.db.profiles.drop()
        self.db.verifications.drop()
        self.db.reset_passwords.drop()

        self.user = self.userdb_new.get_user_by_mail(std_user)
        self.assertIsNotNone(
            self.user,
            "Could not load the standard test user {!r} from the database {!s}"
            .format(std_user, self.userdb_new))
        self.logged_in_user = None

        # Copy all the users from the eduid userdb into the dashboard applications userdb
        # since otherwise the out-of-sync check will trigger on every save to the dashboard
        # applications database because there is no document there with the right modified_ts
        for userdoc in self.userdb_new._get_all_docs():
            logger.debug("COPYING USER INTO PROFILES:\n{!s}".format(userdoc))
            self.db.profiles.insert(userdoc)

        self.initial_verifications = (getattr(self, 'initial_verifications',
                                              None) or INITIAL_VERIFICATIONS)

        for verification_data in self.initial_verifications:
            self.db.verifications.insert(verification_data)

        logger.debug("setUp finished\n\n" + ('-=-' * 30) + "\n\n")

    def tearDown(self):
        super(LoggedInRequestTests, self).tearDown()
        logger.debug(
            "tearDown: Dropping profiles, verifications and reset_passwords from {!s}"
            .format(self.db))
        for userdoc in self.db.profiles.find({}):
            assert DashboardUser(data=userdoc)
        self.db.profiles.drop()
        self.db.verifications.drop()
        self.db.reset_passwords.drop()
        self.testapp.reset()

    def dummy_get_user(self, userid=''):
        return self.user

    def set_mocked_get_user(self):
        patcher = patch(
            'eduid_userdb.dashboard.UserDBWrapper.get_user_by_eppn',
            self.dummy_get_user)
        patcher.start()

    def dummy_request(self, cookies={}):
        request = DummyRequest()
        request.context = DummyResource()
        request.context.request = request
        request.userdb_new = self.userdb_new
        request.db = self.db
        request.dashboard_userdb = self.dashboard_db
        request.registry.settings = self.settings

        def propagate_user_changes(user):
            """
            Make sure there is a request.context.propagate_user_changes in testing too.
            """
            logger.debug(
                'FREDRIK: Testing dummy_request.context propagate_user_changes'
            )
            return self.request.amrelay.request_sync(user)

        request.context.propagate_user_changes = propagate_user_changes

        return request

    def set_logged(self, email='*****@*****.**', extra_session_data={}):
        request = self.set_user_cookie(email)
        user_obj = self.userdb_new.get_user_by_mail(email,
                                                    raise_on_missing=True)
        if not user_obj:
            logging.error(
                "User {!s} not found in database {!r}. Users:".format(
                    email, self.userdb))
            for this in self.userdb_new._get_all_userdocs():
                this_user = DashboardUser(this)
                logging.debug("  User: {!s}".format(this_user))
        # user only exists in eduid-userdb, so need to clear modified-ts to be able
        # to save it to eduid-dashboard.profiles
        user_obj.modified_ts = None
        dummy = DummyRequest()
        dummy.session = {
            'eduPersonAssurance': loa(3),
            'eduPersonIdentityProofing': loa(3),
            'eduPersonPrincipalName': 'hubba-bubba',
            'user_eppn': 'hubba-bubba',
        }
        store_session_user(dummy, user_obj)
        # XXX ought to set self.user = user_obj
        self.logged_in_user = self.userdb_new.get_user_by_id(user_obj.user_id)
        dummy.session.update(extra_session_data)
        request = self.add_to_session(dummy.session)
        return request

    def set_user_cookie(self, user_id):
        request = TestRequest.blank('', {})
        request.registry = self.testapp.app.registry
        remember_headers = remember(request, user_id)
        cookie_value = remember_headers[0][1].split('"')[1]
        self.testapp.set_cookie('auth_tkt', cookie_value)
        return request

    def add_to_session(self, data):
        # Log warning since we're moving away from direct request.session access
        logger.warning('Add to session called with data: {!r}'.format(data))
        queryUtility = self.testapp.app.registry.queryUtility
        session_factory = queryUtility(ISessionFactory)
        request = self.dummy_request()
        session = session_factory(request)
        for key, value in data.items():
            session[key] = value
        session.persist()
        self.testapp.set_cookie(self.settings['session.key'],
                                session._session.token)
        return request

    def check_values(self, fields, values, ignore_not_found=[]):
        for field in fields:
            if field.attrs['type'] == 'checkbox':
                # A webtest.app.Checkbox only has a value if it is checked (!)
                field.checked = True
                if field.value in values:
                    logger.debug("Checked checkbox {!r} (value {!r})".format(
                        field.id, field.value))
                    values.remove(field.value)
                else:
                    # restore the checkbox whose value was not found in values
                    field.checked = False
        values = [x for x in values if x not in ignore_not_found]
        self.assertEqual(
            values, [],
            "Failed checking one or more checkboxes: {!r}".format(values))

    def values_are_checked(self, fields, values):
        checked = [f.value for f in fields if f.value is not None]

        self.assertEqual(values, checked)

    def sync_user_from_dashboard_to_userdb(self, user_id, old_format=False):
        """
        When there is no eduid-dashboard-amp Attribute Manager plugin loaded to
        sync users from dashboard to userdb, this crude function can do it.

        :param user_id: User id
        :param old_format: Write in old format to userdb

        :type user_id: ObjectId
        :type old_format: bool
        :return:
        """
        user = self.dashboard_db.get_user_by_id(user_id)
        logger.debug('Syncing user {!s} from dashboard to userdb'.format(user))
        test_doc = {'_id': user_id}
        user_doc = user.to_dict(old_userdb_format=old_format)
        # Fixups to turn the DashboardUser into a User
        user_doc['terminated'] = True
        self.userdb_new._coll.update(test_doc, user_doc, upsert=False)
예제 #3
0
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)