Esempio n. 1
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')
Esempio n. 2
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')
     eq_('240', response['patron_identifier'])
Esempio n. 3
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()
        eq_(1, sip.sequence_number)

        # Test wraparound from 9 to 0
        sip.sequence_number=9
        sip.queue_response('941')
        response = sip.login()
        eq_(0, sip.sequence_number)
Esempio n. 4
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'])
Esempio n. 5
0
    def test_login_happens_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_(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.
        eq_(2, len(sip.requests))
        eq_(2, sip.sequence_number)

        # We ended up with the right data.
        eq_('12345', response['patron_identifier'])
Esempio n. 6
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".
        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)
Esempio n. 7
0
 def test_login_password_is_optional(self):
     """You can specify a login_id without specifying a login_password."""
     sip = MockSIPClient('user_id')
     sip.queue_response('941')
     response = sip.login()
     eq_({'login_ok': '1', '_status': '94'}, response)
Esempio n. 8
0
 def test_login_success(self):
     sip = MockSIPClient('user_id', 'password')
     sip.queue_response('941')
     response = sip.login()
     eq_({'login_ok': '1', '_status': '94'}, response)
Esempio n. 9
0
    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)

        # 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_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)

        # Two cases where the patron's authorization identifier was
        # just not recognized. One on an ILS that sets
        # valid_patron_password='******' when that happens.
        client.queue_response(self.polaris_no_such_patron)
        patrondata = auth.remote_authenticate("user", "pass")
        eq_(None, patrondata)

        # And once on an ILS that leaves valid_patron_password blank
        # when that happens.
        client.queue_response(self.tlc_no_such_patron)
        patrondata = auth.remote_authenticate("user", "pass")
        eq_(None, patrondata)
Esempio n. 10
0
class TestPatronResponse(object):

    def setup(self):
        self.sip = MockSIPClient()

    def test_incorrect_card_number(self):
        self.sip.queue_response("64Y                201610050000114734                        AOnypl |AA240|AENo Name|BLN|AFYour library card number cannot be located.|AY1AZC9DE")
        response = self.sip.patron_information('identifier')

        # Test some of the basic fields.
        eq_(response['institution_id'], 'nypl ')
        eq_(response['personal_name'], 'No Name')
        eq_(response['screen_message'], ['Your library card number cannot be located.'])
        eq_(response['valid_patron'], 'N')
        eq_(response['patron_status'], 'Y             ')
        parsed = response['patron_status_parsed']
        eq_(True, parsed['charge privileges denied'])
        eq_(False, parsed['too many items charged'])

    def test_hold_items(self):
        "A patron has multiple items on hold."
        self.sip.queue_response("64              000201610050000114837000300020002000000000000AOnypl |AA233|AEBAR, FOO|BZ0030|CA0050|CB0050|BLY|CQY|BV0|CC15.00|AS123|AS456|AS789|[email protected]|AY1AZC848")
        response = self.sip.patron_information('identifier')
        eq_('0003', response['hold_items_count'])
        eq_(['123', '456', '789'], response['hold_items'])

    def test_multiple_screen_messages(self):
        self.sip.queue_response("64Y  YYYYYYYYYYY000201610050000115040000000000000000000000000AOnypl |AA233|AESHELDON, ALICE|BZ0030|CA0050|CB0050|BLY|CQN|BV0|CC15.00|AFInvalid PIN entered.  Please try again or see a staff member for assistance.|AFThere are unresolved issues with your account.  Please see a staff member for assistance.|AY2AZ9B64")
        response = self.sip.patron_information('identifier')
        eq_(2, len(response['screen_message']))

    def test_extension_field_captured(self):
        """This SIP2 message includes an extension field with the code XI.
        """
        self.sip.queue_response("64  Y           00020161005    122942000000000000000000000000AA240|AEBooth Active Test|BHUSD|BDAdult Circ Desk 1 Newtown, CT USA 06470|AQNEWTWN|BLY|CQN|PA20191004|PCAdult|PIAllowed|XI86371|AOBiblioTest|ZZfoo|AY2AZ0000")
        response = self.sip.patron_information('identifier')

        # The Evergreen XI field is a known extension and is picked up
        # as sipserver_internal_id.
        eq_("86371", response['sipserver_internal_id'])

        # The ZZ field is an unknown extension and is captured under
        # its SIP code.
        eq_(["foo"], response['ZZ'])

    def test_embedded_pipe(self):
        """In most cases we can handle data even if it contains embedded
        instances of the separator character.
        """
        self.sip.queue_response('64              000201610050000134405000000000000000000000000AOnypl |AA12345|AERICHARDSON, LEONARD|BZ0030|CA0050|CB0050|BLY|CQY|BV0|CC15.00|BEleona|rdr@|bar.com|AY1AZD1BB\r')
        response = self.sip.patron_information('identifier')
        eq_("leona|rdr@|bar.com", response['email_address'])

    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')
        eq_('240', response['patron_identifier'])

    def test_location_code_is_optional(self):
        """You can specify a location_code when logging in, or not."""
        without_code = self.sip.login_message(
            "login_id", "login_password"
        )
        assert without_code.endswith("COlogin_password")
        with_code = self.sip.login_message(
            "login_id", "login_password", "location_code"
        )
        assert with_code.endswith("COlogin_password|CPlocation_code")

    def test_patron_password_is_optional(self):
        without_password = self.sip.patron_information_request(
            "patron_identifier"
        )
        assert without_password.endswith('AApatron_identifier|AC')
        with_password = self.sip.patron_information_request(
            "patron_identifier", "patron_password"
        )
        assert with_password.endswith(
            'AApatron_identifier|AC|ADpatron_password'
        )

    def test_parse_patron_status(self):
        m = MockSIPClient.parse_patron_status
        assert_raises(ValueError, m, None)
        assert_raises(ValueError, m, "")
        assert_raises(ValueError, m, " " * 20)
        parsed = m("Y Y Y Y Y Y Y ")
        for yes in [
                'charge privileges denied',
                #'renewal privileges denied',
                'recall privileges denied',
                #'hold privileges denied',
                'card reported lost',
                #'too many items charged',
                'too many items overdue',
                #'too many renewals',
                'too many claims of items returned',
                #'too many items lost',
                'excessive outstanding fines',
                #'excessive outstanding fees',
                'recall overdue',
                #'too many items billed',
        ]:
            eq_(parsed[yes], True)

        for no in [
                #'charge privileges denied',
                'renewal privileges denied',
                #'recall privileges denied',
                'hold privileges denied',
                #'card reported lost',
                'too many items charged',
                #'too many items overdue',
                'too many renewals',
                #'too many claims of items returned',
                'too many items lost',
                #'excessive outstanding fines',
                'excessive outstanding fees',
                #'recall overdue',
                'too many items billed',
        ]:
            eq_(parsed[no], False)
    def test_remote_authenticate(self):
        client = MockSIPClient()
        auth = SIP2AuthenticationProvider(
            None, None, None, None, None, None, 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)
        
        client.queue_response(self.sierra_invalid_login)
        eq_(None, auth.remote_authenticate("user", "pass"))
        
        # 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)
        
        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)

        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)

        # 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)
Esempio n. 12
0
 def test_login_failure(self):
     sip = MockSIPClient('user_id', 'password')
     sip.queue_response('940')
     assert_raises(IOError, sip.login)
Esempio n. 13
0
 def test_login_password_is_optional(self):
     """You can specify a login_id without specifying a login_password."""
     sip = MockSIPClient('user_id')
     sip.queue_response('941')
     response = sip.login()
     eq_({'login_ok': '1', '_status': '94'}, response)
Esempio n. 14
0
 def test_login_success(self):
     sip = MockSIPClient('user_id', 'password')
     sip.queue_response('941')
     response = sip.login()
     eq_({'login_ok': '1', '_status': '94'}, response)
Esempio n. 15
0
 def test_login_failure(self):
     sip = MockSIPClient('user_id', 'password')
     sip.queue_response('940')
     assert_raises(IOError, sip.login)
Esempio n. 16
0
class TestPatronResponse(object):
    def setup(self):
        self.sip = MockSIPClient()

    def test_incorrect_card_number(self):
        self.sip.queue_response(
            "64Y                201610050000114734                        AOnypl |AA240|AENo Name|BLN|AFYour library card number cannot be located.|AY1AZC9DE"
        )
        response = self.sip.patron_information('identifier')

        # Test some of the basic fields.
        eq_(response['institution_id'], 'nypl ')
        eq_(response['personal_name'], 'No Name')
        eq_(response['screen_message'],
            ['Your library card number cannot be located.'])
        eq_(response['valid_patron'], 'N')
        eq_(response['patron_status'], 'Y             ')
        parsed = response['patron_status_parsed']
        eq_(True, parsed['charge privileges denied'])
        eq_(False, parsed['too many items charged'])

    def test_hold_items(self):
        "A patron has multiple items on hold."
        self.sip.queue_response(
            "64              000201610050000114837000300020002000000000000AOnypl |AA233|AEBAR, FOO|BZ0030|CA0050|CB0050|BLY|CQY|BV0|CC15.00|AS123|AS456|AS789|[email protected]|AY1AZC848"
        )
        response = self.sip.patron_information('identifier')
        eq_('0003', response['hold_items_count'])
        eq_(['123', '456', '789'], response['hold_items'])

    def test_multiple_screen_messages(self):
        self.sip.queue_response(
            "64Y  YYYYYYYYYYY000201610050000115040000000000000000000000000AOnypl |AA233|AESHELDON, ALICE|BZ0030|CA0050|CB0050|BLY|CQN|BV0|CC15.00|AFInvalid PIN entered.  Please try again or see a staff member for assistance.|AFThere are unresolved issues with your account.  Please see a staff member for assistance.|AY2AZ9B64"
        )
        response = self.sip.patron_information('identifier')
        eq_(2, len(response['screen_message']))

    def test_extension_field_captured(self):
        """This SIP2 message includes an extension field with the code XI.
        """
        self.sip.queue_response(
            "64  Y           00020161005    122942000000000000000000000000AA240|AEBooth Active Test|BHUSD|BDAdult Circ Desk 1 Newtown, CT USA 06470|AQNEWTWN|BLY|CQN|PA20191004|PCAdult|PIAllowed|XI86371|AOBiblioTest|ZZfoo|AY2AZ0000"
        )
        response = self.sip.patron_information('identifier')

        # The Evergreen XI field is a known extension and is picked up
        # as sipserver_internal_id.
        eq_("86371", response['sipserver_internal_id'])

        # The ZZ field is an unknown extension and is captured under
        # its SIP code.
        eq_(["foo"], response['ZZ'])

    def test_embedded_pipe(self):
        """In most cases we can handle data even if it contains embedded
        instances of the separator character.
        """
        self.sip.queue_response(
            '64              000201610050000134405000000000000000000000000AOnypl |AA12345|AERICHARDSON, LEONARD|BZ0030|CA0050|CB0050|BLY|CQY|BV0|CC15.00|BEleona|rdr@|bar.com|AY1AZD1BB\r'
        )
        response = self.sip.patron_information('identifier')
        eq_("leona|rdr@|bar.com", response['email_address'])

    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')
        eq_('240', response['patron_identifier'])

    def test_location_code_is_optional(self):
        """You can specify a location_code when logging in, or not."""
        without_code = self.sip.login_message("login_id", "login_password")
        assert without_code.endswith("COlogin_password")
        with_code = self.sip.login_message("login_id", "login_password",
                                           "location_code")
        assert with_code.endswith("COlogin_password|CPlocation_code")

    def test_institution_id_field_is_always_provided(self):
        without_institution_arg = self.sip.patron_information_request(
            "patron_identifier", "patron_password")
        assert without_institution_arg.startswith('AO|', 33)

    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)

    def test_patron_password_is_optional(self):
        without_password = self.sip.patron_information_request(
            "patron_identifier")
        assert without_password.endswith('AApatron_identifier|AC')
        with_password = self.sip.patron_information_request(
            "patron_identifier", "patron_password")
        assert with_password.endswith(
            'AApatron_identifier|AC|ADpatron_password')

    def test_parse_patron_status(self):
        m = MockSIPClient.parse_patron_status
        assert_raises(ValueError, m, None)
        assert_raises(ValueError, m, "")
        assert_raises(ValueError, m, " " * 20)
        parsed = m("Y Y Y Y Y Y Y ")
        for yes in [
                'charge privileges denied',
                #'renewal privileges denied',
                'recall privileges denied',
                #'hold privileges denied',
                'card reported lost',
                #'too many items charged',
                'too many items overdue',
                #'too many renewals',
                'too many claims of items returned',
                #'too many items lost',
                'excessive outstanding fines',
                #'excessive outstanding fees',
                'recall overdue',
                #'too many items billed',
        ]:
            eq_(parsed[yes], True)

        for no in [
                #'charge privileges denied',
                'renewal privileges denied',
                #'recall privileges denied',
                'hold privileges denied',
                #'card reported lost',
                'too many items charged',
                #'too many items overdue',
                'too many renewals',
                #'too many claims of items returned',
                'too many items lost',
                #'excessive outstanding fines',
                'excessive outstanding fees',
                #'recall overdue',
                'too many items billed',
        ]:
            eq_(parsed[no], False)
Esempio n. 17
0
 def test_login_failure(self):
     sip = MockSIPClient()
     sip.queue_response('940')
     response = sip.login('user_id', 'password')
     eq_('0', response['login_ok'])