def sendAll(self, args={}): logging.critical('Setting All Lights') data = args.get('lightCommand') url = self._api_url + 'all/state' treq.put(url, headers=self.header, data=data) #if r.status_code == 200: return True
def test_no_read_permissions_queue_read_failure(admin_user): """ Assert an errback occurs when unable to read from the queue due to permissions. This is a bit weird because the consumer has permission to register itself, but not to actually read from the queue so the result is what errors back. """ url = "{base}permissions/%2F/{user}".format(base=HTTP_API, user=admin_user) body = {"configure": ".*", "write": ".*", "read": ""} resp = yield treq.put(url, json=body, auth=HTTP_AUTH, timeout=3) assert resp.code == 204 queue = str(uuid.uuid4()) queues = { queue: { "auto_delete": False, "arguments": { "x-expires": 60 * 1000 } } } amqp_url = "amqp://{user}:guest@localhost:5672/%2F".format(user=admin_user) with mock.patch.dict(config.conf, {"amqp_url": amqp_url}): consumers = yield api.twisted_consume(lambda x: x, [], queues) _add_timeout(consumers[0].result, 5) try: yield consumers[0].result pytest.fail("Call failed to raise an exception") except exceptions.PermissionException as e: assert e.reason == ( "ACCESS_REFUSED - access to queue '{}' in vhost '/' refused for user " "'{}'".format(queue, admin_user)) except (defer.TimeoutError, defer.CancelledError): pytest.fail("Timeout reached without consumer calling its errback!")
def put_doc(self, uuid, doc_id, content): """ Make a PUT request to Soledad's incoming API, delivering a message into user's database. :param uuid: The uuid of a user :type uuid: str :param content: Message content. :type content: str :return: A deferred which fires after the HTTP request is complete, or which fails with the correspondent exception if there was any error. """ url = self._incoming_url + "%s/%s" % (uuid, doc_id) try: response = yield treq.put( url, BytesIO(str(content)), headers=self._auth_header, persistent=False) except Exception as original_exception: error_message = "Server unreacheable or unknown error: %s" error_message %= (original_exception.message) our_exception = UnavailableIncomingAPIException(error_message) raise_from(our_exception, original_exception) if not response.code == 200: error_message = '%s returned status %s instead of 200' error_message %= (url, response.code) raise UnavailableIncomingAPIException(error_message)
def admin_user(): """ Fixture that creates a random admin user and deletes the user afterwards. Useful if the test wishes to alter permissions to test failure cases. Default permissions is complete access to the "/" vhost. Returns: The username of the new administrator. The password is "guest". """ # Create a user with no permissions username = str(uuid.uuid4()) url = "{base}users/{user}".format(base=HTTP_API, user=username) body = {"username": username, "password": "******", "tags": "administrator"} deferred_resp = treq.put(url, json=body, auth=HTTP_AUTH, timeout=3) @pytest_twisted.inlineCallbacks def cp(resp): assert resp.code == 201 url = "{base}permissions/%2F/{user}".format(base=HTTP_API, user=username) body = {"configure": ".*", "write": ".*", "read": ".*"} resp = yield treq.put(url, json=body, auth=HTTP_AUTH, timeout=3) assert resp.code == 201 deferred_resp.addCallbacks(cp, cp) pytest_twisted.blockon(deferred_resp) yield username # Cleanup deferred_resp = treq.delete(url, auth=HTTP_AUTH, timeout=3) pytest_twisted.blockon(deferred_resp)
def cp(resp): assert resp.code == 201 url = "{base}permissions/%2F/{user}".format(base=HTTP_API, user=username) body = {"configure": ".*", "write": ".*", "read": ".*"} resp = yield treq.put(url, json=body, auth=HTTP_AUTH, timeout=3) assert resp.code == 201
def test_no_read_permissions_queue_read_failure_pika1(admin_user, queue_and_binding): """ Assert an errback occurs when unable to read from the queue due to permissions. This is a bit weird because the consumer has permission to register itself, but not to actually read from the queue so the result is what errors back. """ queues, bindings = queue_and_binding url = "{base}permissions/%2F/{user}".format(base=HTTP_API, user=admin_user) body = {"configure": ".*", "write": ".*", "read": ""} resp = yield treq.put(url, json=body, auth=HTTP_AUTH, timeout=3) assert resp.code == 204 amqp_url = "amqp://{user}:guest@localhost:5672/%2F".format(user=admin_user) with mock.patch.dict(config.conf, {"amqp_url": amqp_url}): try: consumers = api.twisted_consume(lambda x: x, [], queues) _add_timeout(consumers, 5) yield consumers pytest.fail("Call failed to raise an exception") except exceptions.PermissionException as e: assert e.reason == ( "ACCESS_REFUSED - access to queue '{}' in vhost '/' refused for user " "'{}'".format(list(queues.keys())[0], admin_user)) except (defer.TimeoutError, defer.CancelledError): pytest.fail( "Timeout reached without consumer calling its errback!")
def replace_group_config(self, rcs, replacement_config): """ Replace the current group configuration with the provided config and update the stored groupConfiguration information. :param TestResources rcs: The integration test resources instance. This provides useful information to complete the request, like which endpoint to use to make the API request. :param dict replacement_config: A dictionary representation of the JSON description of a scaling group groupConfiguration Note that since this is a replacement config, all fields in the JSON descirptor are mandatory. """ def record_results(_, replacement_config): # Replace the stored value in the group_config self.group_config["groupConfiguration"] = replacement_config # Find the correct group from the list (by id) for g in rcs.groups: if g['group']['id'] == self.group_id: g['group']['groupConfiguration'] = replacement_config if verbosity > 0: print('Update group_config with {}'.format(replacement_config)) return rcs return (treq.put( "{0}/groups/{1}/config".format(rcs.endpoints["otter"], self.group_id), json.dumps(replacement_config), headers=headers(str(rcs.token)), pool=self.pool, ).addCallback(check_success, [204]).addCallback(record_results, replacement_config))
def start_responding(self, server_name, challenge, response): validation = _validation(response) full_name = challenge.validation_domain_name(server_name) # subdomain = _split_zone(full_name, self._zone_name) zones_list_url = str(base.child("zones").set("name", self._zone_name)) response = yield treq.get(zones_list_url, headers=self._headers()) data = yield response.json() assert len(data['result']) == 1 zone_id = data['result'][0]['id'] records_base = base.child("zones").child(zone_id).child("dns_records") records_query_url = str( records_base.set("type", "TXT").set("name", full_name)) response = yield treq.get(records_query_url, headers=self._headers()) data = yield response.json() records = data['result'] dns_record = { "type": "TXT", "ttl": 120, "name": full_name, "content": validation } if records: put_to = str(records_base.child(records[0]["id"])) response = yield treq.put(put_to, json=dns_record, headers=self._headers()) else: post_to = str(records_base) response = yield treq.post(post_to, json=dns_record, headers=self._headers()) yield response.json() yield deferLater(self._reactor, self._settle_delay, lambda: None)
def test_no_write_permissions(admin_user): """Assert the call to twisted_consume errbacks on write permissions errors.""" url = "{base}permissions/%2F/{user}".format(base=HTTP_API, user=admin_user) body = {"configure": ".*", "write": "", "read": ".*"} resp = yield treq.put(url, json=body, auth=HTTP_AUTH, timeout=3) assert resp.code == 204 queue = str(uuid.uuid4()) queues = { queue: { "auto_delete": False, "arguments": { "x-expires": 60 * 1000 } } } bindings = [{ "queue": queue, "exchange": "amq.topic", "routing_keys": ["#"] }] amqp_url = "amqp://{user}:guest@localhost:5672/%2F".format(user=admin_user) try: with mock.patch.dict(config.conf, {"amqp_url": amqp_url}): yield api.twisted_consume(lambda x: x, bindings, queues) pytest.fail("Call failed to raise an exception") except exceptions.BadDeclaration as e: assert e.reason.args[0] == 403 assert e.reason.args[1] == ( "ACCESS_REFUSED - access to queue '{}' in vhost '/' refused for user" " '{}'".format(queue, admin_user))
def set(self, key, value): value_serialized = json.dumps(value).encode() response = yield treq.put('{}/v2/keys/{}/{}'.format(self._select_host(), self._prefix, key), data={'value': value_serialized}, headers={'Content-Type': ['application/x-www-form-urlencoded']}) if response.code != 200: pass print(response.code)
def set(self, key, value): value_serialized = json.dumps(value).encode() response = yield treq.put( '{}/v2/keys/{}/{}'.format(self._select_host(), self._prefix, key), data={'value': value_serialized}, headers={'Content-Type': ['application/x-www-form-urlencoded']}) if response.code != 200: pass print(response.code)
def _put(self, path, headers, data): response = yield treq.put(path, data=data, agent=self.custom_agent, headers=headers) content = yield treq.content(response) final_response = self.decode_results(content, self.response_headers(response), response.code, response.phrase) returnValue(final_response)
def upload(self, local_path): log.debug("Uploading %s...", local_path) with open(local_path, 'rb') as f: resp = yield treq.put('{}uri'.format(self.nodeurl), f) if resp.code == 200: content = yield treq.content(resp) log.debug("Successfully uploaded %s", local_path) return content.decode('utf-8') content = yield treq.content(resp) raise TahoeWebError(content.decode('utf-8'))
def upload(url, log_identifier, delay=0): logfile = open(path, "rb") if delay != 0: reactor.callLater(delay, upload, url, log_identifier=log_identifier) else: # FIXME persistent=False is a workaround to help with some # problems in unit testing. deferred = treq.put(url=url, data=logfile, headers={"Content-Type": ["text/csv"]}, persistent=False) deferred.addCallback(lambda x: result_callback(url, log_identifier, x)) deferred.addErrback(lambda x: error_callback(url, log_identifier, x))
def test_put_incoming_creates_a_document_using_couch(self): self.prepare('couch') user_id, doc_id = self.user_id, uuid4().hex content, scheme = 'Hi', EncryptionSchemes.PUBKEY formatter = IncomingFormatter() incoming_endpoint = self.uri + '%s/%s' % (user_id, doc_id) yield treq.put(incoming_endpoint, BytesIO(content), persistent=False) db = self.state.open_database(user_id) doc = db.get_doc(doc_id) self.assertEquals(doc.content, formatter.format(content, scheme))
def upload(self, local_path): log.debug("Uploading %s...", local_path) yield self.await_ready() with open(local_path, "rb") as f: resp = yield treq.put("{}uri".format(self.nodeurl), f) if resp.code == 200: content = yield treq.content(resp) log.debug("Successfully uploaded %s", local_path) return content.decode("utf-8") content = yield treq.content(resp) raise TahoeWebError(content.decode("utf-8"))
def test_create_url_no_user_token(self): yield ShortenerTables(self.account, self.conn).create_tables() payload = {'long_url': 'foo'} resp = yield treq.put(self.make_url('/api/create'), data=json.dumps(payload), allow_redirects=False, pool=self.pool) result = yield treq.json_content(resp) self.assertEqual(result['short_url'], 'http://wtxt.io/qr0') self.assertTrue( self.tr.value().startswith("test-account.wtxtio.created.count 1"))
def test_cached_pool(self): """ The first use of the module-level API populates the global connection pool, which is used for all subsequent requests. """ pool = SyntacticAbominationHTTPConnectionPool() self.patch(treq.api, "HTTPConnectionPool", lambda reactor, persistent: pool) self.failureResultOf(treq.head("http://test.com"), TabError) self.failureResultOf(treq.get("http://test.com"), TabError) self.failureResultOf(treq.post("http://test.com"), TabError) self.failureResultOf(treq.put("http://test.com"), TabError) self.failureResultOf(treq.delete("http://test.com"), TabError) self.failureResultOf(treq.request("OPTIONS", "http://test.com"), TabError) self.assertEqual(pool.requests, 6)
def test_put_incoming_creates_a_blob_using_filesystem(self): self.prepare('filesystem') user_id, doc_id = self.user_id, uuid4().hex content = 'Hi' formatter = IncomingFormatter() incoming_endpoint = self.uri + '%s/%s' % (user_id, doc_id) yield treq.put(incoming_endpoint, BytesIO(content), persistent=False) db = self.state.open_database(user_id) request = DummyRequest([user_id, doc_id]) yield db.read_blob(user_id, doc_id, request, 'MX') flags = db.get_flags(user_id, doc_id, request, 'MX') flags = json.loads(flags) expected = formatter.preamble(content, doc_id) + ' ' + content self.assertEquals(expected, request.written[0]) self.assertIn(Flags.PENDING, flags)
def handle_room_window_PUT(request): if xwot_app.resources['window']: data = request.content.read() content_type = request.getHeader('Content-Type') dic = deserialize(data, content_type) window.update(dic, content_type) _data = window.serialize('application/json') accept = request.getHeader('Accept') d = treq.put(xwot_app.resources['window'], data=_data, headers={'Content-Type': 'application/json'}) request.setHeader('Content-Type', accept) d.addCallback(treq.content) return window.serialize(accept) else: request.setResponseCode(404) return ''
def test_no_write_permissions(admin_user, queue_and_binding): """Assert the call to twisted_consume errbacks on write permissions errors.""" queues, bindings = queue_and_binding url = "{base}permissions/%2F/{user}".format(base=HTTP_API, user=admin_user) body = {"configure": ".*", "write": "", "read": ".*"} resp = yield treq.put(url, json=body, auth=HTTP_AUTH, timeout=3) assert resp.code == 204 amqp_url = "amqp://{user}:guest@localhost:5672/%2F".format(user=admin_user) try: with mock.patch.dict(config.conf, {"amqp_url": amqp_url}): yield api.twisted_consume(lambda x: x, bindings, queues) pytest.fail("Call failed to raise an exception") except exceptions.BadDeclaration as e: assert e.reason.args[0] == 403 assert e.reason.args[1] == ( "ACCESS_REFUSED - access to queue '{}' in vhost '/' refused for user" " '{}'".format(list(queues.keys())[0], admin_user))
def set_launch_config(self, rcs, launch_config): """Changes the launch configuration used by the scaling group. :param TestResources rcs: The integration test resources instance. This provides useful information to complete the request, like which endpoint to use to make the API request. :param dict launch_config: The new launch configuration. :return: A :class:`Deferred` which, upon firing, alters the scaling group's launch configuration. If successful, the test resources provided will be returned. Otherwise, an exception will rise. """ return (treq.put( "{0}/groups/{1}/launch".format(rcs.endpoints["otter"], self.group_id), json.dumps(launch_config), headers=headers(str(rcs.token)), pool=self.pool).addCallback(check_success, [204]).addCallback(lambda _: rcs))
def test_put_incoming_creates_a_blob_using_filesystem(self): self.prepare('filesystem') user_id, doc_id = self.user_id, uuid4().hex content = 'Hi' formatter = IncomingFormatter() incoming_endpoint = self.uri + '%s/%s' % (user_id, doc_id) yield treq.put(incoming_endpoint, BytesIO(content), persistent=False) db = self.state.open_database(user_id) consumer = DummyRequest(['']) yield db.read_blob(user_id, doc_id, consumer, namespace='MX') flags = yield db.get_flags(user_id, doc_id, namespace='MX') data = consumer.written.pop() expected_preamble = formatter.preamble(content, doc_id) expected_preamble = decode_preamble(expected_preamble, True) written_preamble, written_content = data.split() written_preamble = decode_preamble(written_preamble, True) self.assertEquals(expected_preamble, written_preamble) self.assertEquals(content, written_content) self.assertIn(Flags.PENDING, flags)
def sendToCollector(client, session, settings, useZmq=True): """deferred to the time in seconds it took to get a response from collector""" sendTime = time.time() msg = toCollectorJson(client, session, settings) if useZmq: d = sendToCollectorZmq(msg) else: d = treq.put(networking.collector.path('attrs'), data=msg) def onDone(result): dt = time.time() - sendTime if dt > .1: log.warn('sendToCollector request took %.1fms', dt * 1000) return dt d.addCallback(onDone) def onErr(err): log.warn('sendToCollector failed: %r', err) d.addErrback(onErr) return d
def makeRequest(self): if self.payload is None: log.debug("PUT None to %s - waiting", self.url) return h = {} if self.foafAgent: h['x-foaf-agent'] = self.foafAgent if self.nextCall and self.nextCall.active(): self.nextCall.cancel() self.nextCall = None self.lastErr = None log.debug("PUT %s payload=%s agent=%s", self.url, self.payload, self.foafAgent) if not self.mockOutput: self.currentRequest = treq.put(self.url, data=self.payload.encode('utf8'), headers=h, timeout=3) self.currentRequest.addCallback(self.onResponse).addErrback( self.onError) else: reactor.callLater(.2, self.onResponse, None) self.numRequests += 1
def set_launch_config(self, rcs, launch_config): """Changes the launch configuration used by the scaling group. :param TestResources rcs: The integration test resources instance. This provides useful information to complete the request, like which endpoint to use to make the API request. :param dict launch_config: The new launch configuration. :return: A :class:`Deferred` which, upon firing, alters the scaling group's launch configuration. If successful, the test resources provided will be returned. Otherwise, an exception will rise. """ return ( treq.put( "{0}/groups/{1}/launch".format(rcs.endpoints["otter"], self.group_id), json.dumps(launch_config), headers=headers(str(rcs.token)), pool=self.pool ).addCallback(check_success, [204]) .addCallback(lambda _: rcs) )
def replace_group_config(self, rcs, replacement_config): """ Replace the current group configuration with the provided config and update the stored groupConfiguration information. :param TestResources rcs: The integration test resources instance. This provides useful information to complete the request, like which endpoint to use to make the API request. :param dict replacement_config: A dictionary representation of the JSON description of a scaling group groupConfiguration Note that since this is a replacement config, all fields in the JSON descirptor are mandatory. """ def record_results(_, replacement_config): # Replace the stored value in the group_config self.group_config["groupConfiguration"] = replacement_config # Find the correct group from the list (by id) for g in rcs.groups: if g['group']['id'] == self.group_id: g['group']['groupConfiguration'] = replacement_config if verbosity > 0: print('Update group_config with {}'.format(replacement_config)) return rcs return ( treq.put( "{0}/groups/{1}/config".format(rcs.endpoints["otter"], self.group_id), json.dumps(replacement_config), headers=headers(str(rcs.token)), pool=self.pool, ) .addCallback(check_success, [204]) .addCallback(record_results, replacement_config) )
def sendLightCommand(self, args={}): logging.critical(args) data = args.get('lightCommand') url = self._api_url + 'id:' + str(args.get('lightID')) + '/state' treq.put(url, headers=self.header, data=data) return True
def deliver_using_incoming_api(url, user_uuid, token, data): auth = 'Token %s' % base64.b64encode('%s:%s' % (user_uuid, token)) uri = "%s/incoming/%s/%s?namespace=MX" % (url, user_uuid, uuid.uuid4().hex) return treq.put(uri, headers={'Authorization': auth}, data=BytesIO(data))
def put_mapping(_): print("Putting mapping {0}".format(event_mapping)) with open(event_mapping) as f: d = treq.put('http://localhost:9200/history/event/_mapping', f.read()) return d.addCallback(treq.json_content).addCallback(print)
def ensure(_): d = treq.put('http://localhost:9200/history') d.addCallback(treq.json_content) return d.addCallback(print)