def setUp(self): self.db = MySQLdb.connect( host=DB_config.MYSQL_HOST, # your host, usually localhost user=DB_config.MYSQL_USER, # your username passwd=DB_config.MYSQL_PASSWORD, # your password db=DB_config.MYSQL_DB_TEST) self.db.autocommit = "false" self.cur = self.db.cursor() self.controller = DBController(self.cur, self.db) self.username = '******' self.ride_id = 183 self.event_id = '5096273' self.request_id = 52 self.status = 'accepted' self.firstName = 'Wall' self.lastName = 'Street' self.email = '*****@*****.**' self.phoneNumber = '123-456-7890' self.password = "******" self.offerData = { "eventId": "5026583", "username": "******", "carModel": "BMW", "noOfSeats": "2", "startTime": "01:52", "address1": "3456 test lane", "address2": "", "city": "Charlotte", "state": "NC", "zipCode": "28243", "eventDate": "2019-11-18 01:52:40", }
def signup(): try: if request.method == "POST": print("Signup is hit!!") data = request.get_json(silent=True) print("Received data is", request.get_json()) cursor = mysql.connection.cursor() connection = mysql.connection controller = DBController(cursor, connection) response = controller.enterUser(data) if response == "Success": returnData = {"response": response} print("Sending resposne", returnData) return returnData elif response == "Email already present. Try a new email-id": returnData = {"response": response} print("Sending resposne", returnData) return {"response": response} else: print("error is:", response) (abort(500, {"response": response})) except Exception as e: print("error:", e) return e
def login(): # cur = mysql.connection.cursor() email = request.get_json()['email'] password = request.get_json()['password'].encode('utf-8') # result = "" cursor = mysql.connection.cursor() connection = mysql.connection controller = DBController(cursor, connection) rv = controller.Userlogin(email, password) # cur.execute("SELECT * FROM USER where email_id = '" + str(email) + "'") # rv = cur.fetchone() print(rv) if rv['PASSWORD'] == (hashlib.md5(password) ).hexdigest(): #hashing password and validating result = create_access_token( identity={ 'first_name': rv['FIRST_NAME'], 'last_name': rv['LAST_NAME'], 'username': rv['USERNAME'], 'email': rv['EMAIL_ID'] }) else: result = jsonify({"error": "Invalid username and password"}) return result
def getUsers(userid): cursor = mysql.connection.cursor() connection = mysql.connection controller = DBController(cursor, connection) response = controller.getUser(userid) print("db op", response) return response
def save_request(): data = request.get_json(silent=True) RideID = data.get("rideId") eventID = data.get("eventId") userID = data.get("userId") status = "pending" # print(RideID, eventID, userID, status) cursor = mysql.connection.cursor() connection = mysql.connection controller = DBController(cursor, connection) # controller.saveRequest("124", "1", "ageldartp", "pending") controller.saveRequest(RideID, eventID, userID, status) Response = app.response_class() return Response
def rides(eventId): # eventId = 4704993 # hardcoded as we have data for this few events only cursor = mysql.connection.cursor() connection = mysql.connection controller = DBController(cursor, connection) print("user id is: ", request.args.get("userId")) if "userId" in request.args and (request.args.get("userId") not in [ "", "None", "undefined" ]): # condition to check if userId is sent in request response = controller.getrides_username( eventId, request.args.get( "userId")) # sending offered rides data without his own rides else: response = controller.getrides_wo_username( eventId) # sending all offered rides data for any given event return response
def offerRide(): try: if request.method == "POST": data = request.get_json(silent=True) print("Received offer ride data is", request.get_json()) cursor = mysql.connection.cursor() connection = mysql.connection controller = DBController(cursor, connection) response = controller.saveOfferRide(data) if response == "Success": returnData = {"response": response} print("Sending resposne", returnData) return returnData else: print("error is:", response) (abort(500, {"response": response})) except Exception as e: print("error:", e) return e
def modifyRequest(): data = request.get_json(silent=True) cursor = mysql.connection.cursor() connection = mysql.connection controller = DBController(cursor, connection) if data["status"] in ["accepted", "declined"]: result = controller.updateRequest(data["requestId"], data["status"]) if result == data["status"]: return result else: response = json.loads( json.dumps({ "status": "error", "message": "Error while modifying request" })) (abort(500, {"response": response})) else: response = json.loads( json.dumps({ "status": "error", "message": "Invalid status. Please check" })) (abort(500, {"response": response}))
class TestDBController(unittest.TestCase): def setUp(self): self.db = MySQLdb.connect( host=DB_config.MYSQL_HOST, # your host, usually localhost user=DB_config.MYSQL_USER, # your username passwd=DB_config.MYSQL_PASSWORD, # your password db=DB_config.MYSQL_DB_TEST) self.db.autocommit = "false" self.cur = self.db.cursor() self.controller = DBController(self.cur, self.db) self.username = '******' self.ride_id = 183 self.event_id = '5096273' self.request_id = 52 self.status = 'accepted' self.firstName = 'Wall' self.lastName = 'Street' self.email = '*****@*****.**' self.phoneNumber = '123-456-7890' self.password = "******" self.offerData = { "eventId": "5026583", "username": "******", "carModel": "BMW", "noOfSeats": "2", "startTime": "01:52", "address1": "3456 test lane", "address2": "", "city": "Charlotte", "state": "NC", "zipCode": "28243", "eventDate": "2019-11-18 01:52:40", } def tearDown(self): self.cur.close() self.db.close() def test_getUserData(self): result = self.controller.getUser(self.username) self.cur.execute( """ SELECT RIDE_ID, EVENT_ID, USERNAME, STATUS FROM RIDES_REQUESTED WHERE RIDE_ID=%s AND EVENT_ID=%s AND USERNAME=%s""", (self.ride_id, self.event_id, self.username)) result = self.cur.fetchall()[0] self.assertEqual(result[0], self.ride_id) self.assertEqual(result[1], self.event_id) self.assertEqual(result[2], self.username) self.assertEqual(result[3], self.status) self.cur.execute( """ DELETE FROM RIDES_REQUESTED WHERE RIDE_ID=%s AND EVENT_ID=%s AND USERNAME=%s""", (self.ride_id, self.event_id, self.username), ) self.db.commit() def test_saveRequest(self): self.controller.saveRequest(self.ride_id, self.event_id, self.username, self.status) self.cur.execute( """ SELECT RIDE_ID, EVENT_ID, USERNAME, STATUS FROM RIDES_REQUESTED WHERE RIDE_ID=%s AND EVENT_ID=%s AND USERNAME=%s""", (self.ride_id, self.event_id, self.username)) result = self.cur.fetchall()[0] self.assertEqual(result[0], self.ride_id) self.assertEqual(result[1], self.event_id) self.assertEqual(result[2], self.username) self.assertEqual(result[3], self.status) #To check count of the table given details in where clause self.cur.execute( """ SELECT COUNT(*) FROM RIDES_REQUESTED WHERE RIDE_ID=%s AND EVENT_ID=%s AND USERNAME=%s""", (self.ride_id, self.event_id, self.username)) result = self.cur.fetchall()[0] self.assertEqual(result[0], 1) #To delete inserted data self.cur.execute( """ DELETE FROM RIDES_REQUESTED WHERE RIDE_ID=%s AND EVENT_ID=%s AND USERNAME=%s""", (self.ride_id, self.event_id, self.username), ) self.db.commit() def test_updateRequest(self): self.cur.execute( """ SELECT STATUS FROM RIDES_REQUESTED WHERE REQUEST_ID=%s""", (self.request_id)) before_status = self.cur.fetchall()[0][0] self.assertEqual(self.status, before_status) self.controller.saveRequest(self.request_id, self.status) self.cur.execute( """ SELECT REQUEST_ID, STATUS FROM RIDES_REQUESTED WHERE REQUEST_ID=%s AND STATUS=%s""", (self.request_id, self.status)) result = self.cur.fetchall()[0] self.assertEqual(result[0], self.request_id) self.assertEqual(result[1], self.status) self.assertEqual(before_status, result) #To check count of the table given details in where clause self.cur.execute( """ SELECT COUNT(*) FROM RIDES_REQUESTED WHERE REQUEST_ID=%s AND STATUS=%s """, (self.request_id, self.status)) result = self.cur.fetchall()[0] self.assertEqual(result[0], 1) #To delete inserted data self.cur.execute( """ DELETE FROM RIDES_REQUESTED WHERE REQUEST_ID=%s AND STATUS=%s""", (self.request_id, self.status), ) self.db.commit() def test_enterUserSuccess(self): data = { "firstName": self.firstName, "lastName": self.lastName, "password": self.password, "phoneNumber": self.phoneNumber, "email": self.email } print('data', data) response = self.controller.enterUser(data) print('response', response) self.assertEqual(response, "Success") self.cur.execute( """ SELECT * FROM USER WHERE FIRST_NAME=%s AND LAST_NAME=%s""", (self.firstName, self.lastName)) result = self.cur.fetchall()[0] print('result', result) self.assertEqual(result[2], self.firstName) self.assertEqual(result[3], self.lastName) self.assertEqual(result[4], self.phoneNumber) self.assertEqual(result[5], self.email) def test_enterUserFailure(self): data = { "firstName": self.firstName, "lastName": self.lastName, "password": self.password, "phoneNumber": self.phoneNumber, "email": self.email } print('data', data) response = self.controller.enterUser(data) print('response', response) self.assertEqual(response, "Email already present. Try a new email-id") #To delete inserted data self.cur.execute( """ DELETE FROM USER WHERE FIRST_NAME=%s AND LAST_NAME=%s""", (self.firstName, self.lastName)) self.db.commit() def test_saveOfferRide(self): response = self.controller.saveOfferRide(self.offerData) print(response) self.cur.execute( """SELECT * FROM RIDES_OFFERED WHERE EVENT_ID=%s AND USERNAME=%s""", (self.offerData["eventId"], self.offerData["username"]), ) result = self.cur.fetchall()[0] print("result is:", result) self.assertEqual(result[0], self.offerData["eventId"]) self.assertEqual(result[2], self.offerData["username"]) self.assertEqual(result[3], self.offerData["carModel"]) self.assertEqual(result[4], self.offerData["noOfSeats"]) self.assertEqual(result[6], self.offerData["address1"]) self.assertEqual(result[7], self.offerData["address2"]) self.assertEqual(result[8], self.offerData["city"]) self.assertEqual(result[9], self.offerData["state"]) self.assertEqual(result[10], self.offerData["zipCode"]) # To delete inserted data self.cur.execute( """ DELETE FROM RIDES_OFFERED WHERE EVENT_ID=%s AND USERNAME=%s""", (self.offerData["eventId"], self.offerData["username"]), ) self.db.commit()