def testShouldRetrieveCurrentSessionWhenGivenValidSessionId(self): # Given... class StubConnection (object): def request(self, url, method, data, headers): self.requestheaders = headers from StringIO import StringIO response = StringIO('<html><head><title>Reservation Manager</title></head><body><p>Jalani Bakari, you are signed in (Residential)!</body></html>') response.getheaders = lambda: {} return response StubConnection = Stub(PcsConnection)(StubConnection) conn = StubConnection() source = SessionScreenscrapeSource() @patch(source) def create_host_connection(self): return conn # When... session = source.fetch_session('valid_user', '12345abcde') # Then... self.assertEqual(session.user, 'valid_user') self.assertEqual(session.name, 'Jalani Bakari') self.assertEqual(session.id, '12345abcde')
def testShouldKnowAboutASimpleInvalidSessionDocumentBody(self): """Screenscrape source should have a simple document that triggers login failure""" source = SessionScreenscrapeSource() document = SessionScreenscrapeSource.SIMPLE_FAILURE_DOCUMENT is_valid = source.body_is_valid_session(document) self.assert_(not is_valid)
def testShouldThinkSessionDocumentIsInvalidIfTitleIsCorrect(self): """Login is failure if the response document has the correct title.""" source = SessionScreenscrapeSource() document = "<html><head><title>Please Login</title></head><body></body></html>" is_valid = source.body_is_valid_session(document) self.assertEqual(is_valid, False)
def testShouldThinkSessionDocumentIsValidIfTitleIsCorrect(self): """Login is successful if the response document has the correct title.""" source = SessionScreenscrapeSource() document = "<html><head><title>My Message Manager</title></head><body></body></html>" is_valid = source.body_is_valid_session(document) self.assertEqual(is_valid, True)
def testShouldCreateNoSessionIfLoginResultsInInvalidSessionDocument(self): """Expected session is returned from a failed login (expected session is None).""" source = SessionScreenscrapeSource() @patch(source) def login_to_pcs(self, conn, userid, password): return ("<html><head><title>Please Login</title></head><body></body></html>", {}) try: session = source.create_session('uid', 'pass') except SessionLoginError: return self.fail('Should have raised SessionLoginError')
def testShouldNotGetStuckOnTheCallToConnectionDotRequest(self): """Resuming session will get expected response from connection""" source = SessionScreenscrapeSource() class StubConnection (object): def request(self, url, method, data, headers): self.requestheaders = headers from StringIO import StringIO response = StringIO('Body Text') response.getheaders = lambda: {'h1':1,'h2':2} return response StubConnection = Stub(PcsConnection)(StubConnection) conn = StubConnection() body, headers = source.reconnect_to_pcs(conn, '1234567')
def testShouldRaiseErrorWhenRetrievingExistingSessionFails(self): # Given... source = SessionScreenscrapeSource() @patch(source) def reconnect_to_pcs(self, conn, sessionid): return ("<html><head><title>Please Login</title></head><body></body></html>", {}) # When... try: session = source.fetch_session('valid_user', 'expiredsession12345abcde') # Then... except SessionExpiredError: return self.fail('Should have raised SessionExpiredError')
def testShouldReturnExpectedBodyTextFromConnectionWhenLoggingIn(self): """Logging in will get the expected response from the connection""" source = SessionScreenscrapeSource() class StubConnection (object): def request(self, url, method, data, headers): from StringIO import StringIO response = StringIO('Body Text') response.getheaders = lambda: {'h1':1,'h2':2} return response StubConnection = Stub(PcsConnection)(StubConnection) conn = StubConnection() body, headers = source.login_to_pcs(conn, 'uid', 'pass') self.assertEqual(body, 'Body Text') self.assertEqual(headers, {'h1':1,'h2':2})
def testShouldSendCookiesToAndReturnExpectedBodyTextFromConnectionWhenReconnectingToSession(self): """Resuming session will get expected response from connection""" source = SessionScreenscrapeSource() class StubConnection (object): def request(self, url, method, data, headers): self.requestheaders = headers from StringIO import StringIO response = StringIO('Body Text') response.getheaders = lambda: {'h1':1,'h2':2} return response StubConnection = Stub(PcsConnection)(StubConnection) conn = StubConnection() body, headers = source.reconnect_to_pcs(conn, '1234567') self.assertEqual(conn.requestheaders, {'Cookie':'sid=1234567'}) self.assertEqual(body, 'Body Text') self.assertEqual(headers, {'h1':1,'h2':2})
def testShouldCreateNewSessionAccordingToContentInValidSessionDocument(self): """Expected session is returned from a successful login.""" source = SessionScreenscrapeSource() @patch(source) def login_to_pcs(self, conn, userid, password): return ("""<html> <head> <title>My Message Manager</title> </head> <body> <p>Mr. Bojangles, you are signed in. Yay!</p> </body> </html> """, {'set-cookie':'sid=abcde'}) session = source.create_session('uid', 'pass') self.assertEqual(session.id, 'abcde') self.assertEqual(session.user, 'uid') self.assertEqual(session.name, 'Mr. Bojangles')