Exemplo n.º 1
0
    def test_remote_authenticate_no_password(self):

        integration = self._external_integration(self._str)
        p = SIP2AuthenticationProvider
        integration.setting(p.PASSWORD_KEYBOARD).value = p.NULL_KEYBOARD
        client = MockSIPClient()
        auth = SIP2AuthenticationProvider(
            self._default_library, integration, client=client
        )

        # This Evergreen instance doesn't use passwords.
        client.queue_response(self.evergreen_active_user)
        patrondata = auth.remote_authenticate("user", None)
        eq_("12345", patrondata.authorization_identifier)
        eq_("863715", patrondata.permanent_id)
        eq_("Booth Active Test", patrondata.personal_name)
        eq_(0, patrondata.fines)
        eq_(datetime(2019, 10, 4), patrondata.authorization_expires)
        eq_("Adult", patrondata.external_type)

        # If a password is specified, it is not sent over the wire.
        client.queue_response(self.evergreen_active_user)
        patrondata = auth.remote_authenticate("user2", "some password")
        eq_("12345", patrondata.authorization_identifier)
        request = client.requests[-1]
        assert 'user2' in request
        assert 'some password' not in request
Exemplo n.º 2
0
    def test_login_happens_implicitly_when_user_id_and_password_specified(
            self):
        sip = MockSIPClient('user_id', 'password')
        # We're not logged in, and we must log in before sending a real
        # message.
        eq_(False, sip.logged_in)
        eq_(True, sip.must_log_in)

        sip.queue_response('941')
        sip.queue_response(
            '64Y                201610050000114734                        AOnypl |AA12345|AENo Name|BLN|AFYour library card number cannot be located.  Please see a staff member for assistance.|AY1AZC9DE'
        )
        response = sip.patron_information('patron_identifier')

        # Two requests were made.
        eq_(2, len(sip.requests))
        eq_(2, sip.sequence_number)

        # We're logged in.
        eq_(True, sip.logged_in)

        # We ended up with the right data.
        eq_('12345', response['patron_identifier'])

        # If we reset the connection, we stop being logged in.
        sip.connect()
        eq_(False, sip.logged_in)
        eq_(0, sip.sequence_number)
Exemplo n.º 3
0
    def test_login_failure_interrupts_other_request(self):
        sip = MockSIPClient('user_id', 'password')
        sip.queue_response('940')

        # We don't even get a chance to make the patron information request
        # because our login attempt fails.
        assert_raises(IOError, sip.patron_information, 'patron_identifier')
Exemplo n.º 4
0
    def test_info_to_patrondata_no_validate_password(self):
        integration = self._external_integration(self._str)
        integration.url = 'server.local'
        client = MockSIPClient()
        provider = SIP2AuthenticationProvider(
            self._default_library, integration, client=client
        )

        # Test with valid login, should return PatronData
        info = client.patron_information_parser(TestSIP2AuthenticationProvider.sierra_valid_login)
        patron = provider.info_to_patrondata(info, validate_password=False)
        eq_(patron.__class__, PatronData)
        eq_("12345", patron.authorization_identifier)
        eq_("*****@*****.**", patron.email_address)
        eq_("SHELDON, ALICE", patron.personal_name)
        eq_(0, patron.fines)
        eq_(None, patron.authorization_expires)
        eq_(None, patron.external_type)
        eq_(PatronData.NO_VALUE, patron.block_reason)

        # Test with invalid login, should return PatronData
        info = client.patron_information_parser(TestSIP2AuthenticationProvider.sierra_invalid_login)
        patron = provider.info_to_patrondata(info, validate_password=False)
        eq_(patron.__class__, PatronData)
        eq_("12345", patron.authorization_identifier)
        eq_("*****@*****.**", patron.email_address)
        eq_("SHELDON, ALICE", patron.personal_name)
        eq_(0, patron.fines)
        eq_(None, patron.authorization_expires)
        eq_(None, patron.external_type)
        eq_('no borrowing privileges', patron.block_reason)
Exemplo n.º 5
0
    def test_login_failure_interrupts_other_request(self):
        sip = MockSIPClient(login_user_id="user_id", login_password="******")
        sip.queue_response("940")

        # We don't even get a chance to make the patron information request
        # because our login attempt fails.
        pytest.raises(IOError, sip.patron_information, "patron_identifier")
Exemplo n.º 6
0
 def test_different_separator(self):
     """When you create the SIPClient you get to specify which character
     to use as the field separator.
     """
     sip = MockSIPClient(separator="^")
     sip.queue_response(
         "64Y                201610050000114734                        AOnypl ^AA240^AENo Name^BLN^AFYour library card number cannot be located.^AY1AZC9DE"
     )
     response = sip.patron_information("identifier")
     assert "240" == response["patron_identifier"]
Exemplo n.º 7
0
    def test_sequence_number_increment(self):
        sip = MockSIPClient()
        sip.sequence_number = 0
        sip.queue_response('941')
        response = sip.login('user_id', 'password')
        eq_(1, sip.sequence_number)

        # Test wraparound from 9 to 0
        sip.sequence_number = 9
        sip.queue_response('941')
        response = sip.login('user_id', 'password')
        eq_(0, sip.sequence_number)
Exemplo n.º 8
0
    def test_sequence_number_increment(self):
        sip = MockSIPClient(login_user_id="user_id", login_password="******")
        sip.sequence_number = 0
        sip.queue_response("941")
        response = sip.login()
        assert 1 == sip.sequence_number

        # Test wraparound from 9 to 0
        sip.sequence_number = 9
        sip.queue_response("941")
        response = sip.login()
        assert 0 == sip.sequence_number
Exemplo n.º 9
0
    def test_maximum_resend(self):
        sip = MockSIPClient(login_user_id="user_id", login_password="******")

        # We will keep sending retry messages until we reach the maximum
        sip.queue_response("96")
        sip.queue_response("96")
        sip.queue_response("96")
        sip.queue_response("96")
        sip.queue_response("96")

        # After reaching the maximum the client should give an IOError
        pytest.raises(IOError, sip.login)

        # We should send as many requests as we are allowed retries
        assert sip.MAXIMUM_RETRIES == len(sip.requests)
Exemplo n.º 10
0
    def test_maximum_resend(self):
        sip = MockSIPClient(login_user_id='user_id', login_password='******')

        # We will keep sending retry messages until we reach the maximum
        sip.queue_response('96')
        sip.queue_response('96')
        sip.queue_response('96')
        sip.queue_response('96')
        sip.queue_response('96')

        # After reaching the maximum the client should give an IOError
        assert_raises(IOError, sip.login)

        # We should send as many requests as we are allowed retries
        eq_(sip.MAXIMUM_RETRIES, len(sip.requests))
Exemplo n.º 11
0
    def test_login_does_not_happen_implicitly_when_user_id_and_password_not_specified(
            self):
        sip = MockSIPClient()

        # We're implicitly logged in.
        eq_(False, sip.must_log_in)

        sip.queue_response(
            '64Y                201610050000114734                        AOnypl |AA12345|AENo Name|BLN|AFYour library card number cannot be located.  Please see a staff member for assistance.|AY1AZC9DE'
        )
        response = sip.patron_information('patron_identifier')

        # One request was made.
        eq_(1, len(sip.requests))
        eq_(1, sip.sequence_number)

        # We ended up with the right data.
        eq_('12345', response['patron_identifier'])
Exemplo n.º 12
0
    def test_login_does_not_happen_implicitly_when_user_id_and_password_not_specified(
        self, ):
        sip = MockSIPClient()

        # We're implicitly logged in.
        assert False == sip.must_log_in

        sip.queue_response(
            "64Y                201610050000114734                        AOnypl |AA12345|AENo Name|BLN|AFYour library card number cannot be located.  Please see a staff member for assistance.|AY1AZC9DE"
        )
        response = sip.patron_information("patron_identifier")

        # One request was made.
        assert 1 == len(sip.requests)
        assert 1 == sip.sequence_number

        # We ended up with the right data.
        assert "12345" == response["patron_identifier"]
Exemplo n.º 13
0
    def test_login_happens_when_user_id_and_password_specified(self):
        sip = MockSIPClient(login_user_id="user_id", login_password="******")
        # We're not logged in, and we must log in before sending a real
        # message.
        assert True == sip.must_log_in

        sip.queue_response("941")
        sip.queue_response(
            "64Y                201610050000114734                        AOnypl |AA12345|AENo Name|BLN|AFYour library card number cannot be located.  Please see a staff member for assistance.|AY1AZC9DE"
        )
        sip.login()
        response = sip.patron_information("patron_identifier")

        # Two requests were made.
        assert 2 == len(sip.requests)
        assert 2 == sip.sequence_number

        # We ended up with the right data.
        assert "12345" == response["patron_identifier"]
Exemplo n.º 14
0
    def test_resend(self):
        sip = MockSIPClient(login_user_id="user_id", login_password="******")
        # The first response will be a request to resend the original message.
        sip.queue_response("96")
        # The second response will indicate a successful login.
        sip.queue_response("941")

        response = sip.login()

        # We made two requests for a single login command.
        req1, req2 = sip.requests
        # The first request includes a sequence ID field, "AY", with
        # the value "0".
        assert b"9300CNuser_id|COpassword|AY0AZF556\r" == req1

        # The second request does not include a sequence ID field. As
        # a consequence its checksum is different.
        assert b"9300CNuser_id|COpassword|AZF620\r" == req2

        # The login request eventually succeeded.
        assert {"login_ok": "1", "_status": "94"} == response
Exemplo n.º 15
0
    def test_resend(self):
        sip = MockSIPClient()
        # The first response will be a request to resend the original message.
        sip.queue_response('96')
        # The second response will indicate a successful login.
        sip.queue_response('941')

        response = sip.login('user_id', 'password')

        # We made two requests for a single login command.
        req1, req2 = sip.requests
        # The first request includes a sequence ID field, "AY", with
        # the value "0".
        eq_('9300CNuser_id|COpassword|AY0AZF556\r', req1)

        # The second request does not include a sequence ID field. As
        # a consequence its checksum is different.
        eq_('9300CNuser_id|COpassword|AZF620\r', req2)

        # The login request eventually succeeded.
        eq_({'login_ok': '1', '_status': '94'}, response)
    def test_patron_block_setting(self):
        integration_block = self._external_integration(
            self._str,
            settings={SIP2AuthenticationProvider.PATRON_STATUS_BLOCK: "true"})
        integration_noblock = self._external_integration(
            self._str,
            settings={SIP2AuthenticationProvider.PATRON_STATUS_BLOCK: "false"})
        client = MockSIPClient()

        # Test with blocked patron, block should be set
        p = SIP2AuthenticationProvider(self._default_library,
                                       integration_block,
                                       client=client)
        info = client.patron_information_parser(
            TestSIP2AuthenticationProvider.evergreen_expired_card)
        patron = p.info_to_patrondata(info)
        eq_(patron.__class__, PatronData)
        eq_("12345", patron.authorization_identifier)
        eq_("863716", patron.permanent_id)
        eq_("Booth Expired Test", patron.personal_name)
        eq_(0, patron.fines)
        eq_(datetime(2008, 9, 7), patron.authorization_expires)
        eq_(PatronData.NO_BORROWING_PRIVILEGES, patron.block_reason)

        # Test with blocked patron, block should not be set
        p = SIP2AuthenticationProvider(self._default_library,
                                       integration_noblock,
                                       client=client)
        info = client.patron_information_parser(
            TestSIP2AuthenticationProvider.evergreen_expired_card)
        patron = p.info_to_patrondata(info)
        eq_(patron.__class__, PatronData)
        eq_("12345", patron.authorization_identifier)
        eq_("863716", patron.permanent_id)
        eq_("Booth Expired Test", patron.personal_name)
        eq_(0, patron.fines)
        eq_(datetime(2008, 9, 7), patron.authorization_expires)
        eq_(PatronData.NO_VALUE, patron.block_reason)
Exemplo n.º 17
0
    def test_variant_encoding(self):
        response_unicode = "64              000201610210000142637000000000000000000000000AOnypl |AA12345|AELE CARRÉ, JOHN|BZ0030|CA0050|CB0050|BLY|CQY|BV0|CC15.00|[email protected]|AY1AZD1B7\r"

        # By default, we expect data from a SIP2 server to be encoded
        # as CP850.
        assert "cp850" == self.sip.encoding
        self.sip.queue_response(response_unicode.encode("cp850"))
        response = self.sip.patron_information("identifier")
        assert "LE CARRÉ, JOHN" == response["personal_name"]

        # But a SIP2 server may send some other encoding, such as
        # UTF-8. This can cause odd results if the circulation manager
        # tries to parse the data as CP850.
        self.sip.queue_response(response_unicode.encode("utf-8"))
        response = self.sip.patron_information("identifier")
        assert "LE CARRÉ, JOHN" == response["personal_name"]

        # Giving SIPClient the right encoding means the data is
        # converted correctly.
        sip = MockSIPClient(encoding="utf-8")
        assert "utf-8" == sip.encoding
        sip.queue_response(response_unicode.encode("utf-8"))
        response = sip.patron_information("identifier")
        assert "LE CARRÉ, JOHN" == response["personal_name"]
Exemplo n.º 18
0
 def test_login_failure(self):
     sip = MockSIPClient('user_id', 'password')
     sip.queue_response('940')
     assert_raises(IOError, sip.login)
Exemplo n.º 19
0
 def test_login_success(self):
     sip = MockSIPClient()
     sip.queue_response('941')
     response = sip.login('user_id', 'password')
     eq_({'login_ok': '1', '_status': '94'}, response)
Exemplo n.º 20
0
 def test_login_message(self):
     sip = MockSIPClient()
     message = sip.login_message('user_id', 'password')
     eq_('9300CNuser_id|COpassword', message)
Exemplo n.º 21
0
 def test_login_failure(self):
     sip = MockSIPClient()
     sip.queue_response('940')
     response = sip.login('user_id', 'password')
     eq_('0', response['login_ok'])
Exemplo n.º 22
0
 def test_login_failure(self):
     sip = MockSIPClient(login_user_id="user_id", login_password="******")
     sip.queue_response("940")
     pytest.raises(IOError, sip.login)
Exemplo n.º 23
0
 def test_login_password_is_optional(self):
     """You can specify a login_id without specifying a login_password."""
     sip = MockSIPClient(login_user_id="user_id")
     sip.queue_response("941")
     response = sip.login()
     assert {"login_ok": "1", "_status": "94"} == response
Exemplo n.º 24
0
 def test_login_success(self):
     sip = MockSIPClient(login_user_id="user_id", login_password="******")
     sip.queue_response("941")
     response = sip.login()
     assert {"login_ok": "1", "_status": "94"} == response
Exemplo n.º 25
0
 def setup(self):
     self.sip = MockSIPClient()
Exemplo n.º 26
0
 def setup_method(self):
     self.sip = MockSIPClient()
Exemplo n.º 27
0
 def test_institution_id_field_value_provided(self):
     # Fake value retrieved from DB
     sip = MockSIPClient(institution_id="MAIN")
     with_institution_provided = sip.patron_information_request(
         "patron_identifier", "patron_password")
     assert with_institution_provided.startswith("AOMAIN|", 33)
Exemplo n.º 28
0
 def test_login_message(self):
     sip = MockSIPClient()
     message = sip.login_message("user_id", "password")
     assert "9300CNuser_id|COpassword" == message
    def test_remote_authenticate(self):
        integration = self._external_integration(self._str)
        client = MockSIPClient()
        auth = SIP2AuthenticationProvider(self._default_library,
                                          integration,
                                          client=client)

        # Some examples taken from a Sierra SIP API.
        client.queue_response(self.sierra_valid_login)
        patrondata = auth.remote_authenticate("user", "pass")
        eq_("12345", patrondata.authorization_identifier)
        eq_("*****@*****.**", patrondata.email_address)
        eq_("SHELDON, ALICE", patrondata.personal_name)
        eq_(0, patrondata.fines)
        eq_(None, patrondata.authorization_expires)
        eq_(None, patrondata.external_type)
        eq_(PatronData.NO_VALUE, patrondata.block_reason)

        client.queue_response(self.sierra_invalid_login)
        eq_(None, auth.remote_authenticate("user", "pass"))

        # Since Sierra provides both the patron's fine amount and the
        # maximum allowable amount, we can determine just by looking
        # at the SIP message that this patron is blocked for excessive
        # fines.
        client.queue_response(self.sierra_excessive_fines)
        patrondata = auth.remote_authenticate("user", "pass")
        eq_(PatronData.EXCESSIVE_FINES, patrondata.block_reason)

        # Some examples taken from an Evergreen instance that doesn't
        # use passwords.
        client.queue_response(self.evergreen_active_user)
        patrondata = auth.remote_authenticate("user", "pass")
        eq_("12345", patrondata.authorization_identifier)
        eq_("863715", patrondata.permanent_id)
        eq_("Booth Active Test", patrondata.personal_name)
        eq_(0, patrondata.fines)
        eq_(datetime(2019, 10, 4), patrondata.authorization_expires)
        eq_("Adult", patrondata.external_type)

        # A patron with an expired card.
        client.queue_response(self.evergreen_expired_card)
        patrondata = auth.remote_authenticate("user", "pass")
        eq_("12345", patrondata.authorization_identifier)
        # SIP extension field XI becomes sipserver_internal_id which
        # becomes PatronData.permanent_id.
        eq_("863716", patrondata.permanent_id)
        eq_("Booth Expired Test", patrondata.personal_name)
        eq_(0, patrondata.fines)
        eq_(datetime(2008, 9, 7), patrondata.authorization_expires)
        eq_(PatronData.NO_BORROWING_PRIVILEGES, patrondata.block_reason)

        # A patron with excessive fines
        client.queue_response(self.evergreen_excessive_fines)
        patrondata = auth.remote_authenticate("user", "pass")
        eq_("12345", patrondata.authorization_identifier)
        eq_("863718", patrondata.permanent_id)
        eq_("Booth Excessive Fines Test", patrondata.personal_name)
        eq_(100, patrondata.fines)
        eq_(datetime(2019, 10, 04), patrondata.authorization_expires)

        # We happen to know that this patron can't borrow books due to
        # excessive fines, but that information doesn't show up as a
        # block, because Evergreen doesn't also provide the
        # fine limit. This isn't a big deal -- we'll pick it up later
        # when we apply the site policy.
        #
        # This patron also has "Recall privileges denied" set, but
        # that's not a reason to block them.
        eq_(PatronData.NO_VALUE, patrondata.block_reason)

        # "Hold privileges denied" is not a block because you can
        # still borrow books.
        client.queue_response(self.evergreen_hold_privileges_denied)
        patrondata = auth.remote_authenticate("user", "pass")
        eq_(PatronData.NO_VALUE, patrondata.block_reason)

        client.queue_response(self.evergreen_card_reported_lost)
        patrondata = auth.remote_authenticate("user", "pass")
        eq_(PatronData.CARD_REPORTED_LOST, patrondata.block_reason)

        # Some examples taken from a Polaris instance.
        client.queue_response(self.polaris_valid_pin)
        patrondata = auth.remote_authenticate("user", "pass")
        eq_("25891000331441", patrondata.authorization_identifier)
        eq_("*****@*****.**", patrondata.email_address)
        eq_(9.25, patrondata.fines)
        eq_("Falk, Jen", patrondata.personal_name)
        eq_(datetime(2018, 6, 9, 23, 59, 59), patrondata.authorization_expires)

        client.queue_response(self.polaris_wrong_pin)
        patrondata = auth.remote_authenticate("user", "pass")
        eq_(None, patrondata)

        client.queue_response(self.polaris_no_such_patron)
        patrondata = auth.remote_authenticate("user", "pass")
        eq_(None, patrondata)

        client.queue_response(self.polaris_expired_card)
        patrondata = auth.remote_authenticate("user", "pass")
        eq_(datetime(2016, 10, 25, 23, 59, 59),
            patrondata.authorization_expires)

        client.queue_response(self.polaris_excess_fines)
        patrondata = auth.remote_authenticate("user", "pass")
        eq_(11.50, patrondata.fines)
Exemplo n.º 30
0
 def test_append_checksum(self):
     sip = MockSIPClient()
     sip.sequence_number = 7
     data = "some data"
     new_data = sip.append_checksum(data)
     assert "some data|AY7AZFAAA" == new_data