예제 #1
0
    def test_fulfil(self):
        # Arrange
        lcp_api = LCPAPI(self._db, self._lcp_collection)
        patron = self._patron()
        days = self._lcp_collection.default_loan_period(patron.library)
        today = utc_now()
        expires = today + datetime.timedelta(days=days)
        data_source = DataSource.lookup(self._db,
                                        DataSource.LCP,
                                        autocreate=True)
        data_source_name = data_source.name
        license_pool = self._licensepool(
            edition=None,
            data_source_name=data_source_name,
            collection=self._lcp_collection,
        )
        lcp_license = json.loads(fixtures.LCPSERVER_LICENSE)
        lcp_server_mock = create_autospec(spec=LCPServer)
        lcp_server_mock.get_license = MagicMock(return_value=lcp_license)

        with self._configuration_factory.create(
                self._configuration_storage, self._db,
                LCPServerConfiguration) as configuration:
            with patch(
                    "api.lcp.collection.LCPServer") as lcp_server_constructor:
                lcp_server_constructor.return_value = lcp_server_mock

                configuration.lcpserver_url = fixtures.LCPSERVER_URL
                configuration.lcpserver_user = fixtures.LCPSERVER_USER
                configuration.lcpserver_password = fixtures.LCPSERVER_PASSWORD
                configuration.lcpserver_input_directory = (
                    fixtures.LCPSERVER_INPUT_DIRECTORY)

                configuration.provider_name = fixtures.PROVIDER_NAME
                configuration.passphrase_hint = fixtures.TEXT_HINT
                configuration.encryption_algorithm = (
                    LCPServerConfiguration.DEFAULT_ENCRYPTION_ALGORITHM)

                # Act
                license_pool.loan_to(
                    patron,
                    start=today,
                    end=expires,
                    external_identifier=lcp_license["id"],
                )
                fulfilment_info = lcp_api.fulfill(patron, "pin", license_pool,
                                                  "internal format")

                # Assert
                assert isinstance(fulfilment_info, LCPFulfilmentInfo) == True
                assert fulfilment_info.collection_id == self._lcp_collection.id
                assert fulfilment_info.collection(
                    self._db) == self._lcp_collection
                assert fulfilment_info.license_pool(self._db) == license_pool
                assert fulfilment_info.data_source_name == data_source_name
                assert fulfilment_info.identifier_type == license_pool.identifier.type

                lcp_server_mock.get_license.assert_called_once_with(
                    self._db, lcp_license["id"], patron)
예제 #2
0
    def test_checkout_with_existing_loan(self):
        # Arrange
        lcp_api = LCPAPI(self._db, self._lcp_collection)
        patron = self._patron()
        days = self._lcp_collection.default_loan_period(patron.library)
        start_date = utc_now()
        end_date = start_date + datetime.timedelta(days=days)
        data_source = DataSource.lookup(self._db,
                                        DataSource.LCP,
                                        autocreate=True)
        data_source_name = data_source.name
        edition = self._edition(data_source_name=data_source_name,
                                identifier_id=fixtures.CONTENT_ID)
        license_pool = self._licensepool(
            edition=edition,
            data_source_name=data_source_name,
            collection=self._lcp_collection,
        )
        lcp_license = json.loads(fixtures.LCPSERVER_LICENSE)
        lcp_server_mock = create_autospec(spec=LCPServer)
        lcp_server_mock.get_license = MagicMock(return_value=lcp_license)
        loan_identifier = "e99be177-4902-426a-9b96-0872ae877e2f"

        license_pool.loan_to(patron, external_identifier=loan_identifier)

        with self._configuration_factory.create(
                self._configuration_storage, self._db,
                LCPServerConfiguration) as configuration:
            with patch(
                    "api.lcp.collection.LCPServer") as lcp_server_constructor:
                lcp_server_constructor.return_value = lcp_server_mock

                configuration.lcpserver_url = fixtures.LCPSERVER_URL
                configuration.lcpserver_user = fixtures.LCPSERVER_USER
                configuration.lcpserver_password = fixtures.LCPSERVER_PASSWORD
                configuration.lcpserver_input_directory = (
                    fixtures.LCPSERVER_INPUT_DIRECTORY)
                configuration.provider_name = fixtures.PROVIDER_NAME
                configuration.passphrase_hint = fixtures.TEXT_HINT
                configuration.encryption_algorithm = (
                    LCPServerConfiguration.DEFAULT_ENCRYPTION_ALGORITHM)

                # Act
                loan = lcp_api.checkout(patron, "pin", license_pool,
                                        "internal format")

                # Assert
                assert loan.collection_id == self._lcp_collection.id
                assert loan.collection(self._db) == self._lcp_collection
                assert loan.license_pool(self._db) == license_pool
                assert loan.data_source_name == data_source_name
                assert loan.identifier_type == license_pool.identifier.type
                assert loan.external_identifier == loan_identifier
                assert loan.start_date == start_date
                assert loan.end_date == end_date

                lcp_server_mock.get_license.assert_called_once_with(
                    self._db, loan_identifier, patron)
예제 #3
0
    def test_checkout_without_existing_loan(self):
        # Arrange
        lcp_api = LCPAPI(self._db, self._lcp_collection)
        patron = self._patron()
        days = self._lcp_collection.default_loan_period(patron.library)
        start_date = datetime.datetime.utcnow()
        end_date = start_date + datetime.timedelta(days=days)
        data_source = DataSource.lookup(self._db,
                                        DataSource.LCP,
                                        autocreate=True)
        data_source_name = data_source.name
        edition = self._edition(data_source_name=data_source_name,
                                identifier_id=fixtures.CONTENT_ID)
        license_pool = self._licensepool(edition=edition,
                                         data_source_name=data_source_name,
                                         collection=self._lcp_collection)
        lcp_license = json.loads(fixtures.LCPSERVER_LICENSE)
        lcp_server_mock = create_autospec(spec=LCPServer)
        lcp_server_mock.generate_license = MagicMock(return_value=lcp_license)

        with self._configuration_factory.create(
                self._configuration_storage, self._db,
                LCPServerConfiguration) as configuration:

            with patch(
                    'api.lcp.collection.LCPServer') as lcp_server_constructor:
                lcp_server_constructor.return_value = lcp_server_mock

                configuration.lcpserver_url = fixtures.LCPSERVER_URL
                configuration.lcpserver_user = fixtures.LCPSERVER_USER
                configuration.lcpserver_password = fixtures.LCPSERVER_PASSWORD
                configuration.lcpserver_input_directory = fixtures.LCPSERVER_INPUT_DIRECTORY
                configuration.provider_name = fixtures.PROVIDER_NAME
                configuration.passphrase_hint = fixtures.TEXT_HINT
                configuration.encryption_algorithm = LCPServerConfiguration.DEFAULT_ENCRYPTION_ALGORITHM

                # Act
                loan = lcp_api.checkout(patron, 'pin', license_pool,
                                        'internal format')

                # Assert
                eq_(loan.collection_id, self._lcp_collection.id)
                eq_(loan.collection(self._db), self._lcp_collection)
                eq_(loan.license_pool(self._db), license_pool)
                eq_(loan.data_source_name, data_source_name)
                eq_(loan.identifier_type, license_pool.identifier.type)
                eq_(loan.external_identifier, lcp_license['id'])
                eq_(loan.start_date, start_date)
                eq_(loan.end_date, end_date)

                lcp_server_mock.generate_license.assert_called_once_with(
                    self._db, fixtures.CONTENT_ID, patron, start_date,
                    end_date)
예제 #4
0
    def test_patron_activity_returns_correct_result(self):
        # Arrange
        lcp_api = LCPAPI(self._db, self._lcp_collection)

        # 1. Correct loan
        patron = self._patron()
        days = self._lcp_collection.default_loan_period(patron.library)
        today = utc_now()
        expires = today + datetime.timedelta(days=days)
        data_source = DataSource.lookup(self._db,
                                        DataSource.LCP,
                                        autocreate=True)
        data_source_name = data_source.name
        external_identifier = "1"
        license_pool = self._licensepool(
            edition=None,
            data_source_name=data_source_name,
            collection=self._lcp_collection,
        )
        license_pool.loan_to(patron,
                             start=today,
                             end=expires,
                             external_identifier=external_identifier)

        # 2. Loan from a different collection
        other_collection = self._collection(
            protocol=ExternalIntegration.MANUAL)
        other_external_identifier = "2"
        other_license_pool = self._licensepool(
            edition=None,
            data_source_name=data_source_name,
            collection=other_collection)
        other_license_pool.loan_to(
            patron,
            start=today,
            end=expires,
            external_identifier=other_external_identifier,
        )

        # 3. Other patron's loan
        other_patron = self._patron()
        other_license_pool = self._licensepool(
            edition=None,
            data_source_name=data_source_name,
            collection=other_collection)
        other_license_pool.loan_to(other_patron, start=today, end=expires)

        # 4. Expired loan
        other_license_pool = self._licensepool(
            edition=None,
            data_source_name=data_source_name,
            collection=self._lcp_collection,
        )
        other_license_pool.loan_to(patron,
                                   start=today,
                                   end=today - datetime.timedelta(days=1))

        # 5. Not started loan
        other_license_pool = self._licensepool(
            edition=None,
            data_source_name=data_source_name,
            collection=self._lcp_collection,
        )
        other_license_pool.loan_to(
            patron,
            start=today + datetime.timedelta(days=1),
            end=today + datetime.timedelta(days=2),
        )

        # Act
        loans = lcp_api.patron_activity(patron, "pin")

        # Assert
        assert len(loans) == 1

        loan = loans[0]
        assert loan.collection_id == self._lcp_collection.id
        assert loan.collection(self._db) == self._lcp_collection
        assert loan.license_pool(self._db) == license_pool
        assert loan.data_source_name == data_source_name
        assert loan.identifier_type == license_pool.identifier.type
        assert loan.external_identifier == external_identifier
        assert loan.start_date == today
        assert loan.end_date == expires
예제 #5
0
    def test_patron_activity_returns_correct_result(self):
        # Arrange
        lcp_api = LCPAPI(self._db, self._lcp_collection)

        # 1. Correct loan
        patron = self._patron()
        days = self._lcp_collection.default_loan_period(patron.library)
        today = datetime.datetime.utcnow()
        expires = today + datetime.timedelta(days=days)
        data_source = DataSource.lookup(self._db,
                                        DataSource.LCP,
                                        autocreate=True)
        data_source_name = data_source.name
        external_identifier = '1'
        license_pool = self._licensepool(edition=None,
                                         data_source_name=data_source_name,
                                         collection=self._lcp_collection)
        license_pool.loan_to(patron,
                             start=today,
                             end=expires,
                             external_identifier=external_identifier)

        # 2. Loan from a different collection
        other_collection = self._collection(
            protocol=ExternalIntegration.MANUAL)
        other_external_identifier = '2'
        other_license_pool = self._licensepool(
            edition=None,
            data_source_name=data_source_name,
            collection=other_collection)
        other_license_pool.loan_to(
            patron,
            start=today,
            end=expires,
            external_identifier=other_external_identifier)

        # 3. Other patron's loan
        other_patron = self._patron()
        other_license_pool = self._licensepool(
            edition=None,
            data_source_name=data_source_name,
            collection=other_collection)
        other_license_pool.loan_to(other_patron, start=today, end=expires)

        # 4. Expired loan
        other_license_pool = self._licensepool(
            edition=None,
            data_source_name=data_source_name,
            collection=self._lcp_collection)
        other_license_pool.loan_to(patron,
                                   start=today,
                                   end=today - datetime.timedelta(days=1))

        # 5. Not started loan
        other_license_pool = self._licensepool(
            edition=None,
            data_source_name=data_source_name,
            collection=self._lcp_collection)
        other_license_pool.loan_to(patron,
                                   start=today + datetime.timedelta(days=1),
                                   end=today + datetime.timedelta(days=2))

        # Act
        loans = lcp_api.patron_activity(patron, 'pin')

        # Assert
        eq_(len(loans), 1)

        loan = loans[0]
        eq_(loan.collection_id, self._lcp_collection.id)
        eq_(loan.collection(self._db), self._lcp_collection)
        eq_(loan.license_pool(self._db), license_pool)
        eq_(loan.data_source_name, data_source_name)
        eq_(loan.identifier_type, license_pool.identifier.type)
        eq_(loan.external_identifier, external_identifier)
        eq_(loan.start_date, today)
        eq_(loan.end_date, expires)