def testGetOAuth2Flow(self):
     """Tests that an OAuth2 flow can be constructed."""
     oauth2_config = oauth2_util.OAuth2Config(client_id='id',
                                              client_secret='secret',
                                              scopes=['lorem', 'ipsum'])
     flow = oauth2_util.GetOAuth2Flow(oauth2_config, 'redirect_uri')
     self.assertEqual('id', flow.oauth2session.client_id)
     self.assertEqual(['lorem', 'ipsum'], flow.oauth2session.scope)
     self.assertEqual('redirect_uri', flow.oauth2session.redirect_uri)
  def GetAuthorizationInfo(self, request):
    """Determines a build channel configuration's authorization information.

    Parameters:
      build_channel_id: Build channel ID
      redirect_uri: URL to redirect to after authorization
    """
    build_channel = self._GetBuildChannel(request.build_channel_id)
    redirect_uri, is_manual = oauth2_util.GetRedirectUri(request.redirect_uri)
    flow = oauth2_util.GetOAuth2Flow(build_channel.oauth2_config, redirect_uri)
    auth_url, _ = flow.authorization_url()
    return mtt_messages.AuthorizationInfo(url=auth_url, is_manual=is_manual)
    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 AuthorizeConfig(self, request):
    """Authorizes a build channel configuration with an authorization code.

    Parameters:
      build_channel_id: Build channel ID
      redirect_uri: URL to redirect to after authorization
      code: Authorization code
    """
    build_channel = self._GetBuildChannel(request.build_channel_id)
    redirect_uri, _ = oauth2_util.GetRedirectUri(request.redirect_uri)
    flow = oauth2_util.GetOAuth2Flow(build_channel.oauth2_config, redirect_uri)
    flow.fetch_token(code=request.code)
    build_channel.config.credentials = flow.credentials
    build_channel.config.put()
    return message_types.VoidMessage()
    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 testGetOAuth2Flow_noConfig(self):
     """Tests that the OAuth2 config is required when creating a flow."""
     with self.assertRaises(ValueError):
         oauth2_util.GetOAuth2Flow(None, 'redirect_uri')