Beispiel #1
0
 def testInitNewUser(self):
     users.get_current_user = mock.MagicMock(return_value=self.new_app_user)
     self.assertEqual(self.new_app_user, InitUser('new token').user)
     new_app_user_ndb = AppUser.query(
         AppUser.user == users.get_current_user()).fetch()[0]
     self.assertTrue(new_app_user_ndb)
     self.assertEqual('new token', new_app_user_ndb.refresh_token)
Beispiel #2
0
 def post(self, method):
     """Delegate POST request calls to the DFP API."""
     if method == 'networks':
         user_ndb = InitUser()
         api_handler = APIHandler(_CLIENT_ID, _CLIENT_SECRET, user_ndb,
                                  _APPLICATION_NAME)
         api_handler.MakeTestNetwork()
         return self.redirect('/')
     else:
         self.response.status = 400
         self.response.write(method + ' API POST method not found.')
Beispiel #3
0
    def get(self):
        """Handle get request."""
        user = users.get_current_user()
        user_email = user.email()
        logout_url = users.create_logout_url('/')
        user_ndb = InitUser()

        template = _JINJA_ENVIRONMENT.get_template('index_page.html')
        self.response.write(
            template.render({
                'user_email': user_email,
                'logout_url': logout_url,
            }))
Beispiel #4
0
    def check_dfp_credentials_exist(request, *args, **kwargs):
        """The decorator function.

    Args:
      request: Request The webapp2 request object.
      *args: Additional args.
      **kwargs: Additional kwargs.

    Returns:
      func The decorator function.
    """
        # check if user's refresh token exists
        user_ndb = InitUser()
        if user_ndb.refresh_token:
            # Refresh token already exists in database. Proceed to homepage.
            return view_func(request, *args, **kwargs)

        # Refresh token doesn't exist.
        # Proceed to step 1: Redirecting to authorization server.
        return request.redirect('/login')
Beispiel #5
0
    def get(self):
        """Handle get request."""
        if not self.request.get('code'):
            # User denied OAuth2 permissions.
            logging.info('User denied OAuth2 permissions')
            return self.redirect('/login/error')

        credentials = _FLOW.step2_exchange(self.request.get('code'))
        if credentials:
            # store user's credentials in database
            user_ndb = InitUser(credentials.refresh_token)

            # check if user has any networks
            api_handler = APIHandler(_CLIENT_ID, _CLIENT_SECRET, user_ndb,
                                     _APPLICATION_NAME)
            networks = api_handler.GetAllNetworks()
            if not networks:
                # if user has no networks, redirect to ask if one should be made
                return self.redirect('/make-test-network')
            return self.redirect('/')
        else:
            # failure: no credentials
            logging.error('No credentials found from step 2 in flow')
            return self.redirect('/login/error')
Beispiel #6
0
    def get(self, method):
        """Delegate GET request calls to the DFP API."""
        method = method.lower()
        user_ndb = InitUser()
        api_handler = APIHandler(_CLIENT_ID, _CLIENT_SECRET, user_ndb,
                                 _APPLICATION_NAME)
        network_code = self.request.get('network_code')

        # parse parameters
        try:
            limit = int(self.request.get('limit', api_handler.page_limit))
        except ValueError:
            self.response.status = 400
            return self.response.write('Limit must be an integer')
        try:
            offset = int(self.request.get('offset', 0))
        except ValueError:
            self.response.status = 400
            return self.response.write('Offset must be an integer')

        if method == 'networks':
            return_obj = api_handler.GetAllNetworks()
        else:
            # construct PQL statement
            where_clause = self.request.get('where', '')
            statement = ad_manager.FilterStatement(where_clause,
                                                   limit=limit,
                                                   offset=offset)

            try:
                # throws KeyError if method not found
                api_handler_func = getattr(api_handler,
                                           self.api_handler_method_map[method])
            except KeyError:
                self.response.status = 400
                self.response.write('API method not supported (%s).' % method)

            # retrieve return_obj from api_handler and modify it
            return_obj = api_handler_func(network_code, statement)

        # process return_obj
        if 'columns' in return_obj:
            # special case: return_obj is from PQL Service
            cols = return_obj['columns']
            return_obj['results'] = [
                unpack_row(row, cols) for row in return_obj['results']
            ]
        else:
            # regular case
            return_obj['results'] = [
                serialize_object(obj) for obj in return_obj['results']
            ]

        if self.request.get('limit'):
            return_obj['limit'] = limit
        else:
            try:
                return_obj['limit'] = return_obj['totalResultSetSize']
            except KeyError:
                return_obj['limit'] = api_handler.page_limit
        return_obj['offset'] = offset

        # construct response headers
        self.response.headers['Content-Type'] = 'application/json'

        self.response.write(json.dumps(return_obj))
Beispiel #7
0
 def testRevokeOldUserCredentials(self):
     users.get_current_user = mock.MagicMock(return_value=self.old_user)
     RevokeOldCredentials()
     user_ndb = InitUser()
     self.assertEqual(None, user_ndb.refresh_token)
Beispiel #8
0
 def testInitExistingUser(self):
     users.get_current_user = mock.MagicMock(
         return_value=self.existing_app_user)
     self.assertEqual(self.existing_app_user, InitUser().user)
Beispiel #9
0
 def testNotRevokeNewUserCredentials(self):
     users.get_current_user = mock.MagicMock(return_value=self.recent_user)
     RevokeOldCredentials()
     user_ndb = InitUser()
     self.assertTrue(user_ndb.refresh_token)