Example #1
0
def test_delete_product_if_product_not_found_raises_not_found_error(
        _product_exists_and_belongs_to_user_mock,
        delete_one_by_id_mock, user_data, product_data):
    _product_exists_and_belongs_to_user_mock.side_effect = NotFoundError("not found")
    delete_one_by_id_mock.return_value = False
    with pytest.raises(NotFoundError):
        ProductsService.delete_product(user_data.uid, product_data.product_id)
 def _check_product_exists_and_belongs_to_user(cls, uid, product_id):
     """Checks if product exists and belongs to user."""
     product = ProductMapper.get_by_id(product_id)
     if product is None:
         raise NotFoundError("Product does not exist.")
     if product.seller != uid:
         raise ForbiddenError("User can only delete his own products")
     return True
def test_delete_product_if_not_found_404_response(
        _product_exists_and_belongs_to_user_mock, client, user_data,
        product_data):
    _product_exists_and_belongs_to_user_mock.side_effect = NotFoundError(
        "Product does not exist.")
    response = client.delete('/products/5bbe37a1e3c00f593839d19e',
                             headers=product_data.valid_token_header())
    assert response.status_code == 404
Example #4
0
 def estimate_shipping_cost(cls, uid, estimate_data):
     """Estimates order shipping cost"""
     estimate_info = EstimateShippingInputDataSchema().load(estimate_data)
     product = ProductMapper.get_by_id(estimate_info.product_id)
     if product is None:
         raise NotFoundError("Product not found.")
     buyer = cls._get_user(uid)
     shipping_cost = SharedServer().get_delivery_estimate(product, estimate_info.units, buyer)
     return shipping_cost
 def get_products(cls, filters):
     """Get products services: returns all the
      products that match with filters parameters"""
     filters_schema = ProductsQueryFiltersSchema()
     products = ProductMapper.query(filters_schema.load(filters))
     if not products:
         raise NotFoundError("No product matches the query.")
     products_dict_list = []
     for product in products:
         products_dict_list.append(cls.schema.dump(product))
     return products_dict_list
 def update_status(cls, order):
     """Updates the status and last_status_update of an order"""
     order = OrderMapper.find_one_and_update(
         {'tracking_number': order.tracking_number}, {
             '$set': {
                 'status': order.status,
                 'last_status_update': order.last_status_update
             }
         })
     if order is None:
         raise NotFoundError(
             "Cannot update order status because order was not found.")
 def login(cls, firebase_token):
     """Login services: returns a server access token after authenticating with firebase_token"""
     firebase = Firebase(json.loads(os.environ['FIREBASE_CONFIG']))
     uid = firebase.decode_token(firebase_token)
     user = UserMapper.find_one_and_update\
         ({'uid': uid}, {'$set': {'last_login': str(datetime.now())}})
     if user is None:
         raise NotFoundError("User not found.")
     access_token = create_access_token(identity=uid,
                                        fresh=True,
                                        expires_delta=False)
     return access_token
 def _add_payment_methods(cls, product):
     aux = []
     payment_methods = SharedServer().get_payment_methods()
     for product_payment_method_name in product.payment_methods:
         payment_method_found = False
         for payment_method in payment_methods:
             if product_payment_method_name == payment_method.name:
                 aux.append(payment_method)
                 payment_method_found = True
                 break
         if not payment_method_found:
             raise NotFoundError(product_payment_method_name +
                                 " payment method is not available.")
     return aux
 def add_question(cls, question_dict, product_id, uid):
     """Add question services: adds a question by the user with id uid to the product
     with id product_id. Returns the product with the added question."""
     question_schema = QuestionSchema()
     question = question_schema.load(question_dict)
     question.id = bson.ObjectId()
     question.uid = uid
     question.datetime = str(datetime.now())
     product = ProductMapper.find_one_and_update(
         {'_id': bson.ObjectId(product_id)},
         {'$push': {
             'questions': question_schema.dump(question)
         }})
     if product is None:
         raise NotFoundError("Product not found.")
     return cls.schema.dump(product)
 def modify_category(cls, category_id, category_dict):
     """Modifies a product category"""
     category_schema = CategorySchema()
     category = category_schema.load(category_dict)
     if CategoryMapper.exists({
             '_id': {
                 '$ne': bson.ObjectId(category_id)
             },
             'name': category.name
     }):
         raise DataExistsError("Product category already exists.")
     ret = CategoryMapper.modify({"_id": bson.ObjectId(category_id)},
                                 category)
     if ret is None:
         raise NotFoundError("Category does not exist.")
     return ret
 def add_answer(cls, answer_dict, product_id, question_id, uid):
     """Add answer services: adds an answer to
     question_id question in product_id product"""
     answer_schema = AnswerSchema()
     answer = answer_schema.load(answer_dict)
     cls._check_product_exists_and_belongs_to_user(uid, product_id)
     answer.id = bson.ObjectId()
     answer.uid = uid
     answer.datetime = str(datetime.now())
     product = ProductMapper.find_one_and_update(
         {
             '_id': bson.ObjectId(product_id),
             "questions._id": question_id
         }, {'$set': {
             "questions.$.answer": answer_schema.dump(answer)
         }})
     if product is None:
         raise NotFoundError("Product not found.")
     return cls.schema.dump(product)
Example #12
0
 def _get_user(cls, user_uid):
     buyer = UserMapper.get_one({'uid': user_uid})
     if buyer is None:
         raise NotFoundError("User not found.")
     return buyer
Example #13
0
 def _get_product(cls, product_id):
     product = ProductMapper.get_by_id(product_id)
     if product is None:
         raise NotFoundError("Product not found.")
     return product
Example #14
0
 def _get_order(cls, tracking_number):
     order = OrderMapper.get_one({'tracking_number': tracking_number})
     if order is None:
         raise NotFoundError("Order not found.")
     return order
 def delete_category(cls, category_id):
     """Deletes a product category"""
     if not CategoryMapper.delete_one_by_id(category_id):
         raise NotFoundError("Category not found.")
 def get_profile(cls, uid):
     """Get profile services: returns user profile of user with user id uid"""
     user = UserMapper.get_one({"uid": uid})
     if user is None:
         raise NotFoundError("User not found.")
     return cls.schema.dump(user)
 def delete_product(cls, uid, product_id):
     """Delete product service: deletes product product_id from user uid."""
     cls._check_product_exists_and_belongs_to_user(uid, product_id)
     if not ProductMapper.delete_one_by_id(product_id):
         raise NotFoundError("Product not found.")
     return True
 def get_product_by_id(cls, product_id):
     """Get product by id service"""
     product = ProductMapper.get_by_id(product_id)
     if product is None:
         raise NotFoundError("Product not found.")
     return cls.schema.dump(product)