Пример #1
0
    def testConnectEmail(self):
        source = 'TheSource'
        token = 'TheToken'
        creds = gdata_lib.Creds()
        creds.user = '******'
        creds.password = '******'
        creds.tracker_auth_token = None
        self.mox.StubOutClassWithMocks(gd_ph_client, 'ProjectHostingClient')
        mocked_tcomm = self.mox.CreateMock(gdata_lib.TrackerComm)

        def set_token(*_args, **_kwargs):
            mocked_itclient.auth_token = cros_test_lib.EasyAttr(
                token_string=token)

        # Replay script
        mocked_itclient = gd_ph_client.ProjectHostingClient()
        mocked_itclient.ClientLogin(
            creds.user,
            creds.password,
            source=source,
            service='code',
            account_type='GOOGLE').WithSideEffects(set_token)
        self.mox.ReplayAll()

        # Verify
        with self.OutputCapturer():
            gdata_lib.TrackerComm.Connect(mocked_tcomm,
                                          creds,
                                          'TheProject',
                                          source=source)
        self.mox.VerifyAll()
        self.assertEquals(mocked_tcomm.it_client, mocked_itclient)
Пример #2
0
    def testConnectToken(self):
        source = 'TheSource'
        token = 'TheToken'
        creds = gdata_lib.Creds()
        creds.user = '******'
        creds.password = '******'
        creds.tracker_auth_token = token
        mocked_tcomm = self.mox.CreateMock(gdata_lib.TrackerComm)

        self.mox.StubOutClassWithMocks(gd_ph_client, 'ProjectHostingClient')
        self.mox.StubOutClassWithMocks(gdata.gauth, 'ClientLoginToken')

        # Replay script
        mocked_itclient = gd_ph_client.ProjectHostingClient()
        mocked_token = gdata.gauth.ClientLoginToken(token)
        self.mox.ReplayAll()

        # Verify
        with self.OutputCapturer():
            gdata_lib.TrackerComm.Connect(mocked_tcomm,
                                          creds,
                                          'TheProject',
                                          source=source)
        self.mox.VerifyAll()
        self.assertEquals(mocked_tcomm.it_client, mocked_itclient)
        self.assertEquals(mocked_itclient.auth_token, mocked_token)
Пример #3
0
def main(args):
    if len(args) != 2:
        print __doc__
        sys.exit(1)

    project, filename = args

    # read, normalize, and validate CSV file
    with open(filename) as f:
        rows = list(csv.reader(f, skipinitialspace=True))

    passed = True
    for i, row in enumerate(rows):
        for c in REQUIRED_COLUMNS:
            if not row[c]:
                passed = False
                print >> sys.stderr, 'Error on line %d: Missing required field %s' % (
                    i + 1, HEADER[c + 1])

    if not passed:
        sys.exit(1)

    # local web server that handles the oauth token redirect. (port 0 means pick
    # any available port.)
    server = BaseHTTPServer.HTTPServer(('localhost', 0), OAuthRedirectHandler)
    redirect_url = 'http://%s:%d' % server.server_address

    # start oauth: get a request token
    phc = client.ProjectHostingClient()
    request_token = phc.get_oauth_token(gauth.AUTH_SCOPES['code'],
                                        redirect_url, CLIENT_ID, CLIENT_SECRET)
    print """\
Opening a web page in your browser to request access.
Please click the Grant access button."""
    webbrowser.open_new_tab(str(request_token.generate_authorization_url()))

    # listen for the redirect
    server.handle_request()

    # finish oauth: get an access token
    gauth.authorize_request_token(request_token, OAuthRedirectHandler.url)
    access_token = phc.get_access_token(request_token)

    # import the issues, one at a time
    try:
        for i, row in enumerate(rows):
            if i == 0 and map(string.lower, row) == HEADER:
                continue

            for c in LIST_COLUMNS:
                if c < len(row):
                    # split on comma and space. filter out empty strings.
                    values = map(string.strip, row[c].split(', '))
                    row[c] = list(itertools.ifilter(bool, values))

            phc.add_issue(project, *row, auth_token=access_token)
            print_and_flush('.')
    except:
        print >> sys.stderr, '\nError on line %d:\n%s' % (i + 1, row)
        raise
    finally:
        print '\nImported %d issues.' % (i + 1)