def setUp(self):
        super(TestCallbackView, self).setUp()
        self.user = AuthUserFactory()
        self.external_account = ExternalAccountFactory()
        self.institution = InstitutionFactory()

        self.provider = MockOAuth2Provider(self.external_account)

        self.user.affiliated_institutions.add(self.institution)

        app = flask.Flask(__name__)
        make_url_map(app)
        app.config['SECRET_KEY'] = 'aaaaa'
        self.ctx = app.test_request_context()
        self.ctx.push()

        self.request = RequestFactory().get('/fake_path')
        add_session_to_request(self.request)
        self.view0 = views.ConnectView()
        self.view0 = setup_user_view(self.view0, self.request, user=self.user)
        self.view0.kwargs = {
            'addon_name': self.external_account.provider,
            'institution_id': self.institution.id,
        }

        self.view = views.CallbackView()
        self.view = setup_user_view(self.view, self.request, user=self.user)
        self.view.kwargs = {
            'addon_name': self.external_account.provider,
            'institution_id': self.institution.id,
        }
Exemple #2
0
def init_app(settings_module='website.settings',
             set_backends=True,
             routes=True,
             attach_request_handlers=True):
    """Initializes the OSF. A sort of pseudo-app factory that allows you to
    bind settings, set up routing, and set storage backends, but only acts on
    a single app instance (rather than creating multiple instances).

    :param settings_module: A string, the settings module to use.
    :param set_backends: Whether to set the database storage backends.
    :param routes: Whether to set the url map.

    """
    logger.info(
        'Initializing the application from process {}, thread {}.'.format(
            os.getpid(), thread.get_ident()))

    # The settings module
    settings = importlib.import_module(settings_module)

    init_addons(settings, routes)
    with open(
            os.path.join(settings.STATIC_FOLDER, 'built',
                         'nodeCategories.json'), 'wb') as fp:
        json.dump(settings.NODE_CATEGORY_MAP, fp)

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.base.settings')
    django.setup()

    app.debug = settings.DEBUG_MODE

    # default config for flask app, however, this does not affect setting cookie using set_cookie()
    app.config['SESSION_COOKIE_SECURE'] = settings.SESSION_COOKIE_SECURE
    app.config['SESSION_COOKIE_HTTPONLY'] = settings.SESSION_COOKIE_HTTPONLY

    if set_backends:
        do_set_backends(settings)
    if routes:
        try:
            make_url_map(app)
        except AssertionError:  # Route map has already been created
            pass

    if attach_request_handlers:
        attach_handlers(app, settings)

    if app.debug:
        logger.info("Sentry disabled; Flask's debug mode enabled")
    else:
        sentry.init_app(app)
        logger.info("Sentry enabled; Flask's debug mode disabled")

    if set_backends:
        ensure_schemas()
        ensure_licenses()
    apply_middlewares(app, settings)

    return app
Exemple #3
0
def init_app(settings_module='website.settings',
             set_backends=True,
             routes=True,
             attach_request_handlers=True):
    """Initializes the OSF. A sort of pseudo-app factory that allows you to
    bind settings, set up routing, and set storage backends, but only acts on
    a single app instance (rather than creating multiple instances).

    :param settings_module: A string, the settings module to use.
    :param set_backends: Deprecated.
    :param routes: Whether to set the url map.

    """
    # Ensure app initialization only takes place once
    if app.config.get('IS_INITIALIZED', False) is True:
        return app

    setup_django()

    # The settings module
    settings = importlib.import_module(settings_module)

    init_addons(settings, routes)
    with open(
            os.path.join(settings.STATIC_FOLDER, 'built',
                         'nodeCategories.json'), 'w') as fp:
        json.dump(settings.NODE_CATEGORY_MAP, fp)

    app.debug = settings.DEBUG_MODE

    # default config for flask app, however, this does not affect setting cookie using set_cookie()
    app.config['SESSION_COOKIE_SECURE'] = settings.SESSION_COOKIE_SECURE
    app.config['SESSION_COOKIE_HTTPONLY'] = settings.SESSION_COOKIE_HTTPONLY
    app.config['SESSION_COOKIE_SAMESITE'] = settings.SESSION_COOKIE_SAMESITE

    if routes:
        try:
            from website.routes import make_url_map
            make_url_map(app)
        except AssertionError:  # Route map has already been created
            pass

    if attach_request_handlers:
        attach_handlers(app, settings)

    if app.debug:
        logger.info("Sentry disabled; Flask's debug mode enabled")
    else:
        sentry.init_app(app)
        logger.info("Sentry enabled; Flask's debug mode disabled")

    apply_middlewares(app, settings)

    app.config['IS_INITIALIZED'] = True
    return app
Exemple #4
0
def init_app(settings_module='website.settings', set_backends=True, routes=True,
             attach_request_handlers=True):
    """Initializes the OSF. A sort of pseudo-app factory that allows you to
    bind settings, set up routing, and set storage backends, but only acts on
    a single app instance (rather than creating multiple instances).

    :param settings_module: A string, the settings module to use.
    :param set_backends: Whether to set the database storage backends.
    :param routes: Whether to set the url map.

    """
    logger.info('Initializing the application from process {}, thread {}.'.format(
        os.getpid(), thread.get_ident()
    ))

    # The settings module
    settings = importlib.import_module(settings_module)

    init_addons(settings, routes)
    with open(os.path.join(settings.STATIC_FOLDER, 'built', 'nodeCategories.json'), 'wb') as fp:
        json.dump(settings.NODE_CATEGORY_MAP, fp)

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.base.settings')
    django.setup()

    app.debug = settings.DEBUG_MODE

    # default config for flask app, however, this does not affect setting cookie using set_cookie()
    app.config['SESSION_COOKIE_SECURE'] = settings.SESSION_COOKIE_SECURE
    app.config['SESSION_COOKIE_HTTPONLY'] = settings.SESSION_COOKIE_HTTPONLY

    if set_backends:
        do_set_backends(settings)
    if routes:
        try:
            make_url_map(app)
        except AssertionError:  # Route map has already been created
            pass

    if attach_request_handlers:
        attach_handlers(app, settings)

    if app.debug:
        logger.info("Sentry disabled; Flask's debug mode enabled")
    else:
        sentry.init_app(app)
        logger.info("Sentry enabled; Flask's debug mode disabled")

    if set_backends:
        ensure_schemas()
        ensure_licenses()
    apply_middlewares(app, settings)

    return app
Exemple #5
0
def init_app(settings_module='website.settings',
             set_backends=True,
             routes=True,
             mfr=False,
             attach_request_handlers=True):
    """Initializes the OSF. A sort of pseudo-app factory that allows you to
    bind settings, set up routing, and set storage backends, but only acts on
    a single app instance (rather than creating multiple instances).

    :param settings_module: A string, the settings module to use.
    :param set_backends: Whether to set the database storage backends.
    :param routes: Whether to set the url map.

    """
    # The settings module
    settings = importlib.import_module(settings_module)

    build_log_templates(settings)
    init_addons(settings, routes)
    build_js_config_files(settings)

    app.debug = settings.DEBUG_MODE

    if mfr:
        init_mfr(app)

    if set_backends:
        logger.debug('Setting storage backends')
        set_up_storage(
            website.models.MODELS,
            storage.MongoStorage,
            addons=settings.ADDONS_AVAILABLE,
        )
    if routes:
        try:
            make_url_map(app)
        except AssertionError:  # Route map has already been created
            pass

    if attach_request_handlers:
        attach_handlers(app, settings)

    if app.debug:
        logger.info("Sentry disabled; Flask's debug mode enabled")
    else:
        sentry.init_app(app)
        logger.info("Sentry enabled; Flask's debug mode disabled")

    if set_backends:
        ensure_schemas()
    apply_middlewares(app, settings)
    return app
Exemple #6
0
def init_app(settings_module='website.settings',
             set_backends=True,
             routes=True,
             attach_request_handlers=True):
    """Initializes the OSF. A sort of pseudo-app factory that allows you to
    bind settings, set up routing, and set storage backends, but only acts on
    a single app instance (rather than creating multiple instances).

    :param settings_module: A string, the settings module to use.
    :param set_backends: Whether to set the database storage backends.
    :param routes: Whether to set the url map.

    """
    # The settings module
    settings = importlib.import_module(settings_module)

    build_log_templates(settings)
    init_addons(settings, routes)
    with open(
            os.path.join(settings.STATIC_FOLDER, 'built',
                         'nodeCategories.json'), 'wb') as fp:
        json.dump(settings.NODE_CATEGORY_MAP, fp)

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.base.settings')
    django.setup()

    app.debug = settings.DEBUG_MODE

    if set_backends:
        do_set_backends(settings)
    if routes:
        try:
            make_url_map(app)
        except AssertionError:  # Route map has already been created
            pass

    if attach_request_handlers:
        attach_handlers(app, settings)

    if app.debug:
        logger.info("Sentry disabled; Flask's debug mode enabled")
    else:
        sentry.init_app(app)
        logger.info("Sentry enabled; Flask's debug mode disabled")

    if set_backends:
        ensure_schemas()
        ensure_licenses()
    apply_middlewares(app, settings)

    return app
Exemple #7
0
def init_app():
    from framework.flask import app
    try:
        make_url_map(app)
    except AssertionError:
        pass
    init_addons(website_settings)
    attach_handlers(app, website_settings)
    for addon in website_settings.ADDONS_AVAILABLE:
        try:
            addon.ready()
        except AssertionError:
            pass
    return app
Exemple #8
0
def init_app(settings_module='website.settings', set_backends=True, routes=True, mfr=False,
        attach_request_handlers=True):
    """Initializes the OSF. A sort of pseudo-app factory that allows you to
    bind settings, set up routing, and set storage backends, but only acts on
    a single app instance (rather than creating multiple instances).

    :param settings_module: A string, the settings module to use.
    :param set_backends: Whether to set the database storage backends.
    :param routes: Whether to set the url map.

    """
    # The settings module
    settings = importlib.import_module(settings_module)

    build_log_templates(settings)
    init_addons(settings, routes)
    build_js_config_files(settings)

    app.debug = settings.DEBUG_MODE

    if mfr:
        init_mfr(app)

    if set_backends:
        logger.debug('Setting storage backends')
        set_up_storage(
            website.models.MODELS,
            storage.MongoStorage,
            addons=settings.ADDONS_AVAILABLE,
        )
    if routes:
        try:
            make_url_map(app)
        except AssertionError:  # Route map has already been created
            pass

    if attach_request_handlers:
        attach_handlers(app, settings)

    if app.debug:
        logger.info("Sentry disabled; Flask's debug mode enabled")
    else:
        sentry.init_app(app)
        logger.info("Sentry enabled; Flask's debug mode disabled")

    if set_backends:
        ensure_schemas()
    apply_middlewares(app, settings)
    return app
Exemple #9
0
def init_app(settings_module='website.settings', set_backends=True, routes=True,
             attach_request_handlers=True):
    """Initializes the OSF. A sort of pseudo-app factory that allows you to
    bind settings, set up routing, and set storage backends, but only acts on
    a single app instance (rather than creating multiple instances).

    :param settings_module: A string, the settings module to use.
    :param set_backends: Whether to set the database storage backends.
    :param routes: Whether to set the url map.

    """
    # The settings module
    settings = importlib.import_module(settings_module)

    build_log_templates(settings)
    init_addons(settings, routes)
    with open(os.path.join(settings.STATIC_FOLDER, 'built', 'nodeCategories.json'), 'wb') as fp:
        json.dump(settings.NODE_CATEGORY_MAP, fp)

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.base.settings')
    django.setup()

    app.debug = settings.DEBUG_MODE

    if set_backends:
        do_set_backends(settings)
    if routes:
        try:
            make_url_map(app)
        except AssertionError:  # Route map has already been created
            pass

    if attach_request_handlers:
        attach_handlers(app, settings)

    if app.debug:
        logger.info("Sentry disabled; Flask's debug mode enabled")
    else:
        sentry.init_app(app)
        logger.info("Sentry enabled; Flask's debug mode disabled")

    if set_backends:
        ensure_schemas()
        ensure_licenses()
    apply_middlewares(app, settings)

    return app
Exemple #10
0
 def _send_claim_email(self, *args, **kwargs):
     """ This avoids needing to reimplement all of the logic in the sender methods.
     When v1 is more fully deprecated, those send hooks should be reworked to not
     rely upon a flask context and placed in utils (or elsewhere).
     :param bool registered: Indicates which sender to call (passed in as keyword)
     :param *args: Positional arguments passed to senders
     :param **kwargs: Keyword arguments passed to senders
     :return: None
     """
     from website.app import app
     from website.routes import make_url_map
     try:
         make_url_map(app)
     except AssertionError:
         # Already mapped
         pass
     ctx = app.test_request_context()
     ctx.push()
     if kwargs.pop('registered', False):
         send_claim_registered_email(*args, **kwargs)
     else:
         send_claim_email(*args, **kwargs)
     ctx.pop()
Exemple #11
0
    def _send_claim_email(self, *args, **kwargs):
        """ This avoids needing to reimplement all of the logic in the sender methods.
        When v1 is more fully deprecated, those send hooks should be reworked to not
        rely upon a flask context and placed in utils (or elsewhere).

        :param bool registered: Indicates which sender to call (passed in as keyword)
        :param *args: Positional arguments passed to senders
        :param **kwargs: Keyword arguments passed to senders
        :return: None
        """
        from website.app import app
        from website.routes import make_url_map
        try:
            make_url_map(app)
        except AssertionError:
            # Already mapped
            pass
        ctx = app.test_request_context()
        ctx.push()
        if kwargs.pop('registered', False):
            send_claim_registered_email(*args, **kwargs)
        else:
            send_claim_email(*args, **kwargs)
        ctx.pop()
Exemple #12
0
class RdmAddonRequestContextMixin(object):
    app = flask.Flask(__name__)
    make_url_map(app)
    app.config['SECRET_KEY'] = str(uuid.uuid4())
    ctx_dict = defaultdict(app.test_request_context)

    def get_request_context(self, session_id, institution_id, addon_name):
        return self.ctx_dict[(session_id, institution_id, addon_name)]

    def get_session(self, addon_name):
        if addon_name == 'dropbox':
            return addons.dropbox.models.session
        return osf.models.external.session

    def get_callback_url_func(self, addon_name):
        """Get a function that returns the OAuth Callback Admin'sURL """
        def web_url_for(view_name, _absolute=False, _internal=False, _guid=False, *args, **kwargs):
            path = 'oauth/callback/{}/'.format(addon_name)
            return urljoin(website_settings.ADMIN_URL, path)
        return web_url_for