def test_single_sign_on_receives_correct_token(): mock_sso_registry = Mock(SingleSignOnRegistry) correct_token = SSOToken() mock_sso_registry.is_valid = Mock(side_effect=confirm_token(correct_token)) service = MyService(mock_sso_registry) service.handle(Request("Emily"), correct_token) mock_sso_registry.is_valid.assert_called()
class SsoTest(unittest.TestCase): name = 'Earl' token = SSOToken('e4r1_t0k3n') credentials = 'Earl#passwd' def test_register_new_session_for_valid_credentials(self): single_sign_on.credentials_are_valid = MagicMock(return_value=True) single_sign_on.generate_token = MagicMock(return_value=self.token) SessionStorage.register = MagicMock() sso_registry = SingleSignOnRegistry() response_token = sso_registry.register_new_session(self.credentials) single_sign_on.credentials_are_valid.assert_called_once_with( self.credentials) single_sign_on.generate_token.assert_called_once_with() SessionStorage.register.assert_called_once_with(self.token) self.assertEquals(self.token, response_token) def test_register_new_session_for_invalid_credentials(self): single_sign_on.credentials_are_valid = MagicMock(return_value=False) sso_registry = SingleSignOnRegistry() response_token = sso_registry.register_new_session(self.credentials) self.assertEquals(None, response_token)
def test_single_sign_on_with_invalid_token(): spy_sso_registry = Mock(SingleSignOnRegistry) spy_sso_registry.is_valid.return_value = False service = MyService(spy_sso_registry) token = SSOToken() response = service.handle(Request("Emily"), token) spy_sso_registry.is_valid.assert_called_with(token) assert response.text == "Please sign in"
def test_hello_name(): stub_sso_registry = Mock(SingleSignOnRegistry) service = MyService(stub_sso_registry) response = service.handle(Request("Emily"), SSOToken()) assert response.text == "Hello Emily!"
def test_single_sign_on(): spy_sso_registry = Mock(SingleSignOnRegistry) service = MyService(spy_sso_registry) token = SSOToken() service.handle(Request("Emily"), token) spy_sso_registry.is_valid.assert_called_with(token)