Пример #1
0
    def do_run(self, cmd_args=None, in_unit_test=False):
        parser = self.arg_parser(self._db)
        parsed = self.parse_command_line(self._db, cmd_args)

        url = parsed.registry_url
        registry = RemoteRegistry.for_protocol_goal_and_url(
            self._db, self.PROTOCOL, self.GOAL, url)
        stage = parsed.stage

        # Set up an application context so we have access to url_for.
        from api.app import app
        app.manager = CirculationManager(self._db, testing=in_unit_test)
        base_url = ConfigurationSetting.sitewide(
            self._db, Configuration.BASE_URL_KEY).value
        ctx = app.test_request_context(base_url=base_url)
        ctx.push()
        for library in parsed.libraries:
            registration = Registration(registry, library)
            library_stage = stage or registration.stage_field.value
            self.process_library(registration, library_stage,
                                 app.manager.url_for)
        ctx.pop()

        # For testing purposes, return the application object that was
        # created.
        return app
Пример #2
0
 def __init__(self, _db=None, cmd_args=None, testing=False, *args, **kwargs):
     super(CacheRepresentationPerLane, self).__init__(_db, *args, **kwargs)
     self.parse_args(cmd_args)
     from api.app import app
     app.manager = CirculationManager(self._db, testing=testing)
     self.app = app
     self.base_url = ConfigurationSetting.sitewide(self._db, Configuration.BASE_URL_KEY).value
Пример #3
0
    def setup(self, _db=None):
        super(RouteTest, self).setup(_db=_db, set_up_circulation_manager=False)
        if not RouteTest.REAL_CIRCULATION_MANAGER:
            library = self._default_library
            # Set up the necessary configuration so that when we
            # instantiate the CirculationManager it gets an
            # adobe_vendor_id controller -- this wouldn't normally
            # happen because most circulation managers don't need such a
            # controller.
            self.initialize_adobe(library, [library])
            self.adobe_vendor_id.password = self.TEST_NODE_VALUE
            manager = CirculationManager(self._db, testing=True)
            RouteTest.REAL_CIRCULATION_MANAGER = manager
        app = MockApp()
        self.routes = routes
        self.manager = app.manager
        self.original_app = self.routes.app
        self.resolver = self.original_app.url_map.bind('', '/')

        # For convenience, set self.controller to a specific controller
        # whose routes are being tested.
        controller_name = getattr(self, 'CONTROLLER_NAME', None)
        if controller_name:
            self.controller = getattr(self.manager, controller_name)

            # Make sure there's a controller by this name in the real
            # CirculationManager.
            self.real_controller = getattr(self.REAL_CIRCULATION_MANAGER,
                                           controller_name)
        else:
            self.real_controller = None

        self.routes.app = app
Пример #4
0
 def __init__(self, _db=None, testing=False):
     super(LaneSweeperScript, self).__init__(_db)
     os.environ['AUTOINITIALIZE'] = "False"
     from api.app import app
     del os.environ['AUTOINITIALIZE']
     app.manager = CirculationManager(self._db, testing=testing)
     self.app = app
     self.base_url = Configuration.integration_url(
         Configuration.CIRCULATION_MANAGER_INTEGRATION, required=True)
Пример #5
0
 def __init__(self, _db=None, testing=False):
     _db = _db or self._db
     super(LaneSweeperScript, self).__init__(_db)
     os.environ['AUTOINITIALIZE'] = "False"
     from api.app import app
     del os.environ['AUTOINITIALIZE']
     app.manager = CirculationManager(_db, testing=testing)
     self.app = app
     self.base_url = ConfigurationSetting.sitewide(
         _db, Configuration.BASE_URL_KEY).value
Пример #6
0
    def setup_method(self):
        self.setup_circulation_manager = False
        super(AdminRouteTest, self).setup_method()
        if not self.REAL_CIRCULATION_MANAGER:
            library = self._default_library
            # Set up the necessary configuration so that when we
            # instantiate the CirculationManager it gets an
            # adobe_vendor_id controller -- this wouldn't normally
            # happen because most circulation managers don't need such a
            # controller.
            self.initialize_adobe(library, [library])
            self.adobe_vendor_id.password = self.TEST_NODE_VALUE
            circ_manager = CirculationManager(self._db, testing=True)
            manager = AdminController(circ_manager)
            setup_admin_controllers(circ_manager)
            self.REAL_CIRCULATION_MANAGER = circ_manager

        app = MockAdminApp()
        # Also mock the api app in order to use functions from api/routes
        api_app = MockApp()
        self.routes = routes
        self.api_routes = api_routes
        self.manager = app.manager
        self.original_app = self.routes.app
        self.original_api_app = self.api_routes.app
        self.resolver = self.original_app.url_map.bind("", "/")

        # For convenience, set self.controller to a specific controller
        # whose routes are being tested.
        controller_name = getattr(self, "CONTROLLER_NAME", None)
        if controller_name:
            self.controller = getattr(self.manager, controller_name)

            # Make sure there's a controller by this name in the real
            # CirculationManager.
            self.real_controller = getattr(
                self.REAL_CIRCULATION_MANAGER, controller_name
            )
        else:
            self.real_controller = None

        self.routes.app = app
        # Need to also mock the route app from /api/routes.
        self.api_routes.app = api_app
Пример #7
0
    def test_get_lcp_license_returns_the_same_license_for_authenticated_patron(
            self):
        # Arrange
        license_id = 'e99be177-4902-426a-9b96-0872ae877e2f'
        expected_license = json.loads(fixtures.LCPSERVER_LICENSE)
        lcp_server = create_autospec(spec=LCPServer)
        lcp_server.get_license = MagicMock(return_value=expected_license)
        library = self.make_default_library(self._db)
        lcp_collection = self._collection(LCPAPI.NAME, ExternalIntegration.LCP)
        library.collections.append(lcp_collection)

        with patch('api.lcp.controller.LCPServerFactory'
                   ) as lcp_server_factory_constructor_mock:
            lcp_server_factory = create_autospec(spec=LCPServerFactory)
            lcp_server_factory.create = MagicMock(return_value=lcp_server)
            lcp_server_factory_constructor_mock.return_value = lcp_server_factory

            patron = self.default_patron
            manager = CirculationManager(self._db, testing=True)
            controller = LCPController(manager)
            controller.authenticated_patron_from_request = MagicMock(
                return_value=patron)

            url = 'http://circulationmanager.org/{0}/lcp/licenses{0}'.format(
                self._default_library.short_name, license_id)

            with self.app.test_request_context(url):
                request.library = self._default_library

                # Act
                result1 = controller.get_lcp_license(license_id)
                result2 = controller.get_lcp_license(license_id)

                # Assert
                for result in [result1, result2]:
                    eq_(result.status_code, 200)
                    eq_(result.json, expected_license)
Пример #8
0
    def test_get_lcp_passphrase_returns_the_same_passphrase_for_authenticated_patron(
            self):
        # Arrange
        expected_passphrase = '1cde00b4-bea9-48fc-819b-bd17c578a22c'

        with patch('api.lcp.controller.LCPCredentialFactory'
                   ) as credential_factory_constructor_mock:
            credential_factory = create_autospec(spec=LCPCredentialFactory)
            credential_factory.get_patron_passphrase = MagicMock(
                return_value=expected_passphrase)
            credential_factory_constructor_mock.return_value = credential_factory

            patron = self.default_patron
            manager = CirculationManager(self._db, testing=True)
            controller = LCPController(manager)
            controller.authenticated_patron_from_request = MagicMock(
                return_value=patron)

            url = 'http://circulationmanager.org/{0}/lcp/hint'.format(
                self._default_library.short_name)

            with self.app.test_request_context(url):
                request.library = self._default_library

                # Act
                result1 = controller.get_lcp_passphrase()
                result2 = controller.get_lcp_passphrase()

                # Assert
                for result in [result1, result2]:
                    eq_(result.status_code, 200)
                    eq_('passphrase' in result.json, True)
                    eq_(result.json['passphrase'], expected_passphrase)

                credential_factory.get_patron_passphrase.assert_has_calls(
                    [call(self._db, patron),
                     call(self._db, patron)])
Пример #9
0
    def test_get_lcp_license_returns_problem_detail_when_collection_is_missing(
            self):
        # Arrange
        missing_collection_name = "missing-collection"
        license_id = "e99be177-4902-426a-9b96-0872ae877e2f"
        expected_license = json.loads(fixtures.LCPSERVER_LICENSE)
        lcp_server = create_autospec(spec=LCPServer)
        lcp_server.get_license = MagicMock(return_value=expected_license)
        library = self.make_default_library(self._db)
        lcp_collection = self._collection(LCPAPI.NAME, ExternalIntegration.LCP)
        library.collections.append(lcp_collection)

        with patch("api.lcp.controller.LCPServerFactory"
                   ) as lcp_server_factory_constructor_mock:
            lcp_server_factory = create_autospec(spec=LCPServerFactory)
            lcp_server_factory.create = MagicMock(return_value=lcp_server)
            lcp_server_factory_constructor_mock.return_value = lcp_server_factory

            patron = self.default_patron
            manager = CirculationManager(self._db, testing=True)
            controller = LCPController(manager)
            controller.authenticated_patron_from_request = MagicMock(
                return_value=patron)

            url = "http://circulationmanager.org/{0}/licenses{1}".format(
                missing_collection_name, license_id)

            with self.app.test_request_context(url):
                request.library = self._default_library

                # Act
                result = controller.get_lcp_license(missing_collection_name,
                                                    license_id)

                # Assert
                assert result.status_code == 404
Пример #10
0
    def test_get_lcp_passphrase_returns_the_same_passphrase_for_authenticated_patron(
        self, ):
        # Arrange
        expected_passphrase = "1cde00b4-bea9-48fc-819b-bd17c578a22c"

        with patch("api.lcp.controller.LCPCredentialFactory"
                   ) as credential_factory_constructor_mock:
            credential_factory = create_autospec(spec=LCPCredentialFactory)
            credential_factory.get_patron_passphrase = MagicMock(
                return_value=expected_passphrase)
            credential_factory_constructor_mock.return_value = credential_factory

            patron = self.default_patron
            manager = CirculationManager(self._db, testing=True)
            controller = LCPController(manager)
            controller.authenticated_patron_from_request = MagicMock(
                return_value=patron)

            url = "http://circulationmanager.org/lcp/hint"

            with self.app.test_request_context(url):
                request.library = self._default_library

                # Act
                result1 = controller.get_lcp_passphrase()
                result2 = controller.get_lcp_passphrase()

                # Assert
                for result in [result1, result2]:
                    assert result.status_code == 200
                    assert ("passphrase" in result.json) == True
                    assert result.json["passphrase"] == expected_passphrase

                credential_factory.get_patron_passphrase.assert_has_calls(
                    [call(self._db, patron),
                     call(self._db, patron)])