def GetAuthorizationInfo(self, request):
        """Determine a test run action's authorization information.

    Parameters:
      action_id: Test run action ID
      redirect_uri: URL to redirect to after authorization
    """
        action = self._GetTestRunAction(request.action_id)
        redirect_uri, is_manual = oauth2_util.GetRedirectUri(
            request.redirect_uri)
        oauth2_config = test_run_hook.GetOAuth2Config(action)
        flow = oauth2_util.GetOAuth2Flow(oauth2_config, redirect_uri)
        auth_url, _ = flow.authorization_url()
        return mtt_messages.AuthorizationInfo(url=auth_url,
                                              is_manual=is_manual)
    def Authorize(self, request):
        """Authorize a test run action with an authorization code.

    Parameters:
      action_id: Test run action ID
      redirect_uri: URL to redirect to after authorization
      code: Authorization code
    """
        action = self._GetTestRunAction(request.action_id)
        redirect_uri, _ = oauth2_util.GetRedirectUri(request.redirect_uri)
        oauth2_config = test_run_hook.GetOAuth2Config(action)
        flow = oauth2_util.GetOAuth2Flow(oauth2_config, redirect_uri)
        flow.fetch_token(code=request.code)
        action.credentials = flow.credentials
        action.put()
        return message_types.VoidMessage()
 def testGetOAuth2Config(self):
     """Tests that a hook's OAuth2 config can be retrieved."""
     action = ndb_models.TestRunAction(name='Test',
                                       hook_class_name='oauth2')
     self.assertEqual(OAuth2Hook.oauth2_config,
                      test_run_hook.GetOAuth2Config(action))
 def testGetOAuth2Config_noConfig(self):
     """Tests that no OAuth2 config is returned if class doesn't have any."""
     action = ndb_models.TestRunAction(name='Test',
                                       hook_class_name='simple')
     self.assertIsNone(test_run_hook.GetOAuth2Config(action))
 def testGetOAuth2Config_notFound(self):
     """Tests that no OAuth2 config is returned if hook class not found."""
     action = ndb_models.TestRunAction(name='Test',
                                       hook_class_name='unknown')
     self.assertIsNone(test_run_hook.GetOAuth2Config(action))