def setUp(self): class StubSessionSource (object): pass StubSessionSource = Stub(_SessionSourceInterface)(StubSessionSource) class StubSessionView (object): def render_session(self, session): self.session = session return 'Session' StubSessionView = Stub(_SessionViewInterface)(StubSessionView) class StubErrorView (object): def render_error(self, error_code, error_msg, error_detail): return 'No Session' self.session_view = StubSessionView() self.session_source = StubSessionSource() self.error_view = StubErrorView() # System under test self.handler = SessionHandler(self.session_source, self.session_view, self.error_view) self.handler.request = StubRequest() self.handler.response = StubResponse()
class SessionHandlerTest (unittest.TestCase): def setUp(self): class StubSessionSource (object): pass StubSessionSource = Stub(_SessionSourceInterface)(StubSessionSource) class StubSessionView (object): def render_session(self, session): self.session = session return 'Session' StubSessionView = Stub(_SessionViewInterface)(StubSessionView) class StubErrorView (object): def render_error(self, error_code, error_msg, error_detail): return 'No Session' self.session_view = StubSessionView() self.session_source = StubSessionSource() self.error_view = StubErrorView() # System under test self.handler = SessionHandler(self.session_source, self.session_view, self.error_view) self.handler.request = StubRequest() self.handler.response = StubResponse() def testShouldReturnGivenUseridAndPasswordAsCredentials(self): # Given... self.handler.request['user'] = '******' self.handler.request['password'] = '******' # When... uid, pword = self.handler.get_credentials() # Then... self.assertEqual(uid, 'userid') self.assertEqual(pword, 'pword') def testShouldRespondWithSuccessContentWhenSessionIdIsRecognized(self): # Given... @patch(self.handler) def get_user_id(self): self.userid_called = True return 'user1234' @patch(self.handler) def get_session_id(self): self.sessionid_called = True return 'ses1234' @patch(self.handler) def get_session(self, userid, sessionid): self.userid = userid self.sessionid = sessionid return 'my session' @patch(self.session_view) def render_session(self, session): self.session = session return 'session overview body' # When... self.handler.get() # Then... response_body = self.handler.response.out.getvalue() self.assert_(self.handler.userid_called) self.assert_(self.handler.sessionid_called) self.assertEqual(self.handler.userid, 'user1234') self.assertEqual(self.handler.sessionid, 'ses1234') self.assertEqual(self.session_view.session, 'my session') self.assertEqual(response_body, 'session overview body') def testShouldRespondWithFailureContentWhenSessionIdIsNotRecognized(self): # Given... @patch(self.handler) def get_user_id(self): self.userid_called = True return 'user1234' @patch(self.handler) def get_session_id(self): self.sessionid_called = True return 'ses1234' @patch(self.handler) def get_session(self, userid, sessionid): self.userid = userid self.sessionid = sessionid raise SessionExpiredError() @patch(self.error_view) def render_error(self, error_code, error_msg, error_detail): return error_detail # When... self.handler.get() # Then... response_body = self.handler.response.out.getvalue() self.assert_(self.handler.userid_called) self.assert_(self.handler.sessionid_called) self.assertEqual(self.handler.userid, 'user1234') self.assertEqual(self.handler.sessionid, 'ses1234') self.assert_('SessionExpiredError' in response_body, 'Response does not contain SessionExpiredError: %r' % response_body) def testShouldSaveSessionToSetCookieHeader(self): class StubSession (object): id = 'ses1234' user = '******' name = 'My User Name' session = StubSession() self.handler.save_session(session) self.assertEqual(self.handler.response.headers.header_list, [ ('Set-Cookie',r'session_id=ses1234;') ]) def testShouldRespondWithSuccessContentWhenUserIdAndPasswordAreRecognized(self): # Given... @patch(self.handler) def get_credentials(self): self.credentials_called = True return 'user1234', 'pass1234' @patch(self.session_source) def create_session(self, userid, password): self.userid = userid self.password = password return 'my session' @patch(self.session_view) def render_session(self, session): self.session = session return 'session overview body' @patch(self.handler) def save_session(self, session): self.session = session # When... self.handler.post() # Then... response_body = self.handler.response.out.getvalue() self.assert_(self.handler.credentials_called) self.assertEqual(self.session_source.userid, 'user1234') self.assertEqual(self.session_source.password, 'pass1234') self.assertEqual(self.session_view.session, 'my session') self.assertEqual(self.handler.session, 'my session') self.assertEqual(response_body, 'session overview body') def testShouldRespondWithFailureContentWhenUserIdAndPasswordAreNotRecognized(self): # Given... @patch(self.handler) def get_credentials(self): self.credentials_called = True return 'user1234', 'pass1234' @patch(self.session_source) def create_session(self, userid, password): self.userid = userid self.password = password raise SessionLoginError() @patch(self.error_view) def render_error(self, error_code, error_msg, error_detail): return error_detail # When... self.handler.post() # Then... response_body = self.handler.response.out.getvalue() self.assert_(self.handler.credentials_called) self.assertEqual(self.session_source.userid, 'user1234') self.assertEqual(self.session_source.password, 'pass1234') self.assert_('SessionLoginError' in response_body, 'Response does not contain SessionLoginError: %r' % response_body) def testShouldRaiseWsgiParameterErrorWhenUseridOrPasswordIsEmpty(self): self.handler.request['user'] = '******' self.handler.request['password'] = '' try: self.handler.get_credentials() except WsgiParameterError: pass else: self.fail('Empty password should raise error') self.handler.request['user'] = '' self.handler.request['password'] = '******' try: self.handler.get_credentials() except WsgiParameterError: pass else: self.fail('Empty user id should raise error') def testShouldRaiseWsgiParameterErrorWhenUseridOrPasswordIsMissing(self): self.handler.request['user'] = '******' try: self.handler.get_credentials() except WsgiParameterError: pass else: self.fail('Missing password should raise error') del self.handler.request['user'] self.handler.request['password'] = '******' try: self.handler.get_credentials() except WsgiParameterError: pass else: self.fail('Missing user id should raise error')