def test_post(self): tr = CarCreateResource() res = tr.post() self.assertEqual(res.status_code, 201) tr1 = CarResource() fix = Car.from_json(res.data) tr1.delete(str(fix.id))
def test_post(self): payload = {'fix_id': '3221488228','user_id': '123456', 'name': 'suzuki'} res = requests.post(current_config.CAR_SERVICE_URL + current_config.CAR_SERVICE_PATH + current_config.CREATE_PATH, data=jsonpickle.encode(payload)) self.assertEqual(res.status_code, 201) car = Car.from_json(res.content) requests.delete(current_config.CAR_SERVICE_URL + current_config.CAR_SERVICE_PATH + "/%s" % str(car.id))
def test_delete_right(self): payload = {'fix_id': '123', 'user_id': '321', 'name': 'opel'} res = requests.post(current_config.CAR_SERVICE_URL + current_config.CAR_SERVICE_PATH + current_config.CREATE_PATH, data=jsonpickle.encode(payload)) car = Car.from_json(res.content) res = requests.delete(current_config.CAR_SERVICE_URL + current_config.CAR_SERVICE_PATH + "/%s" % car.id) self.assertEqual(res.status_code, 204)
def test_get_right(self): tr = CarResource() tcr = CarCreateResource() res = tcr.post() car = Car.from_json(res.data) res = tr.get(str(car.id)) self.assertEqual(res.status_code, 200) tr.delete(str(car.id))
def get(self, car_id): if self.exists(car_id): car = Cars.query.get(car_id) return Car(car_id=car.mongo_id, fix_id=car.fix_id, user_id=car.user_id, name=car.name) else: return None
def test_delete_right(self): payload = {'fix_id': '5c409fbe7162372270cf9460', 'name': 'tesla'} res = requests.post(current_config.GATEWAY_URL + current_config.GATEWAY_PATH + '/cars/add', data=jsonpickle.encode(payload)) car = Car.from_json(res.content) res = requests.delete(current_config.GATEWAY_URL + current_config.GATEWAY_PATH + '/cars/return/%s' % str(car.id)) self.assertEqual(res.status_code, 204)
def test_get_right(self): rep = CarRepository() car_id = rep.create(fix_id='1488322', user_id='3212313', name='opel') car1 = rep.get(car_id) car2 = Car(car_id=fields.ObjectId(car_id), fix_id='1488322', user_id='3212313', name='opel') self.assertEqual(car1, car2) rep.delete(car_id)
def read_paginated(self, page_number, page_size): cars = [] cars_paged = Cars.query.paginate(page=page_number, per_page=page_size) for car in cars_paged.items: cars.append( Car(car_id=car.mongo_id, fix_id=car.fix_id, user_id=car.user_id, name=car.name)) is_prev_num = (cars_paged.prev_num > 0) is_next_num = (cars_paged.next_num <= cars_paged.pages) return cars, is_prev_num, is_next_num return cars
def test_delete_right(self): sr = FixResource() ur = UserResource() scr = FixCreateResource() r = scr.post() fix = Fix.from_json(r.data) u = UserCreateResource() us = u.post() user = User.from_json(us.data) payload = {'fix_id': fix.id, 'name': 'suzuki', 'user_id': user.id} res = requests.post(current_config.GATEWAY_URL + current_config.GATEWAY_PATH + '/cars/add', data=jsonpickle.encode(payload)) #sr.delete(fix.id) car = Car.from_json(res.content) ret_prod = GatewayReturnCar() res = ret_prod.delete(car.id) self.assertEqual(res.status_code, 204) ur.delete(user.id) sr.delete(fix.id)
def delete(self, car_id): app.logger.info('Получен запрос на возврат машины') req = requests.session() for cookie in flask.request.cookies: req.cookies[cookie] = flask.request.cookies[cookie] cookies = req.cookies token = cookies['token'] response = requests.get(current_config.USER_SERVICE_URL + current_config.USER_SERVICE_PATH + "/token", data=jsonpickle.encode({'token': token})) result = flask.Response(status=response.status_code, headers=response.headers.items(), response=response.content) if result.status_code != 200: return result response = requests.get(current_config.CAR_SERVICE_URL + current_config.CAR_SERVICE_PATH + "/%s" % car_id) if response.status_code == 200: app.logger.info( 'Запрос на получение информации о машине с идентификатором %s успешно обработан' % car_id) else: app.logger.warning( 'Информация о машине с идентификатором %s не может быть получена' % car_id) result = flask.Response(status=response.status_code, headers=response.headers.items(), response=response.content) return result car = Car.from_json( response.content) #jsonpickle.decode(response.content) paybuy1 = {'car_id': car.id, 'status': 'return'} try: response = requests.patch( current_config.FIX_SERVICE_URL + current_config.FIX_SERVICE_PATH + "/%s" % car.fix_id, jsonpickle.encode(paybuy1)) if response.status_code == 201: app.logger.info( 'Возврат автомобиля для пользователя %s успешно произведен' % car.user_id) else: app.logger.warning( 'Освобождение места на сеансе не может быть завершено, добавление запроса в очередь' ) replay_request_queue.send_message( jsonpickle.encode( Request("CAR_RETURN", data={ "fix_id": car.fix_id, "payload": paybuy1 })), "car_return_handling_request") except: app.logger.warning( 'Освобождение места на сеансе не может быть завершено, добавление запроса в очередь' ) replay_request_queue.send_message( jsonpickle.encode( Request("CAR_RETURN", data={ "fix_id": car.fix_id, "payload": paybuy1 })), "car_return_handling_request") finally: paybuy2 = {'status': 'return'} response = requests.patch( current_config.USER_SERVICE_URL + current_config.USER_SERVICE_PATH + "/%s" % car.user_id, jsonpickle.encode(paybuy1)) if response.status_code == 201: app.logger.info('Освобождение места в мастерской завершен') else: app.logger.warning( 'Освобождение места в мастерской не может быть завершено') result = flask.Response(status=response.status_code, headers=response.headers.items(), response=response.content) paybuy1['status'] = 'fix' requests.patch( current_config.FIX_SERVICE_URL + current_config.FIX_SERVICE_PATH + "/", jsonpickle.encode(paybuy2)) return result response = requests.delete(current_config.CAR_SERVICE_URL + current_config.CAR_SERVICE_PATH + "/%s" % car_id) result = flask.Response(status=response.status_code, headers=response.headers.items(), response=response.content) if response.status_code == 204: app.logger.info('Машина с идентификатором %s успешно удален' % car_id) else: paybuy2['status'] = 'fix' requests.patch( current_config.USER_SERVICE_URL + current_config.USER_SERVICE_PATH + "/%s" % car.user_id, jsonpickle.encode(paybuy2)) paybuy1['status'] = 'fix' requests.patch( current_config.FIX_SERVICE_URL + current_config.FIX_SERVICE_PATH + "/", jsonpickle.encode(paybuy1)) app.logger.warning( 'Машина с идентификатором %s не может быть удален' % car_id) return result
def post(self): app.logger.info('Получен запрос на починку автомобиля') paybuy = jsonpickle.decode(flask.request.data) req = requests.session() for cookie in flask.request.cookies: req.cookies[cookie] = flask.request.cookies[cookie] cookies = req.cookies token = cookies['token'] response = requests.get(current_config.USER_SERVICE_URL + current_config.USER_SERVICE_PATH + "/token", data=jsonpickle.encode({'token': token})) result = flask.Response(status=response.status_code, headers=response.headers.items(), response=response.content) if result.status_code != 200: return result user = User.from_json(response.content) paybuy1 = { 'fix_id': str(paybuy["fix_id"]), 'user_id': user.id, 'name': str(paybuy["name"]) } response = requests.post( current_config.CAR_SERVICE_URL + current_config.CAR_SERVICE_PATH + current_config.CREATE_PATH, jsonpickle.encode(paybuy1)) car = Car.from_json(response.content) if response.status_code == 201: app.logger.info('Товар с идентификатором %s успешно создан' % str(car.id)) else: app.logger.warning('Товар не может быть создан') result = flask.Response(status=response.status_code, headers=response.headers.items(), response=response.content) return result result = flask.Response(status=response.status_code, headers=response.headers.items(), response=response.content) paybuy2 = {'car_id': str(car.id), 'status': 'fix'} response = requests.patch( current_config.FIX_SERVICE_URL + current_config.FIX_SERVICE_PATH + "/%s" % paybuy["fix_id"], jsonpickle.encode(paybuy2)) if response.status_code == 201: app.logger.error('Заявки в мастерскую %s оформлена' % paybuy["fix_id"]) else: result = flask.Response(status=response.status_code, headers=response.headers.items(), response=response.content) app.logger.info('Магазин %s не существуета' % (paybuy["fix_id"])) requests.delete(current_config.CAR_SERVICE_URL + current_config.CAR_SERVICE_PATH + "/" + paybuy2['car_id']) return result paybuy3 = {'status': 'fix'} response = requests.patch( current_config.USER_SERVICE_URL + current_config.USER_SERVICE_PATH + "/%s" % paybuy1["user_id"], jsonpickle.encode(paybuy3)) if response.status_code == 201: app.logger.info('Услуга успешно заказана') else: result = flask.Response(status=response.status_code, headers=response.headers.items(), response=response.content) paybuy2['status'] = 'return' requests.patch( current_config.FIX_SERVICE_URL + current_config.FIX_SERVICE_PATH + "/%s" % paybuy["fix_id"], jsonpickle.encode(paybuy1)) requests.delete(current_config.CAR_SERVICE_URL + current_config.CAR_SERVICE_PATH + "/" + paybuy2['car_id']) app.logger.warning('Услуга не может быть заказана') return result