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)
class TestPatronResponse(object): def setup_method(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. assert response["institution_id"] == "nypl " assert response["personal_name"] == "No Name" assert response["screen_message"] == [ "Your library card number cannot be located." ] assert response["valid_patron"] == "N" assert response["patron_status"] == "Y " parsed = response["patron_status_parsed"] assert True == parsed["charge privileges denied"] assert 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") assert "0003" == response["hold_items_count"] assert ["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") assert 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. assert "86371" == response["sipserver_internal_id"] # The ZZ field is an unknown extension and is captured under # its SIP code. assert ["foo"] == response["ZZ"] 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"] 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") assert "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") assert "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 pytest.raises(ValueError, m, None) pytest.raises(ValueError, m, "") pytest.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', ]: assert 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", ]: assert parsed[no] == False
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)
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. response['institution_id'] = 'nypl ' response['peronal_name'] = 'No Name' response['screen_message'] = [ 'Your library card number cannot be located.' ] response['valid_patron'] = 'N' response['patron_status'] = 'Y ' 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')
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)