How to use decode_request method in autotest

Best Python code snippet using autotest_python

event.py

Source:event.py Github

copy

Full Screen

1import json2from flask import request, Response3from sqlalchemy.exc import SQLAlchemyError4from back.auth import (cso_authentication_authorization,5 review_authentication_authorization,6 scso_authentication_authorization,7 managers_authentication_authorization8 )9from back.config import db, app10from back.models import (11 Application,12 EventCreation,13 PreferencesEvent,14 Roles,15 Status,16 User)17from back.utils import generate_uuid18# parser_event = {19# "client_name": fields.String,20# "event_type": fields.String,21# "from_date": fields.String,22# "to_date": fields.String,23# "expected_number_attendees": fields.Integer,24# "preferences": fields.String,25# "feedback_fm": fields.String26# }27@app.route('/event_application_creation', methods=["POST"])28@scso_authentication_authorization29def event_application_creation(*args):30 decode_request = json.loads(request.data.decode())31 project_reference = generate_uuid()32 application_to_create = Application(33 project_reference=project_reference,34 client_record_number=decode_request.get("record_number"),35 client_name=decode_request.get("client_name"),36 event_type=decode_request.get("event_type"),37 description=decode_request.get("description"),38 from_date=decode_request.get("from_date"),39 to_date=decode_request.get("to_date"),40 expected_number_attendees=int(decode_request.get("expected_number_attendees")),41 planned_budget=int(decode_request.get("planned_budget")),42 decorations=decode_request.get("decorations"),43 food_drinks=decode_request.get("food_drinks"),44 filming_photos=decode_request.get("filming_photos"),45 music=decode_request.get("music"),46 posters_art_work=decode_request.get("posters_art_work"),47 computer_related_issues=decode_request.get("computer_related_issues"),48 other_needs=decode_request.get("other_needs")49 )50 try:51 db.session.add(application_to_create)52 db.session.commit()53 return {54 "project_reference": project_reference55 }, 20056 except SQLAlchemyError as e:57 return {58 "error": e.__dict__["orig"]59 }, 40060@app.route("/event_application_retrieve/", methods=['GET'])61@managers_authentication_authorization62def application_retrieve(*args):63 # either the use indicate the application id or we retrieve all of them64 decoded_request = json.loads(request.data.decode())65 if decoded_request.get("application_id"):66 application_needed = Application.query.filter(67 Application.project_reference == decoded_request.get("application_id")68 ).first()69 if not application_needed:70 return {71 "error": "Application not found (wrong id)"72 }, 40073 return application_needed.to_dict(), 20074 else:75 applications = Application.query.all()76 return {"applications": [application.to_dict() for application in applications]}, 20077@app.route("/event_creation/", methods=["POST"])78@cso_authentication_authorization79def event_creation(*args):80 decode_request = json.loads(request.data.decode())81 event_create = EventCreation(82 record_number=generate_uuid(),83 client_name=decode_request.get("client_name"),84 event_type=decode_request.get("event_type"),85 from_date=decode_request.get("from_date"),86 to_date=decode_request.get("to_date"),87 expected_number_attendees=int(decode_request.get("expected_number_attendees")),88 preferences=PreferencesEvent.__members__[decode_request.get("preferences")]89 )90 try:91 db.session.add(event_create)92 db.session.commit()93 return Response(status=200)94 except SQLAlchemyError as e:95 return {96 "error": e.__dict__["orig"]97 }, 40098# @marshal_with(parser_event)99@app.route("/review_event_creation/", methods=["PUT", "GET", "DELETE"])100@review_authentication_authorization101def review_event_creation(user: User):102 if request.method == "GET":103 if user.role == Roles.ADMIN:104 events = EventCreation.query.all()105 return {"events": [each_event.to_dict() for each_event in events]}, 200106 status_expected = eval("Status.pending_{}".format(user.role.name))107 current_event_request_related = EventCreation.query.filter(108 EventCreation.status == status_expected109 ).all()110 return {"events": [each_event.to_dict() for each_event in current_event_request_related]}, 200111 elif request.method == "PUT":112 decoded_request = json.loads(request.data.decode())113 # in this request user will be sending record_number (done automatically ;) )114 current_event_request_related = EventCreation.query.filter(115 EventCreation.record_number == decoded_request.get("record_number"),116 EventCreation.status == eval("Status.pending_{}".format(user.role.name))117 ).first()118 if not current_event_request_related:119 return {120 "error": "Request can't be fulfilled"121 }, 400122 if decoded_request.get("feedback"):123 current_event_request_related.feedback_fm = decoded_request.get("feedback", "No feedback")124 current_event_request_related.status = eval("Status.pending_{}.next()".format(user.role.name))125 if decoded_request.get("status"):126 current_event_request_related.status = eval("Status.pending_{}.next()".format(user.role.name)) \127 if decoded_request.get("status").lower() != "dismissed" else Status.dismissed128 try:129 db.session.commit()130 return Response(status=200)131 except SQLAlchemyError as e:132 return {133 "error": e.__dict__["orig"]134 }, 400135 else:136 decoded_request = json.loads(request.data.decode())137 if user.role == Roles.FM:138 return Response(status=401)139 event_request_to_delete = EventCreation.query.filter(140 EventCreation.record_number == decoded_request.get("record_number"),141 EventCreation.status == Status.dismissed142 ).first()143 if not event_request_to_delete:144 return {145 "error": "request can't be fulfilled"146 }, 400147 try:148 db.session.delete(event_request_to_delete)149 db.session.commit()150 return Response(status=200)151 except SQLAlchemyError as e:152 return {153 "error": e.__dict__["orig"]...

Full Screen

Full Screen

test_tools.py

Source:test_tools.py Github

copy

Full Screen

...22 def test_form_type(self):23 """Try a normal form-urlencoded request."""24 builder = EnvironBuilder(method='POST', data={'foo': 'bar'})25 request = Request(builder.get_environ())26 data = decode_request(request)27 assert data['foo'] == 'bar'28 def test_json_type(self):29 """Try a normal JSON request."""30 builder = EnvironBuilder(31 method='POST', content_type='application/json',32 data='{"foo": "bar"}')33 request = Request(builder.get_environ())34 data = decode_request(request)35 assert data['foo'] == 'bar'36 def test_content_type_with_options(self):37 """Content-Type can also have options."""38 builder = EnvironBuilder(39 method='POST',40 content_type='application/x-www-form-urlencoded; charset=utf-8')41 request = Request(builder.get_environ())42 # Must populate form field manually with non-default content-type.43 request.form = {'foo': 'bar'}44 data = decode_request(request)45 assert data['foo'] == 'bar'46 def test_form_type_is_default(self):47 """Assume form-urlencoded if blank in the request."""48 builder = EnvironBuilder(method='POST', content_type='')49 request = Request(builder.get_environ())50 # Must populate form field manually with non-default content-type.51 request.form = {'foo': 'bar'}52 data = decode_request(request)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful