def _test_session_renewal(self, test_console):
     """
     First asks for the complete host and user information.
     Then prompts for password renewal with that information filled in.
     :param test_console: True is testing console prompt, False is we are testing ui prompt.
     """
     self._print_message(
         "We're about to test session renewal. We'll first prompt you for your "
         "credentials and then we'll fake a session that is expired.\nYou will then have to "
         "re-enter your password.", test_console
     )
     # Get the basic user credentials.
     host, login, session_token, session_metadata = interactive_authentication.authenticate(
         "https://enter_your_host_name_here.shotgunstudio.com",
         "enter_your_username_here",
         "",
         fixed_host=False
     )
     sg_user = user_impl.SessionUser(
         host=host, login=login, session_token=session_token, http_proxy=None
     )
     self._print_message("We're about to fake an expired session. Hang tight!", test_console)
     # Test the session renewal code.
     tank.authentication.interactive_authentication.renew_session(
         sg_user
     )
     self._print_message("Test successful", test_console)
Exemplo n.º 2
0
 def _create_test_user(self):
     return user.ShotgunUser(user_impl.SessionUser(
         host="https://tank.shotgunstudio.com",
         login="******",
         session_token="session_token",
         http_proxy="http_proxy"
     ))
Exemplo n.º 3
0
    def test_login_value(self):
        session_user = user.ShotgunUser(user_impl.SessionUser(
            host="https://tank.shotgunstudio.com",
            login="******",
            session_token="session_token",
            http_proxy="http_proxy"
        ))
        self.assertEqual(session_user.login, "session_user")

        script_user = user.ShotgunUser(user_impl.ScriptUser(
            host="host",
            api_script="api_script",
            api_key="api_key",
            http_proxy="http_proxy"
        ))
        self.assertIsNone(script_user.login)

        class CustomUser(user_impl.ShotgunUserImpl):

            def __init__(self):
                super(CustomUser, self).__init__("https://test.shotgunstudio.com", None)

            def get_login(self):
                return "custom_user"

        custom_user = user.ShotgunUser(CustomUser())

        self.assertEqual(custom_user.login, "custom_user")
Exemplo n.º 4
0
    def test_create_connection_with_session_renewal(self, renew_session_mock,
                                                    server_caps_mock,
                                                    _call_rpc_mock):
        """
        When there is no valid session cached, the engine's renew session should take care of the
        session renewal
        """

        mocked_result = {"entities": [1, 2, 3]}
        _call_rpc_mock.side_effect = AuthenticationFault()

        user = user_impl.SessionUser("https://host.shotgunstudio.com", "login",
                                     "session", "proxy")

        # Directly call _call_rpc. We should be invoking the derived class here, which will
        # then invoke the base class which is in fact our mock class so it should throw once and then
        # succeed.

        # Make sure that when renewing the session that we update the mock so that the next time _call_rpc
        # is called we don't throw anything.
        def renew_session_side_effect(*args, **kwargs):
            _call_rpc_mock.side_effect = [mocked_result]

        renew_session_mock.side_effect = renew_session_side_effect
        test_result = user.create_sg_connection()._call_rpc()

        # Make sure we tried to renew the sesion
        self.assertTrue(renew_session_mock.called)
        # Make sure _call_rpc was called twice
        self.assertEqual(_call_rpc_mock.call_count, 2)
        self.assertEqual(id(mocked_result), id(test_result))
Exemplo n.º 5
0
    def test_successfull_session_cache_snooping(self, _call_rpc_mock, get_session_data_mock, renew_session_mock, server_caps_mock):
        """
        Tests that if the session token is invalid (mocked by the _call_rpc mocker), that we will try to
        get the session token from disk first before trying to renew the session token.
        """

        # First create the user and the connection object
        user = user_impl.SessionUser("https://host.shotgunstudio.com", "login", "session_token", "proxy")
        connection = user.create_sg_connection()

        # Mock a call to the server that fails authentication.
        _call_rpc_mock.side_effect = AuthenticationFault("This is coming from the _call_rpc_mock.")

        # Implement a fake get_session_data that returns the required information.
        def fake_get_session_data(*args):
            # Disable the mock side effect so that the next time the method is called all is well.
            _call_rpc_mock.side_effect = None
            return {"login": "******", "session_token": "session_token_2"}
        get_session_data_mock.side_effect = fake_get_session_data

        # This should:
        # - Call the RPC and throw because of the mock
        # - Look into the session cache is the session token changed (it did, fake_get_session_data returns session_token_2)
        # - Call the RPC again and not throw (since there is no side effect this time)
        connection._call_rpc()

        # We should look in the cache once.
        self.assertEqual(get_session_data_mock.call_count, 1)
        # We shouldn't try to renew the session.
        self.assertFalse(renew_session_mock.called)
        # We should have talked to the server twice.
        self.assertEqual(_call_rpc_mock.call_count, 2)
Exemplo n.º 6
0
 def _create_test_saml_user(self):
     return user.ShotgunSamlUser(user_impl.SessionUser(
         host="https://tank.shotgunstudio.com",
         login="******",
         session_token="session_token",
         http_proxy="http_proxy",
         session_metadata=valid_sso_session_metadata,
     ))
Exemplo n.º 7
0
    def test_create_connection_with_session_renewal_failure(self, renew_session_mock, server_caps_mock, _call_rpc_mock):
        """
        When there is no valid session cached, the engine's renew session should take care of the
        session renewal, but if the session renewal failed, we should get an AuthenticationFault as
        before.
        """

        _call_rpc_mock.side_effect = AuthenticationFault("This is coming from the _call_rpc_mock.")
        renew_session_mock.side_effect = ShotgunAuthenticationError("This is coming from renew_session_mock.")

        user = user_impl.SessionUser("https://host.shotgunstudio.com", "login", "session", "proxy")
        with self.assertRaisesRegexp(ShotgunAuthenticationError, "This is coming from renew_session_mock."):
            user.create_sg_connection()._call_rpc()

        # Make sure we tried to renew the sesion
        self.assertTrue(renew_session_mock.called)
        self.assertEqual(_call_rpc_mock.call_count, 1)
Exemplo n.º 8
0
    def test_failed_session_cache_snooping(
        self,
        _call_rpc_mock,
        get_session_data_mock,
        renew_session_mock,
        server_caps_mock,
    ):
        """
        Tests that if the session token is invalid (mocked by the _call_rpc mocker), that the session cache
        has been read, did provide an update and wasn't the right one, that we would renew the session.
        """

        # First create the user and the connection object
        user = user_impl.SessionUser("https://host.shotgunstudio.com", "login",
                                     "session_token", "proxy")
        connection = user.create_sg_connection()

        # Mock a call to the server that fails authentication.
        _call_rpc_mock.side_effect = AuthenticationFault(
            "This is coming from the _call_rpc_mock.")

        # Implement a fake get_session_data that returns the required information.
        def fake_get_session_data(*args):
            return {"login": "******", "session_token": "session_token_2"}

        get_session_data_mock.side_effect = fake_get_session_data

        def fake_renew_session(*args):
            _call_rpc_mock.side_effect = None

        renew_session_mock.side_effect = fake_renew_session

        # This should:
        # - Call the RPC and throw because of the mock
        # - Look into the session cache is the session token changed (it did, fake_get_session_data returns session_token_2)
        # - Call the RPC again and fail again since there the session token is still invalid (simulated bu the mock)
        # - Call get_session_data a second time.
        connection._call_rpc()

        # We should look in the cache once.
        self.assertEqual(get_session_data_mock.call_count, 1)
        # We shouldn't try to renew the session.
        self.assertTrue(renew_session_mock.called)
        # We should have talked to the server twice.
        self.assertEqual(_call_rpc_mock.call_count, 3)