def test_change_empty_mapping_list(self): """Test create mapping list proposal to empty.""" with factories.single_commit(): category = factories.ControlCategoryFactory() control = factories.ControlFactory(categories=[category]) data = control.log_json() category_id = category.id control_id = control.id data["categories"] = [] resp = self.api.post( all_models.Proposal, {"proposal": { "instance": { "id": control.id, "type": control.type, }, # "content": {"123": 123}, "full_instance_content": data, "agenda": "update categories", "context": None, }}) self.assertEqual(201, resp.status_code) control = all_models.Control.query.get(control_id) category = all_models.ControlCategory.query.get(category_id) self.assertEqual(1, len(control.proposals)) self.assertIn("mapping_list_fields", control.proposals[0].content) fields = control.proposals[0].content["mapping_list_fields"] self.assertIn("categories", fields) self.assertEqual( {"added": [], "deleted": [json.loads(utils.as_json(category.log_json()))]}, fields["categories"]) self.assertEqual(1, len(control.comments))
def test_change_empty_mapping_list(self): """Test create mapping list proposal to empty.""" with factories.single_commit(): category = factories.ControlCategoryFactory() control = factories.ControlFactory(categories=[category]) data = control.log_json() category_id = category.id control_id = control.id data["categories"] = [] self.create_proposal(control, full_instance_content=data, agenda="update categories", context=None) control = all_models.Control.query.get(control_id) category = all_models.ControlCategory.query.get(category_id) self.assertEqual(1, len(control.proposals)) self.assertIn("mapping_list_fields", control.proposals[0].content) fields = control.proposals[0].content["mapping_list_fields"] self.assertIn("categories", fields) self.assertEqual( { "added": [], "deleted": [json.loads(utils.as_json(category.log_json()))] }, fields["categories"]) self.assertEqual(1, len(control.comments))
def test_change_mapping(self): """Test create mapping proposal.""" setuped_kind, update_kind = all_models.Option.query.filter( all_models.Option.role == "control_kind")[:2] control = factories.ControlFactory(title="1", kind=setuped_kind) control_id = control.id data = control.log_json() update_kind_json = update_kind.log_json() data["kind"] = update_kind_json resp = self.api.post( all_models.Proposal, { "proposal": { "instance": { "id": control.id, "type": control.type, }, # "content": {"123": 123}, "full_instance_content": data, "agenda": "update kind", "context": None, } }) self.assertEqual(201, resp.status_code) control = all_models.Control.query.get(control_id) self.assertEqual(1, len(control.proposals)) self.assertIn("mapping_fields", control.proposals[0].content) self.assertIn("kind", control.proposals[0].content["mapping_fields"]) self.assertEqual( json.loads(utils.as_json(update_kind_json)), control.proposals[0].content["mapping_fields"]["kind"]) self.assertEqual(1, len(control.comments))
def _post(self, data): """Perform a post request.""" return self.client.post( "/api/products", content_type='application/json', data=utils.as_json(data), headers={'X-Requested-By': 'Unit Tests'}, )
def process_bind_param(self, value, dialect): if value is None or isinstance(value, basestring): pass else: value = utils.as_json(value) if len(value.encode('utf-8')) > self.MAX_TEXT_LENGTH: raise exceptions.ValidationError("Log record content too long") return value
def bad_request(err): """Custom handler for BadRequest error Returns JSON object with error code and message in response body. """ resp = {"code": 400, "message": err.description} headers = [('Content-Type', 'application/json'), ] return current_app.make_response((as_json(resp), 400, headers),)
def make_error_response(err, force_json=False): """Compose the response based on the request content_type""" if request.content_type == 'application/json' or force_json: resp = {"code": err.code, "message": err.description} headers = [ ('Content-Type', 'application/json'), ] return current_app.make_response((as_json(resp), err.code, headers), ) return err.get_response()
def make_error_response(err, err_code, force_json=False): """Compose the response based on the request content_type""" if request.content_type == 'application/json' or force_json: resp = {"code": err_code, "message": getattr(err, "description", err)} headers = [('Content-Type', 'application/json'), ] return current_app.make_response((as_json(resp), err_code, headers), ) if hasattr(err, "get_response"): return err.get_response() return err
def bad_request(err): """Custom handler for BadRequest error Returns JSON object with error code and message in response body. """ resp = {"code": 400, "message": err.description} headers = [ ('Content-Type', 'application/json'), ] return current_app.make_response((as_json(resp), 400, headers), )
def _put(self, url, data, extra_headers=None): """Perform a put request.""" headers = {'X-Requested-By': 'Unit Tests'} headers.update(extra_headers) return self.client.put( url, content_type='application/json', data=utils.as_json(data), headers=headers, )
def json_success_response(response_object, last_modified=None, status=200): """Build a 200-response with metadata headers.""" headers = [ ('Etag', etag(response_object)), ('Content-Type', 'application/json'), ] if last_modified is not None: headers.append(('Last-Modified', http_timestamp(last_modified))) return current_app.make_response( (as_json(response_object), status, headers), )
def _handle_if_empty(self): """Check if revision is empty and update is_empty flag if true.""" # Check if new revision contains any changes in resource state. Revisions # created with "created" or "deleted" action are not considered empty. if self in db.session.new and self.action == u"modified": obj = referenced_objects.get(self.resource_type, self.resource_id) # Content serialization and deserialization is needed since content of # prev revision stored in DB was serialized before storing and due to # this couldn't be correctly compared to content of revision in hands. content = json.loads(utils.as_json(self.content)) self.is_empty = bool( obj and not revisions_diff.changes_present(obj, content))
def run_bulk_verify(): """Call bulk verify job""" data = flask.request.json parameters = {"data": data} bg_task = background_task.create_task(name="bulk_verify", url=flask.url_for( bulk_verify.__name__), queued_callback=bulk_verify, parameters=parameters) db.session.commit() return bg_task.make_response( app.make_response( (utils.as_json(bg_task), 200, [('Content-Type', "text/json")])))
def _normalize_content(content): """Prepare content to changes_present. This functionality is needed to convert new revision data into data of the same type as another revision. Args: content: Content of new revision. Returns: Prepared content to changes_present. """ new_content = json.loads(utils.as_json(content)) return new_content
def make_response(errors): """Create response with provided body and status. Args: errors: List with errors to return in response. Returns: Created response. """ error_list = [] for obj, err in errors: error_list.append((obj.type, obj.id, err)) return app.make_response((utils.as_json({"errors": error_list}), 200, [("Content-Type", "application/json")]))
def make_response(errors): """Create response with provided body and status. Args: errors: List with errors to return in response. Returns: Created response. """ error_list = [] for obj, err in errors: error_list.append((obj.type, obj.id, err)) return app.make_response(( utils.as_json({"errors": error_list}), 200, [("Content-Type", "application/json")] ))
def put_resource(context, url, resource): headers={ 'Content-Type': 'application/json', 'If-Match': resource.response.headers['Etag'], 'If-Unmodified-Since': resource.response.headers['Last-Modified'], 'X-Requested-By': 'Reciprocity Behave Tests', } if hasattr(context, 'current_user_json'): headers['X-ggrc-user'] = context.current_user_json data = as_json(resource.value) response = requests.put( context.base_url+url, data=data, headers=headers, cookies=getattr(context, 'cookies', {}) ) context.cookies = response.cookies return response
def modify_object(self, obj, data=None): """Make a put call for a given object. Args: obj (db.Model): Object that should be modified, and it can also contain the changes that should be made. data (dict): dictionary containing the changed values for the object. This is not mandatory if the object itself has the given changes. Returns: response, db.Model: The put response from the server and the modified object if the put request was successful. """ if data is None: data = {} obj_dict = builder.json.publish(obj) builder.json.publish_representation(obj_dict) obj_dict = flask.json.loads(utils.as_json(obj_dict)) obj_dict.update(data) data = {obj._inflector.table_singular: obj_dict} return self.put(obj, data)
def test_change_mapping(self): """Test create mapping proposal.""" setuped_kind, update_kind = all_models.Option.query.filter( all_models.Option.role == "control_kind")[:2] control = factories.ControlFactory(title="1", kind=setuped_kind) control_id = control.id data = control.log_json() update_kind_json = update_kind.log_json() data["kind"] = update_kind_json self.create_proposal(control, full_instance_content=data, agenda="update kind", context=None) control = all_models.Control.query.get(control_id) self.assertEqual(1, len(control.proposals)) self.assertIn("mapping_fields", control.proposals[0].content) self.assertIn("kind", control.proposals[0].content["mapping_fields"]) self.assertEqual( json.loads(utils.as_json(update_kind_json)), control.proposals[0].content["mapping_fields"]["kind"]) self.assertEqual(1, len(control.comments))
def post_example(context, resource_type, example, url=None, rbac_context=None): if rbac_context is None: rbac_context = example.get("context", None) # Find any required FactoryStubMarker and recurse to POST/create # required resources for attr, value in example.items(): if isinstance(value, FactoryStubMarker): value_resource_type = value.class_.__name__ # If the resource has subclasses, then it is abstract, so use one of # its subclasses value_resource_subtypes = [ manager.class_.__name__ for manager in value.class_._sa_class_manager.subclass_managers(True)] if len(value_resource_subtypes) > 0: value_resource_type = value_resource_subtypes[0] value_resource_factory = factory_for(value_resource_type) value_resource = value_resource_factory() value_response = post_example( context, value_resource_type, value_resource, rbac_context=rbac_context) # If not a successful creation, then it didn't create the object we need if value_response.status_code != 201: return value_response # Get the value of the first/only (key,value) pair # (and just assume the root key is correct) value_data = value_response.json().items()[0][1] example[attr] = { 'href': value_data["selfLink"], 'id': value_data["id"], 'type': value_resource_type } # Assign overriding `context`, if specified if rbac_context is not None: example["context"] = rbac_context data = as_json( {get_resource_table_singular(resource_type): example}) return post_to_endpoint(context, resource_type, data, url)
def run_bulk_complete(): """Call bulk complete job""" data = flask.request.json parameters = {"data": data} if _detect_files(data): try: gdrive.get_http_auth() except gdrive.GdriveUnauthorized: response = app.make_response( ("auth", 401, [("Content-Type", "text/html")])) return response parameters["credentials"] = flask.session.get('credentials') bg_task = background_task.create_task(name="bulk_complete", url=flask.url_for( bulk_complete.__name__), queued_callback=bulk_complete, parameters=parameters) db.session.commit() return bg_task.make_response( app.make_response( (utils.as_json(bg_task), 200, [('Content-Type', "text/json")])))
def process_bind_param(self, value, dialect): if value is None or isinstance(value, basestring): pass else: value = as_json(value) return value
def as_json(cls, obj, **kwargs): return as_json(obj, **kwargs)
def put(self, url, data, headers=None): """Make PUT request to provided url.""" return self.make_request(self.client.put, url, data=utils.as_json(data), custom_headers=headers)