def test_non_401_error_response(self):
   http = HttpMockSequence([
     ({'status': '400'}, ''),
     ])
   http = self.credentials.authorize(http)
   resp, content = http.request('http://example.com')
   self.assertEqual(400, resp.status)
  def testPost(self):
    request = webapp2.Request.blank("/step1", POST={
      'domain': 'demo-123.resold.richieforeman.net'
    })
    from mock import MagicMock

    http = HttpMockSequence([
      ({'status': '200'}, RESELLER_DISCOVERY),
      ({'status': '200'}, json.dumps({}))
    ])

    # wrap http.request so we can spy on it.
    http.request = MagicMock(wraps=http.request)

    with patch('main.get_authorized_http', return_value=http):
      response = request.get_response(app)

      self.assertTrue(http.request.called)
      discovery_call = http.request.call_args_list[0]
      create_customer = http.request.call_args_list[1]

      # ensure that a discovery call occured.
      (url,), args = discovery_call
      self.assertEqual(url, RESELLER_DISCOVERY_URL)

      # ensure that a customer was created via the POST api.
      (url, method), args = create_customer
      self.assertEqual(url,
                       'https://www.googleapis'
                       '.com/apps/reseller/v1/customers?alt=json')
      self.assertEqual(method, 'POST')
Exemplo n.º 3
0
    def test_set_user_agent_nested(self):
        http = HttpMockSequence([({"status": "200"}, "echo_request_headers")])

        http = set_user_agent(http, "my_app/5.5")
        http = set_user_agent(http, "my_library/0.1")
        resp, content = http.request("http://example.com")
        self.assertEqual("my_app/5.5 my_library/0.1", content["user-agent"])
 def test_auth_header_sent(self):
   http = HttpMockSequence([
     ({'status': '200'}, 'echo_request_headers'),
     ])
   http = self.credentials.authorize(http)
   resp, content = http.request('http://example.com')
   self.assertEqual('Bearer foo', content['Authorization'])
 def test_assertion_refresh(self):
     http = HttpMockSequence(
         [({"status": "200"}, '{"access_token":"1/3w"}'), ({"status": "200"}, "echo_request_headers")]
     )
     http = self.credentials.authorize(http)
     resp, content = http.request("http://example.com")
     self.assertEqual(content["authorization"], "OAuth 1/3w")
Exemplo n.º 6
0
  def test_set_user_agent(self):
    http = HttpMockSequence([
      ({'status': '200'}, 'echo_request_headers'),
      ])

    http = set_user_agent(http, "my_app/5.5")
    resp, content = http.request("http://example.com")
    self.assertEqual(content['user-agent'], 'my_app/5.5')
 def test_assertion_refresh(self):
   http = HttpMockSequence([
     ({'status': '200'}, '{"access_token":"1/3w"}'),
     ({'status': '200'}, 'echo_request_headers'),
     ])
   http = self.credentials.authorize(http)
   resp, content = http.request('http://example.com')
   self.assertEqual('Bearer 1/3w', content['Authorization'])
Exemplo n.º 8
0
 def test_add_requestor_to_uri(self):
   http = HttpMockSequence([
     ({'status': '200'}, 'echo_request_uri'),
     ])
   http = self.credentials.authorize(http)
   resp, content = http.request("http://example.com")
   self.assertEqual('http://example.com?xoauth_requestor_id=test%40example.org',
                    content)
 def test_token_refresh_failure(self):
     http = HttpMockSequence([({"status": "401"}, ""), ({"status": "400"}, '{"error":"access_denied"}')])
     http = self.credentials.authorize(http)
     try:
         http.request("http://example.com")
         self.fail("should raise AccessTokenRefreshError exception")
     except AccessTokenRefreshError:
         pass
Exemplo n.º 10
0
 def test_token_refresh_success(self):
   http = HttpMockSequence([
     ({'status': '401'}, ''),
     ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'),
     ({'status': '200'}, 'echo_request_headers'),
     ])
   http = self.credentials.authorize(http)
   resp, content = http.request("http://example.com")
   self.assertEqual('Bearer 1/3w', content['Authorization'])
 def _credentials_refresh(self, credentials):
   http = HttpMockSequence([
     ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'),
     ({'status': '401'}, ''),
     ({'status': '200'}, '{"access_token":"3/3w","expires_in":3600}'),
     ({'status': '200'}, 'echo_request_headers'),
     ])
   http = credentials.authorize(http)
   resp, content = http.request('http://example.org')
   return content
Exemplo n.º 12
0
 def test_invalid_token(self):
   http = HttpMockSequence([
     ({'status': '401'}, ''),
     ])
   http = self.credentials.authorize(http)
   try:
     resp, content = http.request("http://example.com")
     self.fail('should raise CredentialsInvalidError')
   except CredentialsInvalidError:
     pass
 def test_token_refresh_success(self):
     http = HttpMockSequence([({"status": "401"}, "")])
     http = self.credentials.authorize(http)
     try:
         resp, content = http.request("http://example.com")
         self.fail("should throw exception if token expires")
     except AccessTokenCredentialsError:
         pass
     except Exception:
         self.fail("should only throw AccessTokenCredentialsError")
Exemplo n.º 14
0
  def test_set_user_agent_nested(self):
    http = HttpMockSequence([
      ({'status': '200'}, 'echo_request_headers'),
      ])

    http = set_user_agent(http, "my_app/5.5")
    http = set_user_agent(http, "my_library/0.1")
    resp, content = http.request("http://example.com")
    content = simplejson.loads(content.decode())
    self.assertEqual('my_app/5.5 my_library/0.1', content['user-agent'])
Exemplo n.º 15
0
 def test_no_requestor(self):
   self.credentials.requestor = None
   http = HttpMockSequence([
     ({'status': '401'}, ''),
     ])
   http = self.credentials.authorize(http)
   try:
     resp, content = http.request("http://example.com")
     self.fail('should raise MissingParameter')
   except MissingParameter:
     pass
 def _credentials_refresh(self, credentials):
     http = HttpMockSequence(
         [
             ({"status": "200"}, '{"access_token":"1/3w","expires_in":3600}'),
             ({"status": "401"}, ""),
             ({"status": "200"}, '{"access_token":"3/3w","expires_in":3600}'),
             ({"status": "200"}, "echo_request_headers"),
         ]
     )
     http = credentials.authorize(http)
     resp, content = http.request("http://example.org")
     return content
 def test_token_refresh_success(self):
   # Older API (GData) respond with 403
   for status_code in ['401', '403']:
     http = HttpMockSequence([
       ({'status': status_code}, ''),
       ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'),
       ({'status': '200'}, 'echo_request_headers'),
       ])
     http = self.credentials.authorize(http)
     resp, content = http.request("http://example.com")
     self.assertEqual('Bearer 1/3w', content['Authorization'])
     self.assertFalse(self.credentials.access_token_expired)
Exemplo n.º 18
0
 def test_token_refresh_failure(self):
   http = HttpMockSequence([
     ({'status': '401'}, ''),
     ({'status': '400'}, '{"error":"access_denied"}'),
     ])
   http = self.credentials.authorize(http)
   try:
     http.request("http://example.com")
     self.fail("should raise AccessTokenRefreshError exception")
   except AccessTokenRefreshError:
     pass
   self.assertTrue(self.credentials.access_token_expired)
 def test_token_refresh_success(self):
   for status_code in REFRESH_STATUS_CODES:
     http = HttpMockSequence([
       ({'status': status_code}, ''),
       ])
     http = self.credentials.authorize(http)
     try:
       resp, content = http.request('http://example.com')
       self.fail('should throw exception if token expires')
     except AccessTokenCredentialsError:
       pass
     except Exception:
       self.fail('should only throw AccessTokenCredentialsError')
 def test_token_refresh_success(self):
   for status_code in REFRESH_STATUS_CODES:
     token_response = {'access_token': '1/3w', 'expires_in': 3600}
     http = HttpMockSequence([
         ({'status': status_code}, ''),
         ({'status': '200'}, simplejson.dumps(token_response)),
         ({'status': '200'}, 'echo_request_headers'),
     ])
     http = self.credentials.authorize(http)
     resp, content = http.request('http://example.com')
     self.assertEqual('Bearer 1/3w', content['Authorization'])
     self.assertFalse(self.credentials.access_token_expired)
     self.assertEqual(token_response, self.credentials.token_response)
Exemplo n.º 21
0
 def test_signed_jwt_assertion_credentials(self):
   private_key = datafile('privatekey.p12')
   credentials = SignedJwtAssertionCredentials(
       '*****@*****.**',
       private_key,
       scope='read+write',
       prn='*****@*****.**')
   http = HttpMockSequence([
     ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'),
     ({'status': '200'}, 'echo_request_headers'),
     ])
   http = credentials.authorize(http)
   resp, content = http.request('http://example.org')
   self.assertEquals(content['authorization'], 'OAuth 1/3w')
 def test_credentials_good(self):
   private_key = datafile('privatekey.%s' % self.format)
   credentials = SignedJwtAssertionCredentials(
       '*****@*****.**',
       private_key,
       scope='read+write',
       sub='*****@*****.**')
   http = HttpMockSequence([
     ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'),
     ({'status': '200'}, 'echo_request_headers'),
     ])
   http = credentials.authorize(http)
   resp, content = http.request('http://example.org')
   self.assertEqual('Bearer 1/3w', content['Authorization'])
 def test_credentials_good(self):
     private_key = datafile("privatekey.p12")
     credentials = SignedJwtAssertionCredentials(
         "*****@*****.**", private_key, scope="read+write", prn="*****@*****.**"
     )
     http = HttpMockSequence(
         [
             ({"status": "200"}, '{"access_token":"1/3w","expires_in":3600}'),
             ({"status": "200"}, "echo_request_headers"),
         ]
     )
     http = credentials.authorize(http)
     resp, content = http.request("http://example.org")
     self.assertEqual("Bearer 1/3w", content["Authorization"])
Exemplo n.º 24
0
    def __enter__(self):
        http = HttpMockSequence(self._responses)
        native_request_method = http.request

        # Collecting requests to validate at __exit__.
        def _request_wrapper(*args, **kwargs):
            self._actual_requests.append(args + (kwargs['body'],))
            return native_request_method(*args, **kwargs)

        http.request = _request_wrapper
        service_mock = build('ml', 'v1', http=http)
        with mock.patch.object(
                hook.CloudMLHook, 'get_conn', return_value=service_mock):
            return hook.CloudMLHook()
 def test_token_refresh_failure(self):
   for status_code in REFRESH_STATUS_CODES:
     http = HttpMockSequence([
       ({'status': status_code}, ''),
       ({'status': '400'}, '{"error":"access_denied"}'),
       ])
     http = self.credentials.authorize(http)
     try:
       http.request('http://example.com')
       self.fail('should raise AccessTokenRefreshError exception')
     except AccessTokenRefreshError:
       pass
     self.assertTrue(self.credentials.access_token_expired)
     self.assertEqual(None, self.credentials.token_response)
 def test_token_refresh_success(self):
   # Older API (GData) respond with 403
   for status_code in ['401', '403']:
     http = HttpMockSequence([
       ({'status': status_code}, ''),
       ])
     http = self.credentials.authorize(http)
     try:
       resp, content = http.request("http://example.com")
       self.fail("should throw exception if token expires")
     except AccessTokenCredentialsError:
       pass
     except Exception:
       self.fail("should only throw AccessTokenCredentialsError")
    def __enter__(self):
        http = HttpMockSequence(self._responses)
        native_request_method = http.request

        # Collecting requests to validate at __exit__.
        def _request_wrapper(*args, **kwargs):
            self._actual_requests.append(args + (kwargs.get('body', ''),))
            return native_request_method(*args, **kwargs)

        http.request = _request_wrapper
        discovery = requests.get(
            'https://www.googleapis.com/discovery/v1/apis/ml/v1/rest')
        service_mock = build_from_document(discovery.json(), http=http)
        with mock.patch.object(
                hook.MLEngineHook, 'get_conn', return_value=service_mock):
            return hook.MLEngineHook()
Exemplo n.º 28
0
    def test_exchange_code_for_token_fail(self):
        http = HttpMockSequence([
            ({
                'status': '400'
            }, '{"error":"invalid_request"}'),
        ])

        try:
            credentials = credentials_from_code(self.client_id,
                                                self.client_secret,
                                                self.scope,
                                                self.code,
                                                redirect_uri=self.redirect_uri,
                                                http=http)
            self.fail('should raise exception if exchange doesn\'t get 200')
        except FlowExchangeError:
            pass
    def test_should_not_create_backups_entity_if_backup_table_doesnt_exist(
            self, _create_http, error_reporting, _):
        # given
        _create_http.return_value = HttpMockSequence([
            ({
                'status': '200'
            }, content('tests/json_samples/bigquery_v2_test_schema.json')),
            (
                {
                    'status': '404'
                },  # Table not found
                content('tests/json_samples/table_get/'
                        'bigquery_partitioned_table_get.json'))
        ])

        table_entity = Table(project_id="source_project_id",
                             dataset_id="source_dataset_id",
                             table_id="source_table_id",
                             partition_id="123")
        table_entity.put()

        source_bq_table = TableReference.from_table_entity(
            table_entity).create_big_query_table()
        destination_bq_table = BigQueryTable("target_project_id",
                                             "target_dataset_id",
                                             "target_table_id")
        data = {
            "sourceBqTable": source_bq_table,
            "targetBqTable": destination_bq_table
        }
        payload = json.dumps({
            "data": data,
            "jobJson": JobResultExample.DONE
        },
                             cls=RequestEncoder)

        # when
        response = self.under_test.post(
            '/callback/backup-created/project/dataset/table', params=payload)
        backup = table_entity.last_backup

        # then
        self.assertEqual(response.status_int, 200)
        self.assertIsNone(backup)
        error_reporting.assert_called_once()
Exemplo n.º 30
0
    def test_destroy_good(self):
        """We can destroy live units"""

        fh = open(os.path.join(self._BASE_DIR, 'fixtures/fleet_v1.json'))
        discovery = fh.read()
        fh.close()

        http = HttpMockSequence([({
            'status': '200'
        }, discovery), ({
            'status': '204'
        }, None)])

        client = Client('http://198.51.100.23:9160', http=http)

        unit = Unit(client=client, data={'name': 'test.service'})

        assert unit.destroy()
Exemplo n.º 31
0
    def test_exchange_id_token_fail(self):
        body = {'foo': 'bar'}
        json = simplejson.dumps(body).encode()
        payload = base64.urlsafe_b64encode(json).decode('ascii').strip('=')
        jwt = (base64.urlsafe_b64encode(b'stuff').decode('ascii') + '.' +
               payload + '.' +
               base64.urlsafe_b64encode(b'signature').decode('ascii'))

        http = HttpMockSequence([
            ({
                'status': '200'
            }, """{ "access_token":"SlAV32hkKG",
       "refresh_token":"8xLOxBtZp8",
       "id_token": "%s"}""" % jwt),
        ])

        credentials = self.flow.step2_exchange('some random code', http=http)
        self.assertEqual(credentials.id_token, body)
Exemplo n.º 32
0
    def test_ensure_response_callback(self):
        m = JsonModel()
        request = HttpRequest(
            None,
            m.response,
            'https://www.googleapis.com/someapi/v1/collection/?foo=bar',
            method='POST',
            body='{}',
            headers={'content-type': 'application/json'})
        h = HttpMockSequence([({'status': 200}, '{}')])
        responses = []

        def _on_response(resp, responses=responses):
            responses.append(resp)

        request.add_response_callback(_on_response)
        request.execute(http=h)
        self.assertEqual(1, len(responses))
Exemplo n.º 33
0
 def test_userip_is_added_to_discovery_uri(self):
     # build() will raise an HttpError on a 400, use this to pick the request uri
     # out of the raised exception.
     os.environ['REMOTE_ADDR'] = '10.0.0.1'
     try:
         http = HttpMockSequence([
             ({
                 'status': '400'
             }, open(datafile('zoo.json'), 'rb').read()),
         ])
         zoo = build('zoo',
                     'v1',
                     http=http,
                     developerKey='foo',
                     discoveryServiceUrl='http://example.com')
         self.fail('Should have raised an exception.')
     except HttpError as e:
         self.assertEqual(e.uri, 'http://example.com?userIp=10.0.0.1')
Exemplo n.º 34
0
    def test_create_sheet(self):
        title = "xyz"
        _id = '123'

        http = HttpMockSequence([({
            'status': '200'
        }, self._sheets_discovery),
                                 ({
                                     'status': '200'
                                 }, _CREATE_RESPONSE % (title, _id)),
                                 ({
                                     'status': '200'
                                 }, _MOVE_RESPONSE % _id)])
        gsheet = GSheetManager()
        with patch.object(gsheet, 'get_sheets_service',
                          create_service_mock('sheets', http)):
            sheet_id = gsheet.create_sheet(title)
        self.assertEqual(_id, sheet_id)
  def test_resumable_media_fail_unknown_response_code_first_request(self):
    """Not a multipart upload."""
    self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
    zoo = build('zoo', 'v1', http=self.http)

    media_upload = MediaFileUpload(datafile('small.png'), resumable=True)
    request = zoo.animals().insert(media_body=media_upload, body=None)

    http = HttpMockSequence([
      ({'status': '400',
        'location': 'http://upload.example.com'}, ''),
      ])

    try:
      request.execute(http=http)
      self.fail('Should have raised ResumableUploadError.')
    except ResumableUploadError as e:
      self.assertEqual(400, e.resp.status)
Exemplo n.º 36
0
  def test_media_io_base_download_handle_redirects(self):
    self.request.http = HttpMockSequence([
      ({'status': '307',
        'location': 'https://secure.example.net/lion'}, ''),
      ({'status': '200',
        'content-range': '0-2/5'}, 'abc'),
    ])

    download = MediaIoBaseDownload(
        fd=self.fd, request=self.request, chunksize=3)

    status, done = download.next_chunk()

    self.assertEqual('https://secure.example.net/lion', download._uri)
    self.assertEqual(self.fd.getvalue(), 'abc')
    self.assertEqual(False, done)
    self.assertEqual(3, download._progress)
    self.assertEqual(5, download._total_size)
Exemplo n.º 37
0
    def test_bq_load(self):
        """Tests whether BQ load processes data and returns a job ID"""

        # It's important to call the discovery method first, that gets passed into build()
        http = HttpMockSequence([
            ({'status': '200'}, self.bigquery_discovery),
            ({'status': '200'}, self.bigquery_running),
            ({'status': '200'}, self.bigquery_done),])

        gs = build("bigquery", "v2", http=http)

        job_id = bigquery.load("test.csv", destination_table="destination",
                               destination_dataset="ds",
                               fields=["asdf"], service=gs)
        self.assertEqual(job_id, "job_UmMf6X71NCgp4PxL1xE6WUfS8n0")

        bqj = models.BigQuery_Job(job_id, gs=gs)

        bqj.wait(timeout=.05)
Exemplo n.º 38
0
 def test_update_sheet_data(self):
     sheet_id = 'xyz'
     ref_dict = {
         'spreadsheetId': sheet_id,
         'updatedCells': 2,
         'updatedColumns': 2,
         'updatedRange': 'TEST!A1:B1',
         'updatedRows': 1
     }
     http = HttpMockSequence([({
         'status': '200'
     }, self._sheets_discovery), ({
         'status': '200'
     }, json.dumps(ref_dict))])
     gsheet = GSheetManager()
     with patch.object(gsheet, 'get_sheets_service',
                       create_service_mock('sheets', http)):
         resp = gsheet.update_sheet(sheet_id, "TEST!A1:B2", ['2', '3'])
     self.assertEqual(ref_dict, resp)
    def test_exchange_failure_with_json_error(self):
        # Some providers have 'error' attribute as a JSON object
        # in place of regular string.
        # This test makes sure no strange object-to-string coversion
        # exceptions are being raised instead of FlowExchangeError.
        http = HttpMockSequence([
            ({
                'status': '400'
            }, """ {"error": {
              "type": "OAuthException",
              "message": "Error validating verification code."} }"""),
        ])

        try:
            credentials = self.flow.step2_exchange('some random code',
                                                   http=http)
            self.fail('should raise exception if exchange doesn\'t get 200')
        except FlowExchangeError as e:
            pass
Exemplo n.º 40
0
    def test_execute_batch_http_error(self):
        callbacks = Callbacks()
        batch = BatchHttpRequest(callback=callbacks.f)

        batch.add(self.request1)
        batch.add(self.request2)
        http = HttpMockSequence([
            ({
                'status': '200',
                'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'
            }, BATCH_ERROR_RESPONSE),
        ])
        batch.execute(http=http)
        self.assertEqual({'foo': 42}, callbacks.responses['1'])
        expected = (
            '<HttpError 403 when requesting '
            'https://www.googleapis.com/someapi/v1/collection/?foo=bar returned '
            '"Access Not Configured">')
        self.assertEqual(expected, str(callbacks.exceptions['2']))
Exemplo n.º 41
0
 def testPollAndEmptyConfigs(self):
     app = service.CreateApp({
         'environ': {
             'GOOGLE_CLOUD_PROJECT': 'chromeperf',
             'GAE_SERVICE': 'sheriff-config',
         },
         'datastore_client':
         datastore.Client(credentials=credentials.AnonymousCredentials(),
                          project='chromeperf'),
         'http':
         HttpMockSequence([({
             'status': '200'
         }, self.discovery_file), ({
             'status': '200'
         }, '{}')])
     })
     client = app.test_client()
     response = client.get('/configs/update',
                           headers={'X-Forwarded-Proto': 'https'})
     self.assertEqual(response.status_code, 200)
Exemplo n.º 42
0
    def test_process_message_error(self):
        """Tests whether process_message returns False for an errored job, and checks dynamodb for errors"""

        http = HttpMockSequence([
            ({
                'status': '200'
            }, self.bigquery_discovery),
            ({
                'status': '200'
            }, self.bigquery_error),
        ])

        gs = build("bigquery", "v2", http=http)

        ret = job_check.process_message(self.data, self.queue_name, gs=gs)
        self.assertEqual(ret, False)

        r = get_item({"job_id": self.data["job_id"]}, self.queue_name)
        print(r)
        self.assertIsNotNone(r)
Exemplo n.º 43
0
    def test_bq_wait(self):
        http = HttpMockSequence([
            ({'status': '200'}, self.bigquery_discovery),
            ({'status': '200'}, self.bigquery_running),
            ({'status': '200'}, self.bigquery_running),
            ({'status': '200'}, self.bigquery_running),
            ({'status': '200'}, self.bigquery_error)])

        gs = build("bigquery", "v2", http=http)

        resp = bigquery.job_status("job_UmMf6X71NCgp4PxL1xE6WUfS8n0", service=gs)

        bqj = models.BigQuery_Job(resp["jobReference"]["jobId"], gs=gs)

        with self.assertRaises(BigQueryException):
            bqj.wait(timeout=.05)

        self.assertEqual(bqj.state, "ERROR")
        self.assertIsNotNone(bqj.errors)
        self.assertIsNotNone(bqj.errorResult)
Exemplo n.º 44
0
    def test_resumable_media_handle_uploads_of_unknown_size(self):
        http = HttpMockSequence([
            ({
                'status': '200',
                'location': 'http://upload.example.com'
            }, ''),
            ({
                'status': '200'
            }, 'echo_request_headers_as_json'),
        ])

        self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
        zoo = build('zoo', 'v1', http=self.http)

        # Create an upload that doesn't know the full size of the media.
        class IoBaseUnknownLength(MediaUpload):
            def chunksize(self):
                return 10

            def mimetype(self):
                return 'image/png'

            def size(self):
                return None

            def resumable(self):
                return True

            def getbytes(self, begin, length):
                return '0123456789'

        upload = IoBaseUnknownLength()

        # TODO: Google API does not recognize the PNG content type
        return
        request = zoo.animals().insert(media_body=upload, body=None)
        status, body = request.next_chunk(http=http)
        self.assertEqual(body, {
            'Content-Range': 'bytes 0-9/*',
            'Content-Length': '10',
        })
Exemplo n.º 45
0
    def testRun_emptyFolder(self, mockOAuth2CredentialsFromJsonMethod):
        # TODO(michaelcupino): Move test json files into a test_utils package.
        http = HttpMockSequence([
            ({
                'status': '200'
            }, open(datafile('test-drive-children-list-empty.json'),
                    'rb').read()),
            ({
                'status': '200'
            }, open(datafile('test-drive-children-list-empty.json'),
                    'rb').read()),
        ])
        mockCredentials = MagicMock(name='mockCredentials')
        mockCredentials.authorize = MagicMock(return_value=http)
        mockOAuth2CredentialsFromJsonMethod.return_value = mockCredentials

        pipeline = FolderFetcherPipeline('/folder123', None)
        pipeline.start_test()
        result = pipeline.outputs.default.value

        self.assertEqual([], result)
Exemplo n.º 46
0
    def test_set_unit_name_desired_state_good(self):
        """Unit Desired State can be updated by name"""

        self.mock(
            HttpMockSequence([
                ({
                    'status': '204'
                }, None),
                ({
                    'status': '200'
                },
                 '{"currentState":"launched","desiredState":"inactive","machineID":'
                 '"b4104f4b83fd48b2acc16a085b0ec2ce","name":"test.service","options":'
                 '[{"name":"ExecStart","section":"Service","value":"/usr/bin/sleep 1d"}]}'
                 )
            ]))

        unit = self.client.set_unit_desired_state('test.service', 'inactive')

        assert unit
        assert unit.desiredState == 'inactive'
Exemplo n.º 47
0
 def __execute_long_query_responses():
     return HttpMockSequence([
         ({
             'status': '200'
         }, content('tests/json_samples/bigquery_v2_test_schema.json')),
         ({
             'status': '200'
         }, content('tests/json_samples/big_query/query_response.json')),
         ({
             'status': '200'
         },
          content(
              'tests/json_samples/big_query/get_query_results_job_not_completed.json'
          )),
         ({
             'status': '200'
         },
          content(
              'tests/json_samples/big_query/get_query_results_job_completed.json'
          ))
     ])
Exemplo n.º 48
0
 def setUp(self):
     with open('tests/config-discovery.json') as discovery_file:
         self.discovery_file = discovery_file.read()
     with open('tests/sample-configs-get_project_configs.json'
               ) as sample_config_file:
         self.sample_config = sample_config_file.read()
     self.app = service.CreateApp({
         'environ': {
             'GOOGLE_CLOUD_PROJECT': 'chromeperf',
             'GAE_SERVICE': 'sheriff-config',
         },
         'datastore_client':
         datastore.Client(credentials=credentials.AnonymousCredentials(),
                          project='chromeperf'),
         'http':
         HttpMockSequence([({
             'status': '200'
         }, self.discovery_file), ({
             'status': '200'
         }, self.sample_config)]),
     })
Exemplo n.º 49
0
    def testGet_withNextPageToken(self, MockOauth2Decorator):
        # TODO(michaelcupino): Move test json files into a test_utils package.
        http = HttpMockSequence([
            ({
                'status': '200'
            }, open(datafile('test-drive-children-list-0.json'), 'rb').read()),
            ({
                'status': '200'
            }, open(datafile('test-drive-children-list-1.json'), 'rb').read()),
            ({
                'status': '200'
            }, open(datafile('test-drive-children-list-2.json'), 'rb').read()),
            ({
                'status': '200'
            }, open(datafile('test-drive-children-list-3.json'), 'rb').read()),
        ])
        MockOauth2Decorator.has_credentials = MagicMock(return_value=True)
        MockOauth2Decorator.http = MagicMock(return_value=http)

        response = self.testapp.get('/folder/123')

        self.assertIn('<pre>343 documents</pre>', response.body)
Exemplo n.º 50
0
    def test_should_propagate_dataset_500_error(self, _create_http):
        # given
        http_mock = Mock(wraps=HttpMockSequence([({
            'status': '200'
        }, test_utils.content('tests/json_samples/bigquery_v2_test_schema.json'
                              )), ({
                                  'status': '500'
                              }, '')]))
        _create_http.return_value = http_mock
        model_provider = Mock()
        model_provider.list_groups.return_value = ["missing_dataset1"]

        under_test = ModelCreator(model_provider)

        # when
        with self.assertRaises(HttpError) as context:
            under_test.create_missing_datasets()

        # then
        calls = http_mock.mock_calls
        self.assertEqual(2, len(calls))
        self.assertEqual(500, context.exception.resp.status)
Exemplo n.º 51
0
    def test_execute_global_callback(self):
        class Callbacks(object):
            def __init__(self):
                self.responses = {}

            def f(self, request_id, response):
                self.responses[request_id] = response

        callbacks = Callbacks()
        batch = BatchHttpRequest(callback=callbacks.f)

        batch.add(self.request1)
        batch.add(self.request2)
        http = HttpMockSequence([
            ({
                'status': '200',
                'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'
            }, BATCH_RESPONSE),
        ])
        batch.execute(http)
        self.assertEqual(callbacks.responses['1'], {'foo': 42})
        self.assertEqual(callbacks.responses['2'], {'baz': 'qux'})
  def test_resumable_media_handle_uploads_of_unknown_size_eof(self):
    http = HttpMockSequence([
      ({'status': '200',
        'location': 'http://upload.example.com'}, ''),
      ({'status': '200'}, 'echo_request_headers_as_json'),
      ])

    self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
    zoo = build('zoo', 'v1', http=self.http)

    fd = io.StringIO('data goes here')

    # Create an upload that doesn't know the full size of the media.
    upload = MediaIoBaseUpload(
        fd=fd, mimetype='image/png', chunksize=15, resumable=True)

    request = zoo.animals().insert(media_body=upload, body=None)
    status, body = request.next_chunk(http=http)
    self.assertEqual(body, {
        'Content-Range': 'bytes 0-13/14',
        'Content-Length': '14',
        })
Exemplo n.º 53
0
  def test_execute_request_body(self):
    batch = BatchHttpRequest()

    batch.add(self.request1)
    batch.add(self.request2)
    http = HttpMockSequence([
      ({'status': '200',
        'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'},
        'echo_request_body'),
      ])
    try:
      batch.execute(http=http)
      self.fail('Should raise exception')
    except BatchError, e:
      boundary, _ = e.content.split(None, 1)
      self.assertEqual('--', boundary[:2])
      parts = e.content.split(boundary)
      self.assertEqual(4, len(parts))
      self.assertEqual('', parts[0])
      self.assertEqual('--', parts[3])
      header = parts[1].splitlines()[1]
      self.assertEqual('Content-Type: application/http', header)
Exemplo n.º 54
0
    def test_listing_table_partitions(self, _create_http):  # nopep8 pylint: disable=W0613
        # given
        _create_http.return_value = HttpMockSequence([
            ({
                'status': '200'
            },
             test_utils.content(
                 'tests/json_samples/bigquery_v2_test_schema.json')),
            ({
                'status': '200'
            },
             test_utils.content(
                 'tests/json_samples/bigquery_v2_query_for_partitions.json')),
            ({
                'status': '200'
            },
             test_utils.content(
                 'tests/json_samples/'
                 'bigquery_v2_query_for_partitions_results_1.json')),
            ({
                'status': '200'
            },
             test_utils.content(
                 'tests/json_samples/'
                 'bigquery_v2_query_for_partitions_results_last.json'))
        ])
        under_test = BigQuery()

        # when
        partitions = under_test.list_table_partitions("project123",
                                                      "dataset123", "table123")

        # then
        self.assertEqual(self.count(partitions), 5)
        self.assertEqual(partitions[0]['partitionId'], '20170317')
        self.assertEqual(partitions[0]['creationTime'],
                         '2017-03-17 14:32:17.755000')
        self.assertEqual(partitions[0]['lastModifiedTime'],
                         '2017-03-17 14:32:19.289000')
Exemplo n.º 55
0
 def __create_table_partititions_list_responses():
     return HttpMockSequence([
         ({
             'status': '200'
         }, content('tests/json_samples/bigquery_v2_test_schema.json')),
         ({
             'status': '200'
         },
          content('tests/json_samples/bigquery_query_for_partitions.json')),
         ({
             'status': '200'
         },
          content(
              'tests/json_samples/bigquery_query_for_partitions_results_1.json'
          )),
         ({
             'status': '200'
         },
          content(
              'tests/json_samples/bigquery_query_for_partitions_results_last.json'
          ))
     ])
Exemplo n.º 56
0
    def test_media_io_base_download(self):
        self.request.http = HttpMockSequence([
            ({
                'status': '200',
                'content-range': '0-2/5'
            }, '123'),
            ({
                'status': '200',
                'content-range': '3-4/5'
            }, '45'),
        ])
        self.assertEqual(True, self.request.http.follow_redirects)

        download = MediaIoBaseDownload(fd=self.fd,
                                       request=self.request,
                                       chunksize=3)

        self.assertEqual(self.fd, download._fd)
        self.assertEqual(3, download._chunksize)
        self.assertEqual(0, download._progress)
        self.assertEqual(None, download._total_size)
        self.assertEqual(False, download._done)
        self.assertEqual(self.request.uri, download._uri)

        status, done = download.next_chunk()

        self.assertEqual(self.fd.getvalue(), '123')
        self.assertEqual(False, done)
        self.assertEqual(3, download._progress)
        self.assertEqual(5, download._total_size)
        self.assertEqual(3, status.resumable_progress)

        status, done = download.next_chunk()

        self.assertEqual(self.fd.getvalue(), '12345')
        self.assertEqual(True, done)
        self.assertEqual(5, download._progress)
        self.assertEqual(5, download._total_size)
Exemplo n.º 57
0
    def testPollConfigAddsAndRemoves(self):
        with open('tests/sample-configs-get_project_configs_reduced.json'
                  ) as sample_config_file:
            sample_config_reduced = sample_config_file.read()
        app = service.CreateApp({
            'environ': {
                'GOOGLE_CLOUD_PROJECT': 'chromeperf',
                'GAE_SERVICE': 'sheriff-config',
            },
            'datastore_client':
            datastore.Client(credentials=credentials.AnonymousCredentials(),
                             project='chromeperf'),
            'http':
            HttpMockSequence([({
                'status': '200'
            }, self.discovery_file), ({
                'status': '200'
            }, self.sample_config), ({
                'status': '200'
            }, sample_config_reduced)]),
        })

        # Step 1: Get one configuration with two config sets.
        client = app.test_client()
        response = client.get('/configs/update',
                              headers={'X-Forwarded-Proto': 'https'})
        self.assertEqual(response.status_code, 200)

        self.AssertProjectConfigSet1Holds(client, 200)
        self.AssertProjectConfigSet2Holds(client, 200)

        # Step 2: Get another configuration, this time with just one config set.
        response = client.get('/configs/update',
                              headers={'X-Forwarded-Proto': 'https'})
        self.assertEqual(response.status_code, 200)

        self.AssertProjectConfigSet1Holds(client, 404)
        self.AssertProjectConfigSet2Holds(client, 200)
 def test_auth_header_sent(self):
     http = HttpMockSequence([({"status": "200"}, "echo_request_headers")])
     http = self.credentials.authorize(http)
     resp, content = http.request("http://example.com")
     self.assertEqual(content["authorization"], "OAuth foo")
 def test_non_401_error_response(self):
     http = HttpMockSequence([({"status": "400"}, "")])
     http = self.credentials.authorize(http)
     resp, content = http.request("http://example.com")
     self.assertEqual(400, resp.status)