def test_find_all(self):
        user = self.db.session.query(User).get(4)
        AuthenticationContext.init_context(user)

        brands = self.service.find_all()

        self.assertEqual(8, len(brands))
        self.assertEqual({100, 102, 103, 106, 107, 108, 109, 110}, set([b.id for b in brands]))
    def test_find_all(self):
        user = self.db.session.query(User).get(4)
        AuthenticationContext.init_context(user)

        product_types = self.service.find_all()

        self.assertEqual(11, len(product_types))
        self.assertNotIn(3, [p.id for p in product_types])
    def test_find_all(self):
        user = self.db.session.query(User).get(5)
        AuthenticationContext.init_context(user)

        purchase_lists = self.service.find_all()

        self.assertEqual(2, len(purchase_lists))
        self.assertEqual(2, purchase_lists[0].id)
        self.assertEqual(6, purchase_lists[1].id)
    def test_update_name_user_by_dto(self):
        dto = {"id": 2, "name": "new_name", "login": "******", "password": None}

        user = self.service.find_by_id(2)
        AuthenticationContext.init_context(user)

        self.service.update_from_dto(dto)

        user = self.service.find_by_id(2)

        self.assertEqual("new_name", user.name)
Esempio n. 5
0
    def setUp(self):
        Base.metadata.create_all(self.db.get_engine())
        default_user = BaseTest.__get_default_user()

        self.db.session.add(default_user.user_group)
        self.db.session.add(default_user)
        self.initial_load()
        self.db.session.commit()

        self.token = "JWT " + self.jwt.jwt_encode_callback(
            default_user).decode()

        AuthenticationContext.init_context(default_user)
Esempio n. 6
0
    def update_from_dto(self, dto: dict):
        self.mapper.validate_dto_to_update(dto)

        product = self.product_dao.find_by_id(dto.get("id"), AuthenticationContext.get_current_user())

        if not product:
            raise ObjectNotFoundException(ErrorCode.PRODUCT_TO_UPDATE_NOT_FOUND,
                                          Product.__name__, {"id": dto.get("id")})

        product = self.mapper.to_object(dto)
        ProductService.fill_to_create(product, datetime.utcnow(), AuthenticationContext.get_current_user())

        self.product_dao.update(product)

        return product
Esempio n. 7
0
    def to_object(self,
                  dto: dict,
                  not_update: bool = False) -> Optional[Brand]:
        if not dto:
            return None

        brand = None
        found = False
        if dto.get("id"):
            brand = self.brand_dao.find_by_id(
                dto.get("id"), AuthenticationContext.get_current_user())
            found = brand is not None

        if not found:
            brand = Brand()
            brand.id = dto.get("id")
            brand.created_at = dto.get("created_at")
            brand.is_private = dto.get("is_private")
            brand.created_by = self.user_mapper.to_object(
                dto.get("created_by"), not_update=True)

        if not found or not not_update:
            brand.name = dto.get("name")

        return brand
Esempio n. 8
0
    def to_object(self,
                  dto: dict,
                  not_update: bool = False) -> Optional[object]:
        if not dto:
            return None

        product_type = None
        found = False
        if dto.get("id"):
            product_type = self.product_type_dao.find_by_id(
                dto.get("id"), AuthenticationContext.get_current_user())
            found = product_type is not None

        if not found:
            product_type = ProductType()
            product_type.id = dto.get("id")
            product_type.created_by = self.user_mapper.to_object(
                dto.get("created_by"))
            product_type.created_at = dto.get("created_at")
            product_type.is_private = dto.get("is_private")

        if not found or not not_update:
            product_type.name = dto.get("name")
            product_type.parent_product_type = self.to_object(
                dto.get("parent_product_type"), not_update=True)
            product_type.description = dto.get("description")

        return product_type
Esempio n. 9
0
    def to_object(self,
                  dto: dict,
                  not_update: bool = False) -> Optional[Product]:
        if not dto:
            return None

        product = None
        found = False
        if dto.get("id"):
            product = self.product_dao.find_by_id(
                dto.get("id"), AuthenticationContext.get_current_user())
            found = product is not None

        if not found:
            product = Product()
            product.id = dto.get("id")
            product.created_at = dto.get("created_at")
            product.created_by = self.user_mapper.to_object(
                dto.get("created_by"), not_update=True)
            product.is_private = dto.get("is_private")

        if not found or not not_update:
            product.name = dto.get("name")
            product.image_url = dto.get("image_url")
            product.brand = self.brand_mapper.to_object(dto.get("brand"),
                                                        not_update=True)
            product.product_type = self.product_type_mapper.to_object(
                dto.get("product_type"), not_update=True)

        return product
Esempio n. 10
0
    def delete_by_id(self, product_id: int):
        product = self.product_dao.find_by_id(product_id, AuthenticationContext.get_current_user())

        if not product:
            raise ObjectNotFoundException(ErrorCode.PRODUCT_TO_DELETE_NOT_FOUND, Product.__name__, {"id": product_id})

        self.product_dao.delete(product)
Esempio n. 11
0
    def to_object(self,
                  dto: dict,
                  not_update: bool = False) -> Optional[Purchase]:
        if dto is None:
            return None

        purchase = None
        found = False
        if dto.get("id"):
            purchase = self.purchase_dao.find_by_id(
                dto.get("id"), AuthenticationContext.get_current_user())
            found = purchase is not None

        if not found:
            purchase = Purchase()
            purchase.id = dto.get("id")
            purchase.created_at = dto.get("created_at")
            purchase.created_by = self.user_mapper.to_object(
                dto.get("created_by"), not_update=True)

        if not found or not not_update:
            purchase.name = dto.get("name")
            purchase.purchase_list = self.purchase_list_mapper.to_object(
                dto.get("purchase_list"), not_update=True)
            self.__products_to_obj(dto.get("products"), purchase)

        return purchase
    def create(self, product_type: ProductType):
        ProductTypeService.fill_to_create(
            product_type, datetime.utcnow(),
            AuthenticationContext.get_current_user())

        self.product_type_dao.add(product_type)

        return product_type
Esempio n. 13
0
    def test_update_user_by_dto_invalid_id(self):
        dto = {
            "id": 290,
            "name": "new_name",
            "login": "******",
            "password": "******"
        }

        user = User()
        user.id = 290

        AuthenticationContext.init_context(user)

        with self.assertRaises(ObjectNotFoundException) as exception:
            self.service.update_from_dto(dto)

        self.assertEqual({"id": 290}, exception.exception.entity_identifier)
        self.assertEqual(User.__name__, exception.exception.entity_name)
Esempio n. 14
0
    def find_or_create_from_dto(self, dto: dict) -> Optional[Product]:
        if not dto.get("id"):
            return None

        product = self.product_dao.find_by_id(dto.get("id"), AuthenticationContext.get_current_user())

        if not product:
            product = self.create_from_dto(dto)

        return product
Esempio n. 15
0
    def test_update_pass_and_name_user_by_dto(self):
        dto = {
            "id": 2,
            "name": "new_name",
            "login": "******",
            "password": "******"
        }

        user = self.service.find_by_id(2)
        AuthenticationContext.init_context(user)

        self.service.update_from_dto(dto)

        user = self.service.find_by_id(2)

        self.assertEqual("new_name", user.name)
        self.assertTrue(
            PasswordEncryption.check_encrypted_password(
                "new_pass", user.password))
    def create_from_dto(self, dto: dict):
        self.mapper.validate_dto_to_insert(dto)

        purchase_list = self.mapper.to_object(dto)

        PurchaseListService.fill_to_create(
            purchase_list, datetime.utcnow(),
            AuthenticationContext.get_current_user())
        self.purchase_list_dao.add(purchase_list)

        return purchase_list
Esempio n. 17
0
    def update_from_dto(self, dto: dict):
        self.mapper.validate_dto_to_update(dto)

        brand = self.brand_dao.find_by_id(dto.get("id"), AuthenticationContext.get_current_user())

        if not brand:
            raise ObjectNotFoundException(ErrorCode.BRAND_TO_UPDATE_NOT_FOUND,  Brand.__name__, {"id": dto.get("id")})

        brand = self.mapper.to_object(dto)

        self.brand_dao.update(brand)

        return brand
    def update_from_dto(self, dto: dict):
        self.mapper.validate_dto_to_update(dto)

        purchase_list = self.find_by_id(dto.get("id"))

        if not purchase_list:
            raise ObjectNotFoundException(
                ErrorCode.PURCHASE_LIST_TO_UPDATE_NOT_FOUND,
                PurchaseList.__name__, {"id": dto.get("id")})

        self.mapper.to_object(dto)

        PurchaseListService.fill_to_create(
            purchase_list, datetime.utcnow(),
            AuthenticationContext.get_current_user())

        self.purchase_list_dao.update(purchase_list)

        return purchase_list
Esempio n. 19
0
    def update_from_dto(self, dto: dict):
        self.mapper.validate_dto_to_update(dto)

        user = self.find_by_id(dto.get("id"))

        if not user:
            raise ObjectNotFoundException(ErrorCode.USER_TO_UPDATE_NOT_FOUND,
                                          User.__name__, {"id": dto.get("id")})

        if user != AuthenticationContext.get_current_user():
            raise PermissionException(ErrorCode.UPDATE_USER_PERMISSION,
                                      User.__name__, Actions.UPDATE)

        user = self.mapper.to_object(dto)

        if dto.get("password"):
            user.password = PasswordEncryption.encrypt_password(
                dto.get("password"))

        self.user_dao.update(user, commit=True)

        return user
 def find_by_id(self, purchase_list_id: int) -> Optional[PurchaseList]:
     return self.purchase_list_dao.find_by_id(
         purchase_list_id, AuthenticationContext.get_current_user())
Esempio n. 21
0
 def find_all(self) -> List[Product]:
     return self.product_dao.find_all(AuthenticationContext.get_current_user())
Esempio n. 22
0
 def find_by_id(self, product_id: int) -> Product:
     return self.product_dao.find_by_id(product_id, AuthenticationContext.get_current_user())
Esempio n. 23
0
def get_current_user(user_mapper: UserMapper):
    return jsonify(user_mapper.to_dto(
        AuthenticationContext.get_current_user()))
Esempio n. 24
0
 def find_by_id(self, brand_id: int) -> Brand:
     return self.brand_dao.find_by_id(brand_id, AuthenticationContext.get_current_user())
Esempio n. 25
0
    def create(self, brand: Brand) -> Brand:
        BrandService.fill_to_create(brand, datetime.utcnow(), AuthenticationContext.get_current_user())

        self.brand_dao.add(brand)

        return brand
 def find_all(self) -> List[PurchaseList]:
     return self.purchase_list_dao.find_all_by_user(
         AuthenticationContext.get_current_user())
    def test_find_all(self):
        AuthenticationContext.init_context(self.db.session.query(User).get(6))
        products = self.service.find_all()

        self.assertEqual(14, len(products))
Esempio n. 28
0
 def find_all(self) -> List[Brand]:
     return self.brand_dao.find_all(AuthenticationContext.get_current_user())
Esempio n. 29
0
 def test_get_current_user(self):
     self.assertIsNotNone(AuthenticationContext.get_current_user())
 def find_all(self):
     return self.product_type_dao.find_allowed(
         AuthenticationContext.get_current_user())