예제 #1
0
 def test_index_setup(self):
     indexes = {
         # 'index-name': {'key': [('key', 1)], 'param1': True, 'param2': False}
         # http://docs.mongodb.org/manual/reference/method/db.collection.ensureIndex/
         'mail-index-v2': {
             'key': [('mail', 1)],
             'unique': True,
             'sparse': True
         },
         'eppn-index-v1': {
             'key': [('eduPersonPrincipalName', 1)],
             'unique': True
         },
         'norEduPersonNIN-index-v2': {
             'key': [('norEduPersonNIN', 1)],
             'unique': True,
             'sparse': True
         },
         'mobile-index-v1': {
             'key': [('mobile.mobile', 1), ('mobile.verified', 1)]
         },
         'mailAliases-index-v1': {
             'key': [('mailAliases.email', 1), ('mailAliases.verified', 1)]
         }
     }
     db = UserDB(self.settings.get('MONGO_URI'))
     print db._coll.index_information()
     db.setup_indexes(indexes)
     current_indexes = db._coll.index_information()
     print current_indexes
예제 #2
0
 def test_index_setup(self):
     indexes = {
         # 'index-name': {'key': [('key', 1)], 'param1': True, 'param2': False}
         # http://docs.mongodb.org/manual/reference/method/db.collection.ensureIndex/
         'mail-index-v2': {'key': [('mail', 1)], 'unique': True, 'sparse': True},
         'eppn-index-v1': {'key': [('eduPersonPrincipalName', 1)], 'unique': True},
         'norEduPersonNIN-index-v2': {'key': [('norEduPersonNIN', 1)], 'unique': True, 'sparse': True},
         'mobile-index-v1': {'key': [('mobile.mobile', 1), ('mobile.verified', 1)]},
         'mailAliases-index-v1': {'key': [('mailAliases.email', 1), ('mailAliases.verified', 1)]}
     }
     db = UserDB(self.settings.get('MONGO_URI'))
     print(db._coll.index_information())
     db.setup_indexes(indexes)
     current_indexes = db._coll.index_information()
     print(current_indexes)
예제 #3
0
 def __init__(self, config: Mapping[str, Any],
              internal_attributes: Dict[str, Any], *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.config = Config(**config)
     # Setup databases
     self.eduid_userdb = UserDB(db_uri=self.config.mongo_uri,
                                db_name='eduid_scimapi')
     logger.info(f'Connected to eduid db: {self.eduid_userdb}')
     # TODO: Implement real 'data owner' to database lookup
     data_owner = 'eduid.se'
     _owner = data_owner.replace('.', '_')  # replace dots with underscores
     coll = f'{_owner}__users'
     # TODO: rename old collection and remove this
     if data_owner == 'eduid.se':
         coll = 'profiles'
     self._userdbs = {
         'eduid.se':
         ScimApiUserDB(db_uri=self.config.mongo_uri, collection=coll)
     }
     self.converter = AttributeMapper(internal_attributes)
     # Get the internal attribute name for the eduPersonPrincipalName that will be
     # used to find users in the SCIM database
     _int = self.converter.to_internal(
         'saml', {'eduPersonPrincipalName': 'something'})
     self.ext_id_attr = list(_int.keys())[0]
     logger.debug(
         f'SCIM externalId internal attribute name: {self.ext_id_attr}')
예제 #4
0
def setup_indexes(db_name, collection):
    """
    Ensure that indexes in eduid_am.attributes collection are correctly setup.
    To update an index add a new item in indexes and remove the previous version.
    """
    indexes = {
        # 'index-name': {'key': [('key', 1)], 'param1': True, 'param2': False}
        # http://docs.mongodb.org/manual/reference/method/db.collection.ensureIndex/
        'mail-index-v2': {'key': [('mail', 1)], 'unique': True, 'sparse': True},
        'eppn-index-v1': {'key': [('eduPersonPrincipalName', 1)], 'unique': True},
        'norEduPersonNIN-index-v2': {'key': [('norEduPersonNIN', 1)], 'unique': True, 'sparse': True},
        'mobile-index-v1': {'key': [('mobile.mobile', 1), ('mobile.verified', 1)]},
        'mailAliases-index-v1': {'key': [('mailAliases.email', 1), ('mailAliases.verified', 1)]}
    }
    userdb = UserDB(celery.conf.get('MONGO_URI'), db_name=db_name, collection=collection)
    userdb.setup_indexes(indexes)
예제 #5
0
    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.userdb = app.registry.settings['userdb']
        _userdoc = self.userdb.get_user_by_mail(std_user)
        self.assertIsNotNone(_userdoc, "Could not load the standard test user {!r} from the database {!s}".format(
            std_user, self.userdb))
        self.user = OldUser(data=_userdoc)
        self.logged_in_user = None

        #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()

        # 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._get_all_docs():
            logger.debug("Copying user {!r}\nfrom {!r}\nto {!r}:\n{!s}".format(userdoc.get('eduPersonPrincipalName'),
                                                                               self.userdb, self.db.profiles,
                                                                               pprint.pformat(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")
예제 #6
0
파일: worker.py 프로젝트: SUNET/eduid-am
def setup_indexes(db_uri, db_name, collection):
    """
    Ensure that indexes in eduid_am.attributes collection are correctly setup.
    To update an index add a new item in indexes and remove the previous version.
    """
    indexes = {
        # 'index-name': {'key': [('key', 1)], 'param1': True, 'param2': False}
        # http://docs.mongodb.org/manual/reference/method/db.collection.ensureIndex/
        'mail-index-v2': {'key': [('mail', 1)], 'unique': True, 'sparse': True},
        'eppn-index-v1': {'key': [('eduPersonPrincipalName', 1)], 'unique': True},
        'norEduPersonNIN-index-v2': {'key': [('norEduPersonNIN', 1)], 'unique': True, 'sparse': True},
        'mobile-index-v1': {'key': [('mobile.mobile', 1), ('mobile.verified', 1)]},
        'mailAliases-index-v1': {'key': [('mailAliases.email', 1), ('mailAliases.verified', 1)]}
    }
    userdb = UserDB(db_uri, db_name=db_name, collection=collection)
    userdb.setup_indexes(indexes)
    userdb.close()
예제 #7
0
    def __init__(self):
        self.default_db_uri = self.app.conf.get('MONGO_URI')

        if self.default_db_uri is not None:
            # self.userdb is the UserDB to which AM will write the updated users. This setting
            # will be None when this class is instantiated on the 'client' side of the AMQP bus,
            # such as in the eduid-signup application.
            self.userdb = UserDB(self.default_db_uri, 'eduid_am', 'attributes')

        self.registry = PluginsRegistry(self.app.conf)
예제 #8
0
 def load_app(self, config):
     """
     Called from the parent class, so we can provide the appropriate flask
     app for this test case.
     """
     app = Flask('test.localhost')
     app.request_class = Request
     app.config.update(config)
     app.register_blueprint(test_views)
     app.central_userdb = UserDB(app.config['MONGO_URI'], 'eduid_am')
     app.session_interface = SessionFactory(app.config)
     return app
예제 #9
0
def init_idproofing_letter_app(name, config=None):
    """
    :param name: The name of the instance, it will affect the configuration loaded.
    :param config: any additional configuration settings. Specially useful
                   in test cases

    :type name: str
    :type config: dict

    :return: the flask app
    :rtype: flask.Flask
    """
    app = Flask(name, static_folder=None)

    # Load configuration
    app.config.from_object('idproofing_letter.settings.common')
    app.config.from_envvar('IDPROOFING_LETTER_SETTINGS', silent=True)
    if config:
        app.config.update(config)

    # Setup logging
    app = init_logging(app)

    # Setup exception handling
    app = init_exception_handlers(app)

    # Register views
    from idproofing_letter.views import idproofing_letter_views
    app.register_blueprint(idproofing_letter_views)

    # Init dbs
    app.central_userdb = UserDB(app.config['MONGO_URI'], 'eduid_am')
    app.proofing_statedb = LetterProofingStateDB(app.config['MONGO_URI'])

    # Init celery
    init_celery(app)

    # Initiate external modules
    app.ekopost = Ekopost(app)

    # Check for secret key
    if app.config['SECRET_KEY'] is None:
        app.logger.error('Missing SECRET_KEY in the settings file')

    app.logger.info('Application initialized')
    return app
예제 #10
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.userdb = app.registry.settings['userdb']
        _userdoc = self.userdb.get_user_by_mail(std_user)
        self.assertIsNotNone(_userdoc, "Could not load the standard test user {!r} from the database {!s}".format(
            std_user, self.userdb))
        self.user = OldUser(data=_userdoc)
        self.logged_in_user = None

        #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()

        # 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._get_all_docs():
            logger.debug("Copying user {!r}\nfrom {!r}\nto {!r}:\n{!s}".format(userdoc.get('eduPersonPrincipalName'),
                                                                               self.userdb, self.db.profiles,
                                                                               pprint.pformat(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 OldUser(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.userdb = self.userdb
        request.userdb_new = self.userdb_new
        request.db = self.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.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._get_all_userdocs():
                this_user = OldUser(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.set_modified_ts(None)
        dummy = DummyRequest()
        dummy.session = {
            'eduPersonAssurance': loa(3),
            'eduPersonIdentityProofing': loa(3),
        }
        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.get_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 (!)
                old_status = field.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 = True):
        """
        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
        del user_doc['terminated']
        self.userdb_new._coll.update(test_doc, user_doc, upsert=False)
예제 #11
0
    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")
예제 #12
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)
예제 #13
0
def eduid_init_app(name, config, app_class=AuthnApp):
    app = eduid_init_app_no_db(name, config, app_class=app_class)
    app.central_userdb = UserDB(
        app.config['MONGO_URI'],
        'eduid_am')  # XXX: Needs updating when we change db
    return app
예제 #14
0
 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)
예제 #15
0
def includeme(config):
    # DB setup
    settings = config.registry.settings
    mongodb = MongoDB(db_uri=settings['mongo_uri'])
    authninfodb = MongoDB(db_uri=settings['mongo_uri'])

    config.registry.settings['mongodb'] = mongodb
    config.registry.settings['authninfodb'] = authninfodb
    config.registry.settings['db_conn'] = mongodb.get_connection

    config.set_request_property(lambda x: x.registry.settings['mongodb'].
                                get_database('eduid_dashboard'),
                                'db',
                                reify=True)
    config.set_request_property(lambda x: x.registry.settings['authninfodb'].
                                get_database('eduid_idp_authninfo'),
                                'authninfodb',
                                reify=True)

    # Create userdb instance and store it in our config,
    # and make a getter lambda for pyramid to retreive it
    _userdb = UserDBWrapper(config.registry.settings['mongo_uri'])
    config.registry.settings['userdb'] = _userdb
    config.add_request_method(lambda x: x.registry.settings['userdb'],
                              'userdb',
                              reify=True)

    # same DB using new style users
    config.registry.settings['userdb_new'] = UserDB(
        config.registry.settings['mongo_uri'], db_name='eduid_am')
    config.add_request_method(lambda x: x.registry.settings['userdb_new'],
                              'userdb_new',
                              reify=True)

    # Set up handle to Dashboards private UserDb (DashboardUserDB)
    _dashboard_userdb = DashboardUserDB(config.registry.settings['mongo_uri'])
    config.registry.settings['dashboard_userdb'] = _dashboard_userdb
    config.add_request_method(
        lambda x: x.registry.settings['dashboard_userdb'],
        'dashboard_userdb',
        reify=True)

    config.registry.settings['msgrelay'] = MsgRelay(config.registry.settings)
    config.add_request_method(lambda x: x.registry.settings['msgrelay'],
                              'msgrelay',
                              reify=True)

    config.registry.settings['lookuprelay'] = LookupMobileRelay(
        config.registry.settings)
    config.add_request_method(lambda x: x.registry.settings['lookuprelay'],
                              'lookuprelay',
                              reify=True)

    config.registry.settings['idproofinglog'] = IDProofingLog(
        config.registry.settings)
    config.add_request_method(lambda x: x.registry.settings['idproofinglog'],
                              'idproofinglog',
                              reify=True)

    config.registry.settings['amrelay'] = AmRelay(config.registry.settings)
    config.add_request_method(lambda x: x.registry.settings['amrelay'],
                              'amrelay',
                              reify=True)

    config.set_request_property(is_logged, 'is_logged', reify=True)

    config.registry.settings['stats'] = get_stats_instance(settings, log)
    # Make the result of the lambda available as request.stats
    config.set_request_property(lambda x: x.registry.settings['stats'],
                                'stats',
                                reify=True)

    #
    # Route setups
    #
    config.add_route('home', '/', factory=HomeFactory)
    if settings['workmode'] == 'personal':
        config.include(profile_urls, route_prefix='/profile/')
        config.include(disabled_admin_urls, route_prefix='/admin/{userid}/')
    else:
        config.include(profile_urls, route_prefix='/users/{userid}/')
        config.include(admin_urls, route_prefix='/admin/{userid}/')

    config.add_route('token-login', '/tokenlogin/')
    config.add_route('logout', '/logout')
    settings['token_service_url_logout'] = urlappend(
        settings['token_service_url'], 'logout')
    if settings['workmode'] == 'personal':
        config.add_route('verifications',
                         '/verificate/{model}/{code}/',
                         factory=VerificationsFactory)
    else:
        config.add_route('verifications',
                         '/verificate/{model}/{code}/',
                         factory=ForbiddenFactory)
    config.add_route('help', '/help/', factory=HelpFactory)
    # Seems unused -- ft@ 2016-01-14
    #config.add_route('session-reload', '/session-reload/',
    #                 factory=PersonFactory)

    config.add_route('set_language', '/set_language/')
    config.add_route('error500test', '/error500test/')
    config.add_route('error500', '/error500/')

    config.add_route('error404', '/error404/')

    if not settings.get('testing', False):
        config.add_view(context=Exception,
                        view='eduiddashboard.views.portal.exception_view',
                        renderer='templates/error500.jinja2')
        config.add_view(context=HTTPNotFound,
                        view='eduiddashboard.views.portal.not_found_view',
                        renderer='templates/error404.jinja2')

    # Favicon
    config.add_route('favicon', '/favicon.ico')
    config.add_view('eduiddashboard.views.static.favicon_view',
                    route_name='favicon')
예제 #16
0
    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)
예제 #17
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)
예제 #18
0
 def __init__(self, db_uri, db_name='eduid_am', collection='attributes'):
     UserDB.__init__(self, db_uri, db_name, collection)
예제 #19
0
 def __init__(self, logger, config, userdb = None):
     self.logger = logger
     self.config = config
     if userdb is None:
         userdb = UserDB(config.mongo_uri, db_name=config.userdb_mongo_database, user_class=IdPUser)
     self.userdb = userdb