def address_validation(address: Address): if not address.province: raise ValueException('province is required!') if not address.city: raise ValueException('city is required!') if not address.zip_code or len(address.zip_code) != 10: raise ValueException('zip code is required!') if not address.postal_address or len(address.postal_address) >= 512: raise ValueException('postal address is required!')
def phone_validation(phone: str) -> bool: if not isinstance(phone, str): raise TypeException( f'Phone Type must be string! Type is: {type(phone)}') if not re.match(r'^9[0-9]{9}$', phone): raise ValueException(f'Input Phone is not valid! phone is:{phone}') return True
def user_validation(user: User): if not user.first_name: raise ValueException("First Name is Required!") if not user.last_name: raise ValueException("Last Name is Required!") if not user.password: raise ValueException("Password is Required!") if not password_validation(user.password): raise ValueException("Entered password is not valid!") if user.email: email_validation(user.email) if user.phone: phone_validation(user.phone) if user.addresses: for address in user.addresses: address_validation(user.address) return True
def email_validation(email: str) -> bool: if not isinstance(email, str): raise TypeException( f'Email Type must be string! Type is: {type(email)}') if not re.match( r"^[a-zA-Z0-9][\w\d_.]{1,63}@[\w\d\-.]{2,256}\.[\w0-9]{2,64}$", email, re.I): raise ValueException(f'Email input is not valid! Email:{email}') return True
def password_validation(password: str): # check if it is hashed with sha256 algorithm if is_it_hashed(password): raise ValueException('Password already is Hashed!') regex = r"^(?=.*[A-Z])(?=.*[a-z])(?=.*[\d\-_\.!@#$%^&*()+=~`:;<>\?,{}[\] ])[a-zA-Z\d\-_\.!@#$%^&*()+=~`:;<>\?," \ r"{}[\] ]{6,31}$" if re.match(regex, password): return True return False
def add_address(cls, uid, address: Address): session = cls._Session() user = session.query(User).get(uid) if not user: raise ValueException(f"there is no user with this id: {uid}") cls._address_validation(address) user.addresses.append(address) session.commit() return True
def get_user_by_id(cls, uid: int) -> User: if not isinstance(uid, int): raise TypeException("uid parameter type must be int!") session = cls._Session() u = session.query(User).get(uid) if not u: raise ValueException(f"user with this id <{uid}> doesn't exist!") u.password = None return u
def change_password_by_url_token(cls, url_token, new_password): if not passwordservice.password_validation(new_password): raise ValueException("Password is not valid!") user_id, _ = cls._url_token_verification(url_token) session = cls._Session() user = session.query(User).get(user_id) user.password = passwordservice.hashing_password(new_password) session.commit() return True
def change_password_by_hex_token(cls, user_id, hex_token, new_password): if not passwordservice.password_validation(new_password): raise ValueException("Password is not valid!") if cls._hex_token_verification(user_id, hex_token): session = cls._Session() user = session.query(User).get(user_id) user.password = cls._password_service.hashing_password( new_password) session.commit() return True raise SecurityException("Token is not valid!")
def log_in_by_email(cls, email: str, password: str) -> User: if cls._email_validation(email): session = cls._Session() user = session.query(User).filter_by(email=email).first() if not user: raise AuthenticationException("Wrong Email Address!") if cls._password_service.password_verification( user.password, password): user.password = None return user raise AuthenticationException("Wrong Password!") raise ValueException("Invalid email address value!")
def log_in_by_phone(cls, phone: str, password: str) -> User: if cls._phone_validation(phone): session = cls._Session() user = session.query(User).filter_by(phone=phone).first() if not user: raise AuthenticationException("Wrong Phone Number!") if cls._password_service.password_verification( user.password, password): user.password = None return user raise AuthenticationException("Wrong Password!") raise ValueException("Invalid phone value!")
def change_password(cls, user_id, old_password, new_password): session = cls._Session() user = session.query(User).get(user_id) if not user: raise SecurityException("User doesn't exist!") if not cls._password_service.password_verification( user.password, old_password): raise AuthenticationException("Wrong Password!") if not cls._password_service.password_validation(new_password): raise ValueException("Entered password is not valid!") pss = cls._password_service.hashing_password(new_password) user.password = pss session.commit() return True
def raise_exception(): raise ValueException("value exception!")
def test_does_value_exception_text_appears(self): with self.assertRaises(ValueException) as context: raise ValueException("Value exception!") self.assertEqual("Value exception!", str(context.exception))