Exemplo n.º 1
0
    def __init__(self, _db, collection):
        self.odilo_bibliographic_coverage_provider = (
            OdiloBibliographicCoverageProvider(collection, api_class=self))
        if collection.protocol != ExternalIntegration.ODILO:
            raise ValueError(
                "Collection protocol is %s, but passed into OdiloAPI!" %
                collection.protocol)

        self._db = _db
        self.analytics = Analytics(self._db)

        self.collection_id = collection.id
        self.token = None
        self.client_key = collection.external_integration.username
        self.client_secret = collection.external_integration.password
        self.library_api_base_url = collection.external_integration.setting(
            self.LIBRARY_API_BASE_URL).value

        if not self.client_key or not self.client_secret or not self.library_api_base_url:
            raise CannotLoadConfiguration("Odilo configuration is incomplete.")

        # Use utf8 instead of unicode encoding
        settings = [
            self.client_key, self.client_secret, self.library_api_base_url
        ]
        self.client_key, self.client_secret, self.library_api_base_url = (
            setting.encode('utf8') for setting in settings)

        # Get set up with up-to-date credentials from the API.
        self.check_creds()
        if not self.token:
            raise CannotLoadConfiguration(
                "Invalid credentials for %s, cannot intialize API %s" %
                (self.client_key, self.library_api_base_url))
Exemplo n.º 2
0
    def from_config(cls, _db):
        integration = ExternalIntegration.lookup(
            _db, ExternalIntegration.NYPL_SHADOWCAT,
            ExternalIntegration.METADATA_GOAL)
        if not integration.url:
            raise CannotLoadConfiguration('No url found for NYPL Shadowcat')

        return cls(integration.url)
Exemplo n.º 3
0
    def from_config(cls, library):
        profile, password = cls.values(library)
        if not (profile and password):
            raise CannotLoadConfiguration(
                "No NoveList integration configured for library (%s)." %
                library.short_name)

        _db = Session.object_session(library)
        return cls(_db, profile, password)
Exemplo n.º 4
0
    def from_config(cls, _db, mirror, **kwargs):
        integration = ExternalIntegration.lookup(
            _db, ExternalIntegration.CONTENT_CAFE,
            ExternalIntegration.METADATA_GOAL)

        if not integration or not (integration.username
                                   and integration.password):
            raise CannotLoadConfiguration(
                'Content Cafe not properly configured')

        return cls(_db, mirror, integration.username, integration.password,
                   **kwargs)
Exemplo n.º 5
0
    def from_config(cls, _db, **kwargs):
        """Create a ContentCafeAPI object based on database
        configuration.
        """
        integration = ExternalIntegration.lookup(
            _db, ExternalIntegration.CONTENT_CAFE,
            ExternalIntegration.METADATA_GOAL)
        if not integration or not (integration.username
                                   and integration.password):
            raise CannotLoadConfiguration(
                'Content Cafe not properly configured')

        return cls(_db, integration.username, integration.password, **kwargs)
Exemplo n.º 6
0
    def __init__(self, collection, viaf=None, **kwargs):
        _db = Session.object_session(collection)
        api_class = kwargs.pop('api_class', OverdriveAPI)
        if callable(api_class):
            api = self.generic_overdrive_api(_db, api_class)
        else:
            # The API 'class' is actually an object, probably a mock.
            api = api_class
        if not api:
            raise CannotLoadConfiguration(
                """OverdriveBibliographicCoverageProvider requires at least one fully configured Overdrive collection."""
            )

        self.viaf = viaf or VIAFClient(_db)

        kwargs['registered_only'] = True
        super(OverdriveBibliographicCoverageProvider,
              self).__init__(collection, api_class=api, **kwargs)
Exemplo n.º 7
0
 def __init__(self, _db, collection):
     raise CannotLoadConfiguration("doomed!")
    def test_run_self_tests(self):
        integration = self._external_integration(self._str)
        integration.url = "server.com"

        class MockBadConnection(MockSIPClient):
            def connect(self):
                # probably a timeout if the server or port values are not valid
                raise IOError("Could not connect")

        class MockSIPLogin(MockSIPClient):
            def now(self):
                return datetime(2019, 1, 1).strftime("%Y%m%d0000%H%M%S")

            def login(self):
                if not self.login_user_id and not self.login_password:
                    raise IOError("Error logging in")

            def patron_information(self, username, password):
                return self.patron_information_parser(
                    TestSIP2AuthenticationProvider.sierra_valid_login)

        badConnectionClient = MockBadConnection()
        auth = SIP2AuthenticationProvider(self._default_library,
                                          integration,
                                          client=badConnectionClient)
        results = [r for r in auth._run_self_tests(self._db)]

        # If the connection doesn't work then don't bother running the other tests
        eq_(len(results), 1)
        eq_(results[0].name, "Test Connection")
        eq_(results[0].success, False)
        assert (results[0].exception, IOError("Could not connect"))

        badLoginClient = MockSIPLogin()
        auth = SIP2AuthenticationProvider(self._default_library,
                                          integration,
                                          client=badLoginClient)
        results = [x for x in auth._run_self_tests(self._db)]

        eq_(len(results), 2)
        eq_(results[0].name, "Test Connection")
        eq_(results[0].success, True)

        eq_(results[1].name,
            "Test Login with username 'None' and password 'None'")
        eq_(results[1].success, False)
        assert (results[1].exception, IOError("Error logging in"))

        # Set the log in username and password
        integration.username = "******"
        integration.password = "******"
        goodLoginClient = MockSIPLogin(login_user_id="user1",
                                       login_password="******")
        auth = SIP2AuthenticationProvider(self._default_library,
                                          integration,
                                          client=goodLoginClient)
        results = [x for x in auth._run_self_tests(self._db)]

        eq_(len(results), 3)
        eq_(results[0].name, "Test Connection")
        eq_(results[0].success, True)

        eq_(results[1].name,
            "Test Login with username 'user1' and password 'pass1'")
        eq_(results[1].success, True)

        eq_(results[2].name, "Authenticating test patron")
        eq_(results[2].success, False)
        assert (results[2].exception,
                CannotLoadConfiguration(
                    "No test patron identifier is configured."))

        # Now add the test patron credentials into the mocked client and SIP2 authenticator provider
        patronDataClient = MockSIPLogin(login_user_id="user1",
                                        login_password="******")
        valid_login_patron = patronDataClient.patron_information_parser(
            TestSIP2AuthenticationProvider.sierra_valid_login)

        class MockSIP2PatronInformation(SIP2AuthenticationProvider):
            def patron_information(self, username, password):
                return valid_login_patron

        auth = MockSIP2PatronInformation(self._default_library,
                                         integration,
                                         client=patronDataClient)
        # The actual test patron credentials
        auth.test_username = "******"
        auth.test_password = "******"
        results = [x for x in auth._run_self_tests(self._db)]

        eq_(len(results), 6)
        eq_(results[0].name, "Test Connection")
        eq_(results[0].success, True)

        eq_(results[1].name,
            "Test Login with username 'user1' and password 'pass1'")
        eq_(results[1].success, True)

        eq_(results[2].name, "Authenticating test patron")
        eq_(results[2].success, True)

        # Since test patron authentication is true, we can now see self
        # test results for syncing metadata and the raw data from `patron_information`
        eq_(results[3].name, "Syncing patron metadata")
        eq_(results[3].success, True)

        eq_(results[4].name, "Patron information request")
        eq_(results[4].success, True)
        eq_(
            results[4].result,
            patronDataClient.patron_information_request(
                "usertest1", "userpassword1"))

        eq_(results[5].name, "Raw test patron information")
        eq_(results[5].success, True)
        eq_(results[5].result, json.dumps(valid_login_patron, indent=1))